KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > expr > StoreInstruction


1 /* StoreInstruction Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: StoreInstruction.java,v 4.21.2.3 2002/05/28 17:34:06 hoenicke Exp $
18  */

19
20 package jode.expr;
21 import jode.type.Type;
22 import jode.GlobalOptions;
23 import jode.decompiler.TabbedPrintWriter;
24
25 public class StoreInstruction extends Operator
26     implements CombineableOperator {
27
28     boolean opAssign = false;
29
30     public StoreInstruction(LValueExpression lvalue) {
31         super(Type.tVoid, ASSIGN_OP);
32     initOperands(2);
33     setSubExpressions(0, (Operator) lvalue);
34     }
35
36     public LValueExpression getLValue() {
37     return (LValueExpression) subExpressions[0];
38     }
39
40     public void makeOpAssign(int operatorIndex) {
41     setOperatorIndex(operatorIndex);
42     if (subExpressions[1] instanceof NopOperator)
43         subExpressions[1].type = Type.tUnknown;
44     opAssign = true;
45     }
46
47     public boolean isOpAssign() {
48     return opAssign;
49     }
50
51     /**
52      * Makes a non void expression out of this store instruction.
53      */

54     public void makeNonVoid() {
55         if (type != Type.tVoid)
56             throw new jode.AssertError("already non void");
57     type = subExpressions[0].getType();
58     }
59
60     public boolean lvalueMatches(Operator loadop) {
61     return getLValue().matches(loadop);
62     }
63
64     public int getPriority() {
65         return 100;
66     }
67
68     public void updateSubTypes() {
69     if (!isVoid()) {
70         subExpressions[0].setType(type);
71         subExpressions[1].setType(Type.tSubType(type));
72     }
73     }
74
75     public void updateType() {
76
77     Type newType;
78
79     if (!opAssign) {
80         /* An opassign (+=, -=, etc.) doesn't merge rvalue type. */
81         Type lvalueType = subExpressions[0].getType();
82         Type rvalueType = subExpressions[1].getType();
83         subExpressions[0].setType(Type.tSuperType(rvalueType));
84         subExpressions[1].setType(Type.tSubType(lvalueType));
85     }
86
87     if (!isVoid())
88         updateParentType(subExpressions[0].getType());
89     }
90
91     public Expression simplify() {
92     if (subExpressions[1] instanceof ConstOperator) {
93             ConstOperator one = (ConstOperator) subExpressions[1];
94
95             if ((getOperatorIndex() == OPASSIGN_OP+ADD_OP ||
96                  getOperatorIndex() == OPASSIGN_OP+SUB_OP)
97         && one.isOne(subExpressions[0].getType())) {
98         
99                 int op = (getOperatorIndex() == OPASSIGN_OP+ADD_OP)
100                     ? INC_OP : DEC_OP;
101         
102                 return new PrePostFixOperator
103                     (getType(), op, getLValue(), isVoid()).simplify();
104             }
105         }
106     return super.simplify();
107     }
108
109     public boolean opEquals(Operator o) {
110     return o instanceof StoreInstruction
111         && o.operatorIndex == operatorIndex
112         && o.isVoid() == isVoid();
113     }
114
115     public void dumpExpression(TabbedPrintWriter writer)
116     throws java.io.IOException JavaDoc
117     {
118     writer.startOp(writer.NO_PAREN, 2);
119     subExpressions[0].dumpExpression(writer);
120     writer.endOp();
121     writer.breakOp();
122     writer.print(getOperatorString());
123     subExpressions[1].dumpExpression(writer, 100);
124     }
125 }
126
Popular Tags