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.codegen.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
18 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
19 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypeBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
21 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
22 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
24 public class LocalDeclaration extends AbstractVariableDeclaration {
26 public LocalVariableBinding binding;
28 public LocalDeclaration(
34 initialization = expr;
36 this.sourceStart = sourceStart;
37 this.sourceEnd = sourceEnd;
38 if (initialization != null) {
39 this.declarationSourceEnd = initialization.sourceEnd;
41 this.declarationSourceEnd = sourceEnd;
43 this.declarationEnd = this.declarationSourceEnd;
46 public FlowInfo analyseCode(
47 BlockScope currentScope,
48 FlowContext flowContext,
51 // record variable initialization if any
52 if (!flowInfo.isDeadEnd() && !flowInfo.isFakeReachable()) {
53 bits |= IsLocalDeclarationReachableMASK; // only set if actually reached
55 if (initialization == null)
59 .analyseCode(currentScope, flowContext, flowInfo)
60 .unconditionalInits();
61 flowInfo.markAsDefinitelyAssigned(binding);
65 public void checkModifiers() {
66 //only potential valid modifier is <<final>>
68 if (((modifiers & AccJustFlag) | AccFinal) != AccFinal)
69 //AccModifierProblem -> other (non-visibility problem)
70 //AccAlternateModifierProblem -> duplicate modifier
71 //AccModifierProblem | AccAlternateModifierProblem -> visibility problem"
72 // -x-1 returns the bitInvert
75 (modifiers & (-AccAlternateModifierProblem - 1)) | AccModifierProblem;
79 * Code generation for a local declaration:
80 * i.e. normal assignment to a local variable + unused variable handling
82 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
84 if ((bits & IsReachableMASK) == 0) {
87 int pc = codeStream.position;
88 Constant inlinedValue;
89 // something to initialize?
90 if (binding.resolvedPosition != -1) {
91 codeStream.addVisibleLocalVariable(binding);
93 if (initialization != null) {
94 // initialize to constant value?
95 if ((inlinedValue = initialization.constant) != NotAConstant) {
96 // forget initializing unused or final locals set to constant value (final ones are inlined)
97 if (binding.resolvedPosition != -1) { // may need to preserve variable
98 int initPC = codeStream.position;
99 codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
100 codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
101 codeStream.store(binding, false);
102 binding.recordInitializationStartPC(codeStream.position);
103 // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
104 // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
106 } else { // initializing to non-constant value
107 initialization.generateCode(currentScope, codeStream, true);
108 // if binding unused generate then discard the value
109 if (binding.resolvedPosition != -1) {
110 codeStream.store(binding, false);
111 if (binding.initializationCount == 0) {
112 /* Variable may have been initialized during the code initializing it
113 e.g. int i = (i = 1);
115 binding.recordInitializationStartPC(codeStream.position);
116 // codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index
117 // codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index
120 if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
128 codeStream.recordPositionsFrom(pc, this.sourceStart);
131 public String name() {
133 return String.valueOf(name);
136 public void resolve(BlockScope scope) {
138 // create a binding and add it to the scope
139 TypeBinding tb = type.resolveType(scope);
144 if (tb == VoidBinding) {
145 scope.problemReporter().variableTypeCannotBeVoid(this);
148 if (tb.isArrayType() && ((ArrayBinding) tb).leafComponentType == VoidBinding) {
149 scope.problemReporter().variableTypeCannotBeVoidArray(this);
155 if ((binding = scope.duplicateName(name)) != null) {
156 // the name already exists... may carry on with the first binding...
157 scope.problemReporter().redefineLocal(this);
159 binding = new LocalVariableBinding(this, tb, modifiers, false);
160 scope.addLocalVariable(binding);
161 binding.constant = NotAConstant;
162 // allow to recursivelly target the binding....
163 // the correct constant is harmed if correctly computed at the end of this method
167 if (initialization != null)
168 initialization.resolveType(scope); // want to report all possible errors
172 // store the constant for final locals
173 if (initialization != null) {
174 if (initialization instanceof ArrayInitializer) {
175 TypeBinding initTb = initialization.resolveTypeExpecting(scope, tb);
176 if (initTb != null) {
177 ((ArrayInitializer) initialization).binding = (ArrayBinding) initTb;
178 initialization.implicitWidening(tb, initTb);
181 TypeBinding initTb = initialization.resolveType(scope);
182 if (initTb != null) {
183 if (initialization.isConstantValueOfTypeAssignableToType(initTb, tb)
184 || (tb.isBaseType() && BaseTypeBinding.isWidening(tb.id, initTb.id))
185 || BlockScope.areTypesCompatible(initTb, tb))
186 initialization.implicitWidening(tb, initTb);
188 scope.problemReporter().typeMismatchError(initTb, tb, this);
192 // change the constant in the binding when it is final
193 // (the optimization of the constant propagation will be done later on)
194 // cast from constant actual type to variable type
197 ? initialization.constant.castTo((tb.id << 4) + initialization.constant.typeID())
202 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
204 if (visitor.visit(this, scope)) {
205 type.traverse(visitor, scope);
206 if (initialization != null)
207 initialization.traverse(visitor, scope);
209 visitor.endVisit(this, scope);