Skip to content

Commit 83ee108

Browse files
committed
Verilog: explicitly put modules and packages into top scope
This changes the parser to explicitly put modules and packages into the top (definitions) scope, not the current scope.
1 parent dc46d28 commit 83ee108

3 files changed

Lines changed: 46 additions & 18 deletions

File tree

src/verilog/parser.y

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,9 @@ module_identifier_with_scope:
629629
{
630630
$$ = $1;
631631
auto base_name = stack_expr($1).get(ID_base_name);
632-
push_scope(base_name, ".", verilog_scopet::MODULE);
632+
// modules go into the top scope, not into $unit
633+
auto &module_scope = PARSER.scopes.top_scope.add_scope(base_name, ".", verilog_scopet::MODULE);
634+
PARSER.scopes.enter_scope(module_scope);
633635
}
634636
;
635637

@@ -742,9 +744,9 @@ checker_declaration:
742744
TOK_CHECKER { init($$); }
743745
checker_identifier
744746
{
745-
// these create a scope
747+
// these create a scope, which goes into the current scope
746748
auto base_name = stack_expr($3).get(ID_base_name);
747-
push_scope(base_name, ".", verilog_scopet::MODULE);
749+
push_scope(base_name, ".", verilog_scopet::CHECKER);
748750
}
749751
checker_port_list_paren_opt ';'
750752
checker_or_generate_item_brace
@@ -815,7 +817,9 @@ class_declaration:
815817
init($$, ID_verilog_class);
816818
auto base_name = stack_expr($2).get(ID_base_name);
817819
stack_expr($$).set(ID_base_name, base_name);
818-
push_scope(base_name, "::", verilog_scopet::CLASS);
820+
// classes go into the top scope, not $unit
821+
auto &class_scope = PARSER.scopes.top_scope.add_scope(base_name, "::", verilog_scopet::CLASS);
822+
PARSER.scopes.enter_scope(class_scope);
819823
}
820824
class_item_brace
821825
TOK_ENDCLASS
@@ -831,7 +835,10 @@ package_declaration:
831835
lifetime_opt
832836
any_identifier ';'
833837
{
834-
push_scope(stack_expr($5).get(ID_base_name), "::", verilog_scopet::PACKAGE);
838+
// packages go into the top scope, not $unit
839+
auto base_name = stack_expr($5).get(ID_base_name);
840+
auto &package_scope = PARSER.scopes.top_scope.add_scope(base_name, "::", verilog_scopet::PACKAGE);
841+
PARSER.scopes.enter_scope(package_scope);
835842
}
836843
package_item_brace
837844
TOK_ENDPACKAGE endpackage_identifier_opt
@@ -2352,7 +2359,8 @@ function_declaration: TOK_FUNCTION lifetime_opt function_body_declaration
23522359
function_body_declaration:
23532360
function_data_type_or_implicit
23542361
function_identifier
2355-
{ push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::FUNCTION); }
2362+
{ // functions go into the current scope, say module or $unit
2363+
push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::FUNCTION); }
23562364
';'
23572365
tf_item_declaration_brace
23582366
function_statement_or_null_brace
@@ -2368,7 +2376,8 @@ function_body_declaration:
23682376
}
23692377
| function_data_type_or_implicit
23702378
function_identifier
2371-
{ push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::FUNCTION); }
2379+
{ // functions go into the current scope, say module or $unit
2380+
push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::FUNCTION); }
23722381
'(' tf_port_list_opt ')' ';'
23732382
block_item_declaration_brace
23742383
function_statement_or_null_brace
@@ -2410,7 +2419,8 @@ function_prototype: TOK_FUNCTION data_type_or_void function_identifier
24102419

24112420
task_declaration:
24122421
TOK_TASK task_identifier
2413-
{ push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::TASK); }
2422+
{ // tasks go into the current scope, module or $unit
2423+
push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::TASK); }
24142424
';'
24152425
tf_item_declaration_brace
24162426
task_statement_or_null_brace
@@ -2423,7 +2433,8 @@ task_declaration:
24232433
pop_scope();
24242434
}
24252435
| TOK_TASK task_identifier
2426-
{ push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::TASK); }
2436+
{ // tasks go into the current scope, module or $unit
2437+
push_scope(stack_expr($2).get(ID_base_name), ".", verilog_scopet::TASK); }
24272438
'(' tf_port_list_opt ')' ';'
24282439
tf_item_declaration_brace
24292440
task_statement_or_null_brace
@@ -2614,7 +2625,8 @@ assertion_item_declaration:
26142625

