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