1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.impl.*;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.flow.*;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
19 public class IfStatement extends Statement {
21 //this class represents the case of only one statement in
22 //either else and/or then branches.
24 public Expression condition;
25 public Statement thenStatement;
26 public Statement elseStatement;
30 // for local variables table attributes
31 int thenInitStateIndex = -1;
32 int elseInitStateIndex = -1;
33 int mergedInitStateIndex = -1;
35 public IfStatement(Expression condition, Statement thenStatement, int sourceStart, int sourceEnd) {
37 this.condition = condition;
38 this.thenStatement = thenStatement;
39 // remember useful empty statement
40 if (thenStatement instanceof EmptyStatement) thenStatement.bits |= IsUsefulEmptyStatementMASK;
41 this.sourceStart = sourceStart;
42 this.sourceEnd = sourceEnd;
45 public IfStatement(Expression condition, Statement thenStatement, Statement elseStatement, int sourceStart, int sourceEnd) {
47 this.condition = condition;
48 this.thenStatement = thenStatement;
49 // remember useful empty statement
50 if (thenStatement instanceof EmptyStatement) thenStatement.bits |= IsUsefulEmptyStatementMASK;
51 this.elseStatement = elseStatement;
52 if (elseStatement instanceof IfStatement) elseStatement.bits |= IsElseIfStatement;
53 this.sourceStart = sourceStart;
54 this.sourceEnd = sourceEnd;
57 public FlowInfo analyseCode(
58 BlockScope currentScope,
59 FlowContext flowContext,
62 // process the condition
63 flowInfo = condition.analyseCode(currentScope, flowContext, flowInfo);
65 Constant cst = this.condition.optimizedBooleanConstant();
66 boolean isConditionOptimizedTrue = cst != NotAConstant && cst.booleanValue() == true;
67 boolean isConditionOptimizedFalse = cst != NotAConstant && cst.booleanValue() == false;
69 // process the THEN part
70 FlowInfo thenFlowInfo = flowInfo.initsWhenTrue().copy();
71 if (isConditionOptimizedFalse) {
72 thenFlowInfo.setReachMode(FlowInfo.UNREACHABLE);
74 if (this.thenStatement != null) {
75 // Save info for code gen
77 currentScope.methodScope().recordInitializationStates(thenFlowInfo);
78 if (!thenStatement.complainIfUnreachable(thenFlowInfo, currentScope, false)) {
80 thenStatement.analyseCode(currentScope, flowContext, thenFlowInfo);
83 // code gen: optimizing the jump around the ELSE part
84 this.thenExit = !thenFlowInfo.isReachable();
86 // process the ELSE part
87 FlowInfo elseFlowInfo = flowInfo.initsWhenFalse().copy();
88 if (isConditionOptimizedTrue) {
89 elseFlowInfo.setReachMode(FlowInfo.UNREACHABLE);
91 if (this.elseStatement != null) {
92 // signal else clause unnecessarily nested, tolerate else-if code pattern
93 if (thenFlowInfo == FlowInfo.DEAD_END
94 && (this.bits & IsElseIfStatement) == 0 // else of an else-if
95 && !(this.elseStatement instanceof IfStatement)) {
96 currentScope.problemReporter().unnecessaryElse(this.elseStatement);
98 // Save info for code gen
100 currentScope.methodScope().recordInitializationStates(elseFlowInfo);
101 if (!elseStatement.complainIfUnreachable(elseFlowInfo, currentScope, false)) {
103 elseStatement.analyseCode(currentScope, flowContext, elseFlowInfo);
107 // merge THEN & ELSE initializations
108 FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranches(
110 isConditionOptimizedTrue,
112 isConditionOptimizedFalse,
113 true /*if(true){ return; } fake-reachable(); */);
114 mergedInitStateIndex = currentScope.methodScope().recordInitializationStates(mergedInfo);
121 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
122 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
124 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
126 if ((this.bits & IsReachableMASK) == 0) {
129 int pc = codeStream.position;
130 Label endifLabel = new Label(codeStream);
132 // optimizing the then/else part code gen
134 boolean hasThenPart =
135 !(((cst = this.condition.optimizedBooleanConstant()) != NotAConstant
136 && cst.booleanValue() == false)
137 || this.thenStatement == null
138 || this.thenStatement.isEmptyBlock());
139 boolean hasElsePart =
140 !((cst != NotAConstant && cst.booleanValue() == true)
141 || this.elseStatement == null
142 || this.elseStatement.isEmptyBlock());
146 // generate boolean condition
147 this.condition.generateOptimizedBoolean(
151 (falseLabel = new Label(codeStream)),
153 // May loose some local variable initializations : affecting the local variable attributes
154 if (thenInitStateIndex != -1) {
155 codeStream.removeNotDefinitelyAssignedVariables(
158 codeStream.addDefinitelyAssignedVariables(currentScope, thenInitStateIndex);
160 // generate then statement
161 this.thenStatement.generateCode(currentScope, codeStream);
162 // jump around the else statement
163 if (hasElsePart && !thenExit) {
164 this.thenStatement.branchChainTo(endifLabel);
165 int position = codeStream.position;
166 codeStream.goto_(endifLabel);
167 codeStream.updateLastRecordedEndPC(position);
168 //goto is tagged as part of the thenAction block
173 // generate boolean condition
174 this.condition.generateOptimizedBoolean(
181 // generate condition side-effects
182 this.condition.generateCode(currentScope, codeStream, false);
183 codeStream.recordPositionsFrom(pc, this.sourceStart);
186 // generate else statement
188 // May loose some local variable initializations : affecting the local variable attributes
189 if (elseInitStateIndex != -1) {
190 codeStream.removeNotDefinitelyAssignedVariables(
193 codeStream.addDefinitelyAssignedVariables(currentScope, elseInitStateIndex);
195 this.elseStatement.generateCode(currentScope, codeStream);
198 // May loose some local variable initializations : affecting the local variable attributes
199 if (mergedInitStateIndex != -1) {
200 codeStream.removeNotDefinitelyAssignedVariables(
202 mergedInitStateIndex);
204 codeStream.recordPositionsFrom(pc, this.sourceStart);
207 public StringBuffer printStatement(int indent, StringBuffer output) {
209 printIndent(indent, output).append("if ("); //$NON-NLS-1$
210 condition.printExpression(0, output).append(")\n"); //$NON-NLS-1$
211 thenStatement.printStatement(indent + 2, output);
212 if (elseStatement != null) {
214 printIndent(indent, output);
215 output.append("else\n"); //$NON-NLS-1$
216 elseStatement.printStatement(indent + 2, output);
221 public void resolve(BlockScope scope) {
223 TypeBinding type = condition.resolveTypeExpecting(scope, BooleanBinding);
224 condition.implicitWidening(type, type);
225 if (thenStatement != null)
226 thenStatement.resolve(scope);
227 if (elseStatement != null)
228 elseStatement.resolve(scope);
231 public void traverse(
233 BlockScope blockScope) {
235 if (visitor.visit(this, blockScope)) {
236 condition.traverse(visitor, blockScope);
237 if (thenStatement != null)
238 thenStatement.traverse(visitor, blockScope);
239 if (elseStatement != null)
240 elseStatement.traverse(visitor, blockScope);
242 visitor.endVisit(this, blockScope);