d5c34791586cfbfdde6c7dda6773566ff099ff44
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ReturnStatement.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
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;
23
24 public class ReturnStatement extends Statement {
25         public Expression expression;
26
27         public TypeBinding expressionType;
28         public boolean isSynchronized;
29         public AstNode[] subroutines;
30         public LocalVariableBinding saveValueVariable;
31
32 public ReturnStatement(Expression expr, int s, int e ) {
33         sourceStart = s;
34         sourceEnd = e;
35         expression = expr ;
36 }
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.
39
40         // lookup the label, this should answer the returnContext
41
42         if (expression != null) {
43                 flowInfo = expression.analyseCode(currentScope, flowContext, flowInfo);
44         }
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;
50         while (true) {
51                 AstNode sub;
52                 if ((sub = traversedContext.subRoutine()) != null) {
53                         if (this.subroutines == null){
54                                 this.subroutines = new AstNode[maxSub];
55                         }
56                         if (subIndex == maxSub) {
57                                 System.arraycopy(this.subroutines, 0, (this.subroutines = new AstNode[maxSub *= 2]), 0, subIndex); // grow
58                         }
59                         this.subroutines[subIndex++] = sub;
60                         if (sub.cannotReturn()) {
61                                 saveValueNeeded = false;
62                                 break;
63                         }
64                 }
65                 AstNode node;
66
67                 if ((node = traversedContext.associatedNode) instanceof SynchronizedStatement) {
68                         isSynchronized = true;
69
70                 } else if (node instanceof TryStatement && hasValueToSave) {
71                                 if (this.saveValueVariable == null){ // closest subroutine secret variable is used
72                                         prepareSaveValueLocation((TryStatement)node);
73                                 }
74                                 saveValueNeeded = true;
75
76                 } else if (traversedContext instanceof InitializationFlowContext) {
77                                 currentScope.problemReporter().cannotReturnInInitializer(this);
78                                 return FlowInfo.DeadEnd;
79                 }
80
81                 // remember the initialization at this
82                 // point for dealing with blank final variables.
83                 traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
84
85                 FlowContext parentContext;
86                 if ((parentContext = traversedContext.parent) == null) { // top-context
87                         break;
88                 } else {
89                         traversedContext = parentContext;
90                 }
91         }
92         // resize subroutines
93         if ((subroutines != null) && (subIndex != maxSub)) {
94                 System.arraycopy(subroutines, 0, (subroutines = new AstNode[subIndex]), 0, subIndex);
95         }
96
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;
101                 }
102         } else {
103                 this.saveValueVariable = null;
104                 if ((!isSynchronized) && (expressionType == BooleanBinding)) {
105                         this.expression.bits |= ValueForReturnMASK;
106                 }
107         }
108         return FlowInfo.DeadEnd;
109 }
110  
111 /**
112  * Retrun statement code generation
113  *
114  *   generate the finallyInvocationSequence.
115  *
116  * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
117  * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
118  */
119 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
120         if ((bits & IsReachableMASK) == 0) {
121                 return;
122         }
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);
128         }
129         
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++) {
133                         AstNode sub;
134                         if ((sub = subroutines[i]) instanceof SynchronizedStatement) {
135                                 codeStream.load(((SynchronizedStatement) sub).synchroVariable);
136                                 codeStream.monitorexit();
137                         } else {
138                                 TryStatement trySub = (TryStatement) sub;
139                                 if (trySub.subRoutineCannotReturn) {
140                                         codeStream.goto_(trySub.subRoutineStartLabel);
141                                         codeStream.recordPositionsFrom(pc, this.sourceStart);
142                                         return;
143                                 } else {
144                                         codeStream.jsr(trySub.subRoutineStartLabel);
145                                 }
146                         }
147                 }
148         }
149         if (saveValueVariable != null) codeStream.load(saveValueVariable);
150         
151         if ((expression != null) && (expression.constant != NotAConstant)) {
152                 codeStream.generateConstant(expression.constant, expression.implicitConversion);
153                 generateStoreSaveValueIfNecessary(currentScope, codeStream);            
154         }
155         // output the suitable return bytecode or wrap the value inside a descriptor for doits
156         this.generateReturnBytecode(currentScope, codeStream);
157         
158         codeStream.recordPositionsFrom(pc, this.sourceStart);
159 }
160 /**
161  * Dump the suitable return bytecode for a return statement
162  *
163  */
164 public void generateReturnBytecode(BlockScope currentScope, CodeStream codeStream) {
165
166         if (expression == null) {
167                 codeStream.return_();
168         } else {
169                 switch (expression.implicitConversion >> 4) {
170                         case T_boolean :
171                         case T_int :
172                                 codeStream.ireturn();
173                                 break;
174                         case T_float :
175                                 codeStream.freturn();
176                                 break;
177                         case T_long :
178                                 codeStream.lreturn();
179                                 break;
180                         case T_double :
181                                 codeStream.dreturn();
182                                 break;
183                         default :
184                                 codeStream.areturn();
185                 }
186         }
187 }
188 public void generateStoreSaveValueIfNecessary(BlockScope currentScope, CodeStream codeStream){
189
190         if (saveValueVariable != null) codeStream.store(saveValueVariable, false);
191 }
192 public boolean needValue(){
193         return (subroutines == null) || (saveValueVariable != null) || isSynchronized;
194 }
195 public void prepareSaveValueLocation(TryStatement targetTryStatement){
196                 
197         this.saveValueVariable = targetTryStatement.secretReturnValue;
198 }
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 
205                                 ? null 
206                                 : methodBinding.returnType)
207                         : VoidBinding;
208         if (methodType == VoidBinding) {
209                 // the expression should be null
210                 if (expression == null)
211                         return;
212                 if ((expressionType = expression.resolveType(scope)) != null)
213                         scope.problemReporter().attemptToReturnNonVoidExpression(this, expressionType);
214                 return;
215         }
216         if (expression == null) {
217                 if (methodType != null) scope.problemReporter().shouldReturn(methodType, this);
218                 return;
219         }
220         if ((expressionType = expression.resolveType(scope)) == null)
221                 return;
222
223         if (methodType != null && expression.isConstantValueOfTypeAssignableToType(expressionType, methodType)) {
224                 // dealing with constant
225                 expression.implicitWidening(methodType, expressionType);
226                 return;
227         }
228         if (expressionType == VoidBinding) {
229                 scope.problemReporter().attemptToReturnVoidValue(this);
230                 return;
231         }
232         if (methodType != null && BlockScope.areTypesCompatible(expressionType, methodType)) {
233                 expression.implicitWidening(methodType, expressionType);
234                 return;
235         }
236         if (methodType != null){
237                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionType, methodType);
238         }
239 }
240 public String toString(int tab){
241
242         String s = tabString(tab) ;
243         s = s + "return "; //$NON-NLS-1$
244         if (expression != null )
245                 s = s + expression.toStringExpression() ;
246         return s;
247 }
248 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
249         if (visitor.visit(this, scope)) {
250                 if (expression != null)
251                         expression.traverse(visitor, scope);
252         }
253         visitor.endVisit(this, scope);
254 }
255 }