1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.ast;
10 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
11 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
12 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
13 import net.sourceforge.phpdt.internal.compiler.flow.InitializationFlowContext;
14 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
15 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
16 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
20 public class ReturnStatement extends Statement {
21 public Expression expression;
23 public TypeBinding expressionType;
25 public boolean isSynchronized;
27 public ASTNode[] subroutines;
29 public LocalVariableBinding saveValueVariable;
31 public ReturnStatement(Expression expr, int s, int e) {
37 public FlowInfo analyseCode(BlockScope currentScope,
38 FlowContext flowContext, FlowInfo flowInfo) { // here requires to
40 // sequence of finally blocks
41 // invocations depending
43 // to each of the traversed try statements, so that execution will
44 // terminate properly.
46 // lookup the label, this should answer the returnContext
48 if (expression != null) {
49 flowInfo = expression.analyseCode(currentScope, flowContext,
52 // compute the return sequence (running the finally blocks)
53 FlowContext traversedContext = flowContext;
54 int subIndex = 0, maxSub = 5;
55 boolean saveValueNeeded = false;
56 boolean hasValueToSave = expression != null
57 && expression.constant == NotAConstant;
60 if ((sub = traversedContext.subRoutine()) != null) {
61 if (this.subroutines == null) {
62 this.subroutines = new ASTNode[maxSub];
64 if (subIndex == maxSub) {
65 System.arraycopy(this.subroutines, 0,
66 (this.subroutines = new ASTNode[maxSub *= 2]), 0,
69 this.subroutines[subIndex++] = sub;
70 if (sub.cannotReturn()) {
71 saveValueNeeded = false;
75 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
77 ASTNode node = traversedContext.associatedNode;
78 // if ((node = traversedContext.associatedNode) instanceof
79 // SynchronizedStatement) {
80 // isSynchronized = true;
83 if (node instanceof TryStatement) {
84 TryStatement tryStatement = (TryStatement) node;
85 flowInfo.addInitializationsFrom(tryStatement.subRoutineInits); // collect
88 if (this.saveValueVariable == null) { // closest
91 prepareSaveValueLocation(tryStatement);
93 saveValueNeeded = true;
96 } else if (traversedContext instanceof InitializationFlowContext) {
97 currentScope.problemReporter().cannotReturnInInitializer(this);
98 return FlowInfo.DEAD_END;
100 } while ((traversedContext = traversedContext.parent) != null);
102 // resize subroutines
103 if ((subroutines != null) && (subIndex != maxSub)) {
104 System.arraycopy(subroutines, 0,
105 (subroutines = new ASTNode[subIndex]), 0, subIndex);
108 // secret local variable for return value (note that this can only occur
110 if (saveValueNeeded) {
111 if (this.saveValueVariable != null) {
112 this.saveValueVariable.useFlag = LocalVariableBinding.USED;
115 this.saveValueVariable = null;
116 if ((!isSynchronized) && (expressionType == BooleanBinding)) {
117 this.expression.bits |= ValueForReturnMASK;
120 return FlowInfo.DEAD_END;
124 * Retrun statement code generation
126 * generate the finallyInvocationSequence.
128 * @param currentScope
129 * net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
131 * net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
133 // public void generateCode(BlockScope currentScope, CodeStream codeStream)
135 // if ((bits & IsReachableMASK) == 0) {
138 // int pc = codeStream.position;
139 // // generate the expression
140 // if ((expression != null) && (expression.constant == NotAConstant)) {
141 // expression.generateCode(currentScope, codeStream, needValue()); // no
142 // value needed if non-returning subroutine
143 // generateStoreSaveValueIfNecessary(codeStream);
146 // // generation of code responsible for invoking the finally blocks in
148 // if (subroutines != null) {
149 // for (int i = 0, max = subroutines.length; i < max; i++) {
151 // if ((sub = subroutines[i]) instanceof SynchronizedStatement) {
152 // codeStream.load(((SynchronizedStatement) sub).synchroVariable);
153 // codeStream.monitorexit();
155 // TryStatement trySub = (TryStatement) sub;
156 // if (trySub.subRoutineCannotReturn) {
157 // codeStream.goto_(trySub.subRoutineStartLabel);
158 // codeStream.recordPositionsFrom(pc, this.sourceStart);
161 // codeStream.jsr(trySub.subRoutineStartLabel);
166 // if (saveValueVariable != null) codeStream.load(saveValueVariable);
168 // if ((expression != null) && (expression.constant != NotAConstant)) {
169 // codeStream.generateConstant(expression.constant,
170 // expression.implicitConversion);
171 // generateStoreSaveValueIfNecessary(codeStream);
173 // // output the suitable return bytecode or wrap the value inside a
174 // descriptor for doits
175 // this.generateReturnBytecode(codeStream);
177 // codeStream.recordPositionsFrom(pc, this.sourceStart);
180 * Dump the suitable return bytecode for a return statement
183 // public void generateReturnBytecode(CodeStream codeStream) {
185 // if (expression == null) {
186 // codeStream.return_();
188 // switch (expression.implicitConversion >> 4) {
191 // codeStream.ireturn();
194 // codeStream.freturn();
197 // codeStream.lreturn();
200 // codeStream.dreturn();
203 // codeStream.areturn();
207 // public void generateStoreSaveValueIfNecessary(CodeStream codeStream){
208 // if (saveValueVariable != null) codeStream.store(saveValueVariable,
211 public boolean needValue() {
212 return (subroutines == null) || (saveValueVariable != null)
216 public void prepareSaveValueLocation(TryStatement targetTryStatement) {
218 this.saveValueVariable = targetTryStatement.secretReturnValue;
221 public void resolve(BlockScope scope) {
222 MethodScope methodScope = scope.methodScope();
223 MethodBinding methodBinding;
224 TypeBinding methodType = (methodScope.referenceContext instanceof AbstractMethodDeclaration) ? ((methodBinding = ((AbstractMethodDeclaration) methodScope.referenceContext).binding) == null ? null
225 : methodBinding.returnType)
227 if (methodType == VoidBinding) {
228 // the expression should be null
229 if (expression == null)
231 if ((expressionType = expression.resolveType(scope)) != null)
232 scope.problemReporter().attemptToReturnNonVoidExpression(this,
236 if (expression == null) {
237 if (methodType != null)
238 scope.problemReporter().shouldReturn(methodType, this);
241 if ((expressionType = expression.resolveType(scope)) == null)
244 if (methodType != null
245 && expression.isConstantValueOfTypeAssignableToType(
246 expressionType, methodType)) {
247 // dealing with constant
248 expression.implicitWidening(methodType, expressionType);
251 if (expressionType == VoidBinding) {
252 scope.problemReporter().attemptToReturnVoidValue(this);
255 if (methodType != null && expressionType.isCompatibleWith(methodType)) {
256 expression.implicitWidening(methodType, expressionType);
259 if (methodType != null) {
260 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
261 expression, expressionType, methodType);
265 public StringBuffer printStatement(int tab, StringBuffer output) {
267 printIndent(tab, output).append("return "); //$NON-NLS-1$
268 if (expression != null)
269 expression.printExpression(0, output);
270 return output.append(';');
273 public String toString(int tab) {
275 String s = tabString(tab);
276 s = s + "return "; //$NON-NLS-1$
277 if (expression != null)
278 s = s + expression.toStringExpression();
282 public void traverse(ASTVisitor visitor, BlockScope scope) {
283 if (visitor.visit(this, scope)) {
284 if (expression != null)
285 expression.traverse(visitor, scope);
287 visitor.endVisit(this, scope);