88
99from .logging import *
1010from .messages import *
11- from . import ast , logging
11+ from . import ast , logging , compat
1212import dml .globals
1313from . import dmllex12
1414from . import dmllex14
@@ -176,6 +176,7 @@ def prod(f):
176176@prod
177177def device (t ):
178178 'dml : DEVICE objident SEMI maybe_bitorder device_statements'
179+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
179180 t [0 ] = ast .dml (site (t ), t [2 ], t [5 ])
180181
181182# Entry point for imported files
@@ -290,6 +291,7 @@ def array_list(t):
290291@prod
291292def object_regarray (t ):
292293 'object : REGISTER objident array_list sizespec offsetspec maybe_istemplate object_spec'
294+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
293295 t [0 ] = ast .object_ (site (t ), t [2 ], 'register' , t [3 ],
294296 t [4 ] + t [5 ] + t [6 ] + t [7 ])
295297
@@ -306,6 +308,7 @@ def bitrangespec_empty(t):
306308@prod_dml14
307309def object_field (t ):
308310 'object : FIELD objident array_list bitrangespec maybe_istemplate object_spec'
311+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
309312 t [0 ] = ast .object_ (site (t ), t [2 ], 'field' , t [3 ], t [4 ] + t [5 ] + t [6 ])
310313
311314def endian_translate_bit (expr , width , bitorder ):
@@ -454,11 +457,13 @@ def object3(t):
454457 | GROUP objident array_list maybe_istemplate object_spec
455458 | PORT objident array_list maybe_istemplate object_spec
456459 | IMPLEMENT objident array_list maybe_istemplate object_spec'''
460+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
457461 t [0 ] = ast .object_ (site (t ), t [2 ], t [1 ], t [3 ], t [4 ] + t [5 ])
458462
459463@prod_dml14
460464def object_subdevice (t ):
461465 '''object : SUBDEVICE objident array_list maybe_istemplate object_spec'''
466+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
462467 t [0 ] = ast .object_ (site (t ), t [2 ], t [1 ], t [3 ], t [4 ] + t [5 ])
463468
464469@prod_dml12
@@ -582,6 +587,7 @@ def method_qualifiers_check(site, qualifiers, inp, outp, throws, default):
582587def object_method (t ):
583588 '''method : method_qualifiers METHOD objident method_params_typed maybe_default compound_statement'''
584589 name = t [3 ]
590+ ident_enforce_not_discard_ref (site (t , 3 ), name )
585591 (inp , outp , throws ) = t [4 ]
586592 body = t [6 ]
587593 (inp , outp ) = method_qualifiers_check (site (t ), t [1 ], inp , outp , throws ,
@@ -594,6 +600,7 @@ def object_method(t):
594600def object_inline_method (t ):
595601 '''method : INLINE METHOD objident method_params_maybe_untyped maybe_default compound_statement'''
596602 name = t [3 ]
603+ ident_enforce_not_discard_ref (site (t , 3 ), name )
597604 (inp , outp , throws ) = t [4 ]
598605 if all (typ for (_ , asite , name , typ ) in inp ):
599606 # inline annotation would have no effect for fully typed methods.
@@ -642,11 +649,13 @@ def arraydef2(t):
642649@prod_dml14
643650def arraydef (t ):
644651 '''arraydef : ident LT expression'''
652+ ident_enforce_not_discard_ref (site (t , 1 ), t [1 ])
645653 t [0 ] = (t [1 ], t [3 ])
646654
647655@prod_dml14
648656def arraydef_implicit (t ):
649657 '''arraydef : ident LT ELLIPSIS'''
658+ ident_enforce_not_discard_ref (site (t , 1 ), t [1 ])
650659 t [0 ] = (t [1 ], None )
651660
652661# Traits
@@ -745,15 +754,18 @@ def trait_method(t):
745754@prod
746755def shared_method_abstract (t ):
747756 '''shared_method : ident method_params_typed SEMI'''
757+ ident_enforce_not_discard_ref (site (t , 1 ), t [1 ])
748758 t [0 ] = (t [1 ], t [2 ], True , None , site (t , 3 ))
749759@prod
750760def shared_method_default (t ):
751761 '''shared_method : ident method_params_typed DEFAULT compound_statement'''
762+ ident_enforce_not_discard_ref (site (t , 1 ), t [1 ])
752763 t [0 ] = (t [1 ], t [2 ], True , t [4 ], lex_end_site (t , - 1 ))
753764
754765@prod
755766def shared_method_final (t ):
756767 '''shared_method : ident method_params_typed compound_statement'''
768+ ident_enforce_not_discard_ref (site (t , 1 ), t [1 ])
757769 t [0 ] = (t [1 ], t [2 ], False , t [3 ], lex_end_site (t , - 1 ))
758770
759771@prod_dml12
@@ -784,6 +796,7 @@ def template(t):
784796@prod_dml14
785797def template (t ):
786798 'toplevel : TEMPLATE objident maybe_istemplate LBRACE template_stmts RBRACE'
799+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
787800 ises = [s for s in t [5 ] if s .kind == 'is' ]
788801 shared_methods = [s for s in t [5 ] if s .kind == 'sharedmethod' ]
789802 if ises and shared_methods :
@@ -812,13 +825,15 @@ def impl_header(t):
812825@prod
813826def loggroup (t ):
814827 'toplevel : LOGGROUP ident SEMI'
828+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
815829 t [0 ] = ast .loggroup (site (t ), t [2 ])
816830
817831# constant/extern
818832
819833@prod
820834def constant (t ):
821835 'toplevel : CONSTANT ident EQUALS expression SEMI'
836+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
822837 t [0 ] = ast .constant (site (t ), t [2 ], t [4 ])
823838 if logging .show_porting :
824839 report (PCONSTANT (site (t )))
@@ -1019,6 +1034,7 @@ def object_else_if(t):
10191034@prod
10201035def object_parameter (t ):
10211036 '''parameter : param_ objident paramspec'''
1037+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
10221038 if logging .show_porting :
10231039 if t [2 ] == 'hard_reset_value' :
10241040 report (PHARD_RESET_VALUE (site (t , 2 )))
@@ -1031,6 +1047,7 @@ def object_parameter(t):
10311047@prod_dml14
10321048def object_typedparam (t ):
10331049 '''parameter : param_ objident COLON ctypedecl SEMI'''
1050+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
10341051 t [0 ] = ast .typedparam (site (t ), site (t , 2 ), t [2 ], t [4 ])
10351052
10361053@prod
@@ -1207,6 +1224,7 @@ def cdecl_or_ident_decl(t):
12071224@prod_dml14
12081225def cdecl_or_ident_inline (t ):
12091226 '''cdecl_or_ident : INLINE ident'''
1227+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
12101228 t [0 ] = ast .cdecl (site (t ), t [2 ], None )
12111229
12121230# A C-like declaration with required identifier name
@@ -1309,6 +1327,7 @@ def cdecl3(t):
13091327@prod_dml14
13101328def cdecl3 (t ):
13111329 'cdecl3 : ident'
1330+ ident_enforce_not_discard_ref (site (t , 1 ), t [1 ])
13121331 t [0 ] = [t [1 ]]
13131332
13141333@prod
@@ -2211,6 +2230,7 @@ def ident_list_nonempty(t):
22112230@prod_dml14
22122231def ident_list_one (t ):
22132232 'nonempty_ident_list : ident'
2233+ ident_enforce_not_discard_ref (site (t , 1 ), t [1 ])
22142234 t [0 ] = [(site (t , 1 ), t [1 ])]
22152235
22162236@prod_dml14
@@ -2226,6 +2246,7 @@ def statement_delay_hook(t):
22262246@prod_dml14
22272247def statement_delay_hook_one_msg_param (t ):
22282248 'statement_except_hashif : AFTER expression ARROW ident COLON expression SEMI %prec bind'
2249+ ident_enforce_not_discard_ref (site (t , 4 ), t [4 ])
22292250 t [0 ] = ast .afteronhook (site (t ), t [2 ], [(site (t , 4 ), t [4 ])], t [6 ])
22302251
22312252
@@ -2350,23 +2371,27 @@ def hashselect(t):
23502371@prod
23512372def select (t ):
23522373 'statement_except_hashif : hashselect ident IN LPAREN expression RPAREN WHERE LPAREN expression RPAREN statement hashelse statement'
2374+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
23532375 t [0 ] = ast .select (site (t ), t [2 ], t [5 ], t [9 ], t [11 ], t [13 ])
23542376
23552377@prod_dml12
23562378def foreach (t ):
23572379 'statement_except_hashif : FOREACH ident IN LPAREN expression RPAREN statement'
2380+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
23582381 t [0 ] = ast .foreach_dml12 (site (t ), t [2 ], t [5 ], t [7 ])
23592382 if logging .show_porting :
23602383 report (PHASH (site (t )))
23612384
23622385@prod_dml14
23632386def foreach (t ):
23642387 'statement_except_hashif : FOREACH ident IN LPAREN expression RPAREN statement'
2388+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
23652389 t [0 ] = ast .foreach (site (t ), t [2 ], t [5 ], t [7 ])
23662390
23672391@prod_dml14
23682392def hashforeach (t ):
23692393 'statement_except_hashif : HASHFOREACH ident IN LPAREN expression RPAREN statement'
2394+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
23702395 t [0 ] = ast .hashforeach (site (t ), t [2 ], t [5 ], t [7 ])
23712396
23722397@prod_dml12
@@ -2397,6 +2422,7 @@ def case_statement_default(t):
23972422@prod_dml14
23982423def goto_statement (t ):
23992424 'statement_except_hashif : GOTO ident SEMI'
2425+ ident_enforce_not_discard_ref (site (t , 2 ), t [2 ])
24002426 # Restricted goto should be implemented, see SIMICS-6130
24012427 report (ESYNTAX (site (t ), 'goto' ,
24022428 'goto statements are not yet implemented in DML 1.4' ))
@@ -2573,6 +2599,7 @@ def simple_array_list(t):
25732599@prod_dml14
25742600def hook_decl (t ):
25752601 '''hook_decl : HOOK LPAREN cdecl_list RPAREN ident simple_array_list SEMI'''
2602+ ident_enforce_not_discard_ref (site (t , 5 ), t [5 ])
25762603 cdecl_list_enforce_unnamed (t [3 ])
25772604 if t [6 ]:
25782605 # Hook arrays are an internal feature, as their design depends on if we
@@ -2632,6 +2659,12 @@ def ident(t):
26322659def ident (t ):
26332660 t [0 ] = t [1 ]
26342661
2662+ def ident_enforce_not_discard_ref (site , ident ):
2663+ if (str (ident ) == '_'
2664+ and site .dml_version () != (1 , 2 )
2665+ and compat .discard_ref_shadowing not in dml .globals .enabled_compat ):
2666+ report (ESYNTAX (site , '_' , "reserved identifier" ))
2667+
26352668reserved_words_12 = [
26362669 'CLASS' , 'ENUM' , 'NAMESPACE' , 'PRIVATE' , 'PROTECTED' , 'PUBLIC' ,
26372670 'RESTRICT' , 'UNION' , 'USING' , 'VIRTUAL' , 'VOLATILE' ]
0 commit comments