From ecc22f7fedfa639482dbc55a05709da85116a60f Mon Sep 17 00:00:00 2001 From: Peter Crozier Date: Mon, 23 Mar 2020 20:07:22 +0000 Subject: Support module/package/interface/block scope for typedef names. --- frontends/verilog/verilog_parser.y | 51 +++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 15 deletions(-) (limited to 'frontends/verilog/verilog_parser.y') diff --git a/frontends/verilog/verilog_parser.y b/frontends/verilog/verilog_parser.y index f7e3afd13..1a195bbfd 100644 --- a/frontends/verilog/verilog_parser.y +++ b/frontends/verilog/verilog_parser.y @@ -54,7 +54,7 @@ namespace VERILOG_FRONTEND { std::map *attr_list, default_attr_list; std::stack *> attr_list_stack; std::map *albuf; - std::map user_types; + std::vector user_type_stack; std::map pkg_user_types; std::vector ast_stack; struct AstNode *astbuf1, *astbuf2, *astbuf3; @@ -130,14 +130,10 @@ struct specify_rise_fall { static void addTypedefNode(std::string *name, AstNode *node) { log_assert(node); - // seems to be support for local scoped typedefs in simplify() - // and tests redefine types. - //if (user_types.count(*name) > 0) { - // frontend_verilog_yyerror("Type already defined."); - //} auto *tnode = new AstNode(AST_TYPEDEF, node); tnode->str = *name; - user_types[*name] = tnode; + auto user_types = user_type_stack.back(); + (*user_types)[*name] = tnode; if (current_ast_mod && current_ast_mod->type == AST_PACKAGE) { // typedef inside a package so we need the qualified name auto qname = current_ast_mod->str + "::" + (*name).substr(1); @@ -147,6 +143,17 @@ static void addTypedefNode(std::string *name, AstNode *node) ast_stack.back()->children.push_back(tnode); } +static void enterTypeScope() +{ + auto user_types = new UserTypeMap(); + user_type_stack.push_back(user_types); +} + +static void exitTypeScope() +{ + user_type_stack.pop_back(); +} + static AstNode *makeRange(int msb = 31, int lsb = 0, bool isSigned = true) { auto range = new AstNode(AST_RANGE); @@ -359,7 +366,7 @@ hierarchical_type_id: ; module: - attr TOK_MODULE TOK_ID { + attr module_start TOK_ID { do_not_require_port_stubs = false; AstNode *mod = new AstNode(AST_MODULE); ast_stack.back()->children.push_back(mod); @@ -378,9 +385,12 @@ module: ast_stack.pop_back(); log_assert(ast_stack.size() == 1); current_ast_mod = NULL; - user_types.clear(); + exitTypeScope(); }; +module_start: TOK_MODULE { enterTypeScope(); } + ; + module_para_opt: '#' '(' { astbuf1 = nullptr; } module_para_list { if (astbuf1) delete astbuf1; } ')' | /* empty */; @@ -482,7 +492,7 @@ module_arg: }; package: - attr TOK_PACKAGE TOK_ID { + attr package_start TOK_ID { AstNode *mod = new AstNode(AST_PACKAGE); ast_stack.back()->children.push_back(mod); ast_stack.push_back(mod); @@ -492,9 +502,12 @@ package: } ';' package_body TOK_ENDPACKAGE { ast_stack.pop_back(); current_ast_mod = NULL; - user_types.clear(); + exitTypeScope(); }; +package_start: TOK_PACKAGE { enterTypeScope(); } + ; + package_body: package_body package_body_stmt | // optional @@ -505,7 +518,7 @@ package_body_stmt: localparam_decl; interface: - TOK_INTERFACE TOK_ID { + interface_start TOK_ID { do_not_require_port_stubs = false; AstNode *intf = new AstNode(AST_INTERFACE); ast_stack.back()->children.push_back(intf); @@ -522,9 +535,12 @@ interface: ast_stack.pop_back(); log_assert(ast_stack.size() == 1); current_ast_mod = NULL; - user_types.clear(); + exitTypeScope(); }; +interface_start: TOK_INTERFACE { enterTypeScope(); } + ; + interface_body: interface_body interface_body_stmt |; @@ -2210,7 +2226,7 @@ behavioral_stmt: } opt_arg_list ';'{ ast_stack.pop_back(); } | - attr TOK_BEGIN opt_label { + attr begin opt_label { AstNode *node = new AstNode(AST_BLOCK); ast_stack.back()->children.push_back(node); ast_stack.push_back(node); @@ -2218,6 +2234,7 @@ behavioral_stmt: if ($3 != NULL) node->str = *$3; } behavioral_stmt_list TOK_END opt_label { + exitTypeScope(); if ($3 != NULL && $7 != NULL && *$3 != *$7) frontend_verilog_yyerror("Begin label (%s) and end label (%s) don't match.", $3->c_str()+1, $7->c_str()+1); if ($3 != NULL) @@ -2301,6 +2318,9 @@ behavioral_stmt: ast_stack.pop_back(); }; +begin: TOK_BEGIN { enterTypeScope(); } + ; + unique_case_attr: /* empty */ { $$ = false; @@ -2516,12 +2536,13 @@ gen_stmt: case_type_stack.pop_back(); ast_stack.pop_back(); } | - TOK_BEGIN opt_label { + begin opt_label { AstNode *node = new AstNode(AST_GENBLOCK); node->str = $2 ? *$2 : std::string(); ast_stack.back()->children.push_back(node); ast_stack.push_back(node); } module_gen_body TOK_END opt_label { + exitTypeScope(); if ($2 != NULL) delete $2; if ($6 != NULL) -- cgit v1.2.3 From 9a8a644ad11e6fd95b6a1800e357e9cf282f5275 Mon Sep 17 00:00:00 2001 From: Peter Crozier Date: Tue, 24 Mar 2020 14:35:21 +0000 Subject: Error duplicate declarations of a typedef name in the same scope. --- frontends/verilog/verilog_parser.y | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'frontends/verilog/verilog_parser.y') diff --git a/frontends/verilog/verilog_parser.y b/frontends/verilog/verilog_parser.y index 1a195bbfd..d31740c6a 100644 --- a/frontends/verilog/verilog_parser.y +++ b/frontends/verilog/verilog_parser.y @@ -154,6 +154,13 @@ static void exitTypeScope() user_type_stack.pop_back(); } +static bool isInLocalScope(const std::string *name) +{ + // tests if a name was declared in the current block scope + auto user_types = user_type_stack.back(); + return (user_types->count(*name) > 0); +} + static AstNode *makeRange(int msb = 31, int lsb = 0, bool isSigned = true) { auto range = new AstNode(AST_RANGE); @@ -196,7 +203,7 @@ static void addRange(AstNode *parent, int msb = 31, int lsb = 0, bool isSigned = %token TOK_STRING TOK_ID TOK_CONSTVAL TOK_REALVAL TOK_PRIMITIVE %token TOK_SVA_LABEL TOK_SPECIFY_OPER TOK_MSG_TASKS %token TOK_BASE TOK_BASED_CONSTVAL TOK_UNBASED_UNSIZED_CONSTVAL -%token TOK_USER_TYPE +%token TOK_USER_TYPE TOK_PKG_USER_TYPE %token TOK_ASSERT TOK_ASSUME TOK_RESTRICT TOK_COVER TOK_FINAL %token ATTR_BEGIN ATTR_END DEFATTR_BEGIN DEFATTR_END %token TOK_MODULE TOK_ENDMODULE TOK_PARAMETER TOK_LOCALPARAM TOK_DEFPARAM @@ -362,6 +369,7 @@ hierarchical_id: hierarchical_type_id: TOK_USER_TYPE + | TOK_PKG_USER_TYPE // package qualified type name | '(' TOK_USER_TYPE ')' { $$ = $2; } // non-standard grammar ; @@ -1637,7 +1645,7 @@ assign_expr: }; type_name: TOK_ID // first time seen - | TOK_USER_TYPE // redefinition + | TOK_USER_TYPE { if (isInLocalScope($1)) frontend_verilog_yyerror("Duplicate declaration of TYPEDEF '%s'", $1->c_str()+1); } ; typedef_decl: -- cgit v1.2.3 From f8c065ed1cb17057a8317ae5bd47500a6be60c5c Mon Sep 17 00:00:00 2001 From: Peter Crozier Date: Fri, 27 Mar 2020 16:21:45 +0000 Subject: Inline productions to follow house style. --- frontends/verilog/verilog_parser.y | 62 ++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 33 deletions(-) (limited to 'frontends/verilog/verilog_parser.y') diff --git a/frontends/verilog/verilog_parser.y b/frontends/verilog/verilog_parser.y index d31740c6a..be2872e59 100644 --- a/frontends/verilog/verilog_parser.y +++ b/frontends/verilog/verilog_parser.y @@ -374,7 +374,9 @@ hierarchical_type_id: ; module: - attr module_start TOK_ID { + attr TOK_MODULE { + enterTypeScope(); + } TOK_ID { do_not_require_port_stubs = false; AstNode *mod = new AstNode(AST_MODULE); ast_stack.back()->children.push_back(mod); @@ -382,9 +384,9 @@ module: current_ast_mod = mod; port_stubs.clear(); port_counter = 0; - mod->str = *$3; + mod->str = *$4; append_attr(mod, $1); - delete $3; + delete $4; } module_para_opt module_args_opt ';' module_body TOK_ENDMODULE { if (port_stubs.size() != 0) frontend_verilog_yyerror("Missing details for module port `%s'.", @@ -396,9 +398,6 @@ module: exitTypeScope(); }; -module_start: TOK_MODULE { enterTypeScope(); } - ; - module_para_opt: '#' '(' { astbuf1 = nullptr; } module_para_list { if (astbuf1) delete astbuf1; } ')' | /* empty */; @@ -500,12 +499,14 @@ module_arg: }; package: - attr package_start TOK_ID { + attr TOK_PACKAGE { + enterTypeScope(); + } TOK_ID { AstNode *mod = new AstNode(AST_PACKAGE); ast_stack.back()->children.push_back(mod); ast_stack.push_back(mod); current_ast_mod = mod; - mod->str = *$3; + mod->str = *$4; append_attr(mod, $1); } ';' package_body TOK_ENDPACKAGE { ast_stack.pop_back(); @@ -513,9 +514,6 @@ package: exitTypeScope(); }; -package_start: TOK_PACKAGE { enterTypeScope(); } - ; - package_body: package_body package_body_stmt | // optional @@ -526,7 +524,9 @@ package_body_stmt: localparam_decl; interface: - interface_start TOK_ID { + TOK_INTERFACE { + enterTypeScope(); + } TOK_ID { do_not_require_port_stubs = false; AstNode *intf = new AstNode(AST_INTERFACE); ast_stack.back()->children.push_back(intf); @@ -534,8 +534,8 @@ interface: current_ast_mod = intf; port_stubs.clear(); port_counter = 0; - intf->str = *$2; - delete $2; + intf->str = *$3; + delete $3; } module_para_opt module_args_opt ';' interface_body TOK_ENDINTERFACE { if (port_stubs.size() != 0) frontend_verilog_yyerror("Missing details for module port `%s'.", @@ -546,9 +546,6 @@ interface: exitTypeScope(); }; -interface_start: TOK_INTERFACE { enterTypeScope(); } - ; - interface_body: interface_body interface_body_stmt |; @@ -2234,21 +2231,21 @@ behavioral_stmt: } opt_arg_list ';'{ ast_stack.pop_back(); } | - attr begin opt_label { + attr TOK_BEGIN { + enterTypeScope(); + } opt_label { AstNode *node = new AstNode(AST_BLOCK); ast_stack.back()->children.push_back(node); ast_stack.push_back(node); append_attr(node, $1); - if ($3 != NULL) - node->str = *$3; + if ($4 != NULL) + node->str = *$4; } behavioral_stmt_list TOK_END opt_label { exitTypeScope(); - if ($3 != NULL && $7 != NULL && *$3 != *$7) - frontend_verilog_yyerror("Begin label (%s) and end label (%s) don't match.", $3->c_str()+1, $7->c_str()+1); - if ($3 != NULL) - delete $3; - if ($7 != NULL) - delete $7; + if ($4 != NULL && $8 != NULL && *$4 != *$8) + frontend_verilog_yyerror("Begin label (%s) and end label (%s) don't match.", $4->c_str()+1, $8->c_str()+1); + delete $4; + delete $8; ast_stack.pop_back(); } | attr TOK_FOR '(' { @@ -2326,7 +2323,6 @@ behavioral_stmt: ast_stack.pop_back(); }; -begin: TOK_BEGIN { enterTypeScope(); } ; unique_case_attr: @@ -2544,17 +2540,17 @@ gen_stmt: case_type_stack.pop_back(); ast_stack.pop_back(); } | - begin opt_label { + TOK_BEGIN { + enterTypeScope(); + } opt_label { AstNode *node = new AstNode(AST_GENBLOCK); - node->str = $2 ? *$2 : std::string(); + node->str = $3 ? *$3 : std::string(); ast_stack.back()->children.push_back(node); ast_stack.push_back(node); } module_gen_body TOK_END opt_label { exitTypeScope(); - if ($2 != NULL) - delete $2; - if ($6 != NULL) - delete $6; + delete $3; + delete $7; ast_stack.pop_back(); } | TOK_MSG_TASKS { -- cgit v1.2.3