-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNameResolver.js
300 lines (267 loc) · 11.5 KB
/
NameResolver.js
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
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
// This only resolves names. After this phase runs, all names will be resolved. This means that all
// XYZRef objects now know who they resolve to. This does not yet allow type analysis, because there
// are still TypeDefs. This throws type errors for failed name resolutions and for things that are
// more convenient to check here than in the Checker. This usually involves things that need to be
// checked before TypeRefToTypeDefSkipper.
class NameResolver extends Visitor {
constructor(nameContext)
{
super();
this._nameContext = nameContext;
}
doStatement(statement)
{
this._nameContext.doStatement(statement, () => statement.visit(this));
}
_visitTypeParametersAndBuildNameContext(node)
{
let nameContext = new NameContext(this._nameContext);
for (let typeParameter of node.typeParameters) {
nameContext.add(typeParameter);
typeParameter.visit(this);
}
return nameContext;
}
visitFunc(node)
{
let checker = new NameResolver(this._visitTypeParametersAndBuildNameContext(node));
node.returnType.visit(checker);
for (let parameter of node.parameters)
parameter.visit(checker);
}
visitFuncDef(node)
{
let contextWithTypeParameters = this._visitTypeParametersAndBuildNameContext(node);
let checkerWithTypeParameters = new NameResolver(contextWithTypeParameters);
node.returnType.visit(checkerWithTypeParameters);
let contextWithParameters = new NameContext(contextWithTypeParameters);
for (let parameter of node.parameters) {
parameter.visit(checkerWithTypeParameters);
contextWithParameters.add(parameter);
}
let checkerWithParameters = new NameResolver(contextWithParameters);
node.body.visit(checkerWithParameters);
}
visitBlock(node)
{
let checker = new NameResolver(new NameContext(this._nameContext));
for (let statement of node.statements)
statement.visit(checker);
}
visitIfStatement(node)
{
node.conditional.visit(this);
// The bodies might not be Blocks, so we need to explicitly give them a new context.
node.body.visit(new NameResolver(new NameContext(this._nameContext)));
if (node.elseBody)
node.elseBody.visit(new NameResolver(new NameContext(this._nameContext)));
}
visitWhileLoop(node)
{
node.conditional.visit(this);
// The bodies might not be Blocks, so we need to explicitly give them a new context.
node.body.visit(new NameResolver(new NameContext(this._nameContext)));
}
visitDoWhileLoop(node)
{
// The bodies might not be Blocks, so we need to explicitly give them a new context.
node.body.visit(new NameResolver(new NameContext(this._nameContext)));
node.conditional.visit(this);
}
visitForLoop(node)
{
let newResolver = new NameResolver(new NameContext(this._nameContext))
if (node.initialization)
node.initialization.visit(newResolver);
if (node.condition)
node.condition.visit(newResolver);
if (node.increment)
node.increment.visit(newResolver);
node.body.visit(newResolver);
}
visitProtocolDecl(node)
{
for (let parent of node.extends)
parent.visit(this);
let nameContext = new NameContext(this._nameContext);
nameContext.add(node.typeVariable);
let checker = new NameResolver(nameContext);
for (let signature of node.signatures)
signature.visit(checker);
}
visitProtocolRef(node)
{
let result = this._nameContext.get(Protocol, node.name);
if (!result)
throw new WTypeError(node.origin.originString, "Could not find protocol named " + node.name);
node.protocolDecl = result;
}
visitProtocolFuncDecl(node)
{
this.visitFunc(node);
let funcs = this._nameContext.get(Func, node.name);
if (!funcs)
throw new WTypeError(node.origin.originString, "Cannot find any functions named " + node.name);
node.possibleOverloads = funcs;
}
visitTypeDef(node)
{
let nameContext = new NameContext(this._nameContext);
for (let typeParameter of node.typeParameters) {
typeParameter.visit(this);
nameContext.add(typeParameter);
}
let checker = new NameResolver(nameContext);
node.type.visit(checker);
}
visitStructType(node)
{
let nameContext = new NameContext(this._nameContext);
for (let typeParameter of node.typeParameters) {
typeParameter.visit(this);
nameContext.add(typeParameter);
}
let checker = new NameResolver(nameContext);
for (let field of node.fields)
field.visit(checker);
}
_resolveTypeArguments(typeArguments)
{
for (let i = 0; i < typeArguments.length; ++i) {
let typeArgument = typeArguments[i];
if (typeArgument instanceof TypeOrVariableRef) {
let thing = this._nameContext.get(Anything, typeArgument.name);
if (!thing)
new WTypeError(typeArgument.origin.originString, "Could not find type or variable named " + typeArgument.name);
if (thing instanceof Value)
typeArguments[i] = new VariableRef(typeArgument.origin, typeArgument.name);
else if (thing instanceof Type)
typeArguments[i] = new TypeRef(typeArgument.origin, typeArgument.name, []);
else
throw new WTypeError(typeArgument.origin.originString, "Type argument resolved to wrong kind of thing: " + thing.kind);
}
if (typeArgument[i] instanceof Value
&& !typeArgument[i].isConstexpr)
throw new WTypeError(typeArgument[i].origin.originString, "Expected constexpr");
}
}
visitTypeRef(node)
{
this._resolveTypeArguments(node.typeArguments);
let type = node.type;
if (!type) {
type = this._nameContext.get(Type, node.name);
if (!type)
throw new WTypeError(node.origin.originString, "Could not find type named " + node.name);
node.type = type;
}
if (type.typeParameters.length != node.typeArguments.length)
throw new WTypeError(node.origin.originString, "Wrong number of type arguments (passed " + node.typeArguments.length + ", expected " + type.typeParameters.length + ")");
for (let i = 0; i < type.typeParameters.length; ++i) {
let parameterIsType = type.typeParameters[i] instanceof TypeVariable;
let argumentIsType = node.typeArguments[i] instanceof Type;
node.typeArguments[i].visit(this);
if (parameterIsType && !argumentIsType)
throw new WTypeError(node.origin.originString, "Expected type, but got value at argument #" + i);
if (!parameterIsType && argumentIsType)
throw new WTypeError(node.origin.originString, "Expected value, but got type at argument #" + i);
}
}
visitReferenceType(node)
{
let nameContext = new NameContext(this._nameContext);
node.elementType.visit(new NameResolver(nameContext));
}
visitVariableDecl(node)
{
this._nameContext.add(node);
node.type.visit(this);
if (node.initializer)
node.initializer.visit(this);
}
visitVariableRef(node)
{
if (node.variable)
return;
let result = this._nameContext.get(Value, node.name);
if (!result)
throw new WTypeError(node.origin.originString, "Could not find variable named " + node.name);
node.variable = result;
}
visitReturn(node)
{
node.func = this._nameContext.currentStatement;
super.visitReturn(node);
}
_handlePropertyAccess(node)
{
node.possibleGetOverloads = this._nameContext.get(Func, node.getFuncName);
node.possibleSetOverloads = this._nameContext.get(Func, node.setFuncName);
node.possibleAndOverloads = this._nameContext.get(Func, node.andFuncName);
if (!node.possibleGetOverloads && !node.possibleAndOverloads)
throw new WTypeError(node.origin.originString, "Cannot find either " + node.getFuncName + " or " + node.andFuncName);
}
visitDotExpression(node)
{
// This could be a reference to an enum. Let's resolve that now.
if (node.struct instanceof VariableRef) {
let enumType = this._nameContext.get(Type, node.struct.name);
if (enumType && enumType instanceof EnumType) {
let enumMember = enumType.memberByName(node.fieldName);
if (!enumMember)
throw new WTypeError(node.origin.originString, "Enum " + enumType.name + " does not have a member named " + node.fieldName);
node.become(new EnumLiteral(node.origin, enumMember));
return;
}
}
this._handlePropertyAccess(node);
super.visitDotExpression(node);
}
visitIndexExpression(node)
{
this._handlePropertyAccess(node);
super.visitIndexExpression(node);
}
visitCallExpression(node)
{
this._resolveTypeArguments(node.typeArguments);
let funcs = this._nameContext.get(Func, node.name);
if (funcs)
node.possibleOverloads = funcs;
else {
let type = this._nameContext.get(Type, node.name);
if (!type)
throw new WTypeError(node.origin.originString, "Cannot find any function or type named \"" + node.name + "\"");
node.becomeCast(type);
node.possibleOverloads = this._nameContext.get(Func, "operator cast");
if (!node.possibleOverloads)
throw new WTypeError(node.origin.originString, "Cannot find any operator cast implementations in cast to " + type);
}
super.visitCallExpression(node);
}
}