-
Notifications
You must be signed in to change notification settings - Fork 60
/
info.go
466 lines (402 loc) · 11.6 KB
/
info.go
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package meta
import (
"strings"
"sync"
"github.com/VKCOM/noverify/src/types"
)
// Info contains meta information for all classes, functions, etc.
type Info struct {
indexingComplete bool
loadingStubs bool
onIndexingComplete []func(*Info)
sync.Mutex
*Scope
allFiles map[string]bool
allTraits ClassesMap
allClasses ClassesMap
allFunctions FunctionsMap
allConstants ConstantsMap
allFunctionsOverrides FunctionsOverrideMap
perFileTraits map[string]ClassesMap
perFileClasses map[string]ClassesMap
perFileFunctions map[string]FunctionsMap
perFileConstants map[string]ConstantsMap
internalFunctions FunctionsMap
internalFunctionOverrides FunctionsOverrideMap
internalClasses ClassesMap
}
func NewInfo() *Info {
return &Info{
Scope: NewScope(),
allFiles: make(map[string]bool),
allTraits: NewClassesMap(),
allClasses: NewClassesMap(),
allFunctions: NewFunctionsMap(),
allConstants: make(ConstantsMap),
allFunctionsOverrides: make(FunctionsOverrideMap),
perFileTraits: make(map[string]ClassesMap),
perFileClasses: make(map[string]ClassesMap),
perFileFunctions: make(map[string]FunctionsMap),
perFileConstants: make(map[string]ConstantsMap),
}
}
func (i *Info) Clone() *Info {
info := NewInfo()
info.internalFunctions = NewFunctionsMap()
info.internalFunctionOverrides = make(FunctionsOverrideMap)
info.internalClasses = NewClassesMap()
for key, value := range i.allFiles {
info.allFiles[key] = value
}
for key, value := range i.allTraits.H {
info.allTraits.H[key] = value
}
for key, value := range i.allClasses.H {
info.allClasses.H[key] = value
}
for key, value := range i.allFunctions.H {
info.allFunctions.H[key] = value
}
for key, value := range i.allConstants {
info.allConstants[key] = value
}
for key, value := range i.allFunctionsOverrides {
info.allFunctionsOverrides[key] = value
}
for key, value := range i.perFileTraits {
info.perFileTraits[key] = value
}
for key, value := range i.perFileClasses {
info.perFileClasses[key] = value
}
for key, value := range i.perFileFunctions {
info.perFileFunctions[key] = value
}
for key, value := range i.perFileConstants {
info.perFileConstants[key] = value
}
for key, value := range i.internalFunctions.H {
info.internalFunctions.H[key] = value
}
for key, value := range i.internalFunctionOverrides {
info.internalFunctionOverrides[key] = value
}
for key, value := range i.internalClasses.H {
info.internalClasses.H[key] = value
}
return info
}
func (i *Info) OnIndexingComplete(cb func(*Info)) {
if i.indexingComplete {
cb(i)
} else {
i.onIndexingComplete = append(i.onIndexingComplete, cb)
}
}
// IsLoadingStubs reports whether we're parsing stub files right now.
func (i *Info) IsLoadingStubs() bool {
return i.loadingStubs
}
// SetLoadingStubs changes IsLoadingStubs() return value.
//
// Should be only called from linter.InitStubs() function.
func (i *Info) SetLoadingStubs(isLoading bool) {
i.loadingStubs = isLoading
}
func (i *Info) IsIndexingComplete() bool {
return i.indexingComplete
}
func (i *Info) SetIndexingComplete(complete bool) {
i.indexingComplete = complete
if complete {
for _, cb := range i.onIndexingComplete {
cb(i)
}
}
}
func (i *Info) GetConstant(nm string) (res ConstInfo, ok bool) {
res, ok = i.allConstants[nm]
return res, ok
}
func (i *Info) NumConstants() int {
return len(i.allConstants)
}
func (i *Info) GetClass(nm string) (res ClassInfo, ok bool) {
return i.allClasses.Get(nm)
}
func (i *Info) GetTrait(nm string) (res ClassInfo, ok bool) {
return i.allTraits.Get(nm)
}
func (i *Info) GetClassOrTrait(nm string) (res ClassInfo, ok bool) {
res, ok = i.allClasses.Get(nm)
if ok {
return res, true
}
res, ok = i.allTraits.Get(nm)
return res, ok
}
func (i *Info) NumClasses() int {
return i.allClasses.Len()
}
func (i *Info) GetFunction(nm string) (res FuncInfo, ok bool) {
res, ok = i.allFunctions.Get(nm)
return res, ok
}
func (i *Info) GetFunctionOverride(nm string) (res FuncInfoOverride, ok bool) {
res, ok = i.allFunctionsOverrides[nm]
return res, ok
}
func (i *Info) NumFunctions() int {
return i.allFunctions.Len()
}
func (i *Info) NumFilesWithFunctions() int {
return len(i.perFileFunctions)
}
func (i *Info) FindFunctions(substr string) (res []string) {
for _, fn := range i.allFunctions.H {
if strings.HasPrefix(fn.Name, substr) {
res = append(res, fn.Name)
}
}
return res
}
func (i *Info) FindConstants(substr string) (res []string) {
for c := range i.allConstants {
if strings.HasPrefix(c, substr) {
res = append(res, c)
}
}
return res
}
func (i *Info) InitKphpStubs() {
i.internalFunctions.H[`\array_first_value`] = FuncInfo{
Name: `\array_first_value`,
Params: []FuncParam{{Name: "el"}},
MinParamsCnt: 1,
Typ: types.NewMap("mixed"),
}
i.internalFunctions.H[`\array_last_value`] = FuncInfo{
Name: `\array_last_value`,
Params: []FuncParam{{Name: "el"}},
MinParamsCnt: 1,
Typ: types.NewMap("mixed"),
}
i.internalFunctions.H[`\array_filter_by_key`] = FuncInfo{
Name: `\array_filter_by_key`,
Params: []FuncParam{{Name: "array"}, {Name: "callback"}},
MinParamsCnt: 2,
Typ: types.NewMap("mixed"),
}
i.internalFunctions.H[`\instance_deserialize`] = FuncInfo{
Name: `\instance_deserialize`,
Params: []FuncParam{{Name: "packed_str"}, {Name: "type_of_instance"}},
MinParamsCnt: 2,
Typ: types.NewMap("object|null"),
}
i.internalFunctions.H[`\instance_cache_fetch`] = FuncInfo{
Name: `\instance_cache_fetch`,
Params: []FuncParam{{Name: "type"}, {Name: "key"}},
MinParamsCnt: 2,
Typ: types.NewMap("object|null"),
}
i.internalFunctions.H[`\instance_cast`] = FuncInfo{
Name: `\instance_cast`,
Params: []FuncParam{{Name: "instance"}, {Name: "class_name"}},
MinParamsCnt: 2,
Typ: types.NewMap("object"),
}
i.internalFunctions.H[`\not_null`] = FuncInfo{
Name: `\not_null`,
Params: []FuncParam{{Name: "any_value"}},
MinParamsCnt: 1,
Typ: types.NewMap("mixed"),
}
i.internalFunctions.H[`\not_false`] = FuncInfo{
Name: `\not_false`,
Params: []FuncParam{{Name: "any_value"}},
MinParamsCnt: 1,
Typ: types.NewMap("mixed"),
}
i.internalFunctions.H[`\create_vector`] = FuncInfo{
Name: `\create_vector`,
Params: []FuncParam{{Name: "count"}, {Name: "el"}},
MinParamsCnt: 2,
Typ: types.NewMap("mixed[]"),
}
i.internalFunctionOverrides[`\array_first_value`] = FuncInfoOverride{
OverrideType: OverrideElementType,
ArgNum: 0,
}
i.internalFunctionOverrides[`\array_last_value`] = FuncInfoOverride{
OverrideType: OverrideElementType,
ArgNum: 0,
}
i.internalFunctionOverrides[`\array_filter_by_key`] = FuncInfoOverride{
OverrideType: OverrideArgType,
ArgNum: 0,
}
i.internalFunctionOverrides[`\instance_deserialize`] = FuncInfoOverride{
OverrideType: OverrideNullableClassType,
ArgNum: 1,
}
i.internalFunctionOverrides[`\instance_cast`] = FuncInfoOverride{
OverrideType: OverrideClassType,
ArgNum: 1,
}
i.internalFunctionOverrides[`\instance_cache_fetch`] = FuncInfoOverride{
OverrideType: OverrideNullableClassType,
ArgNum: 0,
}
i.internalFunctionOverrides[`\not_null`] = FuncInfoOverride{
OverrideType: OverrideArgType,
Properties: NotNull,
ArgNum: 0,
}
i.internalFunctionOverrides[`\not_false`] = FuncInfoOverride{
OverrideType: OverrideArgType,
Properties: NotFalse,
ArgNum: 0,
}
i.internalFunctionOverrides[`\create_vector`] = FuncInfoOverride{
OverrideType: OverrideArgType,
Properties: ArrayOf,
ArgNum: 1,
}
}
func (i *Info) InitStubs() {
i.Lock()
defer i.Unlock()
{
i.internalFunctions = NewFunctionsMap()
h := make(map[lowercaseString]FuncInfo, len(i.allFunctions.H))
for k, v := range i.allFunctions.H {
h[k] = v
}
i.internalFunctions.H = h
}
{
i.internalClasses = NewClassesMap()
h := make(map[lowercaseString]ClassInfo, len(i.allClasses.H))
for k, v := range i.allClasses.H {
h[k] = v
}
i.internalClasses.H = h
}
i.internalFunctionOverrides = make(FunctionsOverrideMap)
for k, v := range i.allFunctionsOverrides {
i.internalFunctionOverrides[k] = v
}
}
func (i *Info) AddFilenameNonLocked(filename string) {
i.allFiles[filename] = true
}
func (i *Info) FileExists(filename string) bool {
return i.allFiles[filename]
}
func (i *Info) GetMetaForFile(filename string) (res PerFile) {
if t, ok := i.perFileTraits[filename]; ok {
res.Traits = t
}
if c, ok := i.perFileConstants[filename]; ok {
res.Constants = c
}
if f, ok := i.perFileFunctions[filename]; ok {
res.Functions = f
}
if c, ok := i.perFileClasses[filename]; ok {
res.Classes = c
}
return res
}
func (i *Info) DeleteMetaForFileNonLocked(filename string) {
oldClasses := i.perFileClasses[filename]
delete(i.allFiles, filename)
delete(i.perFileClasses, filename)
for f := range oldClasses.H {
delete(i.allClasses.H, f)
}
oldTraits := i.perFileTraits[filename]
delete(i.perFileTraits, filename)
for f := range oldTraits.H {
delete(i.allTraits.H, f)
}
oldFunctions := i.perFileFunctions[filename]
delete(i.perFileFunctions, filename)
{
allFuncs := i.allFunctions.H
for f, oldFn := range oldFunctions.H {
fn, ok := allFuncs[f]
if !ok || oldFn.Pos.Length != fn.Pos.Length {
continue
}
delete(allFuncs, f)
}
}
oldConstants := i.perFileConstants[filename]
delete(i.perFileConstants, filename)
for f := range oldConstants {
delete(i.allConstants, f)
}
}
func (i *Info) AddClassesNonLocked(filename string, m ClassesMap) {
i.perFileClasses[filename] = m
allClasses := i.allClasses.H
for k, v := range m.H {
prevClass, ok := allClasses[k]
if !ok || v.Pos.Length > prevClass.Pos.Length {
allClasses[k] = v
}
}
}
func (i *Info) AddTraitsNonLocked(filename string, m ClassesMap) {
i.perFileTraits[filename] = m
allTraits := i.allTraits.H
for k, v := range m.H {
prevTrait, ok := allTraits[k]
if !ok || v.Pos.Length > prevTrait.Pos.Length {
allTraits[k] = v
}
}
}
func (i *Info) AddFunctionsNonLocked(filename string, m FunctionsMap) {
i.perFileFunctions[filename] = m
allFuncs := i.allFunctions.H
for k, v := range m.H {
prevFn, ok := allFuncs[k]
if !ok || v.Pos.Length > prevFn.Pos.Length {
allFuncs[k] = v
}
}
}
func (i *Info) AddFunctionsOverridesNonLocked(filename string, m FunctionsOverrideMap) {
// TODO: support filename map
for k, v := range m {
i.allFunctionsOverrides[k] = v
}
}
func (i *Info) AddConstantsNonLocked(filename string, m ConstantsMap) {
i.perFileConstants[filename] = m
for k, v := range m {
// This may cause a name conflict if we have several
// constants with the same name inside the project.
// When we'll store a list of symbols for the every name,
// it won't be a problem anymore.
i.allConstants[k] = v
}
}
func (i *Info) AddToGlobalScopeNonLocked(filename string, sc *Scope) {
sc.Iterate(func(nm string, typ types.Map, flags VarFlags) {
i.AddVarName(nm, typ, "global", flags)
})
}
func (i *Info) IsInternalClass(className string) bool {
_, ok := i.internalClasses.Get(className)
return ok
}
func (i *Info) GetInternalFunctionInfo(fn string) (info FuncInfo, ok bool) {
return i.internalFunctions.Get(fn)
}
func (i *Info) GetInternalFunctionOverrideInfo(fn string) (info FuncInfoOverride, ok bool) {
info, ok = i.internalFunctionOverrides[fn]
return info, ok
}