Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some inference based on use of getfield. #134

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/StaticLint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ function (state::Toplevel)(x::EXPR)
end

state.scope != s0 && (state.scope = s0)

if state.file == state.targetfile && hasscope(x) && scopeof(x) !== state.scope && typof(x) !== CSTParser.ModuleH && typof(x) !== CSTParser.BareModule && typof(x) !== CSTParser.FileH && !CSTParser.defines_datatype(x)
for (n,b) in scopeof(x).names
infer_type_by_use(b, state.server)
end
end

return state.scope
end

Expand All @@ -88,6 +95,7 @@ function (state::Delayed)(x::EXPR)

traverse(x, state)

# needs to call to add infer_type_by_use
state.scope != s0 && (state.scope = s0)
return state.scope
end
Expand Down
42 changes: 18 additions & 24 deletions src/imports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ function resolve_import(x, state::State)
end
end

function _mark_import_arg(arg, par, state, u)
function add_to_imported_modules(scope::Scope, name::Symbol, val)
if scope.modules isa Dict
scope.modules[name] = val
else
modules = Dict(name => val)
end
end

function _mark_import_arg(arg, par, state, usinged)
if par !== nothing && (typof(arg) === IDENTIFIER || typof(arg) === MacroName)
if par isa Binding # mark reference to binding
push!(par.refs, arg)
Expand All @@ -65,29 +73,15 @@ function _mark_import_arg(arg, par, state, u)
end
arg.meta.binding = Binding(arg, par, _typeof(par, state), [], nothing, nothing)
end
if u && par isa SymbolServer.ModuleStore
if state.scope.modules isa Dict
state.scope.modules[Symbol(valof(arg))] = par
else
state.scope.modules = Dict(Symbol(valof(arg)) => par)
end
elseif u && par isa Binding && par.val isa SymbolServer.ModuleStore
if state.scope.modules isa Dict
state.scope.modules[Symbol(valof(arg))] = par.val
else
state.scope.modules = Dict(Symbol(valof(arg)) => par.val)
end
elseif u && par isa Binding && par.val isa EXPR && (typof(par.val) === CSTParser.ModuleH || typof(par.val) === CSTParser.BareModule)
if state.scope.modules isa Dict
state.scope.modules[Symbol(valof(arg))] = scopeof(par.val)
else
state.scope.modules = Dict(Symbol(valof(arg)) => scopeof(par.val))
end
elseif u && par isa Binding && par.val isa Binding && par.val.val isa EXPR && (typof(par.val.val) === CSTParser.ModuleH || typof(par.val.val) === CSTParser.BareModule)
if state.scope.modules isa Dict
state.scope.modules[Symbol(valof(arg))] = scopeof(par.val.val)
else
state.scope.modules = Dict(Symbol(valof(arg)) => scopeof(par.val.val))
if usinged
if par isa SymbolServer.ModuleStore
add_to_imported_modules(state.scope, Symbol(valof(arg)), par)
elseif par isa Binding && par.val isa SymbolServer.ModuleStore
add_to_imported_modules(state.scope, Symbol(valof(arg)), par.val)
elseif par isa Binding && par.val isa EXPR && (typof(par.val) === CSTParser.ModuleH || typof(par.val) === CSTParser.BareModule)
add_to_imported_modules(state.scope, Symbol(valof(arg)), scopeof(par.val))
elseif par isa Binding && par.val isa Binding && par.val.val isa EXPR && (typof(par.val.val) === CSTParser.ModuleH || typof(par.val.val) === CSTParser.BareModule)
add_to_imported_modules(state.scope, Symbol(valof(arg)), scopeof(par.val.val))
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ mutable struct FileServer <: AbstractServer
roots::Set{File}
symbolserver::SymbolServer.EnvStore
symbol_extends::Dict{SymbolServer.VarRef, Vector{SymbolServer.VarRef}}
symbol_fieldtypemap::Dict{Symbol, Vector{SymbolServer.VarRef}}
end
FileServer() = FileServer(Dict{String,File}(), Set{File}(), deepcopy(SymbolServer.stdlibs), SymbolServer.collect_extended_methods(SymbolServer.stdlibs))
FileServer() = FileServer(Dict{String,File}(), Set{File}(), deepcopy(SymbolServer.stdlibs), SymbolServer.collect_extended_methods(SymbolServer.stdlibs), fieldname_type_map(SymbolServer.stdlibs))

# Interface spec.
# AbstractServer :-> (has/canload/load/set/get)file, getsymbolserver, getsymbolextends
Expand All @@ -37,6 +38,7 @@ function loadfile(server::FileServer, path::String)
end
getsymbolserver(server::FileServer) = server.symbolserver
getsymbolextendeds(server::FileServer) = server.symbol_extends
getsymbolfieldtypemap(server::FileServer) = server.symbol_fieldtypemap

function scopepass(file, target = nothing)
server = file.server
Expand Down
Loading