1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.flow.InitializationFlowContext;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
22 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
24 public class ReturnStatement extends Statement {
25 public Expression expression;
27 public TypeBinding expressionType;
28 public boolean isSynchronized;
29 public AstNode[] subroutines;
30 public LocalVariableBinding saveValueVariable;
32 public ReturnStatement(Expression expr, int s, int e ) {
37 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) { // here requires to generate a sequence of finally blocks invocations depending corresponding
38 // to each of the traversed try statements, so that execution will terminate properly.
40 // lookup the label, this should answer the returnContext
42 if (expression != null) {
43 flowInfo = expression.analyseCode(currentScope, flowContext, flowInfo);
45 // compute the return sequence (running the finally blocks)
46 FlowContext traversedContext = flowContext;
47 int subIndex = 0, maxSub = 5;
48 boolean saveValueNeeded = false;
49 boolean hasValueToSave = expression != null && expression.constant == NotAConstant;
52 if ((sub = traversedContext.subRoutine()) != null) {
53 if (this.subroutines == null){
54 this.subroutines = new AstNode[maxSub];
56 if (subIndex == maxSub) {
57 System.arraycopy(this.subroutines, 0, (this.subroutines = new AstNode[maxSub *= 2]), 0, subIndex); // grow
59 this.subroutines[subIndex++] = sub;
60 if (sub.cannotReturn()) {
61 saveValueNeeded = false;
67 if ((node = traversedContext.associatedNode) instanceof SynchronizedStatement) {
68 isSynchronized = true;
70 } else if (node instanceof TryStatement && hasValueToSave) {
71 if (this.saveValueVariable == null){ // closest subroutine secret variable is used
72 prepareSaveValueLocation((TryStatement)node);
74 saveValueNeeded = true;
76 } else if (traversedContext instanceof InitializationFlowContext) {
77 currentScope.problemReporter().cannotReturnInInitializer(this);
78 return FlowInfo.DeadEnd;
81 // remember the initialization at this
82 // point for dealing with blank final variables.
83 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
85 FlowContext parentContext;
86 if ((parentContext = traversedContext.parent) == null) { // top-context
89 traversedContext = parentContext;
93 if ((subroutines != null) && (subIndex != maxSub)) {
94 System.arraycopy(subroutines, 0, (subroutines = new AstNode[subIndex]), 0, subIndex);
97 // secret local variable for return value (note that this can only occur in a real method)
98 if (saveValueNeeded) {
99 if (this.saveValueVariable != null) {
100 this.saveValueVariable.used = true;
103 this.saveValueVariable = null;
104 if ((!isSynchronized) && (expressionType == BooleanBinding)) {
105 this.expression.bits |= ValueForReturnMASK;
108 return FlowInfo.DeadEnd;
112 * Retrun statement code generation
114 * generate the finallyInvocationSequence.
116 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
117 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
119 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
120 if ((bits & IsReachableMASK) == 0) {
123 int pc = codeStream.position;
124 // generate the expression
125 if ((expression != null) && (expression.constant == NotAConstant)) {
126 expression.generateCode(currentScope, codeStream, needValue()); // no value needed if non-returning subroutine
127 generateStoreSaveValueIfNecessary(currentScope, codeStream);
130 // generation of code responsible for invoking the finally blocks in sequence
131 if (subroutines != null) {
132 for (int i = 0, max = subroutines.length; i < max; i++) {
134 if ((sub = subroutines[i]) instanceof SynchronizedStatement) {
135 codeStream.load(((SynchronizedStatement) sub).synchroVariable);
136 codeStream.monitorexit();
138 TryStatement trySub = (TryStatement) sub;
139 if (trySub.subRoutineCannotReturn) {
140 codeStream.goto_(trySub.subRoutineStartLabel);
141 codeStream.recordPositionsFrom(pc, this.sourceStart);
144 codeStream.jsr(trySub.subRoutineStartLabel);
149 if (saveValueVariable != null) codeStream.load(saveValueVariable);
151 if ((expression != null) && (expression.constant != NotAConstant)) {
152 codeStream.generateConstant(expression.constant, expression.implicitConversion);
153 generateStoreSaveValueIfNecessary(currentScope, codeStream);
155 // output the suitable return bytecode or wrap the value inside a descriptor for doits
156 this.generateReturnBytecode(currentScope, codeStream);
158 codeStream.recordPositionsFrom(pc, this.sourceStart);
161 * Dump the suitable return bytecode for a return statement
164 public void generateReturnBytecode(BlockScope currentScope, CodeStream codeStream) {
166 if (expression == null) {
167 codeStream.return_();
169 switch (expression.implicitConversion >> 4) {
172 codeStream.ireturn();
175 codeStream.freturn();
178 codeStream.lreturn();
181 codeStream.dreturn();
184 codeStream.areturn();
188 public void generateStoreSaveValueIfNecessary(BlockScope currentScope, CodeStream codeStream){
190 if (saveValueVariable != null) codeStream.store(saveValueVariable, false);
192 public boolean needValue(){
193 return (subroutines == null) || (saveValueVariable != null) || isSynchronized;
195 public void prepareSaveValueLocation(TryStatement targetTryStatement){
197 this.saveValueVariable = targetTryStatement.secretReturnValue;
199 public void resolve(BlockScope scope) {
200 MethodScope methodScope = scope.methodScope();
201 MethodBinding methodBinding;
202 TypeBinding methodType =
203 (methodScope.referenceContext instanceof AbstractMethodDeclaration)
204 ? ((methodBinding = ((AbstractMethodDeclaration) methodScope.referenceContext).binding) == null
206 : methodBinding.returnType)
208 if (methodType == VoidBinding) {
209 // the expression should be null
210 if (expression == null)
212 if ((expressionType = expression.resolveType(scope)) != null)
213 scope.problemReporter().attemptToReturnNonVoidExpression(this, expressionType);
216 if (expression == null) {
217 if (methodType != null) 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 && BlockScope.areTypesCompatible(expressionType, methodType)) {
233 expression.implicitWidening(methodType, expressionType);
236 if (methodType != null){
237 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionType, methodType);
240 public String toString(int tab){
242 String s = tabString(tab) ;
243 s = s + "return "; //$NON-NLS-1$
244 if (expression != null )
245 s = s + expression.toStringExpression() ;
248 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
249 if (visitor.visit(this, scope)) {
250 if (expression != null)
251 expression.traverse(visitor, scope);
253 visitor.endVisit(this, scope);