summaryrefslogtreecommitdiff
path: root/script-beta/vm/eachDef.lua
blob: 61da11d9a6be1b0d4542fb6b5d4e8e709780b99c (plain)
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
local guide   = require 'parser.guide'
local files   = require 'files'
local vm      = require 'vm.vm'

local function ofParentMT(func, callback)
    if not func or func.type ~= 'function' then
        return
    end
    local parent = func.parent
    if not parent or parent.type ~= 'setmethod' then
        return
    end
    local node = parent.node
    if not node then
        return
    end
    vm.eachDef(node, callback)
end

local function ofLocal(loc, callback)
    -- 方法中的 self 使用了一个虚拟的定义位置
    if loc.tag == 'self' then
        local func = guide.getParentFunction(loc)
        ofParentMT(func, callback)
    else
        callback(loc)
    end
    local refs = loc.ref
    if refs then
        for i = 1, #refs do
            local ref = refs[i]
            if vm.isSet(ref) then
                callback(ref)
            end
        end
    end
end

local function ofGlobal(source, callback)
    local key = vm.getKeyName(source)
    local node = source.node
    if node.tag == '_ENV' then
        local uris = files.findGlobals(key)
        for _, uri in ipairs(uris) do
            local ast = files.getAst(uri)
            local globals = vm.getGlobals(ast.ast)
            if globals and globals[key] then
                for _, src in ipairs(globals[key]) do
                    if vm.isSet(src) then
                        callback(src)
                    end
                end
            end
        end
    else
        vm.eachField(node, function (src)
            if  vm.isSet(src)
            and key == vm.getKeyName(src) then
                callback(src)
            end
        end)
    end
end

local function ofTableField(source, callback)
    callback(source)
end

local function checkMetaRecv(source, callback)
    if not source or source.type ~= 'select' then
        return
    end
    if source.index ~= 1 then
        return
    end
    local call = source.vararg
    if not call or call.type ~= 'call' then
        return
    end
    local special = vm.getSpecial(call.node)
    if special ~= 'setmetatable' then
        return
    end
    vm.eachFieldInTable(call.args[1])
    local mt = call.args[2]
    if mt then
        vm.eachField(mt, function (src)
            if vm.getKeyName(src) == 's|__index' then
                if src.value then
                    vm.eachField(src.value, callback)
                end
            end
        end)
    end
end

local function ofField(source, callback)
    local parent = source.parent
    local key    = vm.getKeyName(source)
    local function checkKey(src)
        if  vm.isSet(src)
        and key == vm.getKeyName(src) then
            callback(src)
        end
    end
    if parent.type == 'tablefield'
    or parent.type == 'tableindex' then
        ofTableField(parent, checkKey)
    else
        local node = parent.node
        vm.eachField(node, checkKey)
        vm.eachDef(node, function (src)
            vm.eachFieldInTable(src.value, checkKey)
            checkMetaRecv(src.value, checkKey)
        end)
    end
end

local function ofLiteral(source, callback)
    local parent = source.parent
    if not parent then
        return
    end
    if parent.type == 'setindex'
    or parent.type == 'getindex'
    or parent.type == 'tableindex' then
        ofField(source, callback)
    end
end

local function ofLabel(source, callback)
    callback(source)
    if source.ref then
        for _, ref in ipairs(source.ref) do
            if ref.type == 'label' then
                callback(ref)
            end
        end
    end
end

local function ofGoTo(source, callback)
    local name = source[1]
    local label = guide.getLabel(source, name)
    if label then
        ofLabel(label, callback)
    end
end

local function findIndex(parent, source)
    for i = 1, #parent do
        if parent[i] == source then
            return i
        end
    end
    return nil
end

local function findCallRecvs(func, index, callback)
    vm.eachRef(func, function (source)
        local parent = source.parent
        if parent.type ~= 'call' then
            return
        end
        if index == 1 then
            local slt = parent.parent
            if not slt or slt.type ~= 'select' then
                return
            end
            callback(slt.parent)
        else
            local slt = parent.extParent and parent.extParent[index-1]
            if not slt or slt.type ~= 'select' then
                return
            end
            callback(slt.parent)
        end
    end)
end

local function ofFunction(source, callback)
    local parent = source.parent
    if not parent then
        return
    end
    if parent.type == 'return' then
        local func = guide.getParentFunction(parent)
        if not func then
            return
        end
        local index = findIndex(parent, source)
        if not index then
            return
        end
        findCallRecvs(func, index, function (src)
            vm.eachRef(src, callback)
        end)
    elseif parent.value == source then
        vm.eachRef(parent, callback)
    end
end

local function eachDef(source, callback)
    local stype = source.type
    if     stype == 'local' then
        ofLocal(source, callback)
    elseif stype == 'getlocal'
    or     stype == 'setlocal' then
        ofLocal(source.node, callback)
    elseif stype == 'setglobal'
    or     stype == 'getglobal' then
        ofGlobal(source, callback)
    elseif stype == 'field'
    or     stype == 'method' then
        ofField(source, callback)
    elseif stype == 'setfield'
    or     stype == 'getfield' then
        ofField(source.field, callback)
    elseif stype == 'setmethod'
    or     stype == 'getmethod' then
        ofField(source.method, callback)
    elseif stype == 'tablefield' then
        ofTableField(source, callback)
    elseif stype == 'number'
    or     stype == 'boolean'
    or     stype == 'string' then
        ofLiteral(source, callback)
    elseif stype == 'function' then
        ofFunction(source, callback)
    elseif stype == 'goto' then
        ofGoTo(source, callback)
    elseif stype == 'label' then
        ofLabel(source, callback)
    end
end

--- 获取所有的定义
function vm.eachDef(source, callback, max)
    local mark = {}
    eachDef(source, function (src)
        if mark[src] then
            return
        end
        mark[src] = true
        callback(src)
    end)
end