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