@@ -11,6 +11,7 @@ generates a new layer.
1111struct ScopeLayer
1212 id:: LayerId
1313 mod:: Module
14+ parent_layer:: LayerId # Index of parent layer in a macro expansion. Equal to 0 for no parent
1415 is_macro_expansion:: Bool # FIXME
1516end
1617
@@ -21,6 +22,11 @@ struct MacroExpansionContext{GraphType} <: AbstractLoweringContext
2122 scope_layer_stack:: Vector{LayerId}
2223end
2324
25+ function MacroExpansionContext (graph:: SyntaxGraph , mod:: Module )
26+ layers = ScopeLayer[ScopeLayer (1 , mod, 0 , false )]
27+ MacroExpansionContext (graph, Bindings (), layers, LayerId[length (layers)])
28+ end
29+
2430current_layer (ctx:: MacroExpansionContext ) = ctx. scope_layers[last (ctx. scope_layer_stack)]
2531current_layer_id (ctx:: MacroExpansionContext ) = last (ctx. scope_layer_stack)
2632
@@ -254,7 +260,8 @@ function expand_macro(ctx, ex)
254260 # method was defined (may be different from `parentmodule(macfunc)`)
255261 mod_for_ast = lookup_method_instance (macfunc, macro_args,
256262 macro_world). def. module
257- new_layer = ScopeLayer (length (ctx. scope_layers)+ 1 , mod_for_ast, true )
263+ new_layer = ScopeLayer (length (ctx. scope_layers)+ 1 , mod_for_ast,
264+ current_layer_id (ctx), true )
258265 push! (ctx. scope_layers, new_layer)
259266 push! (ctx. scope_layer_stack, new_layer. id)
260267 expanded = expand_forms_1 (ctx, expanded)
@@ -324,8 +331,9 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
324331 push! (ctx. scope_layer_stack, top_layer)
325332 escaped_ex
326333 elseif k == K " hygienic_scope"
327- @chk numchildren (ex) >= 2 && ex[2 ]. value isa Module " `hygienic_scope` requires an AST and a module"
328- new_layer = ScopeLayer (length (ctx. scope_layers)+ 1 , ex[2 ]. value, true )
334+ @chk numchildren (ex) >= 2 && ex[2 ]. value isa Module (ex," `hygienic_scope` requires an AST and a module" )
335+ new_layer = ScopeLayer (length (ctx. scope_layers)+ 1 , ex[2 ]. value,
336+ current_layer_id (ctx), true )
329337 push! (ctx. scope_layers, new_layer)
330338 push! (ctx. scope_layer_stack, new_layer. id)
331339 hyg_ex = expand_forms_1 (ctx, ex[1 ])
@@ -433,8 +441,7 @@ function expand_forms_1(mod::Module, ex::SyntaxTree)
433441 scope_layer= LayerId,
434442 __macro_ctx__= Nothing,
435443 meta= CompileHints)
436- layers = ScopeLayer[ScopeLayer (1 , mod, false )]
437- ctx = MacroExpansionContext (graph, Bindings (), layers, LayerId[1 ])
444+ ctx = MacroExpansionContext (graph, mod)
438445 ex2 = expand_forms_1 (ctx, reparent (ctx, ex))
439446 graph2 = delete_attributes (graph, :__macro_ctx__ )
440447 # TODO : Returning the context with pass-specific mutable data is a bad way
0 commit comments