first scanner /parser copied from the jdt java version
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / LocalDeclaration.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.impl.*;
15 import net.sourceforge.phpdt.internal.compiler.codegen.*;
16 import net.sourceforge.phpdt.internal.compiler.flow.*;
17 import net.sourceforge.phpdt.internal.compiler.lookup.*;
18
19 public class LocalDeclaration extends AbstractVariableDeclaration {
20
21         public LocalVariableBinding binding;
22
23         public LocalDeclaration(
24                 Expression expr,
25                 char[] name,
26                 int sourceStart,
27                 int sourceEnd) {
28
29                 initialization = expr;
30                 this.name = name;
31                 this.sourceStart = sourceStart;
32                 this.sourceEnd = sourceEnd;
33                 if (initialization != null) {
34                         this.declarationSourceEnd = initialization.sourceEnd;
35                 } else {
36                         this.declarationSourceEnd = sourceEnd;
37                 }
38                 this.declarationEnd = this.declarationSourceEnd;
39         }
40
41         public FlowInfo analyseCode(
42                 BlockScope currentScope,
43                 FlowContext flowContext,
44                 FlowInfo flowInfo) {
45
46                 // record variable initialization if any
47                 if (!flowInfo.isDeadEnd() && !flowInfo.isFakeReachable()) {
48                         bits |= IsLocalDeclarationReachableMASK; // only set if actually reached
49                 }
50                 if (initialization == null)
51                         return flowInfo;
52                 flowInfo =
53                         initialization
54                                 .analyseCode(currentScope, flowContext, flowInfo)
55                                 .unconditionalInits();
56                 flowInfo.markAsDefinitelyAssigned(binding);
57                 return flowInfo;
58         }
59
60         public void checkModifiers() {
61                 //only potential valid modifier is <<final>>
62
63                 if (((modifiers & AccJustFlag) | AccFinal) != AccFinal)
64                         //AccModifierProblem -> other (non-visibility problem)
65                         //AccAlternateModifierProblem -> duplicate modifier
66                         //AccModifierProblem | AccAlternateModifierProblem -> visibility problem"
67                         // -x-1 returns the bitInvert 
68
69                         modifiers =
70                                 (modifiers & (-AccAlternateModifierProblem - 1)) | AccModifierProblem;
71         }
72
73         /**
74          * Code generation for a local declaration:
75          *      i.e.&nbsp;normal assignment to a local variable + unused variable handling 
76          */
77         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
78
79                 if ((bits & IsReachableMASK) == 0) {
80                         return;
81                 }
82                 int pc = codeStream.position;
83                 Constant inlinedValue;
84                 // something to initialize?
85                 if (binding.resolvedPosition != -1) {
86                         codeStream.addVisibleLocalVariable(binding);
87                 }
88                 if (initialization != null) {
89                         // initialize to constant value?
90                         if ((inlinedValue = initialization.constant) != NotAConstant) {
91                                 // forget initializing unused or final locals set to constant value (final ones are inlined)
92                                 if (binding.resolvedPosition != -1) { // may need to preserve variable
93                                         int initPC = codeStream.position;
94                                         codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
95                                         codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
96                                         codeStream.store(binding, false);
97                                         binding.recordInitializationStartPC(codeStream.position);
98                                         //                              codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
99                                         //                              codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index            
100                                 }
101                         } else { // initializing to non-constant value
102                                 initialization.generateCode(currentScope, codeStream, true);
103                                 // if binding unused generate then discard the value
104                                 if (binding.resolvedPosition != -1) {
105                                         codeStream.store(binding, false);
106                                         if (binding.initializationCount == 0) {
107                                                 /* Variable may have been initialized during the code initializing it
108                                                         e.g. int i = (i = 1);
109                                                 */
110                                                 binding.recordInitializationStartPC(codeStream.position);
111                                                 //                                      codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
112                                                 //                                      codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index 
113                                         }
114                                 } else {
115                                         if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
116                                                 codeStream.pop2();
117                                         } else {
118                                                 codeStream.pop();
119                                         }
120                                 }
121                         }
122                 }
123                 codeStream.recordPositionsFrom(pc, this.sourceStart);
124         }
125
126         public String name() {
127
128                 return String.valueOf(name);
129         }
130
131         public void resolve(BlockScope scope) {
132
133                 // create a binding and add it to the scope
134                 TypeBinding tb = type.resolveType(scope);
135
136                 checkModifiers();
137
138                 if (tb != null) {
139                         if (tb == VoidBinding) {
140                                 scope.problemReporter().variableTypeCannotBeVoid(this);
141                                 return;
142                         }
143                         if (tb.isArrayType() && ((ArrayBinding) tb).leafComponentType == VoidBinding) {
144                                 scope.problemReporter().variableTypeCannotBeVoidArray(this);
145                                 return;
146                         }
147                 }
148
149                 // duplicate checks
150                 if ((binding = scope.duplicateName(name)) != null) {
151                         // the name already exists... may carry on with the first binding...
152                         scope.problemReporter().redefineLocal(this);
153                 } else {
154                         binding = new LocalVariableBinding(this, tb, modifiers, false);
155                         scope.addLocalVariable(binding);
156                         binding.constant = NotAConstant;
157                         // allow to recursivelly target the binding....
158                         // the correct constant is harmed if correctly computed at the end of this method
159                 }
160
161                 if (tb == null) {
162                         if (initialization != null)
163                                 initialization.resolveType(scope); // want to report all possible errors
164                         return;
165                 }
166
167                 // store the constant for final locals  
168                 if (initialization != null) {
169                         if (initialization instanceof ArrayInitializer) {
170                                 TypeBinding initTb = initialization.resolveTypeExpecting(scope, tb);
171                                 if (initTb != null) {
172                                         ((ArrayInitializer) initialization).binding = (ArrayBinding) initTb;
173                                         initialization.implicitWidening(tb, initTb);
174                                 }
175                         } else {
176                                 TypeBinding initTb = initialization.resolveType(scope);
177                                 if (initTb != null) {
178                                         if (initialization.isConstantValueOfTypeAssignableToType(initTb, tb)
179                                                 || (tb.isBaseType() && BaseTypeBinding.isWidening(tb.id, initTb.id))
180                                                 || scope.areTypesCompatible(initTb, tb))
181                                                 initialization.implicitWidening(tb, initTb);
182                                         else
183                                                 scope.problemReporter().typeMismatchError(initTb, tb, this);
184                                 }
185                         }
186
187                         // change the constant in the binding when it is final
188                         // (the optimization of the constant propagation will be done later on)
189                         // cast from constant actual type to variable type
190                         binding.constant =
191                                 binding.isFinal()
192                                         ? initialization.constant.castTo((tb.id << 4) + initialization.constant.typeID())
193                                         : NotAConstant;
194                 }
195         }
196
197         public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
198
199                 if (visitor.visit(this, scope)) {
200                         type.traverse(visitor, scope);
201                         if (initialization != null)
202                                 initialization.traverse(visitor, scope);
203                 }
204                 visitor.endVisit(this, scope);
205         }
206 }