77# -------------------------------------------------------------------------------
88# Functions/types used by code emitted from lowering, but not called by it directly
99
10+ @inline function _invoke_in_world (w:: UInt , f:: F , @nospecialize (args... )) where {F}
11+ if ccall (:jl_is_in_pure_context , Int8, ()) != 0
12+ # Similar to `Base.invoke_in_world` but also works inside generated-function
13+ # expansion (see `jl_code_for_staged`)
14+ return Core. _call_in_world_total (w, f, args... )
15+ end
16+ return Base. invoke_in_world (w, f, args... )
17+ end
18+
19+ # Re-dispatch `f(args...)` at the pinned lowering world (see `jl_lowering_world`)
20+ @inline function invoke_in_lowering_world (f:: F , @nospecialize (args... )) where {F}
21+ w = unsafe_load (cglobal (:jl_lowering_world , Csize_t))
22+ if w == 0
23+ # Fallback when the Base lowering hook is not set up
24+ w = Base. tls_world_age ()
25+ # FIXME : as a side effect, enabling the Base lowering hook now affects
26+ # JuliaLowering execution not passing through the hook
27+ end
28+ return _invoke_in_world (w, f, args... )
29+ end
30+
1031# Return the current exception. In JuliaLowering we use this rather than the
1132# special form `K"the_exception"` to reduce the number of special forms.
1233Base. @assume_effects :removable function current_exception ()
1334 @ccall jl_current_exception (current_task ():: Any ):: Any
1435end
1536
16- function _interpolate_expr (@nospecialize (ex), depth, @nospecialize (vals:: Tuple ), val_i)
37+ function __interpolate_expr (@nospecialize (ex), depth, @nospecialize (vals:: Tuple ), val_i)
1738 if ex isa QuoteNode
18- out = _interpolate_expr (Expr (:inert , ex. value), depth, vals, val_i)
39+ out = __interpolate_expr (Expr (:inert , ex. value), depth, vals, val_i)
1940 QuoteNode (only (out. args))
2041 elseif ! (ex isa Expr)
2142 ex
@@ -30,18 +51,21 @@ function _interpolate_expr(@nospecialize(ex), depth, @nospecialize(vals::Tuple),
3051 push! (cs_out, v)
3152 end
3253 else
33- push! (cs_out, _interpolate_expr (e, inner_depth, vals, val_i))
54+ push! (cs_out, __interpolate_expr (e, inner_depth, vals, val_i))
3455 end
3556 end
3657 Expr (ex. head, cs_out... )
3758 end
3859end
39- function interpolate_expr (@nospecialize (ex), @nospecialize (values... ))
60+ function _interpolate_expr (@nospecialize (ex), @nospecialize (values:: Tuple ))
4061 @jl_assert ! Meta. isexpr (ex, :$ ) (expr_to_est (ex), " expand_quote should handle this" )
41- _interpolate_expr (ex, 0 , values, Ref (0 ))
62+ __interpolate_expr (ex, 0 , values, Ref (0 ))
63+ end
64+ function interpolate_expr (@nospecialize (ex), @nospecialize (values... ))
65+ return invoke_in_lowering_world (_interpolate_expr, ex, values)
4266end
4367
44- function _interpolate_syntax (st:: SyntaxTree , depth, @nospecialize (vals), val_i)
68+ function __interpolate_syntax (st:: SyntaxTree , depth, @nospecialize (vals), val_i)
4569 is_leaf (st) && return mkleaf (st)
4670 k = kind (st)
4771 inner_depth = k == K " syntaxquote" ? depth + 1 :
@@ -58,19 +82,22 @@ function _interpolate_syntax(st::SyntaxTree, depth, @nospecialize(vals), val_i)
5882 push! (cs_out, v2)
5983 end
6084 else
61- push! (cs_out, _interpolate_syntax (c, inner_depth, vals, val_i))
85+ push! (cs_out, __interpolate_syntax (c, inner_depth, vals, val_i))
6286 end
6387 end
6488 mknode (st, cs_out)
6589end
66- function interpolate_syntax (st:: SyntaxTree , @nospecialize (vals... ))
90+ function _interpolate_syntax (st:: SyntaxTree , @nospecialize (vals:: Tuple ))
6791 st = copy_ast (ensure_macro_attributes! (SyntaxGraph ()), st)
6892 val_i = Ref (0 )
69- out = _interpolate_syntax ((@ast st. _graph st [K " None" st]), 0 , vals, val_i)
93+ out = __interpolate_syntax ((@ast st. _graph st [K " None" st]), 0 , vals, val_i)
7094 @jl_assert val_i[] == length (vals) st
7195 @jl_assert numchildren (out) == 1 st
7296 out[1 ]
7397end
98+ function interpolate_syntax (st:: SyntaxTree , @nospecialize (vals... ))
99+ return invoke_in_lowering_world (_interpolate_syntax, st, vals)
100+ end
74101
75102# --------------------------------------------------
76103# Functions called by closure conversion
@@ -246,6 +273,18 @@ function (g::GeneratedFunctionStub)(world::UInt, source::Method, @nospecialize a
246273
247274 # Run code generator - this acts like a macro expander
248275 ex0 = g. gen (sc, args... )
276+
277+ # Note that we expand in `tls_world_age()` (see Core.GeneratedFunctionStub)
278+ world = Base. tls_world_age ()
279+
280+ # Lower the generated code in the lowering world
281+ return invoke_in_lowering_world (_lower_generated_code, g, source, graph, sc,
282+ __module__, world, ex0)
283+ end
284+
285+ function _lower_generated_code (g:: GeneratedFunctionStub , source:: Method , graph,
286+ sc:: SyntaxContext , __module__:: Module ,
287+ world:: UInt , @nospecialize (ex0))
249288 if ex0 isa Expr
250289 ex0 = expr_to_est (
251290 graph, ex0, source_location (LineNumberNode, g. srcref))
@@ -261,8 +300,6 @@ function (g::GeneratedFunctionStub)(world::UInt, source::Method, @nospecialize a
261300 end
262301
263302 @jl_assert base_layer (sc). mod == __module__ ex0
264- # Note that we expand in `tls_world_age()` (see Core.GeneratedFunctionStub)
265- world = Base. tls_world_age ()
266303 ex0 = JuliaSyntax. fill_context! (ex0, sc)
267304 ctx1 = MacroExpansionContext (ex0, world, true )
268305 ex1 = expand_forms_1 (ctx1, ex0)
317354#
318355# (This should do what fl_defined_julia_global does for flisp lowering)
319356function is_defined_and_owned_global (mod, name, world:: UInt = Base. get_world_counter ())
320- return Base . invoke_in_world (world, Base. binding_kind, mod, name) === Base. PARTITION_KIND_GLOBAL
357+ return _invoke_in_world (world, Base. binding_kind, mod, name) === Base. PARTITION_KIND_GLOBAL
321358end
322359
323360# "Reserve" a binding: create the binding if it doesn't exist but do not assign
0 commit comments