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