Skip to content

SystemVerilog: track the kind of scope #951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1447,7 +1447,7 @@ type_declaration:
data_type any_identifier ';'
{ $$ = $2;
// add to the scope as a type name
auto &name = PARSER.scopes.add_name(stack_expr($4).get(ID_identifier), "");
auto &name = PARSER.scopes.add_name(stack_expr($4).get(ID_identifier), "", verilog_scopet::TYPEDEF);
name.is_type = true;
addswap($$, ID_type, $3);
stack_expr($4).id(ID_declarator);
Expand Down Expand Up @@ -1571,7 +1571,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 @@ -2139,7 +2139,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 @@ -2154,7 +2154,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 @@ -2195,7 +2195,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 @@ -2207,7 +2207,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 @@ -3387,7 +3387,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
37 changes: 30 additions & 7 deletions src/verilog/verilog_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,41 @@ 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() : parent(nullptr), prefix("Verilog::"), kind(GLOBAL)
{
}

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 @@ -58,17 +75,23 @@ class verilog_scopest

scopet top_scope, *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
Loading