Skip to content

Commit 4931faa

Browse files
authored
allows Core.Compiler to use @invoke and @invokelatest (#41635)
A simple code organization refactor to allow `Core.Compiler` to use `@invoke` and `@invokelatest`, which might be necessary for incoming compiler plugin prototyping.
1 parent 114ee17 commit 4931faa

File tree

3 files changed

+49
-50
lines changed

3 files changed

+49
-50
lines changed

base/essentials.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ call obsolete versions of a function `f`.
713713
`f` directly, and the type of the result cannot be inferred by the compiler.)
714714
"""
715715
function invokelatest(@nospecialize(f), @nospecialize args...; kwargs...)
716-
kwargs = Base.merge(NamedTuple(), kwargs)
716+
kwargs = merge(NamedTuple(), kwargs)
717717
if isempty(kwargs)
718718
return Core._call_latest(f, args...)
719719
end

base/reflection.jl

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,6 @@ min_world(m::Core.CodeInfo) = m.min_world
16451645
max_world(m::Core.CodeInfo) = m.max_world
16461646
get_world_counter() = ccall(:jl_get_world_counter, UInt, ())
16471647

1648-
16491648
"""
16501649
propertynames(x, private=false)
16511650
@@ -1676,3 +1675,51 @@ Return a boolean indicating whether the object `x` has `s` as one of its own pro
16761675
See also: [`propertynames`](@ref), [`hasfield`](@ref).
16771676
"""
16781677
hasproperty(x, s::Symbol) = s in propertynames(x)
1678+
1679+
"""
1680+
@invoke f(arg::T, ...; kwargs...)
1681+
1682+
Provides a convenient way to call [`invoke`](@ref);
1683+
`@invoke f(arg1::T1, arg2::T2; kwargs...)` will be expanded into `invoke(f, Tuple{T1,T2}, arg1, arg2; kwargs...)`.
1684+
When an argument's type annotation is omitted, it's specified as `Any` argument, e.g.
1685+
`@invoke f(arg1::T, arg2)` will be expanded into `invoke(f, Tuple{T,Any}, arg1, arg2)`.
1686+
"""
1687+
macro invoke(ex)
1688+
f, args, kwargs = destructure_callex(ex)
1689+
arg2typs = map(args) do x
1690+
isexpr(x, :(::)) ? (x.args...,) : (x, GlobalRef(Core, :Any))
1691+
end
1692+
args, argtypes = first.(arg2typs), last.(arg2typs)
1693+
return esc(:($(GlobalRef(Core, :invoke))($(f), Tuple{$(argtypes...)}, $(args...); $(kwargs...))))
1694+
end
1695+
1696+
"""
1697+
@invokelatest f(args...; kwargs...)
1698+
1699+
Provides a convenient way to call [`Base.invokelatest`](@ref).
1700+
`@invokelatest f(args...; kwargs...)` will simply be expanded into
1701+
`Base.invokelatest(f, args...; kwargs...)`.
1702+
"""
1703+
macro invokelatest(ex)
1704+
f, args, kwargs = destructure_callex(ex)
1705+
return esc(:($(GlobalRef(@__MODULE__, :invokelatest))($(f), $(args...); $(kwargs...))))
1706+
end
1707+
1708+
function destructure_callex(ex)
1709+
isexpr(ex, :call) || throw(ArgumentError("a call expression f(args...; kwargs...) should be given"))
1710+
1711+
f = first(ex.args)
1712+
args = []
1713+
kwargs = []
1714+
for x in ex.args[2:end]
1715+
if isexpr(x, :parameters)
1716+
append!(kwargs, x.args)
1717+
elseif isexpr(x, :kw)
1718+
push!(kwargs, x)
1719+
else
1720+
push!(args, x)
1721+
end
1722+
end
1723+
1724+
return f, args, kwargs
1725+
end

base/util.jl

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -536,54 +536,6 @@ function _kwdef!(blk, params_args, call_args)
536536
blk
537537
end
538538

539-
"""
540-
@invoke f(arg::T, ...; kwargs...)
541-
542-
Provides a convenient way to call [`invoke`](@ref);
543-
`@invoke f(arg1::T1, arg2::T2; kwargs...)` will be expanded into `invoke(f, Tuple{T1,T2}, arg1, arg2; kwargs...)`.
544-
When an argument's type annotation is omitted, it's specified as `Any` argument, e.g.
545-
`@invoke f(arg1::T, arg2)` will be expanded into `invoke(f, Tuple{T,Any}, arg1, arg2)`.
546-
"""
547-
macro invoke(ex)
548-
f, args, kwargs = destructure_callex(ex)
549-
arg2typs = map(args) do x
550-
is_expr(x, :(::)) ? (x.args...,) : (x, GlobalRef(Core, :Any))
551-
end
552-
args, argtypes = first.(arg2typs), last.(arg2typs)
553-
return esc(:($(GlobalRef(Core, :invoke))($(f), Tuple{$(argtypes...)}, $(args...); $(kwargs...))))
554-
end
555-
556-
"""
557-
@invokelatest f(args...; kwargs...)
558-
559-
Provides a convenient way to call [`Base.invokelatest`](@ref).
560-
`@invokelatest f(args...; kwargs...)` will simply be expanded into
561-
`Base.invokelatest(f, args...; kwargs...)`.
562-
"""
563-
macro invokelatest(ex)
564-
f, args, kwargs = destructure_callex(ex)
565-
return esc(:($(GlobalRef(Base, :invokelatest))($(f), $(args...); $(kwargs...))))
566-
end
567-
568-
function destructure_callex(ex)
569-
is_expr(ex, :call) || throw(ArgumentError("a call expression f(args...; kwargs...) should be given"))
570-
571-
f = first(ex.args)
572-
args = []
573-
kwargs = []
574-
for x in ex.args[2:end]
575-
if is_expr(x, :parameters)
576-
append!(kwargs, x.args)
577-
elseif is_expr(x, :kw)
578-
push!(kwargs, x)
579-
else
580-
push!(args, x)
581-
end
582-
end
583-
584-
return f, args, kwargs
585-
end
586-
587539
# testing
588540

589541
"""

0 commit comments

Comments
 (0)