@@ -952,7 +952,7 @@ def declarations(scope):
952952 if sym .stmt :
953953 continue
954954 decl = sym_declaration (sym )
955- if decl :
955+ if not decl . is_empty :
956956 decls .append (decl )
957957
958958 return decls
@@ -2036,19 +2036,20 @@ def mk_sym(name, typ, mkunique=not dml.globals.debuggable):
20362036 tgts , location , scope )
20372037 if method_invocation is not None and stmt .site .dml_version != (1 , 2 ):
20382038 for sym in syms_to_add :
2039- scope .add (sym )
2039+ if sym .name != '_' :
2040+ scope .add (sym )
20402041 stmts .extend (sym_declaration (sym ) for sym in tgt_syms )
20412042 stmts .append (method_invocation )
2042- stmts .extend (sym_declaration (sym )
2043- for sym in late_declared_syms )
2043+ stmts .extend (sym_declaration (sym ) for sym in late_declared_syms )
20442044 else :
20452045 if len (tgts ) != 1 :
20462046 report (ERETLVALS (stmt .site , 1 , len (tgts )))
20472047 else :
20482048 sym = syms_to_add [0 ]
20492049 sym .init = ExpressionInitializer (
20502050 codegen_expression (inits [0 ].args [0 ], location , scope ))
2051- scope .add (sym )
2051+ if not (sym .name == '_' and stmt .site .dml_version () != (1 , 2 )):
2052+ scope .add (sym )
20522053 stmts .append (sym_declaration (sym ))
20532054 else :
20542055 # Initializer evaluation and variable declarations are done in separate
@@ -2057,9 +2058,13 @@ def mk_sym(name, typ, mkunique=not dml.globals.debuggable):
20572058 inits = [get_initializer (stmt .site , typ , init , location , scope )
20582059 for ((_ , typ ), init ) in zip (decls , inits )]
20592060 for ((name , typ ), init ) in zip (decls , inits ):
2060- sym = scope .add_variable (
2061- name , type = typ , site = stmt .site , init = init , stmt = True ,
2062- make_unique = not dml .globals .debuggable )
2061+ if not (name == '_' and stmt .site .dml_version () != (1 , 2 )):
2062+ sym = scope .add_variable (
2063+ name , type = typ , site = stmt .site , init = init ,
2064+ stmt = True , make_unique = not dml .globals .debuggable )
2065+ else :
2066+ sym = mk_sym (name , typ )
2067+
20632068 stmts .append (sym_declaration (sym ))
20642069
20652070 return stmts
@@ -2401,7 +2406,7 @@ def stmt_assign(stmt, location, scope):
24012406
24022407 lscope = Symtab (scope )
24032408 sym = lscope .add_variable (
2404- 'tmp ' , type = init_typ , site = init .site , init = init ,
2409+ '_tmp ' , type = init_typ , site = init .site , init = init ,
24052410 stmt = True )
24062411 init_expr = mkLocalVariable (init .site , sym )
24072412 stmts = [sym_declaration (sym )]
@@ -2437,7 +2442,7 @@ def stmt_assign(stmt, location, scope):
24372442 else :
24382443 init = eval_initializer (site , tgt .ctype (), src_ast , location ,
24392444 scope , False )
2440- name = 'tmp %d' % (i ,)
2445+ name = '_tmp %d' % (i ,)
24412446 sym = lscope .add_variable (
24422447 name , type = tgt .ctype (), site = tgt .site , init = init ,
24432448 stmt = True )
@@ -2930,6 +2935,9 @@ def stmt_afteronhook(stmt, location, scope):
29302935
29312936 msg_comp_params = {}
29322937 for (idx , (mcp_site , mcp_name )) in enumerate (msg_comp_param_asts ):
2938+ if mcp_name == '_' :
2939+ continue
2940+
29332941 if mcp_name in msg_comp_params :
29342942 raise EDVAR (mcp_site , msg_comp_params [mcp_name ][1 ],
29352943 mcp_name )
@@ -3079,7 +3087,8 @@ def stmt_select(stmt, location, scope):
30793087 clauses = []
30803088 for it in l :
30813089 condscope = Symtab (scope )
3082- condscope .add (ExpressionSymbol (itername , it , stmt .site ))
3090+ if not (itername == '_' and stmt .site .dml_version () != (1 , 2 )):
3091+ condscope .add (ExpressionSymbol (itername , it , stmt .site ))
30833092 cond = as_bool (codegen_expression (
30843093 cond_ast , location , condscope ))
30853094 if cond .constant and not cond .value :
@@ -3115,9 +3124,10 @@ def foreach_each_in(site, itername, trait, each_in,
31153124 body_ast , location , scope ):
31163125 inner_scope = Symtab (scope )
31173126 trait_type = TTrait (trait )
3118- inner_scope .add_variable (
3119- itername , type = trait_type , site = site ,
3120- init = ForeachSequence .itervar_initializer (site , trait ))
3127+ if itername != '_' :
3128+ inner_scope .add_variable (
3129+ itername , type = trait_type , site = site ,
3130+ init = ForeachSequence .itervar_initializer (site , trait ))
31213131 context = GotoLoopContext ()
31223132 with context :
31233133 inner_body = mkCompound (site , declarations (inner_scope )
@@ -3198,8 +3208,9 @@ def foreach_constant_list(site, itername, lst, statement, location, scope):
31983208 TInt (32 , True ))
31993209 for dim in range (len (items .dimsizes )))
32003210 loopscope = Symtab (scope )
3201- loopscope .add (ExpressionSymbol (
3202- itername , items .expr (loopvars ), site ))
3211+ if not (itername == '_' and site .dml_version () != (1 , 2 )):
3212+ loopscope .add (ExpressionSymbol (
3213+ itername , items .expr (loopvars ), site ))
32033214 stmt = codegen_statement (statement , location , loopscope )
32043215
32053216 if stmt .is_empty :
@@ -3584,6 +3595,7 @@ def codegen_inline(site, meth_node, indices, inargs, outargs,
35843595 # call is safe.
35853596 return codegen_call (site , meth_node , indices ,
35863597 inargs , outargs )
3598+ pre = []
35873599 for (arg , (parmname , parmtype ), argno ) in zip (inargs , meth_node .inp ,
35883600 list (range (len (inargs )))):
35893601 # Create an alias
@@ -3604,7 +3616,12 @@ def codegen_inline(site, meth_node, indices, inargs, outargs,
36043616 raise ECONSTP (site , parmname , "method call" )
36053617 arg = coerce_if_eint (arg )
36063618
3607- if inhibit_copyin or undefined (arg ):
3619+ if (parmname == '_'
3620+ and meth_node .astcode .site .dml_version () != (1 , 2 )):
3621+ if not arg .constant :
3622+ pre .append (mkExpressionStatement (arg .site , arg ,
3623+ explicit_discard = True ))
3624+ elif inhibit_copyin or undefined (arg ):
36083625 param_scope .add (ExpressionSymbol (parmname , arg , arg .site ))
36093626 elif arg .constant and (
36103627 parmtype is None
@@ -3661,13 +3678,13 @@ def codegen_inline(site, meth_node, indices, inargs, outargs,
36613678 exit_handler = GotoExit_dml14 (outargs )
36623679 with exit_handler :
36633680 code = codegen_statements (subs , location , param_scope )
3664- decls = declarations (param_scope )
3681+ pre . extend ( declarations (param_scope ) )
36653682 post = ([mkLabel (site , exit_handler .label )]
36663683 if exit_handler .used else [])
3667- body = mkCompound (site , decls + code , rbrace_site )
3684+ body = mkCompound (site , pre + code , rbrace_site )
36683685 if meth_node .outp and body .control_flow ().fallthrough :
36693686 report (ENORET (meth_node .astcode .site ))
3670- return mkInlinedMethod (site , meth_node , decls , code , post )
3687+ return mkInlinedMethod (site , meth_node , pre , code , post )
36713688
36723689def c_rettype (outp , throws ):
36733690 if throws :
@@ -3725,14 +3742,24 @@ def __init__(self, method, inp, outp, throws, independent, startup,
37253742
37263743 # rettype is the return type of the C function
37273744 self .rettype = c_rettype (outp , throws )
3745+ cparams = list (cparams )
3746+
3747+ if method .astcode .site .dml_version () != (1 , 2 ):
3748+ discarded = 0
3749+ for (i , (n , t )) in enumerate (cparams ):
3750+ if n == '_' :
3751+ cparams [i ] = (f'_unused_{ discarded } ' , t )
3752+ discarded += 1
3753+
37283754 self .cparams = c_inargs (
3729- implicit_params (method ) + list ( cparams ) , outp , throws )
3755+ implicit_params (method ) + cparams , outp , throws )
37303756
37313757 @property
37323758 def prototype (self ):
37333759 return self .rettype .declaration (
37343760 "%s(%s)" % (self .get_cname (),
37353761 ", " .join ([t .declaration (n )
3762+ + ' UNUSED' * n .startswith ('_unused_' )
37363763 for (n , t ) in self .cparams ])))
37373764
37383765 def cfunc_expr (self , site ):
@@ -3826,7 +3853,9 @@ def codegen_method_func(func):
38263853 if dml .globals .dml_version == (1 , 2 ) and (
38273854 compat .dml12_misc not in dml .globals .enabled_compat ):
38283855 check_varname (method .site , name )
3829- if isinstance (e , Expression ):
3856+ if (isinstance (e , Expression )
3857+ and not (name == '_'
3858+ and method .astcode .site .dml_version () != (1 , 2 ))):
38303859 inlined_arg = (
38313860 mkInlinedParam (method .site , e , name , e .ctype ())
38323861 if defined (e ) else e )
@@ -3881,7 +3910,9 @@ def codegen_method(site, inp, outp, throws, independent, memoization, ast,
38813910 with (crep .DeviceInstanceContext () if not independent
38823911 else contextlib .nullcontext ()):
38833912 for (arg , etype ) in inp :
3884- fnscope .add_variable (arg , type = etype , site = site , make_unique = False )
3913+ if not (arg == '_' and ast .site .dml_version () != (1 , 2 )):
3914+ fnscope .add_variable (arg , type = etype , site = site ,
3915+ make_unique = False )
38853916 initializers = [get_initializer (site , parmtype , None , None , None )
38863917 for (_ , parmtype ) in outp ]
38873918
0 commit comments