typesafe
[repair.git] / Repair / RepairCompiler / MCC / IR / OpExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class OpExpr extends Expr {
6
7     Expr left;
8     Expr right;
9     Opcode opcode;
10
11     public OpExpr(Opcode opcode, Expr left, Expr right) {
12         this.opcode = opcode;
13         this.left = left;
14         this.right = right;
15
16         assert (right == null && opcode == Opcode.NOT) || (right != null);
17     }
18
19     public Set getRequiredDescriptors() {
20         Set v = left.getRequiredDescriptors();
21      
22         if (right != null) {
23             v.addAll(right.getRequiredDescriptors());
24         }
25
26         return v;
27     }   
28
29     public void generate(CodeWriter writer, VarDescriptor dest) {
30         VarDescriptor ld = VarDescriptor.makeNew("leftop");
31         left.generate(writer, ld);        
32         VarDescriptor rd = null;
33
34         if (right != null) {
35             rd = VarDescriptor.makeNew("rightop");
36             right.generate(writer, rd);
37         }
38
39         String code;
40         if (opcode != Opcode.NOT) { /* two operands */
41             assert rd != null;
42             writer.outputline("int " + dest.getSafeSymbol() + " = " + 
43                               ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
44         } else {
45             writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
46         }
47     }
48
49     public void prettyPrint(PrettyPrinter pp) {
50         pp.output("(");
51         if (opcode == Opcode.NOT) {
52             left.prettyPrint(pp);
53         } else {           
54             left.prettyPrint(pp);
55             pp.output(" " + opcode.toString() + " ");
56             assert right != null;
57             right.prettyPrint(pp);
58         }
59         pp.output(")");
60     }
61
62     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
63         TypeDescriptor lt = left.typecheck(sa);
64         TypeDescriptor rt = right == null ? null : right.typecheck(sa);
65
66         if (lt == null) {
67             return null;
68         } else if (right != null && rt == null) {
69             return null;
70         }
71
72         boolean ok = true;
73
74         if (lt != ReservedTypeDescriptor.INT) {
75             sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
76             ok = false;
77         }
78
79         if (right != null) {
80             if (rt != ReservedTypeDescriptor.INT) {
81                 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
82                 ok = false;
83             }
84         }
85
86         if (!ok) {
87             return null;
88         }
89
90         this.td = ReservedTypeDescriptor.INT;
91         return this.td;
92     }
93
94 }
95
96
97
98
99