Skip to content

Commit

Permalink
SystemVerilog: track the kind of scope
Browse files Browse the repository at this point in the history
SystemVerilog has numerous different kinds of scopes, and this is now
tracked when creating a scope.
  • Loading branch information
kroening committed Jan 29, 2025
1 parent 2ccff6c commit 4bf602b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
22 changes: 11 additions & 11 deletions src/verilog/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Author: Daniel Kroening, [email protected]
#define mts(x, y) stack_expr(x).move_to_sub((irept &)stack_expr(y))
#define swapop(x, y) stack_expr(x).operands().swap(stack_expr(y).operands())
#define addswap(x, y, z) stack_expr(x).add(y).swap(stack_expr(z))
#define push_scope(x, y) PARSER.scopes.push_scope(x, y)
#define push_scope(name, separator, kind) PARSER.scopes.push_scope(name, separator, kind)
#define pop_scope() PARSER.scopes.pop_scope();

int yyveriloglex();
Expand Down Expand Up @@ -644,7 +644,7 @@ module_identifier_with_scope:
module_identifier
{
$$ = $1;
push_scope(stack_expr($1).id(), ".");
push_scope(stack_expr($1).id(), ".", verilog_scopet::MODULE);
}
;

Expand Down Expand Up @@ -823,7 +823,7 @@ class_declaration:
{
init($$, ID_verilog_class);
stack_expr($$).set(ID_base_name, stack_expr($2).id());
push_scope(stack_expr($2).id(), "::");
push_scope(stack_expr($2).id(), "::", verilog_scopet::CLASS);
}
class_item_brace
TOK_ENDCLASS
Expand All @@ -839,7 +839,7 @@ package_declaration:
lifetime_opt
package_identifier ';'
{
push_scope(stack_expr($5).id(), "::");
push_scope(stack_expr($5).id(), "::", verilog_scopet::PACKAGE);
}
timeunits_declaration_opt
package_item_brace
Expand Down Expand Up @@ -1442,7 +1442,7 @@ net_declaration:
type_declaration:
TOK_TYPEDEF data_type new_identifier ';'
{ // add to the scope as a type name
auto &name = PARSER.scopes.add_name(stack_expr($3).get(ID_identifier), "");
auto &name = PARSER.scopes.add_name(stack_expr($3).get(ID_identifier), "", verilog_scopet::TYPEDEF);
name.is_type = true;

init($$, ID_decl);
Expand Down Expand Up @@ -1569,7 +1569,7 @@ enum_name_declaration:
TOK_NON_TYPE_IDENTIFIER enum_name_value_opt
{
init($$);
auto &scope = PARSER.scopes.add_name(stack_expr($1).id(), "");
auto &scope = PARSER.scopes.add_name(stack_expr($1).id(), "", verilog_scopet::ENUM_NAME);
stack_expr($$).set(ID_base_name, scope.base_name());
stack_expr($$).set(ID_identifier, scope.identifier());
stack_expr($$).add(ID_value).swap(stack_expr($2));
Expand Down Expand Up @@ -2137,7 +2137,7 @@ function_declaration: TOK_FUNCTION lifetime_opt function_body_declaration
function_body_declaration:
function_data_type_or_implicit
function_identifier
{ push_scope(stack_expr($2).get(ID_identifier), "."); }
{ push_scope(stack_expr($2).get(ID_identifier), ".", verilog_scopet::FUNCTION); }
';'
tf_item_declaration_brace statement
TOK_ENDFUNCTION
Expand All @@ -2152,7 +2152,7 @@ function_body_declaration:
}
| function_data_type_or_implicit
function_identifier
{ push_scope(stack_expr($2).get(ID_identifier), "."); }
{ push_scope(stack_expr($2).get(ID_identifier), ".", verilog_scopet::FUNCTION); }
'(' tf_port_list_opt ')' ';'
tf_item_declaration_brace statement
TOK_ENDFUNCTION
Expand Down Expand Up @@ -2193,7 +2193,7 @@ function_prototype: TOK_FUNCTION data_type_or_void function_identifier

task_declaration:
TOK_TASK task_identifier
{ push_scope(stack_expr($2).get(ID_identifier), "."); }
{ push_scope(stack_expr($2).get(ID_identifier), ".", verilog_scopet::TASK); }
';'
tf_item_declaration_brace
statement_or_null TOK_ENDTASK
Expand All @@ -2205,7 +2205,7 @@ task_declaration:
pop_scope();
}
| TOK_TASK task_identifier
{ push_scope(stack_expr($2).get(ID_identifier), "."); }
{ push_scope(stack_expr($2).get(ID_identifier), ".", verilog_scopet::TASK); }
'(' tf_port_list_opt ')' ';'
tf_item_declaration_brace
statement_or_null TOK_ENDTASK
Expand Down Expand Up @@ -3385,7 +3385,7 @@ seq_block:
TOK_END
{ init($$, ID_block); swapop($$, $2); }
| TOK_BEGIN TOK_COLON block_identifier
{ push_scope(stack_expr($3).id(), "."); }
{ push_scope(stack_expr($3).id(), ".", verilog_scopet::BLOCK); }
block_item_declaration_or_statement_or_null_brace
TOK_END
{ init($$, ID_block);
Expand Down
41 changes: 31 additions & 10 deletions src/verilog/verilog_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,37 @@ Author: Daniel Kroening, [email protected]
// parser scopes and identifiers
struct verilog_scopet
{
verilog_scopet() : parent(nullptr), prefix("Verilog::")
{
}
using kindt = enum {
GLOBAL,
FILE,
PACKAGE,
MODULE,
CLASS,
ENUM_NAME,
TASK,
FUNCTION,
BLOCK,
TYPEDEF,
OTHER
};

verilog_scopet(
irep_idt _base_name,
const std::string &separator,
verilog_scopet *_parent)
verilog_scopet *_parent,
kindt _kind)
: parent(_parent),
__base_name(_base_name),
prefix(id2string(_parent->prefix) + id2string(_base_name) + separator)
prefix(id2string(_parent->prefix) + id2string(_base_name) + separator),
kind(_kind)
{
}

verilog_scopet *parent = nullptr;
bool is_type = false;
irep_idt __base_name;
std::string prefix;
kindt kind;

irep_idt identifier() const
{
Expand All @@ -56,19 +69,27 @@ class verilog_scopest
public:
using scopet = verilog_scopet;

scopet top_scope, *current_scope = &top_scope;
scopet top_scope =
verilog_scopet{irep_idt(), "Verilog::", nullptr, scopet::GLOBAL};
scopet *current_scope = &top_scope;

scopet &add_name(irep_idt _base_name, const std::string &separator)
scopet &add_name(
irep_idt _base_name,
const std::string &separator,
scopet::kindt kind)
{
auto result = current_scope->scope_map.emplace(
_base_name, scopet{_base_name, separator, current_scope});
_base_name, scopet{_base_name, separator, current_scope, kind});
return result.first->second;
}

// Create the given sub-scope of the current scope.
void push_scope(irep_idt _base_name, const std::string &separator)
void push_scope(
irep_idt _base_name,
const std::string &separator,
scopet::kindt kind)
{
current_scope = &add_name(_base_name, separator);
current_scope = &add_name(_base_name, separator, kind);
}

void pop_scope()
Expand Down

0 comments on commit 4bf602b

Please sign in to comment.