001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2003 Jerome Miecznikowski
003: * Copyright (C) 2004-2005 Nomair A. Naeem
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
018: * Boston, MA 02111-1307, USA.
019: */
020:
021: package soot.dava.internal.AST;
022:
023: import soot.*;
024: import java.util.*;
025: import soot.jimple.*;
026: import soot.dava.internal.SET.*;
027: import soot.dava.toolkits.base.AST.analysis.*;
028:
029: public class ASTIfElseNode extends ASTControlFlowNode {
030: private List<Object> ifBody, elseBody;
031:
032: public ASTIfElseNode(SETNodeLabel label, ConditionExpr condition,
033: List<Object> ifBody, List<Object> elseBody) {
034: super (label, condition);
035: this .ifBody = ifBody;
036: this .elseBody = elseBody;
037:
038: subBodies.add(ifBody);
039: subBodies.add(elseBody);
040: }
041:
042: /*
043: Nomair A. Naeem 17-FEB-05
044: Needed because of change of grammar of condition being stored as a ASTCondition rather
045: than the ConditionExpr which was the case before
046: */
047: public ASTIfElseNode(SETNodeLabel label, ASTCondition condition,
048: List<Object> ifBody, List<Object> elseBody) {
049: super (label, condition);
050: this .ifBody = ifBody;
051: this .elseBody = elseBody;
052:
053: subBodies.add(ifBody);
054: subBodies.add(elseBody);
055: }
056:
057: /*
058: Nomair A. Naeem 19-FEB-2005
059: Added to support aggregation of conditions
060: */
061: public void replace(SETNodeLabel newLabel, ASTCondition newCond,
062: List<Object> newBody, List<Object> bodyTwo) {
063: this .ifBody = newBody;
064: this .elseBody = bodyTwo;
065: subBodies = new ArrayList<Object>();
066: subBodies.add(newBody);
067: subBodies.add(bodyTwo);
068: set_Condition(newCond);
069: set_Label(newLabel);
070:
071: }
072:
073: /*
074: Nomair A. Naeem 21-FEB-2005
075: Added to support UselessLabelBlockRemover
076: */
077: public void replaceBody(List<Object> ifBody, List<Object> elseBody) {
078: this .ifBody = ifBody;
079: this .elseBody = elseBody;
080:
081: subBodies = new ArrayList<Object>();
082: subBodies.add(ifBody);
083: subBodies.add(elseBody);
084: }
085:
086: /*
087: Nomair A. Naeem 21-FEB-2005
088: Added to support OrAggregatorTwo
089: */
090: public void replaceElseBody(List<Object> elseBody) {
091: this .elseBody = elseBody;
092:
093: subBodies = new ArrayList<Object>();
094: subBodies.add(ifBody);
095: subBodies.add(elseBody);
096: }
097:
098: /*
099: Nomair A. Naeem 21-FEB-05
100: Used by OrAggregatorTwo
101: */
102: public List<Object> getIfBody() {
103: return ifBody;
104: }
105:
106: public List<Object> getElseBody() {
107: return elseBody;
108: }
109:
110: public Object clone() {
111: return new ASTIfElseNode(get_Label(), get_Condition(), ifBody,
112: elseBody);
113: }
114:
115: public void toString(UnitPrinter up) {
116: label_toString(up);
117:
118: up.literal("if");
119: up.literal(" ");
120: up.literal("(");
121: condition.toString(up);
122: up.literal(")");
123: up.newline();
124:
125: up.literal("{");
126: up.newline();
127:
128: up.incIndent();
129: body_toString(up, ifBody);
130: up.decIndent();
131:
132: up.literal("}");
133: up.newline();
134:
135: up.literal("else");
136: up.newline();
137:
138: up.literal("{");
139: up.newline();
140:
141: up.incIndent();
142: body_toString(up, elseBody);
143: up.decIndent();
144:
145: up.literal("}");
146: up.newline();
147: }
148:
149: public String toString() {
150: StringBuffer b = new StringBuffer();
151:
152: b.append(label_toString());
153:
154: b.append("if (");
155: b.append(get_Condition().toString());
156: b.append(")");
157: b.append(NEWLINE);
158:
159: b.append("{");
160: b.append(NEWLINE);
161:
162: b.append(body_toString(ifBody));
163:
164: b.append("}");
165: b.append(NEWLINE);
166:
167: b.append("else");
168: b.append(NEWLINE);
169:
170: b.append("{");
171: b.append(NEWLINE);
172:
173: b.append(body_toString(elseBody));
174:
175: b.append("}");
176: b.append(NEWLINE);
177:
178: return b.toString();
179: }
180:
181: /*
182: Nomair A. Naeem, 7-FEB-05
183: Part of Visitor Design Implementation for AST
184: See: soot.dava.toolkits.base.AST.analysis For details
185: */
186: public void apply(Analysis a) {
187: a.caseASTIfElseNode(this);
188: }
189:
190: }
|