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
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
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.*;
19 public class LocalDeclaration extends AbstractVariableDeclaration {
21 public LocalVariableBinding binding;
23 public LocalDeclaration(
29 initialization = expr;
31 this.sourceStart = sourceStart;
32 this.sourceEnd = sourceEnd;
33 if (initialization != null) {
34 this.declarationSourceEnd = initialization.sourceEnd;
36 this.declarationSourceEnd = sourceEnd;
38 this.declarationEnd = this.declarationSourceEnd;
41 public FlowInfo analyseCode(
42 BlockScope currentScope,
43 FlowContext flowContext,
46 // record variable initialization if any
47 if (!flowInfo.isDeadEnd() && !flowInfo.isFakeReachable()) {
48 bits |= IsLocalDeclarationReachableMASK; // only set if actually reached
50 if (initialization == null)
54 .analyseCode(currentScope, flowContext, flowInfo)
55 .unconditionalInits();
56 flowInfo.markAsDefinitelyAssigned(binding);
60 public void checkModifiers() {
61 //only potential valid modifier is <<final>>
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
70 (modifiers & (-AccAlternateModifierProblem - 1)) | AccModifierProblem;
74 * Code generation for a local declaration:
75 * i.e. normal assignment to a local variable + unused variable handling
77 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
79 if ((bits & IsReachableMASK) == 0) {
82 int pc = codeStream.position;
83 Constant inlinedValue;
84 // something to initialize?
85 if (binding.resolvedPosition != -1) {
86 codeStream.addVisibleLocalVariable(binding);
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
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);
110 binding.recordInitializationStartPC(codeStream.position);
111 // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
112 // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
115 if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
123 codeStream.recordPositionsFrom(pc, this.sourceStart);
126 public String name() {
128 return String.valueOf(name);
131 public void resolve(BlockScope scope) {
133 // create a binding and add it to the scope
134 TypeBinding tb = type.resolveType(scope);
139 if (tb == VoidBinding) {
140 scope.problemReporter().variableTypeCannotBeVoid(this);
143 if (tb.isArrayType() && ((ArrayBinding) tb).leafComponentType == VoidBinding) {
144 scope.problemReporter().variableTypeCannotBeVoidArray(this);
150 if ((binding = scope.duplicateName(name)) != null) {
151 // the name already exists... may carry on with the first binding...
152 scope.problemReporter().redefineLocal(this);
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
162 if (initialization != null)
163 initialization.resolveType(scope); // want to report all possible errors
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);
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);
183 scope.problemReporter().typeMismatchError(initTb, tb, this);
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
192 ? initialization.constant.castTo((tb.id << 4) + initialization.constant.typeID())
197 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
199 if (visitor.visit(this, scope)) {
200 type.traverse(visitor, scope);
201 if (initialization != null)
202 initialization.traverse(visitor, scope);
204 visitor.endVisit(this, scope);