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.IAbstractSyntaxTreeVisitor;
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.flow.InitializationFlowContext;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
19 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
21 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
23 public class ReturnStatement extends Statement {
24 public Expression expression;
26 public TypeBinding expressionType;
27 public boolean isSynchronized;
28 public ASTNode[] subroutines;
29 public LocalVariableBinding saveValueVariable;
31 public ReturnStatement(Expression expr, int s, int e ) {
36 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) { // here requires to generate a sequence of finally blocks invocations depending corresponding
37 // to each of the traversed try statements, so that execution will terminate properly.
39 // lookup the label, this should answer the returnContext
41 if (expression != null) {
42 flowInfo = expression.analyseCode(currentScope, flowContext, flowInfo);
44 // compute the return sequence (running the finally blocks)
45 FlowContext traversedContext = flowContext;
46 int subIndex = 0, maxSub = 5;
47 boolean saveValueNeeded = false;
48 boolean hasValueToSave = expression != null && expression.constant == NotAConstant;
51 if ((sub = traversedContext.subRoutine()) != null) {
52 if (this.subroutines == null){
53 this.subroutines = new ASTNode[maxSub];
55 if (subIndex == maxSub) {
56 System.arraycopy(this.subroutines, 0, (this.subroutines = new ASTNode[maxSub *= 2]), 0, subIndex); // grow
58 this.subroutines[subIndex++] = sub;
59 if (sub.cannotReturn()) {
60 saveValueNeeded = false;
64 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
67 if ((node = traversedContext.associatedNode) instanceof SynchronizedStatement) {
68 isSynchronized = true;
70 } else if (node instanceof TryStatement) {
71 TryStatement tryStatement = (TryStatement) node;
72 flowInfo.addInitializationsFrom(tryStatement.subRoutineInits); // collect inits
74 if (this.saveValueVariable == null){ // closest subroutine secret variable is used
75 prepareSaveValueLocation(tryStatement);
77 saveValueNeeded = true;
80 } else if (traversedContext instanceof InitializationFlowContext) {
81 currentScope.problemReporter().cannotReturnInInitializer(this);
82 return FlowInfo.DEAD_END;
84 } while ((traversedContext = traversedContext.parent) != null);
87 if ((subroutines != null) && (subIndex != maxSub)) {
88 System.arraycopy(subroutines, 0, (subroutines = new ASTNode[subIndex]), 0, subIndex);
91 // secret local variable for return value (note that this can only occur in a real method)
92 if (saveValueNeeded) {
93 if (this.saveValueVariable != null) {
94 this.saveValueVariable.useFlag = LocalVariableBinding.USED;
97 this.saveValueVariable = null;
98 if ((!isSynchronized) && (expressionType == BooleanBinding)) {
99 this.expression.bits |= ValueForReturnMASK;
102 return FlowInfo.DEAD_END;
106 * Retrun statement code generation
108 * generate the finallyInvocationSequence.
110 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
111 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
113 //public void generateCode(BlockScope currentScope, CodeStream codeStream) {
114 // if ((bits & IsReachableMASK) == 0) {
117 // int pc = codeStream.position;
118 // // generate the expression
119 // if ((expression != null) && (expression.constant == NotAConstant)) {
120 // expression.generateCode(currentScope, codeStream, needValue()); // no value needed if non-returning subroutine
121 // generateStoreSaveValueIfNecessary(codeStream);
124 // // generation of code responsible for invoking the finally blocks in sequence
125 // if (subroutines != null) {
126 // for (int i = 0, max = subroutines.length; i < max; i++) {
128 // if ((sub = subroutines[i]) instanceof SynchronizedStatement) {
129 // codeStream.load(((SynchronizedStatement) sub).synchroVariable);
130 // codeStream.monitorexit();
132 // TryStatement trySub = (TryStatement) sub;
133 // if (trySub.subRoutineCannotReturn) {
134 // codeStream.goto_(trySub.subRoutineStartLabel);
135 // codeStream.recordPositionsFrom(pc, this.sourceStart);
138 // codeStream.jsr(trySub.subRoutineStartLabel);
143 // if (saveValueVariable != null) codeStream.load(saveValueVariable);
145 // if ((expression != null) && (expression.constant != NotAConstant)) {
146 // codeStream.generateConstant(expression.constant, expression.implicitConversion);
147 // generateStoreSaveValueIfNecessary(codeStream);
149 // // output the suitable return bytecode or wrap the value inside a descriptor for doits
150 // this.generateReturnBytecode(codeStream);
152 // codeStream.recordPositionsFrom(pc, this.sourceStart);
155 * Dump the suitable return bytecode for a return statement
158 //public void generateReturnBytecode(CodeStream codeStream) {
160 // if (expression == null) {
161 // codeStream.return_();
163 // switch (expression.implicitConversion >> 4) {
166 // codeStream.ireturn();
169 // codeStream.freturn();
172 // codeStream.lreturn();
175 // codeStream.dreturn();
178 // codeStream.areturn();
182 //public void generateStoreSaveValueIfNecessary(CodeStream codeStream){
183 // if (saveValueVariable != null) codeStream.store(saveValueVariable, false);
185 public boolean needValue(){
186 return (subroutines == null) || (saveValueVariable != null) || isSynchronized;
188 public void prepareSaveValueLocation(TryStatement targetTryStatement){
190 this.saveValueVariable = targetTryStatement.secretReturnValue;
192 public void resolve(BlockScope scope) {
193 MethodScope methodScope = scope.methodScope();
194 MethodBinding methodBinding;
195 TypeBinding methodType =
196 (methodScope.referenceContext instanceof AbstractMethodDeclaration)
197 ? ((methodBinding = ((AbstractMethodDeclaration) methodScope.referenceContext).binding) == null
199 : methodBinding.returnType)
201 if (methodType == VoidBinding) {
202 // the expression should be null
203 if (expression == null)
205 if ((expressionType = expression.resolveType(scope)) != null)
206 scope.problemReporter().attemptToReturnNonVoidExpression(this, expressionType);
209 if (expression == null) {
210 if (methodType != null) scope.problemReporter().shouldReturn(methodType, this);
213 if ((expressionType = expression.resolveType(scope)) == null)
216 if (methodType != null && expression.isConstantValueOfTypeAssignableToType(expressionType, methodType)) {
217 // dealing with constant
218 expression.implicitWidening(methodType, expressionType);
221 if (expressionType == VoidBinding) {
222 scope.problemReporter().attemptToReturnVoidValue(this);
225 if (methodType != null && expressionType.isCompatibleWith(methodType)) {
226 expression.implicitWidening(methodType, expressionType);
229 if (methodType != null){
230 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionType, methodType);
233 public String toString(int tab){
235 String s = tabString(tab) ;
236 s = s + "return "; //$NON-NLS-1$
237 if (expression != null )
238 s = s + expression.toStringExpression() ;
241 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
242 if (visitor.visit(this, scope)) {
243 if (expression != null)
244 expression.traverse(visitor, scope);
246 visitor.endVisit(this, scope);