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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
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.lookup.ArrayBinding;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypeBinding;
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.TypeBinding;
23 public class LocalDeclaration extends AbstractVariableDeclaration {
25 public LocalVariableBinding binding;
27 public LocalDeclaration(
33 initialization = expr;
35 this.sourceStart = sourceStart;
36 this.sourceEnd = sourceEnd;
37 if (initialization != null) {
38 this.declarationSourceEnd = initialization.sourceEnd;
39 this.declarationEnd = initialization.sourceEnd;
41 this.declarationEnd = sourceEnd;
45 public FlowInfo analyseCode(
46 BlockScope currentScope,
47 FlowContext flowContext,
50 // record variable initialization if any
51 if (flowInfo.isReachable()) {
52 bits |= IsLocalDeclarationReachableMASK; // only set if actually reached
54 if (initialization == null)
59 .analyseCode(currentScope, flowContext, flowInfo)
60 .unconditionalInits();
62 // final int i = (i = 0);
63 // no need to complain since (i = 0) part will get the blame
64 //if (binding.isFinal() && flowInfo.isPotentiallyAssigned(binding)) {
65 // currentScope.problemReporter().duplicateInitializationOfFinalLocal(binding, this);
68 flowInfo.markAsDefinitelyAssigned(binding);
72 public void checkModifiers() {
74 //only potential valid modifier is <<final>>
75 if (((modifiers & AccJustFlag) & ~AccFinal) != 0)
76 //AccModifierProblem -> other (non-visibility problem)
77 //AccAlternateModifierProblem -> duplicate modifier
78 //AccModifierProblem | AccAlternateModifierProblem -> visibility problem"
80 modifiers = (modifiers & ~AccAlternateModifierProblem) | AccModifierProblem;
84 * Code generation for a local declaration:
85 * normal assignment to a local variable + unused variable handling
87 // public void generateCode(BlockScope currentScope, CodeStream codeStream) {
89 // // even if not reachable, variable must be added to visible if allocated (28298)
90 // if (binding.resolvedPosition != -1) {
91 // codeStream.addVisibleLocalVariable(binding);
93 // if ((bits & IsReachableMASK) == 0) {
96 // int pc = codeStream.position;
97 // Constant inlinedValue;
99 // // something to initialize?
100 // if (initialization != null) {
101 // // initialize to constant value?
102 // if ((inlinedValue = initialization.constant) != NotAConstant) {
103 // // forget initializing unused or final locals set to constant value (final ones are inlined)
104 // if (binding.resolvedPosition != -1) { // may need to preserve variable
105 // int initPC = codeStream.position;
106 // codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
107 // codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
108 // codeStream.store(binding, false);
109 // binding.recordInitializationStartPC(codeStream.position);
110 // // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
111 // // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
113 // } else { // initializing to non-constant value
114 // initialization.generateCode(currentScope, codeStream, true);
115 // // if binding unused generate then discard the value
116 // if (binding.resolvedPosition != -1) {
117 // // 26903, need extra cast to store null in array local var
118 // if (binding.type.isArrayType()
119 // && (initialization.resolvedType == NullBinding // arrayLoc = null
120 // || ((initialization instanceof CastExpression) // arrayLoc = (type[])null
121 // && (((CastExpression)initialization).innermostCastedExpression().resolvedType == NullBinding)))){
122 // codeStream.checkcast(binding.type);
124 // codeStream.store(binding, false);
125 // if (binding.initializationCount == 0) {
126 // /* Variable may have been initialized during the code initializing it
127 // e.g. int i = (i = 1);
129 // binding.recordInitializationStartPC(codeStream.position);
130 // // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
131 // // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
134 // if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
135 // codeStream.pop2();
142 // codeStream.recordPositionsFrom(pc, this.sourceStart);
145 public String name() {
147 return String.valueOf(name);
150 public void resolve(BlockScope scope) {
152 // create a binding and add it to the scope
153 TypeBinding tb = type.resolveType(scope);
158 if (tb == VoidBinding) {
159 scope.problemReporter().variableTypeCannotBeVoid(this);
162 if (tb.isArrayType() && ((ArrayBinding) tb).leafComponentType == VoidBinding) {
163 scope.problemReporter().variableTypeCannotBeVoidArray(this);
169 if ((binding = scope.duplicateName(name)) != null) {
170 // the name already exists... may carry on with the first binding...
171 scope.problemReporter().redefineLocal(this);
173 if ((modifiers & AccFinal)!= 0 && this.initialization == null) {
174 modifiers |= AccBlankFinal;
176 binding = new LocalVariableBinding(this, tb, modifiers, false);
177 scope.addLocalVariable(binding);
178 binding.constant = NotAConstant;
179 // allow to recursivelly target the binding....
180 // the correct constant is harmed if correctly computed at the end of this method
184 if (initialization != null)
185 initialization.resolveType(scope); // want to report all possible errors
189 // store the constant for final locals
190 if (initialization != null) {
191 if (initialization instanceof ArrayInitializer) {
192 TypeBinding initTb = initialization.resolveTypeExpecting(scope, tb);
193 if (initTb != null) {
194 ((ArrayInitializer) initialization).binding = (ArrayBinding) initTb;
195 initialization.implicitWidening(tb, initTb);
198 TypeBinding initTb = initialization.resolveType(scope);
199 if (initTb != null) {
200 if (initialization.isConstantValueOfTypeAssignableToType(initTb, tb)
201 || (tb.isBaseType() && BaseTypeBinding.isWidening(tb.id, initTb.id))
202 || initTb.isCompatibleWith(tb))
203 initialization.implicitWidening(tb, initTb);
205 scope.problemReporter().typeMismatchError(initTb, tb, this);
209 // change the constant in the binding when it is final
210 // (the optimization of the constant propagation will be done later on)
211 // cast from constant actual type to variable type
214 ? initialization.constant.castTo((tb.id << 4) + initialization.constant.typeID())
219 public void traverse(ASTVisitor visitor, BlockScope scope) {
221 if (visitor.visit(this, scope)) {
222 type.traverse(visitor, scope);
223 if (initialization != null)
224 initialization.traverse(visitor, scope);
226 visitor.endVisit(this, scope);