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