Added support to printout data structure update nodes (bindings/updates)
[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 Set freeVars() {
11         return expr.freeVars();
12     }
13
14     public CastExpr(TypeDescriptor type, Expr expr) {
15         this.type = type;
16         this.expr = expr;
17     }
18
19     public String name() {
20         String str="";
21         str="(("+type.toString()+")"+expr.name()+")";
22         return str;
23     }
24
25     public boolean equals(Map remap, Expr e) {
26         if (e==null)
27             return false;
28         else if (!(e instanceof CastExpr))
29             return false;
30         else return ((this.type==((CastExpr)e).type)&&expr.equals(remap,((CastExpr)e).expr));
31     }
32
33     public boolean usesDescriptor(Descriptor d) {
34         return expr.usesDescriptor(d);
35     }
36
37     public Set getRequiredDescriptors() {
38         return expr.getRequiredDescriptors();
39     }
40
41     public void generate(CodeWriter writer, VarDescriptor dest) {
42         VarDescriptor vd = VarDescriptor.makeNew("expr");
43         expr.generate(writer, vd);
44         writer.outputline("int " + dest.getSafeSymbol() + " = (int) " + vd.getSafeSymbol() + ";");
45     }
46
47     public void prettyPrint(PrettyPrinter pp) {
48         pp.output("cast(" + type.getSafeSymbol() + ", ");
49         expr.prettyPrint(pp);
50         pp.output(")");
51     }
52
53     public TypeDescriptor getType() {
54         return type;
55     }
56
57     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
58         TypeDescriptor td = expr.typecheck(sa);
59
60         if (td == null) {
61             return null;
62         }
63
64         if (!type.isSubtypeOf(td)) {
65             sa.getErrorReporter().report(null, "Expression type '" + td.getSymbol() + "' is not a parent of the cast type '" + type.getSymbol() + "'");
66             return null;
67         }
68
69         this.td = type;
70         return type;
71     }
72
73 }
74
75
76
77
78
79
80
81