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.phpeclipse.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, FlowContext flowContext, FlowInfo flowInfo) { // here requires to generate a
38 // sequence of finally blocks
39 // invocations depending
41 // to each of the traversed try statements, so that execution will terminate properly.
43 // lookup the label, this should answer the returnContext
45 if (expression != null) {
46 flowInfo = expression.analyseCode(currentScope, flowContext, flowInfo);
48 // compute the return sequence (running the finally blocks)
49 FlowContext traversedContext = flowContext;
50 int subIndex = 0, maxSub = 5;
51 boolean saveValueNeeded = false;
52 boolean hasValueToSave = expression != null && expression.constant == NotAConstant;
55 if ((sub = traversedContext.subRoutine()) != null) {
56 if (this.subroutines == null) {
57 this.subroutines = new ASTNode[maxSub];
59 if (subIndex == maxSub) {
60 System.arraycopy(this.subroutines, 0, (this.subroutines = new ASTNode[maxSub *= 2]), 0, subIndex); // grow
62 this.subroutines[subIndex++] = sub;
63 if (sub.cannotReturn()) {
64 saveValueNeeded = false;
68 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
70 ASTNode node = traversedContext.associatedNode;
71 // if ((node = traversedContext.associatedNode) instanceof SynchronizedStatement) {
72 // isSynchronized = true;
75 if (node instanceof TryStatement) {
76 TryStatement tryStatement = (TryStatement) node;
77 flowInfo.addInitializationsFrom(tryStatement.subRoutineInits); // collect inits
79 if (this.saveValueVariable == null) { // closest subroutine secret variable is used
80 prepareSaveValueLocation(tryStatement);
82 saveValueNeeded = true;
85 } else if (traversedContext instanceof InitializationFlowContext) {
86 currentScope.problemReporter().cannotReturnInInitializer(this);
87 return FlowInfo.DEAD_END;
89 } while ((traversedContext = traversedContext.parent) != null);
92 if ((subroutines != null) && (subIndex != maxSub)) {
93 System.arraycopy(subroutines, 0, (subroutines = new ASTNode[subIndex]), 0, subIndex);
96 // secret local variable for return value (note that this can only occur in a real method)
97 if (saveValueNeeded) {
98 if (this.saveValueVariable != null) {
99 this.saveValueVariable.useFlag = LocalVariableBinding.USED;
102 this.saveValueVariable = null;
103 if ((!isSynchronized) && (expressionType == BooleanBinding)) {
104 this.expression.bits |= ValueForReturnMASK;
107 return FlowInfo.DEAD_END;
111 * Retrun statement code generation
113 * generate the finallyInvocationSequence.
115 * @param currentScope
116 * net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
118 * net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
120 //public void generateCode(BlockScope currentScope, CodeStream codeStream) {
121 // if ((bits & IsReachableMASK) == 0) {
124 // int pc = codeStream.position;
125 // // generate the expression
126 // if ((expression != null) && (expression.constant == NotAConstant)) {
127 // expression.generateCode(currentScope, codeStream, needValue()); // no value needed if non-returning subroutine
128 // generateStoreSaveValueIfNecessary(codeStream);
131 // // generation of code responsible for invoking the finally blocks in sequence
132 // if (subroutines != null) {
133 // for (int i = 0, max = subroutines.length; i < max; i++) {
135 // if ((sub = subroutines[i]) instanceof SynchronizedStatement) {
136 // codeStream.load(((SynchronizedStatement) sub).synchroVariable);
137 // codeStream.monitorexit();
139 // TryStatement trySub = (TryStatement) sub;
140 // if (trySub.subRoutineCannotReturn) {
141 // codeStream.goto_(trySub.subRoutineStartLabel);
142 // codeStream.recordPositionsFrom(pc, this.sourceStart);
145 // codeStream.jsr(trySub.subRoutineStartLabel);
150 // if (saveValueVariable != null) codeStream.load(saveValueVariable);
152 // if ((expression != null) && (expression.constant != NotAConstant)) {
153 // codeStream.generateConstant(expression.constant, expression.implicitConversion);
154 // generateStoreSaveValueIfNecessary(codeStream);
156 // // output the suitable return bytecode or wrap the value inside a descriptor for doits
157 // this.generateReturnBytecode(codeStream);
159 // codeStream.recordPositionsFrom(pc, this.sourceStart);
162 * Dump the suitable return bytecode for a return statement
165 //public void generateReturnBytecode(CodeStream codeStream) {
167 // if (expression == null) {
168 // codeStream.return_();
170 // switch (expression.implicitConversion >> 4) {
173 // codeStream.ireturn();
176 // codeStream.freturn();
179 // codeStream.lreturn();
182 // codeStream.dreturn();
185 // codeStream.areturn();
189 //public void generateStoreSaveValueIfNecessary(CodeStream codeStream){
190 // if (saveValueVariable != null) codeStream.store(saveValueVariable, false);
192 public boolean needValue() {
193 return (subroutines == null) || (saveValueVariable != null) || isSynchronized;
196 public void prepareSaveValueLocation(TryStatement targetTryStatement) {
198 this.saveValueVariable = targetTryStatement.secretReturnValue;
201 public void resolve(BlockScope scope) {
202 MethodScope methodScope = scope.methodScope();
203 MethodBinding methodBinding;
204 TypeBinding methodType = (methodScope.referenceContext instanceof AbstractMethodDeclaration) ? ((methodBinding = ((AbstractMethodDeclaration) methodScope.referenceContext).binding) == null ? null
205 : methodBinding.returnType)
207 if (methodType == VoidBinding) {
208 // the expression should be null
209 if (expression == null)
211 if ((expressionType = expression.resolveType(scope)) != null)
212 scope.problemReporter().attemptToReturnNonVoidExpression(this, expressionType);
215 if (expression == null) {
216 if (methodType != null)
217 scope.problemReporter().shouldReturn(methodType, this);
220 if ((expressionType = expression.resolveType(scope)) == null)
223 if (methodType != null && expression.isConstantValueOfTypeAssignableToType(expressionType, methodType)) {
224 // dealing with constant
225 expression.implicitWidening(methodType, expressionType);
228 if (expressionType == VoidBinding) {
229 scope.problemReporter().attemptToReturnVoidValue(this);
232 if (methodType != null && expressionType.isCompatibleWith(methodType)) {
233 expression.implicitWidening(methodType, expressionType);
236 if (methodType != null) {
237 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionType, methodType);
241 public StringBuffer printStatement(int tab, StringBuffer output) {
243 printIndent(tab, output).append("return "); //$NON-NLS-1$
244 if (expression != null)
245 expression.printExpression(0, output);
246 return output.append(';');
249 public String toString(int tab) {
251 String s = tabString(tab);
252 s = s + "return "; //$NON-NLS-1$
253 if (expression != null)
254 s = s + expression.toStringExpression();
258 public void traverse(ASTVisitor visitor, BlockScope scope) {
259 if (visitor.visit(this, scope)) {
260 if (expression != null)
261 expression.traverse(visitor, scope);
263 visitor.endVisit(this, scope);