added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / ArrayInitializer.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.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17
18 public class ArrayInitializer extends Expression {
19                 
20         public Expression[] expressions;
21         public ArrayBinding binding; //the type of the { , , , }
22         
23         /**
24          * ArrayInitializer constructor comment.
25          */
26         public ArrayInitializer() {
27
28                 super();
29         }
30
31         public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
32
33                 if (expressions != null) {
34                         for (int i = 0, max = expressions.length; i < max; i++) {
35                                 flowInfo = expressions[i].analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
36                         }
37                 }
38                 return flowInfo;
39         }
40
41         /**
42          * Code generation for a array initializer
43          */
44         public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
45
46                 // Flatten the values and compute the dimensions, by iterating in depth into nested array initializers
47                 int pc = codeStream.position;
48                 int expressionLength = (expressions == null) ? 0: expressions.length;
49                 codeStream.generateInlinedValue(expressionLength);
50                 codeStream.newArray(binding);
51                 if (expressions != null) {
52                         // binding is an ArrayType, so I can just deal with the dimension
53                         int elementsTypeID = binding.dimensions > 1 ? -1 : binding.leafComponentType.id;
54                         for (int i = 0; i < expressionLength; i++) {
55                                 Expression expr;
56                                 if ((expr = expressions[i]).constant != NotAConstant) {
57                                         switch (elementsTypeID) { // filter out initializations to default values
58                                                 case T_int :
59                                                 case T_short :
60                                                 case T_byte :
61                                                 case T_char :
62                                                 case T_long :
63                                                         if (expr.constant.longValue() != 0) {
64                                                                 codeStream.dup();
65                                                                 codeStream.generateInlinedValue(i);
66                                                                 expr.generateCode(currentScope, codeStream, true);
67                                                                 codeStream.arrayAtPut(elementsTypeID, false);
68                                                         }
69                                                         break;
70                                                 case T_float :
71                                                 case T_double :
72                                                         double constantValue = expr.constant.doubleValue();
73                                                         if (constantValue == -0.0 || constantValue != 0) {
74                                                                 codeStream.dup();
75                                                                 codeStream.generateInlinedValue(i);
76                                                                 expr.generateCode(currentScope, codeStream, true);
77                                                                 codeStream.arrayAtPut(elementsTypeID, false);
78                                                         }
79                                                         break;
80                                                 case T_boolean :
81                                                         if (expr.constant.booleanValue() != false) {
82                                                                 codeStream.dup();
83                                                                 codeStream.generateInlinedValue(i);
84                                                                 expr.generateCode(currentScope, codeStream, true);
85                                                                 codeStream.arrayAtPut(elementsTypeID, false);
86                                                         }
87                                                         break;
88                                                 default :
89                                                         if (!(expr instanceof NullLiteral)) {
90                                                                 codeStream.dup();
91                                                                 codeStream.generateInlinedValue(i);
92                                                                 expr.generateCode(currentScope, codeStream, true);
93                                                                 codeStream.arrayAtPut(elementsTypeID, false);
94                                                         }
95                                         }
96                                 } else if (!(expr instanceof NullLiteral)) {
97                                         codeStream.dup();
98                                         codeStream.generateInlinedValue(i);
99                                         expr.generateCode(currentScope, codeStream, true);
100                                         codeStream.arrayAtPut(elementsTypeID, false);
101                                 }
102                         }
103                 }
104                 if (valueRequired) {
105                         codeStream.generateImplicitConversion(this.implicitConversion);
106                 } else {
107                         codeStream.pop();
108                 }
109                 codeStream.recordPositionsFrom(pc, this.sourceStart);
110         }
111
112         public StringBuffer printExpression(int indent, StringBuffer output) {
113         
114                 output.append('{');
115                 if (expressions != null) {      
116                         int j = 20 ; 
117                         for (int i = 0 ; i < expressions.length ; i++) {        
118                                 if (i > 0) output.append(", "); //$NON-NLS-1$
119                                 expressions[i].printExpression(0, output);
120                                 j -- ;
121                                 if (j == 0) {
122                                         output.append('\n');
123                                         printIndent(indent+1, output);
124                                         j = 20;
125                                 }
126                         }
127                 }
128                 return output.append('}');
129         }
130
131         public TypeBinding resolveTypeExpecting(BlockScope scope, TypeBinding expectedTb) {
132                 // Array initializers can only occur on the right hand side of an assignment
133                 // expression, therefore the expected type contains the valid information
134                 // concerning the type that must be enforced by the elements of the array initializer.
135         
136                 // this method is recursive... (the test on isArrayType is the stop case)
137         
138                 this.constant = NotAConstant;
139                 
140                 // allow new List<?>[5]
141                 TypeBinding leafComponentType = expectedTb.leafComponentType();
142                 if (leafComponentType.isBoundParameterizedType() || leafComponentType.isGenericType() || leafComponentType.isTypeVariable()) {
143                     scope.problemReporter().illegalGenericArray(leafComponentType, this);
144                 }
145                         
146                 if (expectedTb.isArrayType()) {
147                         binding = (ArrayBinding) expectedTb;
148                         if (expressions == null)
149                                 return binding;
150                         TypeBinding expectedElementsTb = binding.elementsType();
151                         if (expectedElementsTb.isBaseType()) {
152                                 for (int i = 0, length = expressions.length; i < length; i++) {
153                                         Expression expression = expressions[i];
154                                         TypeBinding expressionTb =
155                                                 (expression instanceof ArrayInitializer)
156                                                         ? expression.resolveTypeExpecting(scope, expectedElementsTb)
157                                                         : expression.resolveType(scope);
158                                         if (expressionTb == null)
159                                                 return null;
160         
161                                         // Compile-time conversion required?
162                                         if (expectedElementsTb != expressionTb) // must call before computeConversion() and typeMismatchError()
163                                                 scope.compilationUnitScope().recordTypeConversion(expectedElementsTb, expressionTb);
164                                         if (expression.isConstantValueOfTypeAssignableToType(expressionTb, expectedElementsTb)
165                                                 || BaseTypeBinding.isWidening(expectedElementsTb.id, expressionTb.id)
166                                                 || scope.isBoxingCompatibleWith(expressionTb, expectedElementsTb)) {
167                                                         expression.computeConversion(scope, expectedElementsTb, expressionTb);
168                                         } else {
169                                                 scope.problemReporter().typeMismatchError(expressionTb, expectedElementsTb, expression);
170                                                 return null;
171                                         }
172                                 }
173                         } else {
174                                 for (int i = 0, length = expressions.length; i < length; i++)
175                                         if (expressions[i].resolveTypeExpecting(scope, expectedElementsTb) == null)
176                                                 return null;
177                         }
178                         return binding;
179                 }
180                 
181                 // infer initializer type for error reporting based on first element
182                 TypeBinding leafElementType = null;
183                 int dim = 1;
184                 if (expressions == null) {
185                         leafElementType = scope.getJavaLangObject();
186                 } else {
187                         Expression currentExpression = expressions[0];
188                         while(currentExpression != null && currentExpression instanceof ArrayInitializer) {
189                                 dim++;
190                                 Expression[] subExprs = ((ArrayInitializer) currentExpression).expressions;
191                                 if (subExprs == null){
192                                         leafElementType = scope.getJavaLangObject();
193                                         currentExpression = null;
194                                         break;
195                                 }
196                                 currentExpression = ((ArrayInitializer) currentExpression).expressions[0];
197                         }
198                         if (currentExpression != null) {
199                                 leafElementType = currentExpression.resolveType(scope);
200                         }
201                 }
202                 if (leafElementType != null) {
203                         TypeBinding probableTb = scope.createArrayType(leafElementType, dim);
204                         scope.problemReporter().typeMismatchError(probableTb, expectedTb, this);
205                 }
206                 return null;
207         }
208         
209         public void traverse(ASTVisitor visitor, BlockScope scope) {
210
211                 if (visitor.visit(this, scope)) {
212                         if (expressions != null) {
213                                 int expressionsLength = expressions.length;
214                                 for (int i = 0; i < expressionsLength; i++)
215                                         expressions[i].traverse(visitor, scope);
216                         }
217                 }
218                 visitor.endVisit(this, scope);
219         }
220 }