From 513743a38391c54c1186b999ac6f9051a99836f3 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 24 Aug 2020 08:10:25 -0500 Subject: [PATCH] Support MethodList This is nice to collect MethodInstances from several methods. --- src/MethodAnalysis.jl | 4 ++-- src/visit.jl | 12 ++++++++++++ test/runtests.jl | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/MethodAnalysis.jl b/src/MethodAnalysis.jl index c06b94d..8ceff8a 100644 --- a/src/MethodAnalysis.jl +++ b/src/MethodAnalysis.jl @@ -116,10 +116,10 @@ end methodinstances(mod::Module) methodinstances(f) -Collect all `MethodInstance`s, optionally restricting them to a particular module or function. +Collect all `MethodInstance`s, optionally restricting them to a particular module, function, method, or methodlist. """ function methodinstances(top=()) - if isa(top, Module) || isa(top, Function) || isa(top, Type) + if isa(top, Module) || isa(top, Function) || isa(top, Type) || isa(top, Method) || isa(top, Base.MethodList) top = (top,) end mis = Core.MethodInstance[] diff --git a/src/visit.jl b/src/visit.jl index 9572e58..c258406 100644 --- a/src/visit.jl +++ b/src/visit.jl @@ -100,6 +100,18 @@ function _visit(@nospecialize(operation), @nospecialize(f::Callable), visited::I return nothing end +function _visit(@nospecialize(operation), ml::Base.MethodList, visited::IdSet{Any}, print::Bool) + ml ∈ visited && return nothing + push!(visited, ml) + print && println("MethodList ", ml) + _visit(operation, ml.mt, visited, print) + for m in ml.ms + _visit(operation, m, visited, print) + end + return nothing +end + + function _visit(@nospecialize(operation), mt::MethodTable, visited::IdSet{Any}, print::Bool) mt ∈ visited && return nothing push!(visited, mt) diff --git a/test/runtests.jl b/test/runtests.jl index 7c62599..ef7b96c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -79,6 +79,14 @@ end @test mis isa Vector{Core.MethodInstance} @test mi ∈ mis @test length(mis) > 1 + + mi = methodinstance(convert, (Type{String}, String)) + mis = methodinstances(methods(convert, (Type{String}, Any))) + @test length(mis) > 10 # in fact, there are many more + @test mi ∈ mis + mis = methodinstances(which(convert, (Type{String}, AbstractString))) + @test length(mis) > 2 + @test mi ∉ mis # that's covered by a different Method end @testset "Backedges" begin