-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAnalyzer.java
More file actions
475 lines (443 loc) · 15.8 KB
/
SemanticAnalyzer.java
File metadata and controls
475 lines (443 loc) · 15.8 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import xtc.tree.Location;
class SemanticAnalyzer extends DepthFirstVisitor {
SymbolTable _symTab;
static final NullType NULL_TYPE = new NullType();
SemanticAnalyzer(SymbolTable symTab) { _symTab = symTab; }
private static boolean knownType(Type type) {
if (null == type)
return false;
if (type instanceof ArrayType)
return knownType(((ArrayType)type)._elem);
if (type instanceof RecordType)
for (FieldType t : ((RecordType)type)._fields)
if (!knownType(t))
return false;
if (type instanceof FieldType)
return knownType(((FieldType)type)._type);
if (type instanceof FunType)
return knownType(((FunType)type)._formals)
&& knownType(((FunType)type)._returnType);
return true;
}
private static boolean sameType(Type t1, Type t2) {
if (t1 == null || t2 == null)
return true;
return t1.equals(t2);
}
private static boolean subType(Type t1, Type t2) {
if (sameType(t1, t2))
return true;
if (t1 instanceof NullType)
return t2 instanceof NullType || t2 instanceof RecordType;
if (t1.getClass() != t2.getClass())
return false;
if (t1 instanceof PrimitiveType)
return ((PrimitiveType)t1)._name == ((PrimitiveType)t2)._name;
if (t1 instanceof ArrayType)
return sameType(((ArrayType)t1)._elem, ((ArrayType)t2)._elem);
if (t1 instanceof RecordType) {
RecordType r1 = (RecordType)t1, r2 = (RecordType)t2;
if (r1._fields.size() < r2._fields.size())
return false;
for (int i=0, n=r2._fields.size(); i<n; i++)
if (!sameType(r1._fields.get(i), r2._fields.get(i)))
return false;
return true;
}
if (t1 instanceof FieldType)
return sameType(t1, t2);
if (t1 instanceof FunType) {
FunType f1 = (FunType)t1, f2 = (FunType)t2;
return subType(f2._formals, f1._formals)
&& subType(f1._returnType, f2._returnType);
}
assert false : "unimplemented case?";
return false;
}
private static boolean castable(Type t1, Type t2) {
if (subType(t1, t2))
return true;
if (t1 instanceof RecordType && t2 instanceof NullType)
return true;
if (t1.getClass() != t2.getClass())
return false;
if (t1 instanceof PrimitiveType)
return true;
if (t1 instanceof ArrayType)
return sameType(((ArrayType)t1)._elem, ((ArrayType)t2)._elem);
if (t1 instanceof RecordType)
return subType(t1, t2) || subType(t2, t1);
if (t1 instanceof FieldType)
return sameType(t1, t2);
if (t1 instanceof FunType) {
FunType f1 = (FunType)t1, f2 = (FunType)t2;
return castable(f2._formals, f1._formals)
&& castable(f1._returnType, f2._returnType);
}
assert false : "unimplemented case?";
return false;
}
// ---------------- top-level ----------------
Object visit(FunDef ast) {
_symTab.push(ast._heldScope);
super.visit(ast);
_symTab.pop(ast._heldScope);
return ast._type;
}
// ---------------- types ----------------
Object visit(RecordType ast) {
_symTab.push(ast._heldScope);
for (FieldType f : ast._fields)
f.accept(this);
_symTab.pop(ast._heldScope);
return null;
}
// ---------------- statements ----------------
Object visit(VarDef ast) {
Type type = (Type)ast._rhs.accept(this);
if (!knownType(type))
ErrorPrinter.print(ast._loc, "Could not resolve type for variable '"
+ ast._var._id + "'");
return null;
}
Object visit(AssignStmt ast) {
Type lhsType = (Type)ast._lhs.accept(this);
Type rhsType = (Type)ast._rhs.accept(this);
if (ast._lhs instanceof VarId
|| ast._lhs instanceof SubscriptExpr
|| ast._lhs instanceof FieldExpr) {
if (!sameType(rhsType, lhsType)) {
if (subType(rhsType, lhsType))
ast._rhs = new CastExpr(ast._rhs, lhsType);
else
ErrorPrinter.print(ast._loc, "Cannot assign to '" + lhsType
+ "' from '" + rhsType + "'");
}
} else {
ErrorPrinter.print(ast._loc, "Assignment to immutable expression");
}
return null;
}
Object visit(BlockStmt ast) {
if (null != ast._heldScope)
_symTab.push(ast._heldScope);
for (Stmt s : ast._stmts)
s.accept(this);
if (null != ast._heldScope)
_symTab.pop(ast._heldScope);
return null;
}
Object visit(ForStmt ast) {
_symTab.push(ast._heldScope);
String varName = ast._var._id;
Type exprType = (Type)ast._expr.accept(this);
if (knownType(exprType)) {
if (!(exprType instanceof ArrayType))
ErrorPrinter.print(ast._loc, "Subject of for-loop must be array");
} else {
ErrorPrinter.print(ast._loc, "Could not resolve type for variable '"
+ varName + "'");
}
ast._body.accept(this);
_symTab.pop(ast._heldScope);
return null;
}
Object visit(IfStmt ast) {
Type condType = (Type)ast._cond.accept(this);
if (!sameType(condType, PrimitiveType.BOOLT))
ErrorPrinter.print(ast._cond._loc, "Boolean expected");
ast._thenBranch.accept(this);
if (null != ast._elseBranch)
ast._elseBranch.accept(this);
return null;
}
Object visit(ReturnStmt ast) {
Type act = PrimitiveType.VOIDT;
if (null != ast._expr)
act = (Type)ast._expr.accept(this);
FunDef fun = null;
for (Scope s = _symTab._current; null != s; s = s._parent)
if (null != s._owner && s._owner instanceof FunDef)
fun = (FunDef)s._owner;
Type frm = fun._type._returnType;
if (!sameType(act, frm)) {
if (subType(act, frm))
ast._expr = new CastExpr(ast._expr, frm);
else
ErrorPrinter.print(ast._loc, "Expected return value of type '"
+ frm + "', found '" + act + "'");
}
return null;
}
Object visit(WhileStmt ast) {
Type condType = (Type)ast._cond.accept(this);
if (!sameType(condType, PrimitiveType.BOOLT))
ErrorPrinter.print(ast._cond._loc, "Boolean expected");
ast._body.accept(this);
return null;
}
// ---------------- expressions ----------------
Object visit(InfixExpr ast) {
Type lhsType = (Type)ast._lhs.accept(this);
Type rhsType = (Type)ast._rhs.accept(this);
if ("||".equals(ast._op) || "&&".equals(ast._op)) {
ast._type = PrimitiveType.BOOLT;
if (!sameType(lhsType, PrimitiveType.BOOLT))
ErrorPrinter.print(ast._lhs._loc, "Boolean expected");
if (!sameType(rhsType, PrimitiveType.BOOLT))
ErrorPrinter.print(ast._rhs._loc, "Boolean expected");
} else if ("==".equals(ast._op) || "!=".equals(ast._op)) {
ast._type = PrimitiveType.BOOLT;
if (!sameType(lhsType, rhsType)) {
if ((lhsType instanceof NullType || lhsType instanceof RecordType) &&
castable(lhsType, rhsType)) {
//no explicit conversion required to compare pointers to records
} else {
ErrorPrinter.print(ast._loc, "Cannot compare '"
+ lhsType + "' and '" + rhsType + "'");
}
}
} else if ("<=".equals(ast._op) || "<".equals(ast._op)
|| ">=".equals(ast._op) || ">".equals(ast._op)) {
ast._type = PrimitiveType.BOOLT;
if (!sameType(lhsType, PrimitiveType.INTT))
ErrorPrinter.print(ast._lhs._loc, "Integer expected");
if (!sameType(rhsType, PrimitiveType.INTT))
ErrorPrinter.print(ast._rhs._loc, "Integer expected");
} else if ("+".equals(ast._op)) {
if (sameType(lhsType, PrimitiveType.STRINGT) ||
sameType(rhsType, PrimitiveType.STRINGT)) {
ast._type = PrimitiveType.STRINGT;
if (!sameType(lhsType, PrimitiveType.STRINGT)) {
if (castable(lhsType, PrimitiveType.STRINGT))
ast._lhs = new CastExpr(ast._lhs, PrimitiveType.STRINGT);
else
ErrorPrinter.print(ast._lhs._loc, "Cannot convert from type '"
+ lhsType + "' to type 'string'");
}
if (!sameType(rhsType, PrimitiveType.STRINGT)) {
if (castable(rhsType, PrimitiveType.STRINGT))
ast._rhs = new CastExpr(ast._rhs, PrimitiveType.STRINGT);
else
ErrorPrinter.print(ast._rhs._loc, "Cannot convert from type '"
+ rhsType + "' to type 'string'");
}
} else {
ast._type = PrimitiveType.INTT;
if (!sameType(lhsType, PrimitiveType.INTT))
ErrorPrinter.print(ast._lhs._loc, "Integer expected");
if (!sameType(rhsType, PrimitiveType.INTT))
ErrorPrinter.print(ast._rhs._loc, "Integer expected");
}
} else if ("-".equals(ast._op) || "*".equals(ast._op)
|| "/".equals(ast._op) || "%".equals(ast._op)) {
ast._type = PrimitiveType.INTT;
if (!sameType(lhsType, PrimitiveType.INTT))
ErrorPrinter.print(ast._lhs._loc, "Integer expected");
if (!sameType(rhsType, PrimitiveType.INTT))
ErrorPrinter.print(ast._rhs._loc, "Integer expected");
} else {
assert false : ast._op;
}
return ast._type;
}
Object visit(PrefixExpr ast) {
Type baseType = (Type)ast._base.accept(this);
if ("-".equals(ast._op)) {
ast._type = PrimitiveType.INTT;
if (!sameType(baseType, PrimitiveType.INTT))
ErrorPrinter.print(ast._base._loc, "Integer expected");
} else if ("!".equals(ast._op)) {
ast._type = PrimitiveType.BOOLT;
if (!sameType(baseType, PrimitiveType.BOOLT))
ErrorPrinter.print(ast._base._loc, "Boolean expected");
} else {
assert false : ast._op;
}
return ast._type;
}
Object visit(CallExpr ast) {
FunSym callee = null;
if (ast._base instanceof FunId)
callee = (FunSym)ast._base.accept(this);
else
ErrorPrinter.print(ast._loc, "Function name must be simple identifier");
List<Type> actuals = new ArrayList<Type>();
for (Expr expr : ast._actuals)
actuals.add((Type)expr.accept(this));
if (null != callee) {
FunType funType = callee._def._type;
ast._type = funType._returnType;
List<FieldType> formals = funType._formals._fields;
if (actuals.size() == formals.size())
for (int i=0, n=formals.size(); i<n; i++) {
Type act = actuals.get(i), frm = formals.get(i)._type;
if (!sameType(act, frm)) {
if (subType(act, frm))
ast._actuals.set(i, new CastExpr(ast._actuals.get(i), frm));
else if (Intrinsics.get(_symTab, "size") == callee &&
act instanceof ArrayType)
/*allow any array type for generic intrinsic*/;
else
ErrorPrinter.print(ast._actuals.get(i)._loc,
"Formal '" + formals.get(i)._field._id
+ "' of function '" + callee.name()
+ "' expects '" + frm + "', found '"
+ act + "' instead");
}
}
else
ErrorPrinter.print(ast._loc, "Function '" + callee.name() + "' has "
+ formals.size() + " formals, but there are "
+ actuals.size() + " actuals");
}
return ast._type;
}
Object visit(CastExpr ast) {
Type srcType = (Type)ast._base.accept(this);
Type tgtType = ast._targetType;
ast._type = ast._targetType;
if (!castable(srcType, tgtType))
ErrorPrinter.print(ast._loc, "Cannot cast from type '" + srcType
+ "' to type '" + tgtType + "'");
return ast._type;
}
Object visit(FieldExpr ast) {
Type baseType = (Type)ast._base.accept(this);
if (knownType(baseType)) {
if (baseType instanceof RecordType) {
Scope scope = ((RecordType)baseType)._heldScope;
String name = ast._field._id;
if (scope.contains(name)) {
FieldSym sym = (FieldSym)scope.get(name);
ast._field._sym = sym;
ast._type = sym.type()._type;
} else {
ErrorPrinter.print(ast._field._loc, "Unknown field '" + name + "'");
}
} else {
ErrorPrinter.print(ast._loc, "Base of field expression must be record");
}
}
return ast._type;
}
Object visit(SubscriptExpr ast) {
Type baseType = (Type)ast._base.accept(this);
Type subscriptType = (Type)ast._subscript.accept(this);
if (null != baseType) {
if (baseType instanceof ArrayType)
ast._type = ((ArrayType)baseType)._elem;
else
ErrorPrinter.print(ast._loc, "Base of subscript must be array");
}
if (null != subscriptType) {
if (!sameType(subscriptType, PrimitiveType.INTT))
ErrorPrinter.print(ast._subscript._loc, "Integer expected");
}
return ast._type;
}
Object visit(ParenExpr ast) {
ast._type = (Type)ast._base.accept(this);
return ast._type;
}
// ---------------- identifiers ----------------
Object visit(FieldId ast) {
Symbol s = _symTab.lookup(ast._id);
if (null == s)
ErrorPrinter.print(ast._loc, "Unknown field '" + ast._id + "'");
else if (!(s instanceof FieldSym))
ErrorPrinter.print(ast._loc, "Field name expected");
else
ast._sym = s;
return null == ast._sym ? null : ast._sym.type();
}
Object visit(FunId ast) {
Symbol s = _symTab.lookup(ast._id);
if (null == s)
ErrorPrinter.print(ast._loc, "Unknown function '" + ast._id + "'");
else if (!(s instanceof FunSym))
ErrorPrinter.print(ast._loc, "Function name expected");
else
ast._sym = (FunSym)s;
return null == ast._sym ? null : ast._sym;
}
Object visit(VarId ast) {
Symbol s = _symTab.lookup(ast._id);
if (null == s) {
ErrorPrinter.print(ast._loc, "Unknown variable '" + ast._id + "'");
} else if (!(s instanceof VarSym)) {
ErrorPrinter.print(ast._loc, "Variable name expected");
} else {
ast._sym = (VarSym)s;
ast._type = ast._sym.type();
}
return ast._type;
}
// ---------------- literals ----------------
Object visit(ArrayLit ast) {
Type elemType = null;
for (Expr expr : ast._elems) {
Type typ = (Type)expr.accept(this);
if (knownType(typ)) {
if (knownType(elemType)) {
if (!sameType(typ, elemType))
ErrorPrinter.print(expr._loc,
"Expected element of type '" + elemType +
"', found '" + typ + "'");
} else {
elemType = typ;
}
} else {
ErrorPrinter.print(expr._loc, "Could not resolve array element type");
}
}
ast._type = new ArrayType(ast._loc, elemType);
return ast._type;
}
Object visit(RecordLit ast) {
_symTab.push(ast._heldScope);
List<FieldType> fieldTypes = new ArrayList<FieldType>();
for (FieldLit lit : ast._fields)
fieldTypes.add((FieldType)lit.accept(this));
boolean anyNull = false;
for (FieldType typ : fieldTypes)
if (null == typ)
anyNull = true;
if (!anyNull) {
RecordType result = new RecordType(ast._loc, fieldTypes);
result._heldScope = ast._heldScope;
ast._type = result;
}
_symTab.pop(ast._heldScope);
return ast._type;
}
Object visit(FieldLit ast) {
ast._field.accept(this);
Type type = (Type)ast._expr.accept(this);
if (knownType(type))
ast._type = new FieldType(ast._loc, ast._field, type);
else
ErrorPrinter.print(ast._loc, "Could not resolve type for field '"
+ ast._field._id + "'");
return ast._type;
}
Object visit(BoolLit ast) {
ast._type = new PrimitiveType(ast._loc, PrimitiveType.BOOL);
return ast._type;
}
Object visit(IntLit ast) {
ast._type = new PrimitiveType(ast._loc, PrimitiveType.INT);
return ast._type;
}
Object visit(NullLit ast) {
ast._type = NULL_TYPE;
return ast._type;
}
Object visit(StringLit ast) {
ast._type = new PrimitiveType(ast._loc, PrimitiveType.STRING);
return ast._type;
}
}