26152626
property_declaration:
26162627
TOK_PROPERTY any_identifier
2617-
{ auto base_name = stack_expr($2).get(ID_base_name);
2628+
{ // properties go into the current scope, say module or $unit
2629+
auto base_name = stack_expr($2).get(ID_base_name);
26182630
push_scope(base_name, ".", verilog_scopet::PROPERTY); }
26192631
property_port_list_paren_opt ';'
26202632
property_spec semicolon_opt
@@ -2812,7 +2824,8 @@ property_case_item:
28122824
sequence_declaration:
28132825
"sequence" { init($$, ID_verilog_sequence_declaration); }
28142826
any_identifier
2815-
{ auto base_name = stack_expr($3).get(ID_base_name);
2827+
{ // sequences go into the current scope, module or $unit
2828+
auto base_name = stack_expr($3).get(ID_base_name);
28162829
push_scope(base_name, ".", verilog_scopet::SEQUENCE);
28172830
}
28182831
sequence_port_list_opt ';'

src/verilog/verilog_scope.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ verilog_scopest::add_identifier(irep_idt _base_name, scopet::kindt kind)
1818
return add_scope(_base_name, std::string{}, kind);
1919
}
2020

21-
verilog_scopet &verilog_scopest::add_scope(
22-
irep_idt _base_name,
21+
verilog_scopet &verilog_scopet::add_scope(
22+
irep_idt base_name,
2323
const std::string &separator,
24-
scopet::kindt kind)
24+
kindt kind)
2525
{
26-
auto result = current_scope().scope_map.emplace(
27-
_base_name, scopet{_base_name, separator, &current_scope(), kind});
26+
auto result = scope_map.emplace(
27+
base_name, verilog_scopet{base_name, separator, this, kind});
2828
return result.first->second;
2929
}
3030

@@ -83,6 +83,7 @@ unsigned verilog_scopet::identifier_token() const
8383
case verilog_scopet::FILE: return TOK_NON_TYPE_IDENTIFIER;
8484
case verilog_scopet::PACKAGE: return TOK_PACKAGE_IDENTIFIER;
8585
case verilog_scopet::MODULE: return TOK_NON_TYPE_IDENTIFIER;
86+
case verilog_scopet::CHECKER: return TOK_NON_TYPE_IDENTIFIER;
8687
case verilog_scopet::CLASS: return TOK_CLASS_IDENTIFIER;
8788
case verilog_scopet::MODULE_INSTANCE: return TOK_NON_TYPE_IDENTIFIER;
8889
case verilog_scopet::BLOCK: return TOK_NON_TYPE_IDENTIFIER;

src/verilog/verilog_scope.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct verilog_scopet
2222
FILE,
2323
PACKAGE,
2424
MODULE,
25+
CHECKER,
2526
CLASS,
2627
ENUM_NAME,
2728
MODULE_INSTANCE,
@@ -88,19 +89,32 @@ struct verilog_scopet
8889

8990
//.the scanner token number
9091
unsigned identifier_token() const;
92+
93+
// add a child scope into this scope
94+
verilog_scopet &
95+
add_scope(irep_idt base_name, const std::string &separator, kindt kind);
9196
};
9297

9398
class verilog_scopest
9499
{
95100
public:
96101
using scopet = verilog_scopet;
97102

103+
// The "definitions" name space, for module, primitive, program,
104+
// and interface identifiers. Identifiers in here are visible in
105+
// any subscopes.
98106
scopet top_scope;
99107

100108
scopet &add_identifier(irep_idt _base_name, scopet::kindt);
101109

102-
scopet &
103-
add_scope(irep_idt _base_name, const std::string &separator, scopet::kindt);
110+
// add a scope to the current scope
111+
scopet &add_scope(
112+
irep_idt base_name,
113+
const std::string &separator,
114+
scopet::kindt kind)
115+
{
116+
return current_scope().add_scope(base_name, separator, kind);
117+
}
104118

105119
// Scope stack
106120
std::vector<scopet *> scope_stack = {&top_scope};

0 commit comments

Comments
 (0)