-
Notifications
You must be signed in to change notification settings - Fork 55
/
SymbolReferenceVisitor.fu
293 lines (252 loc) · 7.46 KB
/
SymbolReferenceVisitor.fu
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
// SymbolReferenceVisitor.fu - visitor of symbol references
//
// Copyright (C) 2024 Piotr Fusik
//
// This file is part of Fusion Transpiler,
// see https://github.com/fusionlanguage/fut
//
// Fusion Transpiler is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fusion Transpiler is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fusion Transpiler. If not, see http://www.gnu.org/licenses/
public abstract class FuSymbolReferenceVisitor : FuVisitor
{
FuSymbol Symbol;
protected abstract void VisitFound(FuStatement reference);
void VisitReference(FuStatement reference, FuSymbol symbol)
{
if (symbol == this.Symbol)
VisitFound(reference);
}
void VisitType(FuType type)
{
if (type is FuClassType klass) {
VisitReference(klass, klass.Class);
if (klass.TypeArg0 != null) {
VisitType(klass.TypeArg0);
if (klass.TypeArg1 != null)
VisitType(klass.TypeArg1);
}
}
}
internal override void VisitConst!(FuConst statement)
{
VisitType(statement.Type);
statement.Value.Accept(this, FuPriority.Statement);
}
internal override void VisitExpr!(FuExpr expr)
{
expr.Accept(this, FuPriority.Statement);
}
void VisitStatements!(List<FuStatement#> statements)
{
foreach (FuStatement statement in statements)
statement.AcceptStatement(this);
}
internal override void VisitBlock!(FuBlock block)
{
VisitStatements(block.Statements);
}
internal override void VisitAssert!(FuAssert statement)
{
statement.Cond.Accept(this, FuPriority.Statement);
VisitOptionalStatement(statement.Message);
}
internal override void VisitBreak!(FuBreak statement)
{
}
internal override void VisitContinue!(FuContinue statement)
{
}
internal override void VisitDoWhile!(FuDoWhile statement)
{
statement.Body.AcceptStatement(this);
statement.Cond.Accept(this, FuPriority.Statement);
}
internal override void VisitFor!(FuFor statement)
{
VisitOptionalStatement(statement.Init);
VisitOptionalStatement(statement.Cond);
VisitOptionalStatement(statement.Advance);
statement.Body.AcceptStatement(this);
}
internal override void VisitForeach!(FuForeach statement)
{
FuVar element = statement.GetVar();
VisitType(element.Type);
if (element.Next != null)
VisitType(element.Next.Type);
statement.Collection.Accept(this, FuPriority.Statement);
statement.Body.AcceptStatement(this);
}
internal override void VisitIf!(FuIf statement)
{
statement.Cond.Accept(this, FuPriority.Statement);
statement.OnTrue.AcceptStatement(this);
VisitOptionalStatement(statement.OnFalse);
}
internal override void VisitLock!(FuLock statement)
{
statement.Lock.Accept(this, FuPriority.Statement);
statement.Body.AcceptStatement(this);
}
internal override void VisitNative!(FuNative statement)
{
}
internal override void VisitReturn!(FuReturn statement)
{
VisitOptionalStatement(statement.Value);
}
internal override void VisitSwitch!(FuSwitch statement)
{
statement.Value.Accept(this, FuPriority.Statement);
foreach (FuCase kase in statement.Cases) {
foreach (FuExpr value in kase.Values)
value.Accept(this, FuPriority.Statement);
VisitStatements(kase.Body);
}
VisitStatements(statement.DefaultBody);
}
internal override void VisitThrow!(FuThrow statement)
{
VisitSymbolReference(statement.Class, FuPriority.Statement);
VisitOptionalStatement(statement.Message);
}
internal override void VisitWhile!(FuWhile statement)
{
statement.Cond.Accept(this, FuPriority.Statement);
statement.Body.AcceptStatement(this);
}
internal override void VisitEnumValue!(FuConst konst, FuConst? previous)
{
assert false;
}
internal override void VisitLiteralNull!()
{
}
internal override void VisitLiteralFalse!()
{
}
internal override void VisitLiteralTrue!()
{
}
internal override void VisitLiteralLong!(long value)
{
}
internal override void VisitLiteralChar!(int value)
{
}
internal override void VisitLiteralDouble!(double value)
{
}
internal override void VisitLiteralString!(string value)
{
}
internal override void VisitAggregateInitializer!(FuAggregateInitializer expr)
{
foreach (FuExpr item in expr.Items)
item.Accept(this, FuPriority.Statement);
}
internal override void VisitInterpolatedString!(FuInterpolatedString expr, FuPriority parent)
{
foreach (FuInterpolatedPart part in expr.Parts) {
part.Argument.Accept(this, FuPriority.Statement);
VisitOptionalStatement(part.WidthExpr);
}
}
internal override void VisitSymbolReference!(FuSymbolReference expr, FuPriority parent)
{
VisitOptionalStatement(expr.Left);
VisitReference(expr, expr.Symbol);
}
internal override void VisitPrefixExpr!(FuPrefixExpr expr, FuPriority parent)
{
if (expr.Op == FuToken.New)
VisitType(expr.Type);
VisitOptionalStatement(expr.Inner);
}
internal override void VisitPostfixExpr!(FuPostfixExpr expr, FuPriority parent)
{
expr.Inner.Accept(this, FuPriority.Statement);
}
internal override void VisitBinaryExpr!(FuBinaryExpr expr, FuPriority parent)
{
expr.Left.Accept(this, FuPriority.Statement);
expr.Right.Accept(this, FuPriority.Statement);
}
internal override void VisitSelectExpr!(FuSelectExpr expr, FuPriority parent)
{
expr.Cond.Accept(this, FuPriority.Statement);
expr.OnTrue.Accept(this, FuPriority.Statement);
expr.OnFalse.Accept(this, FuPriority.Statement);
}
internal override void VisitCallExpr!(FuCallExpr expr, FuPriority parent)
{
VisitSymbolReference(expr.Method, FuPriority.Statement);
foreach (FuExpr arg in expr.Arguments)
arg.Accept(this, FuPriority.Statement);
}
internal override void VisitLambdaExpr!(FuLambdaExpr expr)
{
expr.Body.Accept(this, FuPriority.Statement);
}
internal override void VisitVar!(FuVar expr)
{
VisitType(expr.Type);
VisitOptionalStatement(expr.Value);
}
void VisitClass!(FuClass klass)
{
if (klass.HasBaseClass())
VisitSymbolReference(klass.BaseClass, FuPriority.Statement);
if (klass.Constructor != null)
klass.Constructor.Body.AcceptStatement(this);
for (FuSymbol? symbol = klass.First; symbol != null; symbol = symbol.Next) {
VisitType(symbol.Type);
switch (symbol) {
case FuConst konst:
konst.Value.Accept(this, FuPriority.Statement);
break;
case FuField field:
VisitOptionalStatement(field.Value);
break;
case FuMethod method:
for (FuVar? param = method.FirstParameter(); param != null; param = param.NextVar())
VisitVar(param);
foreach (FuThrowsDeclaration decl in method.Throws)
VisitSymbolReference(decl, FuPriority.Statement);
VisitOptionalStatement(method.Body);
break;
default:
assert false;
}
}
}
public void FindReferences!(FuProgram program, FuSymbol symbol)
{
this.Symbol = symbol;
for (FuSymbol? type = program.First; type != null; type = type.Next) {
switch (type) {
case FuClass klass:
VisitClass(klass);
break;
case FuEnum enu:
for (FuSymbol? symbol = enu.First; symbol != null; symbol = symbol.Next) {
if (symbol is FuConst konst && !(konst.Value is FuImplicitEnumValue))
konst.Value.Accept(this, FuPriority.Statement);
}
break;
default:
assert false;
}
}
}
}