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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
16 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19 //dedicated treatment for the ||
20 public class OR_OR_Expression extends BinaryExpression {
22 int rightInitStateIndex = -1;
23 int mergedInitStateIndex = -1;
25 public OR_OR_Expression(Expression left, Expression right, int operator) {
26 super(left, right, operator);
29 public FlowInfo analyseCode(
30 BlockScope currentScope,
31 FlowContext flowContext,
34 Constant cst = this.left.optimizedBooleanConstant();
35 boolean isLeftOptimizedTrue = cst != NotAConstant && cst.booleanValue() == true;
36 boolean isLeftOptimizedFalse = cst != NotAConstant && cst.booleanValue() == false;
38 if (isLeftOptimizedFalse) {
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);
49 FlowInfo leftInfo = left.analyseCode(currentScope, flowContext, flowInfo);
51 // need to be careful of scenario:
52 // (x || y) || !z, if passing the left info to the right, it would be swapped by the !
53 FlowInfo rightInfo = leftInfo.initsWhenFalse().unconditionalInits().copy();
55 currentScope.methodScope().recordInitializationStates(rightInfo);
57 int previousMode = rightInfo.reachMode();
58 if (isLeftOptimizedTrue){
59 rightInfo.setReachMode(FlowInfo.UNREACHABLE);
61 rightInfo = right.analyseCode(currentScope, flowContext, rightInfo);
62 FlowInfo falseMergedInfo = rightInfo.initsWhenFalse().copy();
63 rightInfo.setReachMode(previousMode); // reset after falseMergedInfo got extracted
65 FlowInfo mergedInfo = FlowInfo.conditional(
66 // merging two true initInfos for such a negative case: if ((t && (b = t)) || f) r = b; // b may not have been initialized
67 leftInfo.initsWhenTrue().copy().unconditionalInits().mergedWith(
68 rightInfo.initsWhenTrue().copy().unconditionalInits()),
70 mergedInitStateIndex =
71 currentScope.methodScope().recordInitializationStates(mergedInfo);
76 * Code generation for a binary operation
78 * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
79 * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
80 * @param valueRequired boolean
82 // public void generateCode(
83 // BlockScope currentScope,
84 // CodeStream codeStream,
85 // boolean valueRequired) {
86 // int pc = codeStream.position;
87 // Label falseLabel, endLabel;
88 // if (constant != Constant.NotAConstant) {
90 // codeStream.generateConstant(constant, implicitConversion);
91 // codeStream.recordPositionsFrom(pc, this.sourceStart);
94 // bits |= OnlyValueRequiredMASK;
95 // generateOptimizedBoolean(
99 // (falseLabel = new Label(codeStream)),
101 // /* improving code gen for such a case: boolean b = i < 0 || true;
102 // * since the label has never been used, we have the inlined value on the stack. */
103 // if (falseLabel.hasForwardReferences()) {
104 // if (valueRequired) {
105 // codeStream.iconst_1();
106 // if ((bits & ValueForReturnMASK) != 0) {
107 // codeStream.ireturn();
108 // falseLabel.place();
109 // codeStream.iconst_0();
111 // codeStream.goto_(endLabel = new Label(codeStream));
112 // codeStream.decrStackSize(1);
113 // falseLabel.place();
114 // codeStream.iconst_0();
118 // falseLabel.place();
121 // if (valueRequired) {
122 // codeStream.generateImplicitConversion(implicitConversion);
124 // codeStream.recordPositionsFrom(pc, this.sourceStart);
128 // * Boolean operator code generation
129 // * Optimized operations are: ||
131 // public void generateOptimizedBoolean(
132 // BlockScope currentScope,
133 // CodeStream codeStream,
136 // boolean valueRequired) {
137 // if (constant != Constant.NotAConstant) {
138 // super.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel, valueRequired);
141 // Constant condConst;
142 // if ((condConst = left.optimizedBooleanConstant()) != NotAConstant) {
143 // if (condConst.booleanValue() == true) {
144 // // <something equivalent to true> || x
145 // left.generateOptimizedBoolean(
151 // if (valueRequired) {
152 // if ((bits & OnlyValueRequiredMASK) != 0) {
153 // codeStream.iconst_1();
155 // if (trueLabel != null) {
156 // codeStream.goto_(trueLabel);
160 // // reposition the endPC
161 // codeStream.updateLastRecordedEndPC(codeStream.position);
163 // // <something equivalent to false> || x
164 // left.generateOptimizedBoolean(
170 // if (rightInitStateIndex != -1) {
171 // codeStream.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex);
173 // if ((bits & OnlyValueRequiredMASK) != 0) {
174 // right.generateCode(currentScope, codeStream, valueRequired);
176 // right.generateOptimizedBoolean(
184 // if (mergedInitStateIndex != -1) {
185 // codeStream.removeNotDefinitelyAssignedVariables(
187 // mergedInitStateIndex);
191 // if ((condConst = right.optimizedBooleanConstant()) != NotAConstant) {
192 // if (condConst.booleanValue() == true) {
193 // // x || <something equivalent to true>
194 // Label internalFalseLabel = new Label(codeStream);
195 // left.generateOptimizedBoolean(
199 // internalFalseLabel, // will be true in the end
201 // if (rightInitStateIndex != -1) {
202 // codeStream.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex);
204 // internalFalseLabel.place();
205 // right.generateOptimizedBoolean(
211 // if (valueRequired) {
212 // if ((bits & OnlyValueRequiredMASK) != 0) {
213 // codeStream.iconst_1();
215 // if (trueLabel != null) {
216 // codeStream.goto_(trueLabel);
220 // // reposition the endPC
221 // codeStream.updateLastRecordedEndPC(codeStream.position);
223 // // x || <something equivalent to false>
224 // if ((bits & OnlyValueRequiredMASK) != 0) {
225 // left.generateCode(currentScope, codeStream, valueRequired);
227 // left.generateOptimizedBoolean(
234 // if (rightInitStateIndex != -1) {
235 // codeStream.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex);
237 // right.generateOptimizedBoolean(
244 // if (mergedInitStateIndex != -1) {
245 // codeStream.removeNotDefinitelyAssignedVariables(
247 // mergedInitStateIndex);
252 // if (falseLabel == null) {
253 // if (trueLabel != null) {
254 // // implicit falling through the FALSE case
255 // left.generateOptimizedBoolean(currentScope, codeStream, trueLabel, null, true);
256 // right.generateOptimizedBoolean(
264 // // implicit falling through the TRUE case
265 // if (trueLabel == null) {
266 // Label internalTrueLabel = new Label(codeStream);
267 // left.generateOptimizedBoolean(
270 // internalTrueLabel,
273 // if (rightInitStateIndex != -1) {
274 // codeStream.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex);
276 // right.generateOptimizedBoolean(
282 // internalTrueLabel.place();
284 // // no implicit fall through TRUE/FALSE --> should never occur
287 // if (mergedInitStateIndex != -1) {
288 // codeStream.removeNotDefinitelyAssignedVariables(
290 // mergedInitStateIndex);
294 public boolean isCompactableOperation() {
298 public void traverse(ASTVisitor visitor, BlockScope scope) {
299 if (visitor.visit(this, scope)) {
300 left.traverse(visitor, scope);
301 right.traverse(visitor, scope);
303 visitor.endVisit(this, scope);