|
| 1 | +""" |
| 2 | + signature(m::Method) -> Dict{Symbol,Any} |
| 3 | +
|
| 4 | +Finds the expression for a method's signature as broken up into its various components |
| 5 | +including: |
| 6 | +
|
| 7 | +- `:name`: Name of the function |
| 8 | +- `:params`: Parametric types defined on constructors |
| 9 | +- `:args`: Positional arguments of the function |
| 10 | +- `:whereparams`: Where parameters |
| 11 | +
|
| 12 | +All components listed above may not be present in the returned dictionary if they are |
| 13 | +not in the function definition. |
| 14 | +
|
| 15 | +Limited support for: |
| 16 | +- `:kwargs`: Keyword arguments of the function. |
| 17 | + Only the names will be included, not the default values or type constraints. |
| 18 | +
|
| 19 | +Unsupported: |
| 20 | +- `:rtype`: Return type of the function |
| 21 | +- `:body`: Function body0 |
| 22 | +- `:head`: Expression head of the function definition (`:function`, `:(=)`, `:(->)`) |
| 23 | +
|
| 24 | +For more complete coverage, consider using [`splitdef`](@ref) |
| 25 | +with [`CodeTracking.definition`](https://github.com/timholy/CodeTracking.jl). |
| 26 | +
|
| 27 | +The dictionary of components returned by `signature` match those returned by |
| 28 | +[`splitdef`](@ref) and include all that are required by [`combinedef`](@ref), except for |
| 29 | +the `:body` component. |
| 30 | +""" |
| 31 | +function signature(m::Method) |
| 32 | + def = Dict{Symbol, Any}() |
| 33 | + def[:name] = m.name |
| 34 | + |
| 35 | + def[:args] = arguments(m) |
| 36 | + def[:whereparams] = where_parameters(m) |
| 37 | + def[:params] = parameters(m) |
| 38 | + def[:kwargs] = kwargs(m) |
| 39 | + |
| 40 | + return Dict(k => v for (k, v) in def if v !== nothing) # filter out nonfields. |
| 41 | +end |
| 42 | + |
| 43 | +function slot_names(m::Method) |
| 44 | + ci = Base.uncompressed_ast(m) |
| 45 | + return ci.slotnames |
| 46 | +end |
| 47 | + |
| 48 | +function argument_names(m::Method) |
| 49 | + slot_syms = slot_names(m) |
| 50 | + @assert slot_syms[1] === Symbol("#self#") |
| 51 | + arg_names = slot_syms[2:m.nargs] # nargs includes 1 for `#self#` |
| 52 | + return arg_names |
| 53 | +end |
| 54 | + |
| 55 | + |
| 56 | +function argument_types(m::Method) |
| 57 | + # First parameter of `sig` is the type of the function itself |
| 58 | + return parameters(m.sig)[2:end] |
| 59 | +end |
| 60 | + |
| 61 | +name_of_type(x) = x |
| 62 | +name_of_type(tv::TypeVar) = tv.name |
| 63 | +function name_of_type(x::DataType) |
| 64 | + name_sym = Symbol(x.name) |
| 65 | + if isempty(x.parameters) |
| 66 | + return name_sym |
| 67 | + else |
| 68 | + parameter_names = name_of_type.(x.parameters) |
| 69 | + return :($(name_sym){$(parameter_names...)}) |
| 70 | + end |
| 71 | +end |
| 72 | +function name_of_type(x::UnionAll) |
| 73 | + name = name_of_type(x.body) |
| 74 | + whereparam = where_parameters(x.var) |
| 75 | + return :($name where $whereparam) |
| 76 | +end |
| 77 | + |
| 78 | + |
| 79 | +function arguments(m::Method) |
| 80 | + arg_names = argument_names(m) |
| 81 | + arg_types = argument_types(m) |
| 82 | + map(arg_names, arg_types) do name, type |
| 83 | + has_name = name !== Symbol("#unused#") |
| 84 | + type_name = name_of_type(type) |
| 85 | + if type === Any && has_name |
| 86 | + name |
| 87 | + elseif has_name |
| 88 | + :($name::$type_name) |
| 89 | + else |
| 90 | + :(::$type_name) |
| 91 | + end |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +function where_parameters(x::TypeVar) |
| 96 | + if x.lb === Union{} && x.ub === Any |
| 97 | + return x.name |
| 98 | + elseif x.lb === Union{} |
| 99 | + return :($(x.name) <: $(Symbol(x.ub))) |
| 100 | + elseif x.ub === Any |
| 101 | + return :($(x.name) >: $(Symbol(x.lb))) |
| 102 | + else |
| 103 | + return :($(Symbol(x.lb)) <: $(x.name) <: $(Symbol(x.ub))) |
| 104 | + end |
| 105 | +end |
| 106 | + |
| 107 | +function where_parameters(m::Method) |
| 108 | + m.sig isa UnionAll || return nothing |
| 109 | + |
| 110 | + whereparams = [] |
| 111 | + sig = m.sig |
| 112 | + while sig isa UnionAll |
| 113 | + push!(whereparams, where_parameters(sig.var)) |
| 114 | + sig = sig.body |
| 115 | + end |
| 116 | + return whereparams |
| 117 | +end |
| 118 | + |
| 119 | +function parameters(m::Method) |
| 120 | + typeof_type = first(parameters(m.sig)) # will be e.g Type{Foo{P}} if it has any parameters |
| 121 | + typeof_type <: Type{<:Any} || return nothing |
| 122 | + |
| 123 | + function_type = first(parameters(typeof_type)) # will be e.g. Foo{P} |
| 124 | + parameter_types = parameters(function_type) |
| 125 | + return [name_of_type(type) for type in parameter_types] |
| 126 | +end |
| 127 | + |
| 128 | +function kwargs(m::Method) |
| 129 | + names = kwarg_names(m) |
| 130 | + isempty(names) && return nothing # we know it has no keywords. |
| 131 | + # TODO: Enhance this to support more than just their names |
| 132 | + # see https://github.com/invenia/ExprTools.jl/issues/6 |
| 133 | + return names |
| 134 | +end |
| 135 | + |
| 136 | +function kwarg_names(m::Method) |
| 137 | + mt = Base.get_methodtable(m) |
| 138 | + !isdefined(mt, :kwsorter) && return [] # no kwsorter means no keywords for sure. |
| 139 | + return Base.kwarg_decl(m, typeof(mt.kwsorter)) |
| 140 | +end |
0 commit comments