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
|
---@class vm
local vm = require 'vm.vm'
local util = require 'utility'
local compiler = require 'vm.node.compiler'
local guide = require 'parser.guide'
local localID = require 'vm.local-id'
local globalNode = require 'vm.global-node'
local simpleMap
local function searchGetLocal(source, node, pushResult)
local key = guide.getKeyName(source)
for _, ref in ipairs(node.node.ref) do
if ref.type == 'getlocal'
and guide.isSet(ref.next)
and guide.getKeyName(ref.next) == key then
pushResult(ref.next)
end
end
end
simpleMap = util.switch()
: case 'local'
: call(function (source, pushResult)
pushResult(source)
if source.ref then
for _, ref in ipairs(source.ref) do
if ref.type == 'setlocal' then
pushResult(ref)
end
end
end
if source.dummy then
for _, res in ipairs(vm.getDefs(source.method.node)) do
pushResult(res)
end
end
end)
: case 'getlocal'
: case 'setlocal'
: call(function (source, pushResult)
simpleMap['local'](source.node, pushResult)
end)
: case 'field'
: call(function (source, pushResult)
local parent = source.parent
simpleMap[parent.type](parent, pushResult)
end)
: case 'setfield'
: case 'getfield'
: call(function (source, pushResult)
local node = source.node
if node.type == 'getlocal' then
searchGetLocal(source, node, pushResult)
return
end
end)
: case 'getindex'
: case 'setindex'
: call(function (source, pushResult)
local node = source.node
if node.type == 'getlocal' then
searchGetLocal(source, node, pushResult)
end
end)
: getMap()
local searchFieldMap = util.switch()
: case 'table'
: call(function (node, key, pushResult)
for _, field in ipairs(node) do
if field.type == 'tablefield'
or field.type == 'tableindex' then
if guide.getKeyName(field) == key then
pushResult(field)
end
end
end
end)
: getMap()
local nodeMap;nodeMap = util.switch()
: case 'field'
: call(function (source, pushResult)
local parent = source.parent
nodeMap[parent.type](parent, pushResult)
end)
: case 'getfield'
: case 'setfield'
: call(function (source, pushResult)
local node = compiler.compileNode(guide.getUri(source.node), source.node)
if not node then
return
end
if searchFieldMap[node.type] then
searchFieldMap[node.type](node, guide.getKeyName(source), pushResult)
end
end)
: getMap()
---@param source parser.object
---@param pushResult fun(src: parser.object)
local function searchBySimple(source, pushResult)
local simple = simpleMap[source.type]
if simple then
simple(source, pushResult)
end
end
---@param source parser.object
---@param pushResult fun(src: parser.object)
local function searchByGlobal(source, pushResult)
local global = globalNode.getNode(source)
if not global then
return
end
for _, src in ipairs(global:getSets()) do
pushResult(src)
end
end
---@param source parser.object
---@param pushResult fun(src: parser.object)
local function searchByID(source, pushResult)
local idSources = localID.getSources(source)
if not idSources then
return
end
for _, src in ipairs(idSources) do
if guide.isSet(src) then
pushResult(src)
end
end
end
---@param source parser.object
---@param pushResult fun(src: parser.object)
local function searchByNode(source, pushResult)
local node = nodeMap[source.type]
if node then
node(source, pushResult)
end
end
---@param source parser.object
---@return parser.object[]
function vm.getDefs(source)
local results = {}
local mark = {}
local function pushResult(src)
if not mark[src] then
mark[src] = true
results[#results+1] = src
end
end
searchBySimple(source, pushResult)
searchByGlobal(source, pushResult)
searchByID(source, pushResult)
searchByNode(source, pushResult)
return results
end
---@param source parser.object
---@return parser.object[]
function vm.getAllDefs(source)
return vm.getDefs(source)
end
|