Patches from user robekras to show the variables view
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / internal / compiler / ast / ReturnStatement.java
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
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.internal.compiler.ast;
9
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;
19
20 public class ReturnStatement extends Statement {
21   public Expression expression;
22
23   public TypeBinding expressionType;
24
25   public boolean isSynchronized;
26
27   public ASTNode[] subroutines;
28
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
37   public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) { // here requires to generate a
38                                                                                                      // sequence of finally blocks
39                                                                                                      // invocations depending
40                                                                                                      // corresponding
41     // to each of the traversed try statements, so that execution will terminate properly.
42
43     // lookup the label, this should answer the returnContext
44
45     if (expression != null) {
46       flowInfo = expression.analyseCode(currentScope, flowContext, flowInfo);
47     }
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;
53     do {
54       ASTNode sub;
55       if ((sub = traversedContext.subRoutine()) != null) {
56         if (this.subroutines == null) {
57           this.subroutines = new ASTNode[maxSub];
58         }
59         if (subIndex == maxSub) {
60           System.arraycopy(this.subroutines, 0, (this.subroutines = new ASTNode[maxSub *= 2]), 0, subIndex); // grow
61         }
62         this.subroutines[subIndex++] = sub;
63         if (sub.cannotReturn()) {
64           saveValueNeeded = false;
65           break;
66         }
67       }
68       traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
69
70       ASTNode node = traversedContext.associatedNode;
71 //      if ((node = traversedContext.associatedNode) instanceof SynchronizedStatement) {
72 //        isSynchronized = true;
73 //
74 //      } else 
75       if (node instanceof TryStatement) {
76         TryStatement tryStatement = (TryStatement) node;
77         flowInfo.addInitializationsFrom(tryStatement.subRoutineInits); // collect inits
78         if (hasValueToSave) {
79           if (this.saveValueVariable == null) { // closest subroutine secret variable is used
80             prepareSaveValueLocation(tryStatement);
81           }
82           saveValueNeeded = true;
83         }
84
85       } else if (traversedContext instanceof InitializationFlowContext) {
86         currentScope.problemReporter().cannotReturnInInitializer(this);
87         return FlowInfo.DEAD_END;
88       }
89     } while ((traversedContext = traversedContext.parent) != null);
90
91     // resize subroutines
92     if ((subroutines != null) && (subIndex != maxSub)) {
93       System.arraycopy(subroutines, 0, (subroutines = new ASTNode[subIndex]), 0, subIndex);
94     }
95
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;
100       }
101     } else {
102       this.saveValueVariable = null;
103       if ((!isSynchronized) && (expressionType == BooleanBinding)) {
104         this.expression.bits |= ValueForReturnMASK;
105       }
106     }
107     return FlowInfo.DEAD_END;
108   }
109
110   /**
111    * Retrun statement code generation
112    * 
113    * generate the finallyInvocationSequence.
114    * 
115    * @param currentScope
116    *          net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
117    * @param codeStream
118    *          net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
119    */
120   //public void generateCode(BlockScope currentScope, CodeStream codeStream) {
121   //    if ((bits & IsReachableMASK) == 0) {
122   //            return;
123   //    }
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);
129   //    }
130   //    
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++) {
134   //                    ASTNode sub;
135   //                    if ((sub = subroutines[i]) instanceof SynchronizedStatement) {
136   //                            codeStream.load(((SynchronizedStatement) sub).synchroVariable);
137   //                            codeStream.monitorexit();
138   //                    } else {
139   //                            TryStatement trySub = (TryStatement) sub;
140   //                            if (trySub.subRoutineCannotReturn) {
141   //                                    codeStream.goto_(trySub.subRoutineStartLabel);
142   //                                    codeStream.recordPositionsFrom(pc, this.sourceStart);
143   //                                    return;
144   //                            } else {
145   //                                    codeStream.jsr(trySub.subRoutineStartLabel);
146   //                            }
147   //                    }
148   //            }
149   //    }
150   //    if (saveValueVariable != null) codeStream.load(saveValueVariable);
151   //    
152   //    if ((expression != null) && (expression.constant != NotAConstant)) {
153   //            codeStream.generateConstant(expression.constant, expression.implicitConversion);
154   //            generateStoreSaveValueIfNecessary(codeStream);
155   //    }
156   //    // output the suitable return bytecode or wrap the value inside a descriptor for doits
157   //    this.generateReturnBytecode(codeStream);
158   //    
159   //    codeStream.recordPositionsFrom(pc, this.sourceStart);
160   //}
161   /**
162    * Dump the suitable return bytecode for a return statement
163    *  
164    */
165   //public void generateReturnBytecode(CodeStream codeStream) {
166   //
167   //    if (expression == null) {
168   //            codeStream.return_();
169   //    } else {
170   //            switch (expression.implicitConversion >> 4) {
171   //                    case T_boolean :
172   //                    case T_int :
173   //                            codeStream.ireturn();
174   //                            break;
175   //                    case T_float :
176   //                            codeStream.freturn();
177   //                            break;
178   //                    case T_long :
179   //                            codeStream.lreturn();
180   //                            break;
181   //                    case T_double :
182   //                            codeStream.dreturn();
183   //                            break;
184   //                    default :
185   //                            codeStream.areturn();
186   //            }
187   //    }
188   //}
189   //public void generateStoreSaveValueIfNecessary(CodeStream codeStream){
190   //    if (saveValueVariable != null) codeStream.store(saveValueVariable, false);
191   //}
192   public boolean needValue() {
193     return (subroutines == null) || (saveValueVariable != null) || isSynchronized;
194   }
195
196   public void prepareSaveValueLocation(TryStatement targetTryStatement) {
197
198     this.saveValueVariable = targetTryStatement.secretReturnValue;
199   }
200
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)
206         : VoidBinding;
207     if (methodType == VoidBinding) {
208       // the expression should be null
209       if (expression == null)
210         return;
211       if ((expressionType = expression.resolveType(scope)) != null)
212         scope.problemReporter().attemptToReturnNonVoidExpression(this, expressionType);
213       return;
214     }
215     if (expression == null) {
216       if (methodType != null)
217         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 && expressionType.isCompatibleWith(methodType)) {
233       expression.implicitWidening(methodType, expressionType);
234       return;
235     }
236     if (methodType != null) {
237       scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionType, methodType);
238     }
239   }
240
241   public StringBuffer printStatement(int tab, StringBuffer output) {
242
243     printIndent(tab, output).append("return "); //$NON-NLS-1$
244     if (expression != null)
245       expression.printExpression(0, output);
246     return output.append(';');
247   }
248
249   public String toString(int tab) {
250
251     String s = tabString(tab);
252     s = s + "return "; //$NON-NLS-1$
253     if (expression != null)
254       s = s + expression.toStringExpression();
255     return s;
256   }
257
258   public void traverse(ASTVisitor visitor, BlockScope scope) {
259     if (visitor.visit(this, scope)) {
260       if (expression != null)
261         expression.traverse(visitor, scope);
262     }
263     visitor.endVisit(this, scope);
264   }
265 }