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