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