1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / AND_AND_Expression.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols.TokenName;
14 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19
20 // dedicated treatment for the &&
21 public class AND_AND_Expression extends BinaryExpression {
22
23         int rightInitStateIndex = -1;
24
25         int mergedInitStateIndex = -1;
26
27         public AND_AND_Expression(Expression left, Expression right, int operator) {
28                 super(left, right, operator);
29         }
30
31         public FlowInfo analyseCode(BlockScope currentScope,
32                         FlowContext flowContext, FlowInfo flowInfo) {
33
34                 Constant cst = this.left.optimizedBooleanConstant();
35                 boolean isLeftOptimizedTrue = cst != NotAConstant
36                                 && cst.booleanValue() == true;
37                 boolean isLeftOptimizedFalse = cst != NotAConstant
38                                 && cst.booleanValue() == false;
39
40                 if (isLeftOptimizedTrue) {
41                         // TRUE && anything
42                         // need to be careful of scenario:
43                         // (x && y) && !z, if passing the left info to the right, it would
44                         // be swapped by the !
45                         FlowInfo mergedInfo = left.analyseCode(currentScope, flowContext,
46                                         flowInfo).unconditionalInits();
47                         mergedInfo = right.analyseCode(currentScope, flowContext,
48                                         mergedInfo);
49                         mergedInitStateIndex = currentScope.methodScope()
50                                         .recordInitializationStates(mergedInfo);
51                         return mergedInfo;
52                 }
53
54                 FlowInfo leftInfo = left.analyseCode(currentScope, flowContext,
55                                 flowInfo);
56                 // need to be careful of scenario:
57                 // (x && y) && !z, if passing the left info to the right, it would be
58                 // swapped by the !
59                 FlowInfo rightInfo = leftInfo.initsWhenTrue().unconditionalInits()
60                                 .copy();
61                 rightInitStateIndex = currentScope.methodScope()
62                                 .recordInitializationStates(rightInfo);
63
64                 int previousMode = rightInfo.reachMode();
65                 if (isLeftOptimizedFalse) {
66                         rightInfo.setReachMode(FlowInfo.UNREACHABLE);
67                 }
68                 rightInfo = right.analyseCode(currentScope, flowContext, rightInfo);
69                 FlowInfo trueMergedInfo = rightInfo.initsWhenTrue().copy();
70                 rightInfo.setReachMode(previousMode); // reset after trueMergedInfo
71                                                                                                 // got extracted
72
73                 FlowInfo mergedInfo = FlowInfo
74                                 .conditional(trueMergedInfo, leftInfo.initsWhenFalse().copy()
75                                                 .unconditionalInits().mergedWith(
76                                                                 rightInfo.initsWhenFalse().copy()
77                                                                                 .unconditionalInits()));
78                 mergedInitStateIndex = currentScope.methodScope()
79                                 .recordInitializationStates(mergedInfo);
80                 return mergedInfo;
81         }
82
83         /**
84          * Code generation for a binary operation
85          */
86         // public void generateCode(
87         // BlockScope currentScope,
88         // CodeStream codeStream,
89         // boolean valueRequired) {
90         //                      
91         // int pc = codeStream.position;
92         // Label falseLabel, endLabel;
93         // if (constant != Constant.NotAConstant) {
94         // // inlined value
95         // if (valueRequired)
96         // codeStream.generateConstant(constant, implicitConversion);
97         // codeStream.recordPositionsFrom(pc, this.sourceStart);
98         // return;
99         // }
100         // bits |= OnlyValueRequiredMASK;
101         // generateOptimizedBoolean(
102         // currentScope,
103         // codeStream,
104         // null,
105         // (falseLabel = new Label(codeStream)),
106         // valueRequired);
107         // /* improving code gen for such a case: boolean b = i < 0 && false
108         // * since the label has never been used, we have the inlined value on the
109         // stack. */
110         // if (falseLabel.hasForwardReferences()) {
111         // if (valueRequired) {
112         // codeStream.iconst_1();
113         // if ((bits & ValueForReturnMASK) != 0) {
114         // codeStream.ireturn();
115         // falseLabel.place();
116         // codeStream.iconst_0();
117         // } else {
118         // codeStream.goto_(endLabel = new Label(codeStream));
119         // codeStream.decrStackSize(1);
120         // falseLabel.place();
121         // codeStream.iconst_0();
122         // endLabel.place();
123         // }
124         // } else {
125         // falseLabel.place();
126         // }
127         // }
128         // if (valueRequired) {
129         // codeStream.generateImplicitConversion(implicitConversion);
130         // }
131         // // reposition the endPC
132         // codeStream.updateLastRecordedEndPC(codeStream.position);
133         // }
134         /**
135          * Boolean operator code generation Optimized operations are: &&
136          */
137         // public void generateOptimizedBoolean(
138         // BlockScope currentScope,
139         // CodeStream codeStream,
140         // Label trueLabel,
141         // Label falseLabel,
142         // boolean valueRequired) {
143         //                      
144         // if (constant != Constant.NotAConstant) {
145         // super.generateOptimizedBoolean(currentScope, codeStream, trueLabel,
146         // falseLabel, valueRequired);
147         // return;
148         // }
149         // Constant condConst;
150         // if ((condConst = left.optimizedBooleanConstant()) != NotAConstant) {
151         // if (condConst.booleanValue() == true) {
152         // // <something equivalent to true> && x
153         // left.generateOptimizedBoolean(
154         // currentScope,
155         // codeStream,
156         // trueLabel,
157         // falseLabel,
158         // false);
159         // if (rightInitStateIndex != -1) {
160         // codeStream.addDefinitelyAssignedVariables(currentScope,
161         // rightInitStateIndex);
162         // }
163         // if ((bits & OnlyValueRequiredMASK) != 0) {
164         // right.generateCode(currentScope, codeStream, valueRequired);
165         // } else {
166         // right.generateOptimizedBoolean(
167         // currentScope,
168         // codeStream,
169         // trueLabel,
170         // falseLabel,
171         // valueRequired);
172         // }
173         // } else {
174         // // <something equivalent to false> && x
175         // left.generateOptimizedBoolean(
176         // currentScope,
177         // codeStream,
178         // trueLabel,
179         // falseLabel,
180         // false);
181         // if (valueRequired) {
182         // if ((bits & OnlyValueRequiredMASK) != 0) {
183         // codeStream.iconst_0();
184         // } else {
185         // if (falseLabel != null) {
186         // // implicit falling through the TRUE case
187         // codeStream.goto_(falseLabel);
188         // }
189         // }
190         // }
191         // // reposition the endPC
192         // codeStream.updateLastRecordedEndPC(codeStream.position);
193         // }
194         // if (mergedInitStateIndex != -1) {
195         // codeStream.removeNotDefinitelyAssignedVariables(
196         // currentScope,
197         // mergedInitStateIndex);
198         // }
199         // return;
200         // }
201         // if ((condConst = right.optimizedBooleanConstant()) != NotAConstant) {
202         // if (condConst.booleanValue() == true) {
203         // // x && <something equivalent to true>
204         // if ((bits & OnlyValueRequiredMASK) != 0) {
205         // left.generateCode(currentScope, codeStream, valueRequired);
206         // } else {
207         // left.generateOptimizedBoolean(
208         // currentScope,
209         // codeStream,
210         // trueLabel,
211         // falseLabel,
212         // valueRequired);
213         // }
214         // if (rightInitStateIndex != -1) {
215         // codeStream.addDefinitelyAssignedVariables(currentScope,
216         // rightInitStateIndex);
217         // }
218         // right.generateOptimizedBoolean(
219         // currentScope,
220         // codeStream,
221         // trueLabel,
222         // falseLabel,
223         // false);
224         // } else {
225         // // x && <something equivalent to false>
226         // Label internalTrueLabel = new Label(codeStream);
227         // left.generateOptimizedBoolean(
228         // currentScope,
229         // codeStream,
230         // internalTrueLabel, // will be false in the end
231         // null,
232         // false);
233         // if (rightInitStateIndex != -1) {
234         // codeStream.addDefinitelyAssignedVariables(currentScope,
235         // rightInitStateIndex);
236         // }
237         // internalTrueLabel.place();
238         // right.generateOptimizedBoolean(
239         // currentScope,
240         // codeStream,
241         // trueLabel,
242         // falseLabel,
243         // false);
244         // if (valueRequired) {
245         // if ((bits & OnlyValueRequiredMASK) != 0) {
246         // codeStream.iconst_0();
247         // } else {
248         // if (falseLabel != null) {
249         // // implicit falling through the TRUE case
250         // codeStream.goto_(falseLabel);
251         // }
252         // }
253         // }
254         // // reposition the endPC
255         // codeStream.updateLastRecordedEndPC(codeStream.position);
256         // }
257         // if (mergedInitStateIndex != -1) {
258         // codeStream.removeNotDefinitelyAssignedVariables(
259         // currentScope,
260         // mergedInitStateIndex);
261         // }
262         // return;
263         // }
264         // // default case
265         // if (falseLabel == null) {
266         // if (trueLabel != null) {
267         // // implicit falling through the FALSE case
268         // Label internalFalseLabel = new Label(codeStream);
269         // left.generateOptimizedBoolean(
270         // currentScope,
271         // codeStream,
272         // null,
273         // internalFalseLabel,
274         // true);
275         // if (rightInitStateIndex != -1) {
276         // codeStream.addDefinitelyAssignedVariables(currentScope,
277         // rightInitStateIndex);
278         // }
279         // right.generateOptimizedBoolean(
280         // currentScope,
281         // codeStream,
282         // trueLabel,
283         // null,
284         // valueRequired);
285         // internalFalseLabel.place();
286         // }
287         // } else {
288         // // implicit falling through the TRUE case
289         // if (trueLabel == null) {
290         // left.generateOptimizedBoolean(currentScope, codeStream, null, falseLabel,
291         // true);
292         // if (rightInitStateIndex != -1) {
293         // codeStream.addDefinitelyAssignedVariables(currentScope,
294         // rightInitStateIndex);
295         // }
296         // right.generateOptimizedBoolean(
297         // currentScope,
298         // codeStream,
299         // null,
300         // falseLabel,
301         // valueRequired);
302         // } else {
303         // // no implicit fall through TRUE/FALSE --> should never occur
304         // }
305         // }
306         // if (mergedInitStateIndex != -1) {
307         // codeStream.removeNotDefinitelyAssignedVariables(
308         // currentScope,
309         // mergedInitStateIndex);
310         // }
311         // }
312         public boolean isCompactableOperation() {
313                 return false;
314         }
315
316         public void traverse(ASTVisitor visitor, BlockScope scope) {
317                 if (visitor.visit(this, scope)) {
318                         left.traverse(visitor, scope);
319                         right.traverse(visitor, scope);
320                 }
321                 visitor.endVisit(this, scope);
322         }
323 }