28
28
#define mts (x, y ) stack_expr(x).move_to_sub((irept &)stack_expr(y))
29
29
#define swapop (x, y ) stack_expr(x).operands().swap(stack_expr(y).operands())
30
30
#define addswap (x, y, z ) stack_expr(x).add(y).swap(stack_expr(z))
31
+ #define push_scope (x ) PARSER.push_scope(x)
32
+ #define pop_scope () PARSER.pop_scope();
31
33
32
34
int yyveriloglex ();
33
35
extern char *yyverilogtext;
@@ -542,6 +544,7 @@ int yyverilogerror(const char *error)
542
544
/* Others */
543
545
%token TOK_ENDOFFILE
544
546
%token TOK_NON_TYPE_IDENTIFIER
547
+ %token TOK_TYPE_IDENTIFIER
545
548
%token TOK_NUMBER // number , any base
546
549
%token TOK_TIME_LITERAL // number followed by time unit
547
550
%token TOK_QSTRING // quoted string
@@ -621,10 +624,20 @@ description:
621
624
| config_declaration
622
625
;
623
626
627
+ // This deviates from the IEEE 1800-2017 grammar
628
+ // to allow the module scope to be opened.
629
+ module_identifier_with_scope :
630
+ module_identifier
631
+ {
632
+ $$ = $1 ;
633
+ push_scope (stack_expr($1 ).id());
634
+ }
635
+ ;
636
+
624
637
module_nonansi_header :
625
638
attribute_instance_brace
626
639
module_keyword
627
- module_identifier
640
+ module_identifier_with_scope
628
641
parameter_port_list_opt
629
642
list_of_ports_opt ' ;'
630
643
{
@@ -640,7 +653,7 @@ module_nonansi_header:
640
653
module_ansi_header :
641
654
attribute_instance_brace
642
655
module_keyword
643
- module_identifier
656
+ module_identifier_with_scope
644
657
parameter_port_list_opt
645
658
list_of_port_declarations ' ;'
646
659
{
@@ -663,6 +676,9 @@ module_declaration:
663
676
stack_expr($1 ).operands()[3],
664
677
stack_expr($1 ).operands()[4],
665
678
stack_expr($2 ));
679
+
680
+ // close the module scope
681
+ pop_scope ();
666
682
}
667
683
| module_ansi_header module_item_brace TOK_ENDMODULE module_identifier_opt
668
684
{
@@ -673,6 +689,9 @@ module_declaration:
673
689
stack_expr($1 ).operands()[3],
674
690
stack_expr($1 ).operands()[4],
675
691
stack_expr($2 ));
692
+
693
+ // close the module scope
694
+ pop_scope ();
676
695
}
677
696
| TOK_EXTERN module_nonansi_header
678
697
/* ignored for now */
@@ -960,9 +979,15 @@ net_declaration:
960
979
swapop ($$, $6 ); }
961
980
;
962
981
982
+ // Note that the identifier that is defined using the typedef may be
983
+ // an existing type or non-type identifier.
963
984
type_declaration :
964
- TOK_TYPEDEF data_type type_identifier ' ;'
965
- { init($$ , ID_decl);
985
+ TOK_TYPEDEF data_type new_identifier ' ;'
986
+ { // add to the scope as a type name
987
+ auto &name = PARSER.add_name(stack_expr($3 ).get(ID_identifier));
988
+ name.is_type = true ;
989
+
990
+ init ($$, ID_decl);
966
991
stack_expr ($$).set(ID_class, ID_typedef);
967
992
addswap ($$, ID_type, $2 );
968
993
mto ($$, $3 );
@@ -1033,9 +1058,8 @@ data_type:
1033
1058
{ init($$ , ID_chandle); }
1034
1059
| TOK_VIRTUAL interface_opt interface_identifier
1035
1060
{ init($$ , " virtual_interface" ); }
1036
- /*
1037
- | scope_opt type_identifier packed_dimension_brace
1038
- */
1061
+ | /* scope_opt*/ type_identifier packed_dimension_brace
1062
+ { $$ = $1 ; stack_expr($$ ).id(ID_typedef_type); }
1039
1063
| class_type
1040
1064
| TOK_EVENT
1041
1065
{ init($$ , ID_event); }
@@ -1451,17 +1475,22 @@ struct_union:
1451
1475
// A.2.6 Function declarations
1452
1476
1453
1477
function_declaration :
1454
- TOK_FUNCTION automatic_opt signing_opt range_or_type_opt
1455
- function_identifier list_of_ports_opt ' ;'
1456
- function_item_declaration_brace statement TOK_ENDFUNCTION
1478
+ TOK_FUNCTION
1479
+ automatic_opt signing_opt range_or_type_opt
1480
+ function_identifier
1481
+ { push_scope(stack_expr($1 ).get(ID_identifier)); }
1482
+ list_of_ports_opt ' ;'
1483
+ function_item_declaration_brace statement
1484
+ TOK_ENDFUNCTION
1457
1485
{ init($$ , ID_decl);
1458
1486
stack_expr ($$).set(ID_class, ID_function);
1459
1487
addswap ($$, ID_type, $4 );
1460
1488
add_as_subtype (stack_type($4 ), stack_type($3 ));
1461
1489
addswap ($$, ID_symbol, $5 );
1462
- addswap ($$, ID_ports, $6 );
1463
- addswap ($$, " declarations" , $8 );
1464
- addswap ($$, ID_body, $9 );
1490
+ addswap ($$, ID_ports, $7 );
1491
+ addswap ($$, " declarations" , $9 );
1492
+ addswap ($$, ID_body, $10 );
1493
+ pop_scope ();
1465
1494
}
1466
1495
;
1467
1496
@@ -1500,15 +1529,18 @@ function_item_declaration:
1500
1529
// A.2.7 Task declarations
1501
1530
1502
1531
task_declaration :
1503
- TOK_TASK task_identifier list_of_ports_opt ' ;'
1532
+ TOK_TASK task_identifier
1533
+ { push_scope(stack_expr($2 ).get(ID_identifier)); }
1534
+ list_of_ports_opt ' ;'
1504
1535
task_item_declaration_brace
1505
1536
statement_or_null TOK_ENDTASK
1506
1537
{ init($$ , ID_decl);
1507
1538
stack_expr ($$).set(ID_class, ID_task);
1508
1539
addswap ($$, ID_symbol, $2 );
1509
- addswap ($$, ID_ports, $3 );
1510
- addswap ($$, " declarations" , $5 );
1511
- addswap ($$, ID_body, $6 );
1540
+ addswap ($$, ID_ports, $4 );
1541
+ addswap ($$, " declarations" , $6 );
1542
+ addswap ($$, ID_body, $7 );
1543
+ pop_scope ();
1512
1544
}
1513
1545
;
1514
1546
@@ -2351,11 +2383,14 @@ seq_block:
2351
2383
TOK_END
2352
2384
{ init($$ , ID_block); swapop($$ , $2 ); }
2353
2385
| TOK_BEGIN TOK_COLON block_identifier
2386
+ { push_scope(stack_expr($3 ).id()); }
2354
2387
statement_or_null_brace
2355
2388
TOK_END
2356
2389
{ init($$ , ID_block);
2357
- swapop ($$, $4 );
2358
- addswap ($$, ID_identifier, $3 ); }
2390
+ swapop ($$, $5 );
2391
+ addswap ($$, ID_identifier, $3 );
2392
+ pop_scope ();
2393
+ }
2359
2394
;
2360
2395
2361
2396
block_item_declaration_brace :
@@ -2841,6 +2876,17 @@ attr_name: identifier
2841
2876
// System Verilog standard 1800-2017
2842
2877
// A.9.3 Identifiers
2843
2878
2879
+ // An extension of the System Verilog grammar to allow defining new types
2880
+ // using an existing type or non-type identifier.
2881
+ new_identifier :
2882
+ type_identifier
2883
+ | non_type_identifier
2884
+ ;
2885
+
2886
+ non_type_identifier : TOK_NON_TYPE_IDENTIFIER
2887
+ { new_symbol($$ , $1 ); }
2888
+ ;
2889
+
2844
2890
block_identifier : TOK_NON_TYPE_IDENTIFIER ;
2845
2891
2846
2892
genvar_identifier : identifier ;
@@ -2869,7 +2915,14 @@ ps_covergroup_identifier:
2869
2915
2870
2916
memory_identifier : identifier ;
2871
2917
2872
- type_identifier : identifier ;
2918
+ type_identifier : TOK_TYPE_IDENTIFIER
2919
+ {
2920
+ init ($$, ID_typedef_type);
2921
+ auto base_name = stack_expr($1 ).id();
2922
+ stack_expr ($$).set(ID_C_base_name, base_name);
2923
+ stack_expr ($$).set(ID_identifier, PARSER.current_scope->prefix+id2string(base_name));
2924
+ }
2925
+ ;
2873
2926
2874
2927
parameter_identifier : TOK_NON_TYPE_IDENTIFIER ;
2875
2928
@@ -2905,9 +2958,7 @@ hierarchical_identifier:
2905
2958
2906
2959
hierarchical_variable_identifier : hierarchical_identifier ;
2907
2960
2908
- identifier : TOK_NON_TYPE_IDENTIFIER
2909
- { new_symbol($$ , $1 ); }
2910
- ;
2961
+ identifier : non_type_identifier ;
2911
2962
2912
2963
property_identifier : TOK_NON_TYPE_IDENTIFIER ;
2913
2964
0 commit comments