From 23e1143ad6870f35aee093f6bc930331a1c4882a Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Tue, 22 Mar 2022 12:00:54 +0100 Subject: [PATCH 1/3] Support unpacked dimensions on typedef overwrites It is possible to declare a new typedef that shadows an existing typedef in a higher level scope. E.g. ``` typedef int T; module M; typedef real T; endmodule ``` In the current implementation this only works as long as the new type is a not an array type. Update the parser to allow to specify unpacked dimension when overwriting a typedef from a different scope. Signed-off-by: Lars-Peter Clausen --- parse.y | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/parse.y b/parse.y index dc4938e7d..eb479697a 100644 --- a/parse.y +++ b/parse.y @@ -2685,13 +2685,13 @@ type_declaration /* If the IDENTIFIER already is a typedef, it is possible for this code to override the definition, but only if the typedef is inherited from a different scope. */ - | K_typedef data_type TYPE_IDENTIFIER ';' + | K_typedef data_type TYPE_IDENTIFIER dimensions_opt ';' { perm_string name = lex_strings.make($3.text); if (pform_test_type_identifier_local(name)) { yyerror(@3, "error: Typedef identifier \"%s\" is already a type name.", $3.text); - + delete $4; } else { - pform_set_typedef(name, $2, NULL); + pform_set_typedef(name, $2, $4); } delete[]$3.text; } From b30d3fc8d7668f50910da554b7768bc7a160e119 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Tue, 22 Mar 2022 14:33:29 +0100 Subject: [PATCH 2/3] Support typedef overwrites in class scopes It is possible to declare a new typedef that shadows an existing typedef in a higher level scope. E.g. ``` typedef int T; class C; typedef real T; endclass ``` In the current implementation this works for scopes that are not class scopes. Update the parser to also support this in class scopes by re-using the existing parser rule that is used for the other scopes. Reusing the existing rule also adds support for class forward typedes inside classes. Signed-off-by: Lars-Peter Clausen --- parse.y | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/parse.y b/parse.y index eb479697a..e9b464a05 100644 --- a/parse.y +++ b/parse.y @@ -916,11 +916,8 @@ class_item /* IEEE1800-2005: A.1.8 */ /* IEEEE1800-2017: A.1.9 Class items: class_item ::= { property_qualifier} data_declaration */ - | property_qualifier_opt K_typedef data_type IDENTIFIER dimensions_opt ';' - { perm_string name = lex_strings.make($4); - delete[]$4; - pform_set_typedef(name, $3, $5); - } + /* TODO: Restrict the access based on the property qualifier. */ + | property_qualifier_opt type_declaration /* IEEE1800-1017: A.1.9 Class items: Class methods... */ From 252174d15a27aa9ce4821e3afed6b52f8832fb69 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Wed, 23 Mar 2022 10:15:37 +0100 Subject: [PATCH 3/3] Add additional regression tests for typedef overwrites Check that it is possible to create a typedef of an array type that shadows an existing typedef in a higher level scope. Also check that it is possible to create a typedef in a class scope that shadows an exiting typedef in a higher level scope. Signed-off-by: Lars-Peter Clausen --- ...sv_typedef_scope.v => sv_typedef_scope1.v} | 0 ivtest/ivltests/sv_typedef_scope2.v | 24 +++++++++++++++ ivtest/ivltests/sv_typedef_scope3.v | 30 +++++++++++++++++++ ivtest/regress-sv.list | 4 ++- ivtest/regress-vlog95.list | 1 + 5 files changed, 58 insertions(+), 1 deletion(-) rename ivtest/ivltests/{sv_typedef_scope.v => sv_typedef_scope1.v} (100%) create mode 100644 ivtest/ivltests/sv_typedef_scope2.v create mode 100644 ivtest/ivltests/sv_typedef_scope3.v diff --git a/ivtest/ivltests/sv_typedef_scope.v b/ivtest/ivltests/sv_typedef_scope1.v similarity index 100% rename from ivtest/ivltests/sv_typedef_scope.v rename to ivtest/ivltests/sv_typedef_scope1.v diff --git a/ivtest/ivltests/sv_typedef_scope2.v b/ivtest/ivltests/sv_typedef_scope2.v new file mode 100644 index 000000000..ec26f5727 --- /dev/null +++ b/ivtest/ivltests/sv_typedef_scope2.v @@ -0,0 +1,24 @@ +// Check that it is possible to overwrite a type identifier declared in a higher +// level scope. Check that this works if the new type is an array type. + +typedef logic [3:0] T; +T x; + +module test; + + typedef logic [7:0] T[1:0]; + T y; + + initial begin + y[0] = 8'hff; + y[1] = 8'hfe; + + if ($bits(T) == 16 && $size(x) == 4 && $size(y) == 2 && + y[0] == 8'hff && y[1] == 8'hfe) begin + $display("PASSED"); + end else begin + $display("FAILED"); + end + end + +endmodule diff --git a/ivtest/ivltests/sv_typedef_scope3.v b/ivtest/ivltests/sv_typedef_scope3.v new file mode 100644 index 000000000..d036d4e4a --- /dev/null +++ b/ivtest/ivltests/sv_typedef_scope3.v @@ -0,0 +1,30 @@ +// Check that it is possible to overwrite a type identifier declared in a higher +// level scope. Check that this works when the new type is declared in a class. + +typedef logic [3:0] T; +T x; + +module test; + + class C; + typedef logic [7:0] T; + T y; + + task t; + y = 8'hff; + if ($bits(x) == 4 && $bits(y) == 8 && y == 8'hff) begin + $display("PASSED"); + end else begin + $display("FAILED"); + end + endtask + endclass + + C c; + + initial begin + c = new; + c.t(); + end + +endmodule diff --git a/ivtest/regress-sv.list b/ivtest/regress-sv.list index 70e7b90c5..ea4ad8398 100644 --- a/ivtest/regress-sv.list +++ b/ivtest/regress-sv.list @@ -568,7 +568,9 @@ sv_timeunit_prec_fail2 CE,-g2009,-u,\ ./ivltests/sv_timeunit_prec_fail2a.v,\ ./ivltests/sv_timeunit_prec_fail2b.v,\ ./ivltests/sv_timeunit_prec_fail2c.v, ivltests gold=sv_timeunit_prec_fail2.gold -sv_typedef_scope normal,-g2009 ivltests +sv_typedef_scope1 normal,-g2009 ivltests +sv_typedef_scope2 normal,-g2009 ivltests +sv_typedef_scope3 normal,-g2009 ivltests sv_union1 normal,-g2009 ivltests sv_union1b normal,-g2009 ivltests sv_union2 normal,-g2009 ivltests diff --git a/ivtest/regress-vlog95.list b/ivtest/regress-vlog95.list index 89af38b69..9a789eaf3 100644 --- a/ivtest/regress-vlog95.list +++ b/ivtest/regress-vlog95.list @@ -387,6 +387,7 @@ sv_port_default7 CE,-g2009,-pallowsigned=1 ivltests sv_port_default8 CE,-g2009,-pallowsigned=1 ivltests sv_port_default9 CE,-g2009 ivltests sv_root_class CE,-g2009 ivltests +sv_typedef_scope3 CE,-g2009 ivltests sv_unit2b CE,-g2009 ivltests sv_unit3b CE,-g2009 ivltests sv_unit4b CE,-g2009 ivltests