Skip to content

Commit db0d678

Browse files
committed
Support "Functor-like" code_typed invocation
This allows you to call, e.g., `code_typed((Foo, Int, Int))` to query the code for `(::Foo)(::Int, ::Int)`
1 parent 0d4d6d9 commit db0d678

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

base/reflection.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,32 @@ julia> code_typed(+, (Float64, Float64))
211211
1 ─ %1 = Base.add_float(x, y)::Float64
212212
└── return %1
213213
) => Float64
214+
215+
julia> code_typed((typeof(-), Float64, Float64))
216+
1-element Vector{Any}:
217+
CodeInfo(
218+
1 ─ %1 = Base.sub_float(x, y)::Float64
219+
└── return %1
220+
) => Float64
221+
222+
julia> code_typed((Type{Int}, UInt8))
223+
1-element Vector{Any}:
224+
CodeInfo(
225+
1 ─ %1 = Core.zext_int(Core.Int64, x)::Int64
226+
└── return %1
227+
) => Int64
228+
229+
julia> code_typed((Base.Broadcast.Pick{2}, Tuple{Bool,Float64,Int64}))
230+
1-element Vector{Any}:
231+
CodeInfo(
232+
1 ─ %1 = \$(Expr(:boundscheck, true))::Bool
233+
│ %2 = Base.getfield(args, 2, %1)::Float64
234+
└── return %2
235+
) => Float64
214236
```
215237
"""
238+
function code_typed end
239+
216240
function code_typed(@nospecialize(f), @nospecialize(types=default_tt(f)); kwargs...)
217241
if isa(f, Core.OpaqueClosure)
218242
return code_typed_opaque_closure(f, types; kwargs...)
@@ -221,6 +245,12 @@ function code_typed(@nospecialize(f), @nospecialize(types=default_tt(f)); kwargs
221245
return code_typed_by_type(tt; kwargs...)
222246
end
223247

248+
# support 'functor'-like queries, such as `(::Foo)(::Int, ::Int)` via `code_typed((Foo, Int, Int))`
249+
function code_typed(@nospecialize(argtypes::Union{Tuple,Type{<:Tuple}}); kwargs...)
250+
tt = to_tuple_type(argtypes)
251+
return code_typed_by_type(tt; kwargs...)
252+
end
253+
224254
# returns argument tuple type which is supposed to be used for `code_typed` and its family;
225255
# if there is a single method this functions returns the method argument signature,
226256
# otherwise returns `Tuple` that doesn't match with any signature

0 commit comments

Comments
 (0)