added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / LocalDeclaration.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12
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.*;
18
19 public class LocalDeclaration extends AbstractVariableDeclaration {
20
21         public LocalVariableBinding binding;
22         
23         public LocalDeclaration(
24                 char[] name,
25                 int sourceStart,
26                 int sourceEnd) {
27
28                 this.name = name;
29                 this.sourceStart = sourceStart;
30                 this.sourceEnd = sourceEnd;
31                 this.declarationEnd = sourceEnd;
32         }
33
34         public FlowInfo analyseCode(
35                 BlockScope currentScope,
36                 FlowContext flowContext,
37                 FlowInfo flowInfo) {
38
39                 // record variable initialization if any
40                 if (flowInfo.isReachable()) {
41                         bits |= IsLocalDeclarationReachableMASK; // only set if actually reached
42                 }
43                 if (this.initialization == null) 
44                         return flowInfo;
45                         
46                 int nullStatus = this.initialization.nullStatus(flowInfo);
47                 flowInfo =
48                         this.initialization
49                                 .analyseCode(currentScope, flowContext, flowInfo)
50                                 .unconditionalInits();
51                 
52                 flowInfo.markAsDefinitelyAssigned(binding);
53                 switch(nullStatus) {
54                         case FlowInfo.NULL :
55                                 flowInfo.markAsDefinitelyNull(this.binding);
56                                 break;
57                         case FlowInfo.NON_NULL :
58                                 flowInfo.markAsDefinitelyNonNull(this.binding);
59                                 break;
60                 }
61                 return flowInfo;
62         }
63
64         public void checkModifiers() {
65
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"
71
72                         modifiers = (modifiers & ~AccAlternateModifierProblem) | AccModifierProblem;
73         }
74
75         /**
76          * Code generation for a local declaration:
77          *      i.e.&nbsp;normal assignment to a local variable + unused variable handling 
78          */
79         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
80
81                 // even if not reachable, variable must be added to visible if allocated (28298)
82                 if (binding.resolvedPosition != -1) {
83                         codeStream.addVisibleLocalVariable(binding);
84                 }
85                 if ((bits & IsReachableMASK) == 0) {
86                         return;
87                 }
88                 int pc = codeStream.position;
89                 Constant inlinedValue;
90
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            
104                                 }
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); 
115                                         }                                       
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);
120                                                 */
121                                                 binding.recordInitializationStartPC(codeStream.position);
122                                                 //                                      codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
123                                                 //                                      codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index 
124                                         }
125                                 } else {
126                                         if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
127                                                 codeStream.pop2();
128                                         } else {
129                                                 codeStream.pop();
130                                         }
131                                 }
132                         }
133                 }
134                 codeStream.recordPositionsFrom(pc, this.sourceStart);
135         }
136
137         /**
138          * @see org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration#getKind()
139          */
140         public int getKind() {
141                 return LOCAL_VARIABLE;
142         }
143         
144         public void resolve(BlockScope scope) {
145
146                 // create a binding and add it to the scope
147                 TypeBinding variableType = type.resolveType(scope, true /* check bounds*/);
148
149                 checkModifiers();
150                 if (variableType != null) {
151                         if (variableType == VoidBinding) {
152                                 scope.problemReporter().variableTypeCannotBeVoid(this);
153                                 return;
154                         }
155                         if (variableType.isArrayType() && ((ArrayBinding) variableType).leafComponentType == VoidBinding) {
156                                 scope.problemReporter().variableTypeCannotBeVoidArray(this);
157                                 return;
158                         }
159                 }
160                 
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);
167                         } else {
168                                 scope.problemReporter().localVariableHiding(this, existingVariable, false);
169                         }
170                 }
171                                 
172                 if (shouldInsertInScope) {
173                         if ((modifiers & AccFinal)!= 0 && this.initialization == null) {
174                                 modifiers |= AccBlankFinal;
175                         }
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
181                         
182                         resolveAnnotations(scope, this.annotations, this.binding);
183                 }
184
185                 if (variableType == null) {
186                         if (initialization != null)
187                                 initialization.resolveType(scope); // want to report all possible errors
188                         return;
189                 }
190
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);
198                                 }
199                         } else {
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);
211                                                 }                                               
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);
219                                         } else {
220                                                 scope.problemReporter().typeMismatchError(initializationType, variableType, this);
221                                         }
222                                 }
223                         }
224
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) {
229                                 binding.setConstant(
230                                         binding.isFinal()
231                                                 ? initialization.constant.castTo((variableType.id << 4) + initialization.constant.typeID())
232                                                 : NotAConstant);
233                         }
234                 }
235         }
236
237         public void traverse(ASTVisitor visitor, BlockScope scope) {
238
239                 if (visitor.visit(this, scope)) {
240                         type.traverse(visitor, scope);
241                         if (initialization != null)
242                                 initialization.traverse(visitor, scope);
243                 }
244                 visitor.endVisit(this, scope);
245         }
246 }