This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
/
symbol.sk
389 lines (327 loc) · 11.2 KB
/
symbol.sk
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
namespace Skew {
enum SymbolKind {
PARAMETER_FUNCTION
PARAMETER_OBJECT
OBJECT_CLASS
OBJECT_ENUM
OBJECT_GLOBAL
OBJECT_INTERFACE
OBJECT_NAMESPACE
OBJECT_WRAPPED
FUNCTION_ANNOTATION
FUNCTION_CONSTRUCTOR
FUNCTION_GLOBAL
FUNCTION_INSTANCE
FUNCTION_LOCAL
OVERLOADED_ANNOTATION
OVERLOADED_GLOBAL
OVERLOADED_INSTANCE
VARIABLE_ARGUMENT
VARIABLE_ENUM
VARIABLE_GLOBAL
VARIABLE_INSTANCE
VARIABLE_LOCAL
def isType bool {
return self >= PARAMETER_FUNCTION && self <= OBJECT_WRAPPED
}
def isParameter bool {
return self >= PARAMETER_FUNCTION && self <= PARAMETER_OBJECT
}
def isObject bool {
return self >= OBJECT_CLASS && self <= OBJECT_WRAPPED
}
def isFunction bool {
return self >= FUNCTION_ANNOTATION && self <= FUNCTION_LOCAL
}
def isOverloadedFunction bool {
return self >= OVERLOADED_ANNOTATION && self <= OVERLOADED_INSTANCE
}
def isFunctionOrOverloadedFunction bool {
return self >= FUNCTION_ANNOTATION && self <= OVERLOADED_INSTANCE
}
def isVariable bool {
return self >= VARIABLE_ARGUMENT && self <= VARIABLE_LOCAL
}
def isLocalOrArgumentVariable bool {
return self == VARIABLE_ARGUMENT || self == VARIABLE_LOCAL
}
def isNamespaceOrGlobal bool {
return self == OBJECT_NAMESPACE || self == OBJECT_GLOBAL
}
def isGlobalReference bool {
return
self == VARIABLE_ENUM || self == VARIABLE_GLOBAL ||
self == FUNCTION_GLOBAL || self == FUNCTION_CONSTRUCTOR ||
self == OVERLOADED_GLOBAL || isType
}
def hasInstances bool {
return self == OBJECT_CLASS || self == OBJECT_ENUM || self == OBJECT_INTERFACE || self == OBJECT_WRAPPED
}
def isOnInstances bool {
return self == FUNCTION_INSTANCE || self == VARIABLE_INSTANCE || self == OVERLOADED_INSTANCE
}
def isLocal bool {
return self == FUNCTION_LOCAL || self == VARIABLE_LOCAL || self == VARIABLE_ARGUMENT
}
}
enum SymbolState {
UNINITIALIZED
INITIALIZING
INITIALIZED
}
namespace Symbol {
# Flags
const IS_AUTOMATICALLY_GENERATED = 1 << 0
const IS_CONST = 1 << 1
const IS_GETTER = 1 << 2
const IS_LOOP_VARIABLE = 1 << 3
const IS_OVER = 1 << 4
const IS_SETTER = 1 << 5
const IS_VALUE_TYPE = 1 << 6
const SHOULD_INFER_RETURN_TYPE = 1 << 7
# Modifiers
const IS_DEPRECATED = 1 << 8
const IS_ENTRY_POINT = 1 << 9
const IS_EXPORTED = 1 << 10
const IS_IMPORTED = 1 << 11
const IS_INLINING_FORCED = 1 << 12
const IS_INLINING_PREVENTED = 1 << 13
const IS_PREFERRED = 1 << 14
const IS_PROTECTED = 1 << 15
const IS_RENAMED = 1 << 16
const IS_SKIPPED = 1 << 17
const SHOULD_SPREAD = 1 << 18
# Pass-specific flags
const IS_OBSOLETE = 1 << 19
const IS_PRIMARY_CONSTRUCTOR = 1 << 20
const IS_VIRTUAL = 1 << 21
}
class Symbol {
const id = _createID
var kind SymbolKind
var name string
var rename string = null
var range Range = null # The location of the name in the source code
var parent Symbol = null # Automatically set by the merging step
var resolvedType Type = null # Automatically set by the resolving step
var scope Scope = null # Automatically set by the merging step (resolving step for local variables)
var state SymbolState = .UNINITIALIZED
var annotations List<Node> = null
var comments List<string> = null
var forwardTo Symbol = null # Set by the interface removal step
var flags = 0
def _cloneFrom(symbol Symbol) {
rename = symbol.rename
range = symbol.range
scope = symbol.scope
state = symbol.state
flags = symbol.flags
}
# Flags
def isAutomaticallyGenerated bool { return (flags & IS_AUTOMATICALLY_GENERATED) != 0 }
def isConst bool { return (flags & IS_CONST) != 0 }
def isGetter bool { return (flags & IS_GETTER) != 0 }
def isLoopVariable bool { return (flags & IS_LOOP_VARIABLE) != 0 }
def isOver bool { return (flags & IS_OVER) != 0 }
def isSetter bool { return (flags & IS_SETTER) != 0 }
def isValueType bool { return (flags & IS_VALUE_TYPE) != 0 }
def shouldInferReturnType bool { return (flags & SHOULD_INFER_RETURN_TYPE) != 0 }
# Modifiers
def isDeprecated bool { return (flags & IS_DEPRECATED) != 0 }
def isEntryPoint bool { return (flags & IS_ENTRY_POINT) != 0 }
def isExported bool { return (flags & IS_EXPORTED) != 0 }
def isImported bool { return (flags & IS_IMPORTED) != 0 }
def isInliningForced bool { return (flags & IS_INLINING_FORCED) != 0 }
def isInliningPrevented bool { return (flags & IS_INLINING_PREVENTED) != 0 }
def isPreferred bool { return (flags & IS_PREFERRED) != 0 }
def isProtected bool { return (flags & IS_PROTECTED) != 0 }
def isRenamed bool { return (flags & IS_RENAMED) != 0 }
def isSkipped bool { return (flags & IS_SKIPPED) != 0 }
def shouldSpread bool { return (flags & SHOULD_SPREAD) != 0 }
# Pass-specific flags
def isObsolete bool { return (flags & IS_OBSOLETE) != 0 }
def isPrimaryConstructor bool { return (flags & IS_PRIMARY_CONSTRUCTOR) != 0 }
def isVirtual bool { return (flags & IS_VIRTUAL) != 0 }
# Combinations
def isImportedOrExported bool { return (flags & (IS_IMPORTED | IS_EXPORTED)) != 0 }
def asParameterSymbol ParameterSymbol {
assert(kind.isParameter)
return self as ParameterSymbol
}
def asObjectSymbol ObjectSymbol {
assert(kind.isObject)
return self as ObjectSymbol
}
def asFunctionSymbol FunctionSymbol {
assert(kind.isFunction)
return self as FunctionSymbol
}
def asOverloadedFunctionSymbol OverloadedFunctionSymbol {
assert(kind.isOverloadedFunction)
return self as OverloadedFunctionSymbol
}
def asVariableSymbol VariableSymbol {
assert(kind.isVariable)
return self as VariableSymbol
}
def fullName string {
if parent != null && parent.kind != .OBJECT_GLOBAL && !kind.isParameter {
return parent.fullName + "." + name
}
return name
}
def forwarded Symbol {
var symbol = self
while symbol.forwardTo != null {
symbol = symbol.forwardTo
}
return symbol
}
def spreadingAnnotations List<Node> {
var result List<Node> = null
if annotations != null {
for annotation in annotations {
if annotation.symbol != null && annotation.symbol.shouldSpread {
if result == null {
result = []
}
result.append(annotation)
}
}
}
return result
}
def mergeInformationFrom(symbol Symbol) {
if annotations == null {
annotations = symbol.annotations
} else if symbol.annotations != null {
annotations.append(symbol.annotations)
}
if comments == null {
comments = symbol.comments
} else if symbol.comments != null {
comments.append(symbol.comments)
}
if rename == null {
rename = symbol.rename
}
}
}
namespace Symbol {
var _nextID = 0
def _createID int {
return ++_nextID
}
def _substituteSymbols(node Node, symbols IntMap<Symbol>) {
if node.symbol != null {
node.symbol = symbols.get(node.symbol.id, node.symbol)
}
for child = node.firstChild; child != null; child = child.nextSibling {
_substituteSymbols(child, symbols)
}
}
}
class ParameterSymbol : Symbol {
def clone ParameterSymbol {
var clone = new(kind, name)
clone._cloneFrom(self)
clone.resolvedType = Type.new(.SYMBOL, clone)
return clone
}
}
class Guard {
var parent ObjectSymbol
var test Node
var contents ObjectSymbol
var elseGuard Guard
}
class ObjectSymbol : Symbol {
var extends Node = null
var implements List<Node> = null
var baseType Type = null
var baseClass ObjectSymbol = null
var interfaceTypes List<Type> = null
var wrappedType Type = null
var members StringMap<Symbol> = {} # Automatically expanded to include members from the base class by the resolving step
var objects List<ObjectSymbol> = []
var functions List<FunctionSymbol> = []
var variables List<VariableSymbol> = []
var parameters List<ParameterSymbol> = null
var guards List<Guard> = null # Compile-time if statements
var hasCheckedInterfacesAndAbstractStatus = false
var isAbstractBecauseOf FunctionSymbol = null # Used for diagnostics
def isAbstract bool {
return isAbstractBecauseOf != null
}
def hasBaseClass(symbol Symbol) bool {
return baseClass != null && (baseClass == symbol || baseClass.hasBaseClass(symbol))
}
def isSameOrHasBaseClass(symbol Symbol) bool {
return self == symbol || hasBaseClass(symbol)
}
}
class FunctionSymbol : Symbol {
var overridden FunctionSymbol = null # For derived class functions
var overloaded OverloadedFunctionSymbol = null # Links overloaded functions to the other overloads on that type (not on derived types)
var implementations List<FunctionSymbol> = null # For interface functions
var parameters List<ParameterSymbol> = null
var arguments List<VariableSymbol> = []
var this VariableSymbol = null # For instance functions and constructors
var argumentOnlyType Type = null # For quickly comparing the argument types of two function symbols
var returnType Node = null
var block Node = null
var namingGroup = -1 # Automatically filled out by the renaming step
def clone FunctionSymbol {
var clone = new(kind, name)
var symbols IntMap<Symbol> = {}
clone._cloneFrom(self)
clone.resolvedType = Type.new(.SYMBOL, clone)
clone.argumentOnlyType = argumentOnlyType
if parameters != null {
clone.parameters = []
for parameter in parameters {
var cloned = parameter.clone
symbols[parameter.id] = cloned
clone.parameters.append(cloned)
}
}
for argument in arguments {
var cloned = argument.clone
symbols[argument.id] = cloned
clone.arguments.append(cloned)
}
if returnType != null {
clone.returnType = returnType.clone
}
if block != null {
clone.block = block.clone
Symbol._substituteSymbols(clone.block, symbols)
}
return clone
}
}
class VariableSymbol : Symbol {
var type Node = null
var value Node = null
def enumValue int {
assert(kind == .VARIABLE_ENUM)
return value.asInt
}
def clone VariableSymbol {
var clone = new(kind, name)
clone._cloneFrom(self)
clone.resolvedType = resolvedType
if type != null {
clone.type = type.clone
}
if value != null {
clone.value = value.clone
Symbol._substituteSymbols(clone.value, {id: clone})
}
return clone
}
}
class OverloadedFunctionSymbol : Symbol {
var symbols List<FunctionSymbol>
}
}