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