684dfc8d630c11d9dc0e08f3c1b945015f359b1b
[repair.git] / Repair / RepairCompiler / MCC / IR / Updates.java
1 package MCC.IR;
2
3 class Updates {
4     static public final int EXPR=0;
5     static public final int POSITION=1;
6     static public final int ABSTRACT=2;
7     int type=-1;
8     int rightposition;
9     Expr rightexpr;
10     Expr leftexpr;
11     Opcode opcode;
12     boolean negate=false;
13
14     public String toString() {
15         if (type==EXPR)
16             return leftexpr.name()+opcode.toString()+rightexpr.name();
17         else if (type==POSITION)
18             return leftexpr.name()+opcode.toString()+"Position("+String.valueOf(rightposition)+")";
19         else if (type==ABSTRACT) {
20             if (negate) return "!"+leftexpr.name();
21             else return leftexpr.name();
22         } else throw new Error("Unrecognized type");
23     }
24
25     public Updates(Expr lexpr, Expr rexpr, Opcode op, boolean negate) {
26         if (!lexpr.isValue())
27             System.out.println("Building invalid update");
28         leftexpr=lexpr;
29         type=Updates.EXPR;
30
31         op=Opcode.translateOpcode(negate,op);
32
33         opcode=op;
34         rightexpr=rexpr;
35     }
36
37     boolean isGlobal() {
38         if (leftexpr instanceof VarExpr)
39             return true;
40         else return false;
41     }
42
43     VarDescriptor getVar() {
44         if (isGlobal()) {
45             return ((VarExpr)leftexpr).getVar();
46         } else if (isField()) {
47             Expr e=leftexpr;
48             do {
49                 for(;e instanceof DotExpr;e=((DotExpr)e).getExpr()) ;
50                 if (e instanceof VarExpr)
51                     break;
52                 if (e instanceof CastExpr)
53                     e=((CastExpr)e).getExpr();
54                 else throw new Error("Unrecognized Expr:"+e.name());
55             } while(true);
56             return ((VarExpr)e).getVar();
57         } else {
58             System.out.println(toString());
59             throw new Error("Unrecognized Update");
60         }
61     }
62
63     Descriptor getDescriptor() {
64         if (isGlobal()) {
65             return ((VarExpr)leftexpr).getVar();
66         } else if (isField()) {
67             return ((DotExpr)leftexpr).getField();
68         } else {
69             System.out.println(toString());
70             throw new Error("Unrecognized Update");
71         }
72     }
73
74     boolean isField() {
75         if (leftexpr instanceof DotExpr) {
76             assert ((DotExpr)leftexpr).getIndex()==null;
77             return true;
78         } else
79             return false;
80     }
81     
82     boolean isExpr() {
83         return type==Updates.EXPR;
84     }
85
86     
87     Opcode getOpcode() {
88         return opcode;
89     }
90
91     public Updates(Expr lexpr, Expr rexpr) {
92         if (!lexpr.isValue())
93             System.out.println("Building invalid update");
94         leftexpr=lexpr;
95         rightexpr=rexpr;
96         type=Updates.EXPR;
97         opcode=Opcode.EQ;
98     }
99
100     public Updates(Expr lexpr, int rpos) {
101         if (!lexpr.isValue())
102             System.out.println("Building invalid update");
103         leftexpr=lexpr;
104         rightposition=rpos;
105         type=Updates.POSITION;
106         opcode=Opcode.EQ;
107     }
108
109     boolean isAbstract() {
110         return type==Updates.ABSTRACT;
111     }
112
113     public Updates(Expr lexpr,boolean negates) {
114         leftexpr=lexpr;
115         type=Updates.ABSTRACT;
116         negate=negates;
117         opcode=null;
118     }
119
120     public int getType() {
121         return type;
122     }
123     public Expr getLeftExpr() {
124         return leftexpr;
125     }
126     public int getRightPos() {
127         assert type==Updates.POSITION;
128         return rightposition;
129     }
130     public Expr getRightExpr() {
131         assert type==Updates.EXPR;
132         return rightexpr;
133     }
134 }