dc9c543c329412865f8a22d675aea5920d6d06ec
[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 abstract boolean equals(Map remap, Expr e) {
16         if (e==null)
17             return false;
18         else if (!e instanceof CastExpr)
19             return false;
20         else return ((this.type==((CastExpr)e).type)&&equals(remap,expr,((CastExpr)e).expr));
21     }
22
23     public Set getRequiredDescriptors() {
24         return expr.getRequiredDescriptors();
25     }
26
27     public void generate(CodeWriter writer, VarDescriptor dest) {
28         VarDescriptor vd = VarDescriptor.makeNew("expr");
29         expr.generate(writer, vd);
30         writer.outputline("int " + dest.getSafeSymbol() + " = (int) " + vd.getSafeSymbol() + ";");
31     }
32
33     public void prettyPrint(PrettyPrinter pp) {
34         pp.output("cast(" + type.getSafeSymbol() + ", ");
35         expr.prettyPrint(pp);
36         pp.output(")");
37     }
38
39     public TypeDescriptor getType() {
40         return type;
41     }
42
43     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
44         TypeDescriptor td = expr.typecheck(sa);
45
46         if (td == null) {
47             return null;
48         }
49
50         if (!type.isSubtypeOf(td)) {
51             sa.getErrorReporter().report(null, "Expression type '" + td.getSymbol() + "' is not a parent of the cast type '" + type.getSymbol() + "'");
52             return null;
53         }
54
55         this.td = type;
56         return type;
57     }
58
59 }
60
61
62
63
64
65
66
67