1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.impl.*;
15 import org.eclipse.jdt.internal.compiler.codegen.*;
16 import org.eclipse.jdt.internal.compiler.flow.*;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
19 public class LocalDeclaration extends AbstractVariableDeclaration {
21 public LocalVariableBinding binding;
23 public LocalDeclaration(
29 this.sourceStart = sourceStart;
30 this.sourceEnd = sourceEnd;
31 this.declarationEnd = sourceEnd;
34 public FlowInfo analyseCode(
35 BlockScope currentScope,
36 FlowContext flowContext,
39 // record variable initialization if any
40 if (flowInfo.isReachable()) {
41 bits |= IsLocalDeclarationReachableMASK; // only set if actually reached
43 if (initialization == null)
48 .analyseCode(currentScope, flowContext, flowInfo)
49 .unconditionalInits();
51 // final int i = (i = 0);
52 // no need to complain since (i = 0) part will get the blame
53 //if (binding.isFinal() && flowInfo.isPotentiallyAssigned(binding)) {
54 // currentScope.problemReporter().duplicateInitializationOfFinalLocal(binding, this);
57 flowInfo.markAsDefinitelyAssigned(binding);
61 public void checkModifiers() {
63 //only potential valid modifier is <<final>>
64 if (((modifiers & AccJustFlag) & ~AccFinal) != 0)
65 //AccModifierProblem -> other (non-visibility problem)
66 //AccAlternateModifierProblem -> duplicate modifier
67 //AccModifierProblem | AccAlternateModifierProblem -> visibility problem"
69 modifiers = (modifiers & ~AccAlternateModifierProblem) | AccModifierProblem;
73 * Code generation for a local declaration:
74 * i.e. normal assignment to a local variable + unused variable handling
76 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
78 // even if not reachable, variable must be added to visible if allocated (28298)
79 if (binding.resolvedPosition != -1) {
80 codeStream.addVisibleLocalVariable(binding);
82 if ((bits & IsReachableMASK) == 0) {
85 int pc = codeStream.position;
86 Constant inlinedValue;
88 // something to initialize?
89 if (initialization != null) {
90 // initialize to constant value?
91 if ((inlinedValue = initialization.constant) != NotAConstant) {
92 // forget initializing unused or final locals set to constant value (final ones are inlined)
93 if (binding.resolvedPosition != -1) { // may need to preserve variable
94 int initPC = codeStream.position;
95 codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
96 codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
97 codeStream.store(binding, false);
98 binding.recordInitializationStartPC(codeStream.position);
99 // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
100 // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
102 } else { // initializing to non-constant value
103 initialization.generateCode(currentScope, codeStream, true);
104 // if binding unused generate then discard the value
105 if (binding.resolvedPosition != -1) {
106 // 26903, need extra cast to store null in array local var
107 if (binding.type.isArrayType()
108 && (initialization.resolvedType == NullBinding // arrayLoc = null
109 || ((initialization instanceof CastExpression) // arrayLoc = (type[])null
110 && (((CastExpression)initialization).innermostCastedExpression().resolvedType == NullBinding)))){
111 codeStream.checkcast(binding.type);
113 codeStream.store(binding, false);
114 if (binding.initializationCount == 0) {
115 /* Variable may have been initialized during the code initializing it
116 e.g. int i = (i = 1);
118 binding.recordInitializationStartPC(codeStream.position);
119 // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
120 // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
123 if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
131 codeStream.recordPositionsFrom(pc, this.sourceStart);
134 public void resolve(BlockScope scope) {
136 // create a binding and add it to the scope
137 TypeBinding typeBinding = type.resolveType(scope);
141 if (typeBinding != null) {
142 if (typeBinding == VoidBinding) {
143 scope.problemReporter().variableTypeCannotBeVoid(this);
146 if (typeBinding.isArrayType() && ((ArrayBinding) typeBinding).leafComponentType == VoidBinding) {
147 scope.problemReporter().variableTypeCannotBeVoidArray(this);
152 Binding existingVariable = scope.getBinding(name, BindingIds.VARIABLE, this, false /*do not resolve hidden field*/);
153 boolean shouldInsertInScope = true;
154 if (existingVariable != null && existingVariable.isValidBinding()){
155 if (existingVariable instanceof LocalVariableBinding && this.hiddenVariableDepth == 0) {
156 shouldInsertInScope = false;
157 scope.problemReporter().redefineLocal(this);
159 scope.problemReporter().localVariableHiding(this, existingVariable, false);
163 if (shouldInsertInScope) {
164 if ((modifiers & AccFinal)!= 0 && this.initialization == null) {
165 modifiers |= AccBlankFinal;
167 binding = new LocalVariableBinding(this, typeBinding, modifiers, false);
168 scope.addLocalVariable(binding);
169 binding.constant = NotAConstant;
170 // allow to recursivelly target the binding....
171 // the correct constant is harmed if correctly computed at the end of this method
174 if (typeBinding == null) {
175 if (initialization != null)
176 initialization.resolveType(scope); // want to report all possible errors
180 // store the constant for final locals
181 if (initialization != null) {
182 if (initialization instanceof ArrayInitializer) {
183 TypeBinding initializationType = initialization.resolveTypeExpecting(scope, typeBinding);
184 if (initializationType != null) {
185 ((ArrayInitializer) initialization).binding = (ArrayBinding) initializationType;
186 initialization.implicitWidening(typeBinding, initializationType);
189 TypeBinding initializationType = initialization.resolveType(scope);
190 if (initializationType != null) {
191 if (initialization.isConstantValueOfTypeAssignableToType(initializationType, typeBinding)
192 || (typeBinding.isBaseType() && BaseTypeBinding.isWidening(typeBinding.id, initializationType.id))
193 || initializationType.isCompatibleWith(typeBinding))
194 initialization.implicitWidening(typeBinding, initializationType);
196 scope.problemReporter().typeMismatchError(initializationType, typeBinding, this);
200 // change the constant in the binding when it is final
201 // (the optimization of the constant propagation will be done later on)
202 // cast from constant actual type to variable type
203 if (binding != null) {
206 ? initialization.constant.castTo((typeBinding.id << 4) + initialization.constant.typeID())
212 public void traverse(ASTVisitor visitor, BlockScope scope) {
214 if (visitor.visit(this, scope)) {
215 type.traverse(visitor, scope);
216 if (initialization != null)
217 initialization.traverse(visitor, scope);
219 visitor.endVisit(this, scope);