|
| 1 | +# The content of this file should be upstreamed to Julia proper |
| 2 | + |
| 3 | +function method_instances(@nospecialize(tt::Type), world::UInt=Base.get_world_counter()) |
| 4 | + return map(Core.Compiler.specialize_method, method_matches(tt; world)) |
| 5 | +end |
| 6 | + |
| 7 | +if VERSION >= v"1.7-" |
| 8 | + const hasgenerator = Base.hasgenerator |
| 9 | +else |
| 10 | + const hasgenerator = Base.isgenerated |
| 11 | +end |
| 12 | + |
| 13 | +function code_lowered_by_type(@nospecialize(tt); generated::Bool=true, debuginfo::Symbol=:default) |
| 14 | + |
| 15 | + debuginfo = Base.IRShow.debuginfo(debuginfo) |
| 16 | + if debuginfo !== :source && debuginfo !== :none |
| 17 | + throw(ArgumentError("'debuginfo' must be either :source or :none")) |
| 18 | + end |
| 19 | + return map(method_instances(tt)) do m |
| 20 | + if generated && hasgenerator(m) |
| 21 | + if Base.may_invoke_generator(m) |
| 22 | + return ccall(:jl_code_for_staged, Any, (Any,), m)::CodeInfo |
| 23 | + else |
| 24 | + error("Could not expand generator for `@generated` method ", m, ". ", |
| 25 | + "This can happen if the provided argument types (", t, ") are ", |
| 26 | + "not leaf types, but the `generated` argument is `true`.") |
| 27 | + end |
| 28 | + end |
| 29 | + code = Base.uncompressed_ir(m.def::Method) |
| 30 | + debuginfo === :none && Base.remove_linenums!(code) |
| 31 | + return code |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +function code_warntype_by_type(io::IO, @nospecialize(tt); |
| 36 | + debuginfo::Symbol=:default, optimize::Bool=false, kwargs...) |
| 37 | + debuginfo = Base.IRShow.debuginfo(debuginfo) |
| 38 | + lineprinter = Base.IRShow.__debuginfo[debuginfo] |
| 39 | + for (src, rettype) in Base.code_typed_by_type(tt; optimize, kwargs...) |
| 40 | + if !(src isa Core.CodeInfo) |
| 41 | + println(io, src) |
| 42 | + println(io, " failed to infer") |
| 43 | + continue |
| 44 | + end |
| 45 | + lambda_io::IOContext = io |
| 46 | + p = src.parent |
| 47 | + nargs::Int = 0 |
| 48 | + if p isa Core.MethodInstance |
| 49 | + println(io, p) |
| 50 | + print(io, " from ") |
| 51 | + println(io, p.def) |
| 52 | + p.def isa Method && (nargs = p.def.nargs) |
| 53 | + if !isempty(p.sparam_vals) |
| 54 | + println(io, "Static Parameters") |
| 55 | + sig = p.def.sig |
| 56 | + warn_color = Base.warn_color() # more mild user notification |
| 57 | + for i = 1:length(p.sparam_vals) |
| 58 | + sig = sig::UnionAll |
| 59 | + name = sig.var.name |
| 60 | + val = p.sparam_vals[i] |
| 61 | + print_highlighted(io::IO, v::String, color::Symbol) = |
| 62 | + if highlighting[:warntype] |
| 63 | + Base.printstyled(io, v; color) |
| 64 | + else |
| 65 | + Base.print(io, v) |
| 66 | + end |
| 67 | + if val isa TypeVar |
| 68 | + if val.lb === Union{} |
| 69 | + print(io, " ", name, " <: ") |
| 70 | + print_highlighted(io, "$(val.ub)", warn_color) |
| 71 | + elseif val.ub === Any |
| 72 | + print(io, " ", sig.var.name, " >: ") |
| 73 | + print_highlighted(io, "$(val.lb)", warn_color) |
| 74 | + else |
| 75 | + print(io, " ") |
| 76 | + print_highlighted(io, "$(val.lb)", warn_color) |
| 77 | + print(io, " <: ", sig.var.name, " <: ") |
| 78 | + print_highlighted(io, "$(val.ub)", warn_color) |
| 79 | + end |
| 80 | + elseif val isa typeof(Vararg) |
| 81 | + print(io, " ", name, "::") |
| 82 | + print_highlighted(io, "Int", warn_color) |
| 83 | + else |
| 84 | + print(io, " ", sig.var.name, " = ") |
| 85 | + print_highlighted(io, "$(val)", :cyan) # show the "good" type |
| 86 | + end |
| 87 | + println(io) |
| 88 | + sig = sig.body |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + if src.slotnames !== nothing |
| 93 | + slotnames = Base.sourceinfo_slotnames(src) |
| 94 | + lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => slotnames) |
| 95 | + slottypes = src.slottypes |
| 96 | + nargs > 0 && println(io, "Arguments") |
| 97 | + for i = 1:length(slotnames) |
| 98 | + if i == nargs + 1 |
| 99 | + println(io, "Locals") |
| 100 | + end |
| 101 | + print(io, " ", slotnames[i]) |
| 102 | + if isa(slottypes, Vector{Any}) |
| 103 | + InteractiveUtils.warntype_type_printer(io, slottypes[i], true) |
| 104 | + end |
| 105 | + println(io) |
| 106 | + end |
| 107 | + end |
| 108 | + print(io, "Body") |
| 109 | + InteractiveUtils.warntype_type_printer(io, rettype, true) |
| 110 | + println(io) |
| 111 | +@static if VERSION < v"1.7.0" |
| 112 | + Base.IRShow.show_ir(lambda_io, src, lineprinter(src), InteractiveUtils.warntype_type_printer) |
| 113 | +else |
| 114 | + irshow_config = Base.IRShow.IRShowConfig(lineprinter(src), InteractiveUtils.warntype_type_printer) |
| 115 | + Base.IRShow.show_ir(lambda_io, src, irshow_config) |
| 116 | +end |
| 117 | + println(io) |
| 118 | + end |
| 119 | + nothing |
| 120 | +end |
0 commit comments