Fix some of Dan's bugs (code generation for relation quantifiers misstyped), didn...
[repair.git] / Repair / RepairCompiler / MCC / IR / CastExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class CastExpr extends Expr {
6     
7     TypeDescriptor type;
8     Expr expr;
9
10     public CastExpr(TypeDescriptor type, Expr expr) {
11         this.type = type;
12         this.expr = expr;
13     }
14
15     public String name() {
16         String str="";
17         str="(("+type.toString()+")"+expr.name()+")";
18         return str;
19     }
20
21     public boolean equals(Map remap, Expr e) {
22         if (e==null)
23             return false;
24         else if (!(e instanceof CastExpr))
25             return false;
26         else return ((this.type==((CastExpr)e).type)&&expr.equals(remap,((CastExpr)e).expr));
27     }
28
29     public boolean usesDescriptor(Descriptor d) {
30         return expr.usesDescriptor(d);
31     }
32
33     public Set getRequiredDescriptors() {
34         return expr.getRequiredDescriptors();
35     }
36
37     public void generate(CodeWriter writer, VarDescriptor dest) {
38         VarDescriptor vd = VarDescriptor.makeNew("expr");
39         expr.generate(writer, vd);
40         writer.outputline("int " + dest.getSafeSymbol() + " = (int) " + vd.getSafeSymbol() + ";");
41     }
42
43     public void prettyPrint(PrettyPrinter pp) {
44         pp.output("cast(" + type.getSafeSymbol() + ", ");
45         expr.prettyPrint(pp);
46         pp.output(")");
47     }
48
49     public TypeDescriptor getType() {
50         return type;
51     }
52
53     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
54         TypeDescriptor td = expr.typecheck(sa);
55
56         if (td == null) {
57             return null;
58         }
59
60         if (!type.isSubtypeOf(td)) {
61             sa.getErrorReporter().report(null, "Expression type '" + td.getSymbol() + "' is not a parent of the cast type '" + type.getSymbol() + "'");
62             return null;
63         }
64
65         this.td = type;
66         return type;
67     }
68
69 }
70
71
72
73
74
75
76
77