@@ -1081,10 +1081,31 @@ function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
1081
1081
debuginfo:: Symbol = :default ,
1082
1082
world = get_world_counter (),
1083
1083
interp = Core. Compiler. NativeInterpreter (world))
1084
- ccall (:jl_is_in_pure_context , Bool, ()) && error (" code reflection cannot be used from generated functions" )
1085
1084
if isa (f, Core. Builtin)
1086
1085
throw (ArgumentError (" argument is not a generic function" ))
1087
1086
end
1087
+ ft = Core. Typeof (f)
1088
+ if isa (types, Type)
1089
+ u = unwrap_unionall (types)
1090
+ tt = rewrap_unionall (Tuple{ft, u. parameters... }, types)
1091
+ else
1092
+ tt = Tuple{ft, types... }
1093
+ end
1094
+ return code_typed_by_type (tt; optimize, debuginfo, world, interp)
1095
+ end
1096
+
1097
+ """
1098
+ code_typed_by_type(types::Type{<:Tuple}; ...)
1099
+
1100
+ Similar to [`code_typed`](@ref), except the argument is a tuple type describing
1101
+ a full signature to query.
1102
+ """
1103
+ function code_typed_by_type (@nospecialize (tt:: Type );
1104
+ optimize= true ,
1105
+ debuginfo:: Symbol = :default ,
1106
+ world = get_world_counter (),
1107
+ interp = Core. Compiler. NativeInterpreter (world))
1108
+ ccall (:jl_is_in_pure_context , Bool, ()) && error (" code reflection cannot be used from generated functions" )
1088
1109
if @isdefined (IRShow)
1089
1110
debuginfo = IRShow. debuginfo (debuginfo)
1090
1111
elseif debuginfo === :default
@@ -1093,10 +1114,14 @@ function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
1093
1114
if debuginfo != = :source && debuginfo != = :none
1094
1115
throw (ArgumentError (" 'debuginfo' must be either :source or :none" ))
1095
1116
end
1096
- types = to_tuple_type (types)
1117
+ tt = to_tuple_type (tt)
1118
+ meths = _methods_by_ftype (tt, - 1 , world)
1119
+ if meths === false
1120
+ error (" signature does not correspond to a generic function" )
1121
+ end
1097
1122
asts = []
1098
- for x in _methods (f, types, - 1 , world)
1099
- meth = func_for_method_checked (x[3 ], types , x[2 ])
1123
+ for x in meths
1124
+ meth = func_for_method_checked (x[3 ], tt , x[2 ])
1100
1125
(code, ty) = Core. Compiler. typeinf_code (interp, meth, x[1 ], x[2 ], optimize)
1101
1126
code === nothing && error (" inference not successful" ) # inference disabled?
1102
1127
debuginfo === :none && remove_linenums! (code)
@@ -1135,6 +1160,15 @@ function which(@nospecialize(f), @nospecialize(t))
1135
1160
end
1136
1161
t = to_tuple_type (t)
1137
1162
tt = signature_type (f, t)
1163
+ return which (tt)
1164
+ end
1165
+
1166
+ """
1167
+ which(types::Type{<:Tuple})
1168
+
1169
+ Returns the method that would be called by the given type signature (as a tuple type).
1170
+ """
1171
+ function which (@nospecialize (tt:: Type ))
1138
1172
m = ccall (:jl_gf_invoke_lookup , Any, (Any, UInt), tt, typemax (UInt))
1139
1173
if m === nothing
1140
1174
error (" no unique matching method found for the specified argument types" )
0 commit comments