-
Notifications
You must be signed in to change notification settings - Fork 33
/
guesstype.jl
308 lines (278 loc) · 8.87 KB
/
guesstype.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using Base: isexported
function arraytype_dims(elt, dimst)
tuplen = StaticTypeAnalysis.length(dimst)
if isnull(tuplen)
return Array{elt}
else
return Array{elt, get(tuplen)}
end
end
"""
stdlibobject(name::Symbol)
If `name` is an export of Base or Core, return `Nullable{Any}(x)` where `x` is
the object itself. Otherwise, return `Nullable{Any}()`.
"""
function stdlibobject(ex::Symbol)
if isexported(Base, ex) && isdefined(Base, ex)
Nullable{Any}(getfield(Base, ex))
elseif isexported(Core, ex) && isdefined(Core, ex)
Nullable{Any}(getfield(Core, ex))
else
Nullable{Any}()
end
end
"""
stdlibobject(ex::Expr)
If the given expression is curly, and each component of the curly is a standard
library object, construct the object `x` as would have been done in the program
itself, and return `Nullable{Any}(x)`.
Otherwise, return `Nullable{Any}()`.
"""
function stdlibobject(ex::Expr)
if isexpr(ex, :curly)
objs = stdlibobject.(ex.args)
if all(!isnull, objs)
try
Nullable{Any}(Core.apply_type(get.(objs)...))
catch
Nullable{Any}()
end
else
Nullable{Any}()
end
else
Nullable{Any}()
end
end
"""
stdlibobject(ex)
Return the literal embedded within a `Nullable{Any}`.
"""
stdlibobject(ex) = Nullable{Any}(ex)
"""
parsetype(ex::Expr)
Obtain a supertype of the type represented by `ex`.
"""
function parsetype(ex)
obj = stdlibobject(ex)
if !isnull(obj) && isa(get(obj), Type)
get(obj)
elseif isexpr(ex, :curly)
obj = stdlibobject(ex.args[1])
if !isnull(obj) && isa(get(obj), Type) && get(obj) !== Union
get(obj)
else
Any
end
else
Any
end
end
function guesstype(ex::Symbol, ctx::LintContext)
stacktop = ctx.callstack[end]
sym = ex
for i in length(stacktop.localvars):-1:1
if haskey(stacktop.localvars[i], sym)
ret = stacktop.localvars[i][sym].typeactual
return ret
end
end
for i in length(stacktop.localarguments):-1:1
if haskey(stacktop.localarguments[i], sym)
ret = stacktop.localarguments[i][sym].typeactual
return ret
end
end
for i in length(ctx.callstack):-1:1
if sym in ctx.callstack[i].types
return Type
end
if sym in ctx.callstack[i].functions
return Function
end
if sym in ctx.callstack[i].modules
return Module
end
end
val = stdlibobject(ex)
if !isnull(val)
if isa(get(val), Type)
return Type{get(val)}
else
return typeof(get(val))
end
end
return Any
end
function guesstype(ex::Expr, ctx::LintContext)
ex = ExpressionUtils.expand_trivial_calls(ex)
if isexpr(ex, :tuple)
ts = Type[]
for a in ex.args
push!(ts, guesstype(a, ctx))
end
return Tuple{ts...}
end
if isexpr(ex, :(::)) && length(ex.args) == 2
return parsetype(ex.args[2])
end
if isexpr(ex, :block)
return isempty(ex.args) ? Void : guesstype(ex.args[end], ctx)
end
if isexpr(ex, :return)
tmp = guesstype(ex.args[1], ctx)
return tmp
end
if isexpr(ex, :call)
fn = ex.args[1]
if any(x -> isexpr(x, :kw) || isexpr(x, :(...)), ex.args[2:end])
# TODO: smarter way to deal with kw/vararg
return Any
end
argtypes = map(x -> guesstype(x, ctx), ex.args[2:end])
# check if it's a constructor for user-defined type, and figure
# out what type
# this is hackish because the return type is a Symbol, not a Type
if fn == :new
return Symbol(ctx.scope)
end
# another detection for constructor calling another constructor
# A() = A(default)
if Symbol(ctx.scope) == fn
found = false
for i = length(ctx.callstack):-1:1
found = in(fn, ctx.callstack[i].types)
if found
return fn
end
end
end
# A() = A{T}(default)
if isexpr(fn, :curly) &&
Symbol(ctx.scope) == fn.args[1]
found = false
for i = length(ctx.callstack):-1:1
found = in(fn.args[1], ctx.callstack[i].types)
if found
return fn.args[1]
end
end
end
# infer return types of Base functions
obj = stdlibobject(fn)
type_argtypes = [isa(t, Type) ? t : Any for t in argtypes]
if !isnull(obj)
inferred = StaticTypeAnalysis.infertype(get(obj), type_argtypes)
if inferred ≠ Any
return inferred
end
end
end
if isexpr(ex, :macrocall)
if ex.args[1] == Symbol("@sprintf") || isexpr(ex, :call) && in(ex.args[1], [
:replace, :string, :utf8, :utf16, :utf32, :repr, :normalize_string,
:join, :chop, :chomp, :lpad, :rpad, :strip, :lstrip, :rstrip,
:uppercase, :lowercase, :ucfirst, :lcfirst, :escape_string,
:unescape_string
])
return AbstractString
elseif ex.args[1] == Symbol("@compat")
return guesstype(ex.args[2], ctx)
end
end
if isexpr(ex, :curly)
return Type
end
if isexpr(ex, :ref) # it could be a ref a[b] or an array Int[1,2,3], Vector{Int}[]
if isexpr(ex.args[1], :curly) # must be a datatype, right?
elt = stdlibobject(ex.args[1])
if !isnull(elt) && isa(get(elt), Type)
return Vector{get(elt)}
else
return Vector
end
end
if isa(ex.args[1], Symbol)
what = registersymboluse(ex.args[1], ctx, false)
if what == :Type
elt = stdlibobject(ex.args[1])
if !isnull(elt) && isa(get(elt), Type)
return Vector{get(elt)}
else
return Vector
end
elseif what == :Any
msg(ctx, :W543, ex.args[1], "Lint cannot determine if Type or not")
return Any
end
end
# not symbol, or symbol but it refers to a variable
partyp = guesstype(ex.args[1], ctx)
if isa(partyp, Symbol)
# we are in a context of a constructor of a new type, so it's
# difficult to figure out the content
return Any
elseif partyp <: AbstractArray && !(partyp <: Range)
eletyp = StaticTypeAnalysis.eltype(partyp)
try
nd = ndims(partyp) # This may throw if we couldn't infer the dimension
tmpdim = nd - (length(ex.args)-1)
if tmpdim < 0
if nd == 0 && ex.args[2] == 1 # ok to do A[1] for a 0-dimensional array
return eletyp
else
msg(ctx, :E436, ex, "more indices than dimensions")
return Any
end
end
for i in 2:length(ex.args)
if ex.args[i] == :(:) || isexpr(ex.args[i], :call) &&
ex.args[i].args[1] == :Colon
tmpdim += 1
end
end
if tmpdim != 0
return Array{eletyp, tmpdim} # is this strictly right?
else
return eletyp
end
end
return Any
else
argtypes = [guesstype(x, ctx) for x in ex.args]
type_argtypes = [isa(t, Type) ? t : Any for t in argtypes]
inferred = StaticTypeAnalysis.infertype(getindex, type_argtypes)
if ctx.versionreachable(VERSION) && inferred == Union{}
indtypes = if length(type_argtypes) == 1
"no indices"
else
string("index types ", join(type_argtypes[2:end], ", "))
end
msg(ctx, :E522, ex,
string("indexing $(type_argtypes[1]) with ",
indtypes,
" is not supported"))
end
return inferred
end
return Any
end
if isexpr(ex, :comparison)
return Bool
end
# simple if statement e.g. test ? 0 : 1
if isexpr(ex, :if) && length(ex.args) == 3
tt = guesstype(ex.args[2], ctx)
ft = guesstype(ex.args[3], ctx)
if tt == ft
return tt
else
return Any
end
end
if isexpr(ex, :(->))
return Function
end
return Any
end
guesstype(ex, ctx::LintContext) = lexicaltypeof(ex)