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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
local vm = require 'vm.vm'
local util = require 'utility'
local typeSort = {
['boolean'] = 1,
['string'] = 2,
['integer'] = 3,
['number'] = 4,
['table'] = 5,
['function'] = 6,
['nil'] = math.maxinteger,
}
NIL = setmetatable({'<nil>'}, { __tostring = function () return 'nil' end })
local function merge(t, b)
if not t then
t = {}
end
if not b then
return t
end
for i = 1, #b do
local o = b[i]
if not t[o] then
t[o] = true
t[#t+1] = o
end
end
return t
end
local function alloc(o)
-- TODO
assert(o.type)
if type(o.type) == 'table' then
local values = {}
for i = 1, #o.type do
local sub = {
type = o.type[i],
value = o.value,
source = o.source,
}
values[i] = sub
values[sub] = true
end
return values
else
return {
[1] = o,
[o] = true,
}
end
end
local function insert(t, o)
if not o then
return
end
if not t[o] then
t[o] = true
t[#t+1] = o
end
return t
end
local function checkLiteral(source)
if source.type == 'string' then
return alloc {
type = 'string',
value = source[1],
source = source,
}
elseif source.type == 'nil' then
return alloc {
type = 'nil',
value = NIL,
source = source,
}
elseif source.type == 'boolean' then
return alloc {
type = 'boolean',
value = source[1],
source = source,
}
elseif source.type == 'number' then
if math.type(source[1]) == 'integer' then
return alloc {
type = 'integer',
value = source[1],
source = source,
}
else
return alloc {
type = 'number',
value = source[1],
source = source,
}
end
elseif source.type == 'integer' then
return alloc {
type = 'integer',
source = source,
}
elseif source.type == 'table' then
return alloc {
type = 'table',
source = source,
}
elseif source.type == 'function' then
return alloc {
type = 'function',
source = source,
}
elseif source.type == '...' then
return alloc {
type = '...',
source = source,
}
end
end
local function checkUnary(source)
if source.type ~= 'unary' then
return
end
local op = source.op
if op.type == 'not' then
local checkTrue = vm.checkTrue(source[1])
local value = nil
if checkTrue == true then
value = false
elseif checkTrue == false then
value = true
end
return alloc {
type = 'boolean',
value = value,
source = source,
}
elseif op.type == '#' then
return alloc {
type = 'integer',
source = source,
}
elseif op.type == '~' then
local l = vm.getLiteral(source[1], 'integer')
return alloc {
type = 'integer',
value = l and ~l or nil,
source = source,
}
elseif op.type == '-' then
local v = vm.getLiteral(source[1], 'integer')
if v then
return alloc {
type = 'integer',
value = - v,
source = source,
}
end
v = vm.getLiteral(source[1], 'number')
return alloc {
type = 'number',
value = v and -v or nil,
source = source,
}
end
end
local function mathCheck(a, b)
local v1 = vm.getLiteral(a, 'integer') or vm.getLiteral(a, 'number')
local v2 = vm.getLiteral(b, 'integer') or vm.getLiteral(a, 'number')
local int = vm.hasType(a, 'integer')
and vm.hasType(b, 'integer')
and not vm.hasType(a, 'number')
and not vm.hasType(b, 'number')
return int and 'integer' or 'number', v1, v2
end
local function checkBinary(source)
if source.type ~= 'binary' then
return
end
local op = source.op
if op.type == 'and' then
local isTrue = vm.checkTrue(source[1])
if isTrue == true then
return vm.getValue(source[2])
elseif isTrue == false then
return vm.getValue(source[1])
else
return merge(
vm.getValue(source[1]),
vm.getValue(source[2])
)
end
elseif op.type == 'or' then
local isTrue = vm.checkTrue(source[1])
if isTrue == true then
return vm.getValue(source[1])
elseif isTrue == false then
return vm.getValue(source[2])
else
return merge(
vm.getValue(source[1]),
vm.getValue(source[2])
)
end
elseif op.type == '==' then
local value = vm.isSameValue(source[1], source[2])
if value ~= nil then
return alloc {
type = 'boolean',
value = value,
source = source,
}
end
local isSame = vm.isSameRef(source[1], source[2])
if isSame == true then
value = true
else
value = nil
end
return alloc {
type = 'boolean',
value = value,
source = source,
}
elseif op.type == '~=' then
local value = vm.isSameValue(source[1], source[2])
if value ~= nil then
return alloc {
type = 'boolean',
value = not value,
source = source,
}
end
local isSame = vm.isSameRef(source[1], source[2])
if isSame == true then
value = false
else
value = nil
end
return alloc {
type = 'boolean',
value = value,
source = source,
}
elseif op.type == '<=' then
local v1 = vm.getLiteral(source[1], 'integer') or vm.getLiteral(source[1], 'number')
local v2 = vm.getLiteral(source[2], 'integer') or vm.getLiteral(source[2], 'number')
local v
if v1 and v2 then
v = v1 <= v2
end
return alloc {
type = 'boolean',
value = v,
source = source,
}
elseif op.type == '>=' then
local v1 = vm.getLiteral(source[1], 'integer') or vm.getLiteral(source[1], 'number')
local v2 = vm.getLiteral(source[2], 'integer') or vm.getLiteral(source[2], 'number')
local v
if v1 and v2 then
v = v1 >= v2
end
return alloc {
type = 'boolean',
value = v,
source = source,
}
elseif op.type == '<' then
local v1 = vm.getLiteral(source[1], 'integer') or vm.getLiteral(source[1], 'number')
local v2 = vm.getLiteral(source[2], 'integer') or vm.getLiteral(source[2], 'number')
local v
if v1 and v2 then
v = v1 < v2
end
return alloc {
type = 'boolean',
value = v,
source = source,
}
elseif op.type == '>' then
local v1 = vm.getLiteral(source[1], 'integer') or vm.getLiteral(source[1], 'number')
local v2 = vm.getLiteral(source[2], 'integer') or vm.getLiteral(source[2], 'number')
local v
if v1 and v2 then
v = v1 > v2
end
return alloc {
type = 'boolean',
value = v,
source = source,
}
elseif op.type == '|' then
local v1 = vm.getLiteral(source[1], 'integer')
local v2 = vm.getLiteral(source[2], 'integer')
local v
if v1 and v2 then
v = v1 | v2
end
return alloc {
type = 'integer',
value = v,
source = source,
}
elseif op.type == '~' then
local v1 = vm.getLiteral(source[1], 'integer')
local v2 = vm.getLiteral(source[2], 'integer')
local v
if v1 and v2 then
v = v1 ~ v2
end
return alloc {
type = 'integer',
value = v,
source = source,
}
elseif op.type == '&' then
local v1 = vm.getLiteral(source[1], 'integer')
local v2 = vm.getLiteral(source[2], 'integer')
local v
if v1 and v2 then
v = v1 & v2
end
return alloc {
type = 'integer',
value = v,
source = source,
}
elseif op.type == '<<' then
local v1 = vm.getLiteral(source[1], 'integer')
local v2 = vm.getLiteral(source[2], 'integer')
local v
if v1 and v2 then
v = v1 << v2
end
return alloc {
type = 'integer',
value = v,
source = source,
}
elseif op.type == '>>' then
local v1 = vm.getLiteral(source[1], 'integer')
local v2 = vm.getLiteral(source[2], 'integer')
local v
if v1 and v2 then
v = v1 >> v2
end
return alloc {
type = 'integer',
value = v,
source = source,
}
elseif op.type == '..' then
local v1 = vm.getLiteral(source[1], 'string')
local v2 = vm.getLiteral(source[2], 'string')
local v
if v1 and v2 then
v = v1 .. v2
end
return alloc {
type = 'string',
value = v,
source = source,
}
elseif op.type == '^' then
local v1 = vm.getLiteral(source[1], 'integer') or vm.getLiteral(source[1], 'number')
local v2 = vm.getLiteral(source[2], 'integer') or vm.getLiteral(source[2], 'number')
local v
if v1 and v2 then
v = v1 ^ v2
end
return alloc {
type = 'number',
value = v,
source = source,
}
elseif op.type == '/' then
local v1 = vm.getLiteral(source[1], 'integer') or vm.getLiteral(source[1], 'number')
local v2 = vm.getLiteral(source[2], 'integer') or vm.getLiteral(source[2], 'number')
local v
if v1 and v2 then
v = v1 > v2
end
return alloc {
type = 'number',
value = v,
source = source,
}
-- 其他数学运算根据2侧的值决定,当2侧的值均为整数时返回整数
elseif op.type == '+' then
local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
type = int,
value = (v1 and v2) and (v1 + v2) or nil,
source = source,
}
elseif op.type == '-' then
local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
type = int,
value = (v1 and v2) and (v1 - v2) or nil,
source = source,
}
elseif op.type == '*' then
local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
type = int,
value = (v1 and v2) and (v1 * v2) or nil,
source = source,
}
elseif op.type == '%' then
local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
type = int,
value = (v1 and v2) and (v1 % v2) or nil,
source = source,
}
elseif op.type == '//' then
local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
type = int,
value = (v1 and v2) and (v1 // v2) or nil,
source = source,
}
end
end
local function checkValue(source)
if source.value then
return vm.getValue(source.value)
end
if source.type == 'paren' then
return vm.getValue(source.exp)
end
end
local function inferByCall(results, source)
if #results ~= 0 then
return
end
if not source.parent then
return
end
if source.parent.type ~= 'call' then
return
end
if source.parent.node == source then
insert(results, {
type = 'function',
source = source,
})
return
end
end
local function inferByGetTable(results, source)
if #results ~= 0 then
return
end
local next = source.next
if not next then
return
end
if next.type == 'getfield'
or next.type == 'getindex'
or next.type == 'getmethod'
or next.type == 'setfield'
or next.type == 'setindex'
or next.type == 'setmethod' then
insert(results, {
type = 'table',
source = source,
})
end
end
local function checkDef(results, source)
vm.eachDef(source, function (info)
local src = info.source
local tp = vm.getValue(src)
if tp then
merge(results, tp)
end
end)
end
local function checkLibraryTypes(source)
if type(source.type) ~= 'table' then
return nil
end
local results = {}
for i = 1, #source.type do
insert(results, {
type = source.type[i],
source = source,
})
end
return results
end
local function checkLibrary(source)
local lib = vm.getLibrary(source)
if not lib then
return nil
end
return alloc {
type = lib.type,
value = lib.value,
source = lib,
}
end
local function checkLibraryReturn(source)
if source.type ~= 'select' then
return nil
end
local index = source.index
local call = source.vararg
if call.type ~= 'call' then
return nil
end
local func = call.node
local lib = vm.getLibrary(func)
if not lib then
return nil
end
if lib.type ~= 'function' then
return nil
end
if not lib.returns then
return nil
end
local rtn = lib.returns[index]
if not rtn then
return nil
end
if not rtn.type then
return nil
end
if rtn.type == '...' then
return
end
return alloc {
type = rtn.type,
value = rtn.value,
source = rtn,
}
end
local function checkLibraryArg(source)
local args = source.parent
if not args then
return
end
if args.type ~= 'callargs' then
return
end
local call = args.parent
if not call then
return
end
local func = call.node
local index
for i = 1, #args do
if args[i] == source then
index = i
break
end
end
if not index then
return
end
local lib = vm.getLibrary(func)
local arg = lib and lib.args and lib.args[index]
if not arg then
return
end
if not arg.type then
return
end
if arg.type == '...' then
return
end
return alloc {
type = arg.type,
value = arg.value,
source = arg,
}
end
local function hasTypeInResults(results, type)
for i = 1, #results do
if results[i].type == 'type' then
return true
end
end
return false
end
local function inferByUnary(results, source)
if #results ~= 0 then
return
end
local parent = source.parent
if not parent or parent.type ~= 'unary' then
return
end
local op = parent.op
if op.type == '#' then
-- 会受顺序影响,不检查了
--if hasTypeInResults(results, 'string')
--or hasTypeInResults(results, 'integer') then
-- return
--end
insert(results, {
type = 'string',
source = source
})
insert(results, {
type = 'table',
source = source
})
elseif op.type == '~' then
insert(results, {
type = 'integer',
source = source
})
elseif op.type == '-' then
insert(results, {
type = 'number',
source = source
})
end
end
local function inferByBinary(results, source)
if #results ~= 0 then
return
end
local parent = source.parent
if not parent or parent.type ~= 'binary' then
return
end
local op = parent.op
if op.type == '<='
or op.type == '>='
or op.type == '<'
or op.type == '>'
or op.type == '^'
or op.type == '/'
or op.type == '+'
or op.type == '-'
or op.type == '*'
or op.type == '%' then
insert(results, {
type = 'number',
source = source
})
elseif op.type == '|'
or op.type == '~'
or op.type == '&'
or op.type == '<<'
or op.type == '>>'
-- 整数的可能性比较高
or op.type == '//' then
insert(results, {
type = 'integer',
source = source
})
elseif op.type == '..' then
insert(results, {
type = 'string',
source = source
})
end
end
local function inferBySetOfLocal(results, source)
if source.ref then
for i = 1, math.min(#source.ref, 100) do
local ref = source.ref[i]
if ref.type == 'setlocal' then
break
end
merge(results, vm.getValue(ref))
end
end
end
local function inferBySet(results, source)
if #results ~= 0 then
return
end
if source.type == 'local' then
inferBySetOfLocal(results, source)
elseif source.type == 'setlocal'
or source.type == 'getlocal' then
merge(results, vm.getValue(source.node))
end
end
local function getValue(source)
local results = checkLiteral(source)
or checkValue(source)
or checkUnary(source)
or checkBinary(source)
or checkLibraryTypes(source)
or checkLibrary(source)
or checkLibraryReturn(source)
or checkLibraryArg(source)
if results then
return results
end
results = {}
checkDef(results, source)
inferBySet(results, source)
inferByCall(results, source)
inferByGetTable(results, source)
inferByUnary(results, source)
inferByBinary(results, source)
if #results == 0 then
return nil
end
return results
end
function vm.checkTrue(source)
local values = vm.getValue(source)
if not values then
return
end
-- 当前认为的结果
local current
for i = 1, #values do
-- 新的结果
local new
local v = values[i]
if v.type == 'nil' then
new = false
elseif v.type == 'boolean' then
if v.value == true then
new = true
elseif v.value == false then
new = false
end
end
if new ~= nil then
if current == nil then
current = new
else
-- 如果2个结果完全相反,则返回 nil 表示不确定
if new ~= current then
return nil
end
end
end
end
return current
end
--- 获取特定类型的字面量值
function vm.getLiteral(source, type)
local values = vm.getValue(source)
if not values then
return nil
end
for i = 1, #values do
local v = values[i]
if v.value ~= nil then
if type == nil or v.type == type then
return v.value
end
end
end
return nil
end
function vm.isSameValue(a, b)
local valuesA = vm.getValue(a)
local valuesB = vm.getValue(b)
if not valuesA or not valuesB then
return false
end
if valuesA == valuesB then
return true
end
local values = {}
for i = 1, #valuesA do
local value = valuesA[i]
local literal = value.value
if literal then
values[literal] = false
end
end
for i = 1, #valuesB do
local value = valuesA[i]
local literal = value.value
if literal then
if values[literal] == nil then
return false
end
values[literal] = true
end
end
for k, v in pairs(values) do
if v == false then
return false
end
end
return true
end
--- 是否包含某种类型
function vm.hasType(source, type)
local values = vm.getValue(source)
if not values then
return false
end
for i = 1, #values do
local value = values[i]
if value.type == type then
return true
end
end
return false
end
local function viewTypes(types)
if #types == 0 then
return 'any'
end
if #types == 1 then
return types[1]
end
table.sort(types, function (a, b)
local sa = typeSort[a]
local sb = typeSort[b]
if sa and sb then
return sa < sb
end
if not sa and not sb then
return a < b
end
if sa and not sb then
return true
end
if not sa and sb then
return false
end
return false
end)
return table.concat(types, '|')
end
function vm.viewType(values)
if not values then
return 'any'
end
if type(values) ~= 'table' then
return values or 'any'
end
local types = {}
for i = 1, #values do
local tp = values[i].type
if not types[tp] and tp ~= 'any' then
types[tp] = true
types[#types+1] = tp
end
end
return viewTypes(types)
end
function vm.mergeTypeViews(...)
local types = {}
for _, view in ipairs {...} do
for tp in view:gmatch '[^|]+' do
if not types[tp] and tp ~= 'any' then
types[tp] = true
types[#types+1] = tp
end
end
end
return viewTypes(types)
end
function vm.getType(source)
local values = vm.getValue(source)
return vm.viewType(values)
end
function vm.getValue(source)
if not source then
return
end
local cache = vm.cache.getValue[source]
if cache ~= nil then
return cache
end
local unlock = vm.lock('getValue', source)
if not unlock then
return
end
cache = getValue(source) or false
vm.cache.getValue[source] = cache
unlock()
return cache
end
|