-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeCheck.java
869 lines (813 loc) · 23 KB
/
TypeCheck.java
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
package decaf.typecheck;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import decaf.Driver;
import decaf.Location;
import decaf.error.BadArgCountError;
import decaf.error.BadArgTypeError;
import decaf.error.BadArrElementError;
import decaf.error.BadArrIndexError;
import decaf.error.BadArrOperArgError;
import decaf.error.BadDefError;
import decaf.error.BadForeachTypeError;
import decaf.error.BadLengthArgError;
import decaf.error.BadLengthError;
import decaf.error.BadNewArrayLength;
import decaf.error.BadPrintArgError;
import decaf.error.BadReturnTypeError;
import decaf.error.BadScopyArgError;
import decaf.error.BadScopySrcError;
import decaf.error.BadTestExpr;
import decaf.error.BreakOutOfLoopError;
import decaf.error.ClassNotFoundError;
import decaf.error.DecafError;
import decaf.error.FieldNotAccessError;
import decaf.error.FieldNotFoundError;
import decaf.error.IncompatBinOpError;
import decaf.error.IncompatUnOpError;
import decaf.error.NotArrayError;
import decaf.error.NotClassError;
import decaf.error.NotClassFieldError;
import decaf.error.NotClassMethodError;
import decaf.error.RefNonStaticError;
import decaf.error.SubNotIntError;
import decaf.error.ThisInStaticFuncError;
import decaf.error.UndeclVarError;
import decaf.frontend.Parser;
import decaf.scope.ClassScope;
import decaf.scope.FormalScope;
import decaf.scope.Scope;
import decaf.scope.Scope.Kind;
import decaf.scope.ScopeStack;
import decaf.symbol.Class;
import decaf.symbol.Function;
import decaf.symbol.Symbol;
import decaf.symbol.Variable;
import decaf.tree.Tree;
import decaf.type.ArrayType;
import decaf.type.BaseType;
import decaf.type.ClassType;
import decaf.type.Type;
public class TypeCheck extends Tree.Visitor {
private ScopeStack table;
private Stack<Tree> breaks;
private Function currentFunction;
public TypeCheck(ScopeStack table) {
this.table = table;
breaks = new Stack<Tree>();
}
public static void checkType(Tree.TopLevel tree) {
new TypeCheck(Driver.getDriver().getTable()).visitTopLevel(tree);
}
@Override
/**
* Edit by weiyx15 to support scopy type check
*/
public void visitScopy(Tree.Scopy scopy)
{
checkScopy(scopy.left, scopy.expr, scopy.loc);
}
/**
* Edit by weiyx15 to support scopy type check
*/
private void checkScopy(String left, Tree.Expr expr, Location loc)
{
expr.accept(this);
Symbol ident = table.lookup(left, true);
if (ident == null)
{
issueError(new UndeclVarError(loc, left));
return;
}
if (!ident.getType().isClassType())
{
issueError(new BadScopyArgError(loc, "dst", ident.getType().toString()));
if (!expr.type.isClassType())
{
issueError(new BadScopyArgError(loc, "src", expr.type.toString()));
}
return;
}
if (!ident.getType().compatible(expr.type))
{
issueError(new BadScopySrcError(loc, ident.getType().toString(), expr.type.toString()));
}
return;
}
@Override
/**
* Edit by weiyx15 to support guard type check
*/
public void visitGuard(Tree.Guard guard)
{
guard.expr.accept(this);
if (!guard.expr.type.equal(BaseType.BOOL))
{
issueError(new BadTestExpr(guard.expr.loc));
}
}
@Override
/**
* Edit by weiyx15
* When encountering guarded stmt, go through guard type check
*/
public void visitGuarded(Tree.Guarded guarded)
{
for (Tree.Guard guard : guarded.guardList)
{
guard.accept(this);
}
}
@Override
/**
* Edit by weiyx15 to support dynamic index type check
*/
public void visitDynamicIndex(Tree.DynamicIndex di)
{
di.parent.accept(this);
di.ind.accept(this);
di.def.accept(this);
if (!di.parent.type.isArrayType())
{
issueError(new BadArrOperArgError(di.parent.loc));
if (!di.def.type.equal(BaseType.UNKNOWN) && !di.def.type.equal(BaseType.VOID))
{
di.type = di.def.type;
}
else
{
di.type = BaseType.ERROR;
}
}
else
{
if (!di.ind.type.equal(BaseType.INT))
{
issueError(new BadArrIndexError(di.ind.loc));
di.type = BaseType.ERROR;
}
ArrayType parenttype = (ArrayType) di.parent.type;
if (parenttype.getElementType().equal(di.def.type))
{
di.type = parenttype.getElementType();
}
else
{
issueError(new BadDefError
(di.loc, parenttype.getElementType().toString(), di.def.type.toString()));
di.type = parenttype.getElementType();
}
}
}
@Override
/**
* Edit by weiyx15 to support foreachstmt type check
*/
public void visitForeachStmt(Tree.ForeachStmt foreachstmt)
{
table.open(foreachstmt.block.associatedScope);
foreachstmt.bVar.accept(this);
foreachstmt.inExpr.accept(this);
foreachstmt.whileExpr.accept(this);
if (foreachstmt.bVar.symbol.getType().equal(BaseType.UNKNOWN))
{
if (foreachstmt.inExpr.type.isArrayType())
{
ArrayType intype = (ArrayType) foreachstmt.inExpr.type;
foreachstmt.bVar.symbol.setType(intype.getElementType());
}
else
{
if (!foreachstmt.inExpr.type.equal(BaseType.ERROR))
{
issueError(new BadArrOperArgError(foreachstmt.inExpr.loc));
}
foreachstmt.bVar.symbol.setType(BaseType.ERROR);;
}
}
else
{
if (foreachstmt.inExpr.type.isArrayType())
{
ArrayType intype = (ArrayType) foreachstmt.inExpr.type;
if (!(intype.getElementType().compatible(foreachstmt.bVar.symbol.getType()) ||
foreachstmt.bVar.symbol.getType().equal(intype.getElementType())))
{
issueError(new BadForeachTypeError(foreachstmt.loc,
foreachstmt.bVar.symbol.getType().toString(),
intype.getElementType().toString()));
}
}
else if (!foreachstmt.inExpr.type.equal(BaseType.ERROR))
{
issueError(new BadArrOperArgError(foreachstmt.inExpr.loc));
}
}
if (foreachstmt.whileExpr != null && !foreachstmt.whileExpr.type.equal(BaseType.BOOL))
{
issueError(new BadTestExpr(foreachstmt.whileExpr.loc));
}
for (Tree s : foreachstmt.block.block)
{
s.accept(this);
}
table.close();
}
@Override
public void visitBinary(Tree.Binary expr) {
expr.type = checkBinaryOp(expr.left, expr.right, expr.tag, expr.loc);
}
@Override
public void visitUnary(Tree.Unary expr) {
expr.expr.accept(this);
if(expr.tag == Tree.NEG){
if (expr.expr.type.equal(BaseType.ERROR)
|| expr.expr.type.equal(BaseType.INT)) {
expr.type = expr.expr.type;
} else {
issueError(new IncompatUnOpError(expr.getLocation(), "-",
expr.expr.type.toString()));
expr.type = BaseType.ERROR;
}
}
else{
if (!(expr.expr.type.equal(BaseType.BOOL) || expr.expr.type
.equal(BaseType.ERROR))) {
issueError(new IncompatUnOpError(expr.getLocation(), "!",
expr.expr.type.toString()));
}
expr.type = BaseType.BOOL;
}
}
@Override
public void visitLiteral(Tree.Literal literal) {
switch (literal.typeTag) {
case Tree.INT:
literal.type = BaseType.INT;
break;
case Tree.BOOL:
literal.type = BaseType.BOOL;
break;
case Tree.STRING:
literal.type = BaseType.STRING;
break;
}
}
@Override
public void visitNull(Tree.Null nullExpr) {
nullExpr.type = BaseType.NULL;
}
@Override
public void visitReadIntExpr(Tree.ReadIntExpr readIntExpr) {
readIntExpr.type = BaseType.INT;
}
@Override
public void visitReadLineExpr(Tree.ReadLineExpr readStringExpr) {
readStringExpr.type = BaseType.STRING;
}
@Override
public void visitIndexed(Tree.Indexed indexed) {
indexed.lvKind = Tree.LValue.Kind.ARRAY_ELEMENT;
indexed.array.accept(this);
if (!indexed.array.type.isArrayType()) {
issueError(new NotArrayError(indexed.array.getLocation()));
indexed.type = BaseType.ERROR;
} else {
indexed.type = ((ArrayType) indexed.array.type)
.getElementType();
}
indexed.index.accept(this);
if (!indexed.index.type.equal(BaseType.INT)) {
issueError(new SubNotIntError(indexed.getLocation()));
}
}
/**
* @param callExpr
* @param f
*/
private void checkCallExpr(Tree.CallExpr callExpr, Symbol f) {
Type receiverType = callExpr.receiver == null ? ((ClassScope) table
.lookForScope(Scope.Kind.CLASS)).getOwner().getType()
: callExpr.receiver.type;
if (f == null) {
issueError(new FieldNotFoundError(callExpr.getLocation(),
callExpr.method, receiverType.toString()));
callExpr.type = BaseType.ERROR;
} else if (!f.isFunction()) {
issueError(new NotClassMethodError(callExpr.getLocation(),
callExpr.method, receiverType.toString()));
callExpr.type = BaseType.ERROR;
}
else {
Function func = (Function) f;
callExpr.symbol = func;
callExpr.type = func.getReturnType();
if (callExpr.receiver == null && currentFunction.isStatik()
&& !func.isStatik()) {
issueError(new RefNonStaticError(callExpr.getLocation(),
currentFunction.getName(), func.getName()));
}
if (!func.isStatik() && callExpr.receiver != null
&& callExpr.receiver.isClass) {
issueError(new NotClassFieldError(callExpr.getLocation(),
callExpr.method, callExpr.receiver.type.toString()));
}
if (func.isStatik()) {
callExpr.receiver = null;
} else {
if (callExpr.receiver == null && !currentFunction.isStatik()) {
callExpr.receiver = new Tree.ThisExpr(callExpr.getLocation());
callExpr.receiver.accept(this);
}
}
for (Tree.Expr e : callExpr.actuals) {
e.accept(this);
}
List<Type> argList = func.getType().getArgList();
int argCount = func.isStatik() ? callExpr.actuals.size()
: callExpr.actuals.size() + 1;
if (argList.size() != argCount) {
issueError(new BadArgCountError(callExpr.getLocation(),
callExpr.method, func.isStatik() ? argList.size()
: argList.size() - 1, callExpr.actuals.size()));
} else {
Iterator<Type> iter1 = argList.iterator();
if (!func.isStatik()) {
iter1.next();
}
Iterator<Tree.Expr> iter2 = callExpr.actuals.iterator();
for (int i = 1; iter1.hasNext(); i++) {
Type t1 = iter1.next();
Tree.Expr e = iter2.next();
Type t2 = e.type;
if (!t2.equal(BaseType.ERROR) && !t2.compatible(t1)) {
issueError(new BadArgTypeError(e.getLocation(), i,
t2.toString(), t1.toString()));
}
}
}
}
}
@Override
public void visitCallExpr(Tree.CallExpr callExpr) {
if (callExpr.receiver == null) {
ClassScope cs = (ClassScope) table.lookForScope(Kind.CLASS);
checkCallExpr(callExpr, cs.lookupVisible(callExpr.method));
return;
}
callExpr.receiver.usedForRef = true;
callExpr.receiver.accept(this);
if (callExpr.receiver.type.equal(BaseType.ERROR)) {
callExpr.type = BaseType.ERROR;
return;
}
if (callExpr.method.equals("length")) {
if (callExpr.receiver.type.isArrayType()) {
if (callExpr.actuals.size() > 0) {
issueError(new BadLengthArgError(callExpr.getLocation(),
callExpr.actuals.size()));
}
callExpr.type = BaseType.INT;
callExpr.isArrayLength = true;
return;
} else if (!callExpr.receiver.type.isClassType()) {
issueError(new BadLengthError(callExpr.getLocation()));
callExpr.type = BaseType.ERROR;
return;
}
}
if (!callExpr.receiver.type.isClassType()) {
issueError(new NotClassFieldError(callExpr.getLocation(),
callExpr.method, callExpr.receiver.type.toString()));
callExpr.type = BaseType.ERROR;
return;
}
ClassScope cs = ((ClassType) callExpr.receiver.type)
.getClassScope();
checkCallExpr(callExpr, cs.lookupVisible(callExpr.method));
}
@Override
public void visitExec(Tree.Exec exec){
exec.expr.accept(this);
}
@Override
public void visitNewArray(Tree.NewArray newArrayExpr) {
newArrayExpr.elementType.accept(this);
if (newArrayExpr.elementType.type.equal(BaseType.ERROR)) {
newArrayExpr.type = BaseType.ERROR;
} else if (newArrayExpr.elementType.type.equal(BaseType.VOID)) {
issueError(new BadArrElementError(newArrayExpr.elementType
.getLocation()));
newArrayExpr.type = BaseType.ERROR;
} else {
newArrayExpr.type = new ArrayType(
newArrayExpr.elementType.type);
}
newArrayExpr.length.accept(this);
if (!newArrayExpr.length.type.equal(BaseType.ERROR)
&& !newArrayExpr.length.type.equal(BaseType.INT)) {
issueError(new BadNewArrayLength(newArrayExpr.length.getLocation()));
}
}
@Override
public void visitNewClass(Tree.NewClass newClass) {
Class c = table.lookupClass(newClass.className);
newClass.symbol = c;
if (c == null) {
issueError(new ClassNotFoundError(newClass.getLocation(),
newClass.className));
newClass.type = BaseType.ERROR;
} else {
newClass.type = c.getType();
}
}
@Override
public void visitThisExpr(Tree.ThisExpr thisExpr) {
if (currentFunction.isStatik()) {
issueError(new ThisInStaticFuncError(thisExpr.getLocation()));
thisExpr.type = BaseType.ERROR;
} else {
thisExpr.type = ((ClassScope) table.lookForScope(Scope.Kind.CLASS))
.getOwner().getType();
}
}
@Override
public void visitTypeTest(Tree.TypeTest instanceofExpr) {
instanceofExpr.instance.accept(this);
if (!instanceofExpr.instance.type.isClassType()) {
issueError(new NotClassError(instanceofExpr.instance.type
.toString(), instanceofExpr.getLocation()));
}
Class c = table.lookupClass(instanceofExpr.className);
instanceofExpr.symbol = c;
instanceofExpr.type = BaseType.BOOL;
if (c == null) {
issueError(new ClassNotFoundError(instanceofExpr.getLocation(),
instanceofExpr.className));
}
}
@Override
public void visitTypeCast(Tree.TypeCast cast) {
cast.expr.accept(this);
if (!cast.expr.type.isClassType()) {
issueError(new NotClassError(cast.expr.type.toString(),
cast.getLocation()));
}
Class c = table.lookupClass(cast.className);
cast.symbol = c;
if (c == null) {
issueError(new ClassNotFoundError(cast.getLocation(),
cast.className));
cast.type = BaseType.ERROR;
} else {
cast.type = c.getType();
}
}
@Override
/**
* Edit by weiyx15 to first report Incompat*OpError rather than UndeclVarError
* when encountered "var Tree.Ident" at the right side of Tree.Assign
*/
public void visitIdent(Tree.Ident ident) {
if (ident.owner == null) {
Symbol v = table.lookupBeforeLocation(ident.name, ident
.getLocation());
if (ident.is_var)
{
ident.type = BaseType.UNKNOWN;
}
else if (v == null) {
issueError(new UndeclVarError(ident.getLocation(), ident.name));
ident.type = BaseType.ERROR;
}
else if (v.isVariable()) {
Variable var = (Variable) v;
ident.type = var.getType();
ident.symbol = var;
if (var.isLocalVar()) {
ident.lvKind = Tree.LValue.Kind.LOCAL_VAR;
} else if (var.isParam()) {
ident.lvKind = Tree.LValue.Kind.PARAM_VAR;
} else {
if (currentFunction.isStatik()) {
issueError(new RefNonStaticError(ident.getLocation(),
currentFunction.getName(), ident.name));
} else {
ident.owner = new Tree.ThisExpr(ident.getLocation());
ident.owner.accept(this);
}
ident.lvKind = Tree.LValue.Kind.MEMBER_VAR;
}
} else {
ident.type = v.getType();
if (v.isClass()) {
if (ident.usedForRef) {
ident.isClass = true;
} else {
issueError(new UndeclVarError(ident.getLocation(),
ident.name));
ident.type = BaseType.ERROR;
}
}
}
} else {
ident.owner.usedForRef = true;
ident.owner.accept(this);
if (!ident.owner.type.equal(BaseType.ERROR)) {
if (ident.owner.isClass || !ident.owner.type.isClassType()) {
issueError(new NotClassFieldError(ident.getLocation(),
ident.name, ident.owner.type.toString()));
ident.type = BaseType.ERROR;
} else {
ClassScope cs = ((ClassType) ident.owner.type)
.getClassScope();
Symbol v = cs.lookupVisible(ident.name);
if (v == null) {
issueError(new FieldNotFoundError(ident.getLocation(),
ident.name, ident.owner.type.toString()));
ident.type = BaseType.ERROR;
} else if (v.isVariable()) {
ClassType thisType = ((ClassScope) table
.lookForScope(Scope.Kind.CLASS)).getOwner()
.getType();
ident.type = v.getType();
if (!thisType.compatible(ident.owner.type)) {
issueError(new FieldNotAccessError(ident
.getLocation(), ident.name,
ident.owner.type.toString()));
} else {
ident.symbol = (Variable) v;
ident.lvKind = Tree.LValue.Kind.MEMBER_VAR;
}
} else
{
ident.type = v.getType();
}
}
} else {
ident.type = BaseType.ERROR;
}
}
}
@Override
public void visitClassDef(Tree.ClassDef classDef) {
table.open(classDef.symbol.getAssociatedScope());
for (Tree f : classDef.fields) {
f.accept(this);
}
table.close();
}
@Override
public void visitMethodDef(Tree.MethodDef func) {
this.currentFunction = func.symbol;
table.open(func.symbol.getAssociatedScope());
func.body.accept(this);
table.close();
}
@Override
public void visitTopLevel(Tree.TopLevel program) {
table.open(program.globalScope);
for (Tree.ClassDef cd : program.classes) {
cd.accept(this);
}
table.close();
}
@Override
public void visitBlock(Tree.Block block) {
table.open(block.associatedScope);
for (Tree s : block.block) {
s.accept(this);
}
table.close();
}
@Override
/**
* Edit by weiyx15 to support auto type induction
*/
public void visitAssign(Tree.Assign assign) {
assign.left.accept(this);
assign.expr.accept(this);
if (assign.left.type.equal(BaseType.UNKNOWN))
{
if (assign.left instanceof Tree.Ident && ((Tree.Ident) assign.left).is_var)
{
assign.left.type = assign.expr.type;
((Tree.Ident) assign.left).symbol.setType(assign.expr.type);
}
}
if (!assign.left.type.equal(BaseType.ERROR)
&& (assign.left.type.isFuncType() || !assign.expr.type
.compatible(assign.left.type))) {
issueError(new IncompatBinOpError(assign.getLocation(),
assign.left.type.toString(), "=", assign.expr.type
.toString()));
}
}
@Override
public void visitBreak(Tree.Break breakStmt) {
if (breaks.empty()) {
issueError(new BreakOutOfLoopError(breakStmt.getLocation()));
}
}
@Override
public void visitForLoop(Tree.ForLoop forLoop) {
if (forLoop.init != null) {
forLoop.init.accept(this);
}
checkTestExpr(forLoop.condition);
if (forLoop.update != null) {
forLoop.update.accept(this);
}
breaks.add(forLoop);
if (forLoop.loopBody != null) {
forLoop.loopBody.accept(this);
}
breaks.pop();
}
@Override
public void visitIf(Tree.If ifStmt) {
checkTestExpr(ifStmt.condition);
if (ifStmt.trueBranch != null) {
ifStmt.trueBranch.accept(this);
}
if (ifStmt.falseBranch != null) {
ifStmt.falseBranch.accept(this);
}
}
@Override
public void visitPrint(Tree.Print printStmt) {
int i = 0;
for (Tree.Expr e : printStmt.exprs) {
e.accept(this);
i++;
if (!e.type.equal(BaseType.ERROR) && !e.type.equal(BaseType.BOOL)
&& !e.type.equal(BaseType.INT)
&& !e.type.equal(BaseType.STRING)) {
issueError(new BadPrintArgError(e.getLocation(), Integer
.toString(i), e.type.toString()));
}
}
}
@Override
public void visitReturn(Tree.Return returnStmt) {
Type returnType = ((FormalScope) table
.lookForScope(Scope.Kind.FORMAL)).getOwner().getReturnType();
if (returnStmt.expr != null) {
returnStmt.expr.accept(this);
}
if (returnType.equal(BaseType.VOID)) {
if (returnStmt.expr != null) {
issueError(new BadReturnTypeError(returnStmt.getLocation(),
returnType.toString(), returnStmt.expr.type.toString()));
}
} else if (returnStmt.expr == null) {
issueError(new BadReturnTypeError(returnStmt.getLocation(),
returnType.toString(), "void"));
} else if (!returnStmt.expr.type.equal(BaseType.ERROR)
&& !returnStmt.expr.type.compatible(returnType)) {
issueError(new BadReturnTypeError(returnStmt.getLocation(),
returnType.toString(), returnStmt.expr.type.toString()));
}
}
@Override
public void visitWhileLoop(Tree.WhileLoop whileLoop) {
checkTestExpr(whileLoop.condition);
breaks.add(whileLoop);
if (whileLoop.loopBody != null) {
whileLoop.loopBody.accept(this);
}
breaks.pop();
}
// visiting types
@Override
public void visitTypeIdent(Tree.TypeIdent type) {
switch (type.typeTag) {
case Tree.VOID:
type.type = BaseType.VOID;
break;
case Tree.INT:
type.type = BaseType.INT;
break;
case Tree.BOOL:
type.type = BaseType.BOOL;
break;
default:
type.type = BaseType.STRING;
}
}
@Override
public void visitTypeClass(Tree.TypeClass typeClass) {
Class c = table.lookupClass(typeClass.name);
if (c == null) {
issueError(new ClassNotFoundError(typeClass.getLocation(),
typeClass.name));
typeClass.type = BaseType.ERROR;
} else {
typeClass.type = c.getType();
}
}
@Override
public void visitTypeArray(Tree.TypeArray typeArray) {
typeArray.elementType.accept(this);
if (typeArray.elementType.type.equal(BaseType.ERROR)) {
typeArray.type = BaseType.ERROR;
} else if (typeArray.elementType.type.equal(BaseType.VOID)) {
issueError(new BadArrElementError(typeArray.getLocation()));
typeArray.type = BaseType.ERROR;
} else {
typeArray.type = new ArrayType(typeArray.elementType.type);
}
}
private void issueError(DecafError error) {
Driver.getDriver().issueError(error);
}
/**
* Edit by weiyx15 to support %% operation type check
* @param left
* @param right
* @param op
* @param location
* @return
*/
private Type checkBinaryOp(Tree.Expr left, Tree.Expr right, int op, Location location) {
left.accept(this);
right.accept(this);
if (left.type.equal(BaseType.ERROR) || right.type.equal(BaseType.ERROR)) {
switch (op) {
case Tree.PLUS:
case Tree.MINUS:
case Tree.MUL:
case Tree.DIV:
return left.type;
case Tree.MOD:
return BaseType.INT;
default:
return BaseType.BOOL;
}
}
boolean compatible = false;
Type returnType = BaseType.ERROR;
switch (op) {
case Tree.PLUS:
case Tree.MINUS:
case Tree.MUL:
case Tree.DIV:
compatible = left.type.equals(BaseType.INT)
&& left.type.equal(right.type);
returnType = left.type;
break;
case Tree.GT:
case Tree.GE:
case Tree.LT:
case Tree.LE:
compatible = left.type.equal(BaseType.INT)
&& left.type.equal(right.type);
returnType = BaseType.BOOL;
break;
case Tree.MOD:
compatible = left.type.equal(BaseType.INT)
&& right.type.equal(BaseType.INT);
returnType = BaseType.INT;
break;
case Tree.EQ:
case Tree.NE:
compatible = left.type.compatible(right.type)
|| right.type.compatible(left.type);
returnType = BaseType.BOOL;
break;
case Tree.AND:
case Tree.OR:
compatible = left.type.equal(BaseType.BOOL)
&& right.type.equal(BaseType.BOOL);
returnType = BaseType.BOOL;
break;
case Tree.ARRAYCOPY:
if (left.type.equal(BaseType.UNKNOWN) || left.type.equal(BaseType.VOID))
{
issueError(new BadArrElementError(left.loc));
returnType = BaseType.ERROR;
compatible = true;
}
else
{
compatible = right.type.equal(BaseType.INT);
returnType = new ArrayType(left.type);
}
break;
default:
break;
}
if (!compatible) {
issueError(new IncompatBinOpError(location, left.type.toString(),
Parser.opStr(op), right.type.toString()));
}
return returnType;
}
private void checkTestExpr(Tree.Expr expr) {
expr.accept(this);
if (!expr.type.equal(BaseType.ERROR) && !expr.type.equal(BaseType.BOOL)) {
issueError(new BadTestExpr(expr.getLocation()));
}
}
}