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 (this.initialization == null)
46 int nullStatus = this.initialization.nullStatus(flowInfo);
49 .analyseCode(currentScope, flowContext, flowInfo)
50 .unconditionalInits();
52 flowInfo.markAsDefinitelyAssigned(binding);
55 flowInfo.markAsDefinitelyNull(this.binding);
57 case FlowInfo.NON_NULL :
58 flowInfo.markAsDefinitelyNonNull(this.binding);
64 public void checkModifiers() {
66 //only potential valid modifier is <<final>>
67 if (((modifiers & AccJustFlag) & ~AccFinal) != 0)
68 //AccModifierProblem -> other (non-visibility problem)
69 //AccAlternateModifierProblem -> duplicate modifier
70 //AccModifierProblem | AccAlternateModifierProblem -> visibility problem"
72 modifiers = (modifiers & ~AccAlternateModifierProblem) | AccModifierProblem;
76 * Code generation for a local declaration:
77 * i.e. normal assignment to a local variable + unused variable handling
79 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
81 // even if not reachable, variable must be added to visible if allocated (28298)
82 if (binding.resolvedPosition != -1) {
83 codeStream.addVisibleLocalVariable(binding);
85 if ((bits & IsReachableMASK) == 0) {
88 int pc = codeStream.position;
89 Constant inlinedValue;
91 // something to initialize?
92 if (initialization != null) {
93 // initialize to constant value?
94 if ((inlinedValue = initialization.constant) != NotAConstant) {
95 // forget initializing unused or final locals set to constant value (final ones are inlined)
96 if (binding.resolvedPosition != -1) { // may need to preserve variable
97 int initPC = codeStream.position;
98 codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
99 codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
100 codeStream.store(binding, false);
101 binding.recordInitializationStartPC(codeStream.position);
102 // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
103 // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
105 } else { // initializing to non-constant value
106 initialization.generateCode(currentScope, codeStream, true);
107 // if binding unused generate then discard the value
108 if (binding.resolvedPosition != -1) {
109 // 26903, need extra cast to store null in array local var
110 if (binding.type.isArrayType()
111 && (initialization.resolvedType == NullBinding // arrayLoc = null
112 || ((initialization instanceof CastExpression) // arrayLoc = (type[])null
113 && (((CastExpression)initialization).innermostCastedExpression().resolvedType == NullBinding)))){
114 codeStream.checkcast(binding.type);
116 codeStream.store(binding, false);
117 if (binding.initializationCount == 0) {
118 /* Variable may have been initialized during the code initializing it
119 e.g. int i = (i = 1);
121 binding.recordInitializationStartPC(codeStream.position);
122 // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
123 // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
126 if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
134 codeStream.recordPositionsFrom(pc, this.sourceStart);
138 * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
140 public int getKind() {
141 return LOCAL_VARIABLE;
144 public void resolve(BlockScope scope) {
146 // create a binding and add it to the scope
147 TypeBinding variableType = type.resolveType(scope, true /* check bounds*/);
150 if (variableType != null) {
151 if (variableType == VoidBinding) {
152 scope.problemReporter().variableTypeCannotBeVoid(this);
155 if (variableType.isArrayType() && ((ArrayBinding) variableType).leafComponentType == VoidBinding) {
156 scope.problemReporter().variableTypeCannotBeVoidArray(this);
161 Binding existingVariable = scope.getBinding(name, Binding.VARIABLE, this, false /*do not resolve hidden field*/);
162 boolean shouldInsertInScope = true;
163 if (existingVariable != null && existingVariable.isValidBinding()){
164 if (existingVariable instanceof LocalVariableBinding && this.hiddenVariableDepth == 0) {
165 shouldInsertInScope = false;
166 scope.problemReporter().redefineLocal(this);
168 scope.problemReporter().localVariableHiding(this, existingVariable, false);
172 if (shouldInsertInScope) {
173 if ((modifiers & AccFinal)!= 0 && this.initialization == null) {
174 modifiers |= AccBlankFinal;
176 this.binding = new LocalVariableBinding(this, variableType, modifiers, false);
177 scope.addLocalVariable(binding);
178 this.binding.setConstant(NotAConstant);
179 // allow to recursivelly target the binding....
180 // the correct constant is harmed if correctly computed at the end of this method
182 resolveAnnotations(scope, this.annotations, this.binding);
185 if (variableType == null) {
186 if (initialization != null)
187 initialization.resolveType(scope); // want to report all possible errors
191 // store the constant for final locals
192 if (initialization != null) {
193 if (initialization instanceof ArrayInitializer) {
194 TypeBinding initializationType = initialization.resolveTypeExpecting(scope, variableType);
195 if (initializationType != null) {
196 ((ArrayInitializer) initialization).binding = (ArrayBinding) initializationType;
197 initialization.computeConversion(scope, variableType, initializationType);
200 this.initialization.setExpectedType(variableType);
201 TypeBinding initializationType = this.initialization.resolveType(scope);
202 if (initializationType != null) {
203 if (variableType != initializationType) // must call before computeConversion() and typeMismatchError()
204 scope.compilationUnitScope().recordTypeConversion(variableType, initializationType);
205 if (initialization.isConstantValueOfTypeAssignableToType(initializationType, variableType)
206 || (variableType.isBaseType() && BaseTypeBinding.isWidening(variableType.id, initializationType.id))
207 || initializationType.isCompatibleWith(variableType)) {
208 this.initialization.computeConversion(scope, variableType, initializationType);
209 if (initializationType.needsUncheckedConversion(variableType)) {
210 scope.problemReporter().unsafeRawConversion(this.initialization, initializationType, variableType);
212 } else if (scope.environment().options.sourceLevel >= JDK1_5 // autoboxing
213 && (scope.isBoxingCompatibleWith(initializationType, variableType)
214 || (initializationType.isBaseType() // narrowing then boxing ?
215 && initializationType != null
216 && !variableType.isBaseType()
217 && initialization.isConstantValueOfTypeAssignableToType(initializationType, scope.environment().computeBoxingType(variableType))))) {
218 this.initialization.computeConversion(scope, variableType, initializationType);
220 scope.problemReporter().typeMismatchError(initializationType, variableType, this);
225 // change the constant in the binding when it is final
226 // (the optimization of the constant propagation will be done later on)
227 // cast from constant actual type to variable type
228 if (binding != null) {
231 ? initialization.constant.castTo((variableType.id << 4) + initialization.constant.typeID())
237 public void traverse(ASTVisitor visitor, BlockScope scope) {
239 if (visitor.visit(this, scope)) {
240 type.traverse(visitor, scope);
241 if (initialization != null)
242 initialization.traverse(visitor, scope);
244 visitor.endVisit(this, scope);