1 // Copyright 2017-2019 VMware, Inc.
2 // SPDX-License-Identifier: BSD-2-Clause
3 //
4 // The BSD-2 license (the License) set forth below applies to all parts of the
5 // Cascade project.  You may not use this file except in compliance with the
6 // License.
7 //
8 // BSD-2 License
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this
14 // list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice,
17 // this list of conditions and the following disclaimer in the documentation
18 // and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
21 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include "verilog/analyze/constant.h"
32 
33 #include "verilog/analyze/resolve.h"
34 
35 namespace cascade {
36 
Constant()37 Constant::Constant() : Visitor() { }
38 
is_static_constant(const Expression * e)39 bool Constant::is_static_constant(const Expression* e) {
40   res_ = true;
41   genvar_ok_ = false;
42   e->accept(this);
43   return res_;
44 }
45 
is_genvar_constant(const Expression * e)46 bool Constant::is_genvar_constant(const Expression* e) {
47   res_ = true;
48   genvar_ok_ = true;
49   e->accept(this);
50   return res_;
51 }
52 
visit(const FeofExpression * fe)53 void Constant::visit(const FeofExpression* fe) {
54   (void) fe;
55   res_ = false;
56 }
57 
visit(const Identifier * i)58 void Constant::visit(const Identifier* i) {
59   Visitor::visit(i);
60 
61   const auto* r = Resolve().get_resolution(i);
62   if (r == nullptr) {
63     res_ = false;
64     return;
65   }
66 
67   const auto* p = r->get_parent();
68   const auto is_param = p->is(Node::Tag::parameter_declaration);
69   const auto is_localparam = p->is(Node::Tag::localparam_declaration);
70   if (is_param || is_localparam) {
71     return;
72   }
73   const auto is_genvar = p->is(Node::Tag::genvar_declaration);
74   if (is_genvar && genvar_ok_) {
75     return;
76   }
77 
78   res_ = false;
79 }
80 
81 } // namespace cascade
82