1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
* Fernflower - The Analytical Java Decompiler
* http://www.reversed-java.com
*
* (C) 2008 - 2010, Stiver
*
* This software is NEITHER public domain NOR free software
* as per GNU License. See license.txt for more details.
*
* This software is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE.
*/
package de.fernflower.modules.decompiler.stats;
import java.util.ArrayList;
import java.util.List;
import de.fernflower.main.DecompilerContext;
import de.fernflower.modules.decompiler.DecHelper;
import de.fernflower.modules.decompiler.ExprProcessor;
import de.fernflower.modules.decompiler.StatEdge;
import de.fernflower.modules.decompiler.exps.Exprent;
import de.fernflower.modules.decompiler.exps.IfExprent;
import de.fernflower.util.InterpreterUtil;
public class IfStatement extends Statement {
public static int IFTYPE_IF = 0;
public static int IFTYPE_IFELSE = 1;
public int iftype;
// *****************************************************************************
// private fields
// *****************************************************************************
private Statement ifstat;
private Statement elsestat;
private StatEdge ifedge;
private StatEdge elseedge;
private boolean negated = false;
private boolean iffflag;
private List<Exprent> headexprent = new ArrayList<Exprent>(); // contains IfExprent
// *****************************************************************************
// constructors
// *****************************************************************************
private IfStatement() {
type = TYPE_IF;
headexprent.add(null);
}
private IfStatement(Statement head, int regedges, Statement postst) {
this();
first = head;
stats.addWithKey(head, head.id);
List<StatEdge> lstHeadSuccs = head.getSuccessorEdges(STATEDGE_DIRECT_ALL);
switch(regedges) {
case 0:
ifstat = null;
elsestat = null;
break;
case 1:
ifstat = null;
elsestat = null;
StatEdge edgeif = lstHeadSuccs.get(1);
if(edgeif.getType() != StatEdge.TYPE_REGULAR) {
post = lstHeadSuccs.get(0).getDestination();
} else {
post = edgeif.getDestination();
negated = true;
}
break;
case 2:
elsestat = lstHeadSuccs.get(0).getDestination();
ifstat = lstHeadSuccs.get(1).getDestination();
List<StatEdge> lstSucc = ifstat.getSuccessorEdges(StatEdge.TYPE_REGULAR);
List<StatEdge> lstSucc1 = elsestat.getSuccessorEdges(StatEdge.TYPE_REGULAR);
if(ifstat.getPredecessorEdges(StatEdge.TYPE_REGULAR).size()>1 || lstSucc.size()>1) {
post = ifstat;
} else if(elsestat.getPredecessorEdges(StatEdge.TYPE_REGULAR).size()>1 || lstSucc1.size()>1) {
post = elsestat;
} else {
if(lstSucc.size() == 0){
post = elsestat;
} else if(lstSucc1.size() == 0){
post = ifstat;
}
}
if(ifstat == post) {
if(elsestat != post) {
ifstat = elsestat;
negated = true;
} else {
ifstat = null;
}
elsestat = null;
} else if(elsestat == post) {
elsestat = null;
} else {
post = postst;
}
if(elsestat == null) {
regedges = 1; // if without else
}
}
ifedge = lstHeadSuccs.get(negated?0:1);
elseedge = (regedges == 2)?lstHeadSuccs.get(negated?1:0):null;
iftype = (regedges == 2)?IFTYPE_IFELSE:IFTYPE_IF;
if(iftype == IFTYPE_IF) {
if(regedges == 0) {
StatEdge edge = lstHeadSuccs.get(0);
head.removeSuccessor(edge);
edge.setSource(this);
this.addSuccessor(edge);
} else if(regedges == 1) {
StatEdge edge = lstHeadSuccs.get(negated?1:0);
head.removeSuccessor(edge);
}
}
if(ifstat != null) {
stats.addWithKey(ifstat, ifstat.id);
}
if(elsestat != null) {
stats.addWithKey(elsestat, elsestat.id);
}
if(post == head) {
post = this;
}
}
// *****************************************************************************
// public methods
// *****************************************************************************
public static Statement isHead(Statement head) {
if(head.type == TYPE_BASICBLOCK && head.getLastBasicType() == LASTBASICTYPE_IF) {
int regsize = head.getSuccessorEdges(StatEdge.TYPE_REGULAR).size();
Statement p = null;
boolean ok = (regsize < 2);
if(!ok) {
List<Statement> lst = new ArrayList<Statement>();
if(DecHelper.isChoiceStatement(head, lst)) {
p = lst.remove(0);
for(Statement st : lst) {
if(st.isMonitorEnter()) {
return null;
}
}
ok = DecHelper.checkStatementExceptions(lst);
}
}
if(ok) {
return new IfStatement(head, regsize, p);
}
}
return null;
}
public String toJava(int indent) {
String indstr = InterpreterUtil.getIndentString(indent);
StringBuffer buf = new StringBuffer();
String new_line_separator = DecompilerContext.getNewLineSeparator();
buf.append(ExprProcessor.listToJava(varDefinitions, indent));
buf.append(first.toJava(indent));
if(isLabeled()) {
buf.append(indstr+"label"+this.id+":" + new_line_separator);
}
buf.append(indstr+headexprent.get(0).toJava(indent)+" {" + new_line_separator);
if(ifstat==null) {
buf.append(InterpreterUtil.getIndentString(indent+1));
if(ifedge.explicit) {
if(ifedge.getType() == StatEdge.TYPE_BREAK) {
// break
buf.append("break");
} else {
// continue
buf.append("continue");
}
if(ifedge.labeled) {
buf.append(" label"+ifedge.closure.id);
}
}
buf.append(";" + new_line_separator);
} else {
buf.append(ExprProcessor.jmpWrapper(ifstat, indent+1, true));
}
boolean elseif = false;
if(elsestat != null) {
if(elsestat.type == Statement.TYPE_IF
&& elsestat.varDefinitions.isEmpty() && elsestat.getFirst().getExprents().isEmpty() &&
!elsestat.isLabeled() &&
(elsestat.getSuccessorEdges(STATEDGE_DIRECT_ALL).isEmpty()
|| !elsestat.getSuccessorEdges(STATEDGE_DIRECT_ALL).get(0).explicit)) { // else if
String content = ExprProcessor.jmpWrapper(elsestat, indent, false);
content = content.substring(indstr.length());
buf.append(indstr+"} else ");
buf.append(content);
elseif = true;
} else {
String content = ExprProcessor.jmpWrapper(elsestat, indent+1, false);
if(content.length() > 0) {
buf.append(indstr+"} else {" + new_line_separator);
buf.append(content);
}
}
}
if(!elseif) {
buf.append(indstr+"}" + new_line_separator);
}
return buf.toString();
}
public void initExprents() {
IfExprent ifexpr = (IfExprent)first.getExprents().remove(first.getExprents().size()-1);
if(negated) {
ifexpr = (IfExprent)ifexpr.copy();
ifexpr.negateIf();
}
headexprent.set(0, ifexpr);
}
public List<Object> getSequentialObjects() {
List<Object> lst = new ArrayList<Object>(stats);
lst.add(1, headexprent.get(0));
return lst;
}
public void replaceExprent(Exprent oldexpr, Exprent newexpr) {
if(headexprent.get(0) == oldexpr) {
headexprent.set(0, newexpr);
}
}
public void replaceStatement(Statement oldstat, Statement newstat) {
super.replaceStatement(oldstat, newstat);
if(ifstat == oldstat) {
ifstat = newstat;
}
if(elsestat == oldstat) {
elsestat = newstat;
}
List<StatEdge> lstSuccs = first.getSuccessorEdges(STATEDGE_DIRECT_ALL);
if(iftype == IFTYPE_IF) {
ifedge = lstSuccs.get(0);
elseedge = null;
} else {
StatEdge edge0 = lstSuccs.get(0);
StatEdge edge1 = lstSuccs.get(1);
if(edge0.getDestination() == ifstat) {
ifedge = edge0;
elseedge = edge1;
} else {
ifedge = edge1;
elseedge = edge0;
}
}
}
public Statement getSimpleCopy() {
IfStatement is = new IfStatement();
is.iftype = this.iftype;
is.negated = this.negated;
is.iffflag = this.iffflag;
return is;
}
public void initSimpleCopy() {
first = stats.get(0);
List<StatEdge> lstSuccs = first.getSuccessorEdges(STATEDGE_DIRECT_ALL);
ifedge = lstSuccs.get((iftype == IFTYPE_IF || negated)?0:1);
if(stats.size() > 1) {
ifstat = stats.get(1);
}
if(iftype == IFTYPE_IFELSE) {
elseedge = lstSuccs.get(negated?1:0);
elsestat = stats.get(2);
}
}
// *****************************************************************************
// getter and setter methods
// *****************************************************************************
public Statement getElsestat() {
return elsestat;
}
public void setElsestat(Statement elsestat) {
this.elsestat = elsestat;
}
public Statement getIfstat() {
return ifstat;
}
public void setIfstat(Statement ifstat) {
this.ifstat = ifstat;
}
public boolean isNegated() {
return negated;
}
public void setNegated(boolean negated) {
this.negated = negated;
}
public List<Exprent> getHeadexprentList() {
return headexprent;
}
public IfExprent getHeadexprent() {
return (IfExprent)headexprent.get(0);
}
public boolean isIffflag() {
return iffflag;
}
public void setIffflag(boolean iffflag) {
this.iffflag = iffflag;
}
public void setElseEdge(StatEdge elseedge) {
this.elseedge = elseedge;
}
public void setIfEdge(StatEdge ifedge) {
this.ifedge = ifedge;
}
public StatEdge getIfEdge() {
return ifedge;
}
public StatEdge getElseEdge() {
return elseedge;
}
}
|