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.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
18 public class ArrayInitializer extends Expression {
20 public Expression[] expressions;
21 public ArrayBinding binding; //the type of the { , , , }
24 * ArrayInitializer constructor comment.
26 public ArrayInitializer() {
31 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
33 if (expressions != null) {
34 for (int i = 0, max = expressions.length; i < max; i++) {
35 flowInfo = expressions[i].analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
42 * Code generation for a array initializer
44 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
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(currentScope, 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++) {
56 if ((expr = expressions[i]).constant != NotAConstant) {
57 switch (elementsTypeID) { // filter out initializations to default values
63 if (expr.constant.longValue() != 0) {
65 codeStream.generateInlinedValue(i);
66 expr.generateCode(currentScope, codeStream, true);
67 codeStream.arrayAtPut(elementsTypeID, false);
72 double constantValue = expr.constant.doubleValue();
73 if (constantValue == -0.0 || constantValue != 0) {
75 codeStream.generateInlinedValue(i);
76 expr.generateCode(currentScope, codeStream, true);
77 codeStream.arrayAtPut(elementsTypeID, false);
81 if (expr.constant.booleanValue() != false) {
83 codeStream.generateInlinedValue(i);
84 expr.generateCode(currentScope, codeStream, true);
85 codeStream.arrayAtPut(elementsTypeID, false);
89 if (!(expr instanceof NullLiteral)) {
91 codeStream.generateInlinedValue(i);
92 expr.generateCode(currentScope, codeStream, true);
93 codeStream.arrayAtPut(elementsTypeID, false);
96 } else if (!(expr instanceof NullLiteral)) {
98 codeStream.generateInlinedValue(i);
99 expr.generateCode(currentScope, codeStream, true);
100 codeStream.arrayAtPut(elementsTypeID, false);
104 if (!valueRequired) {
107 codeStream.recordPositionsFrom(pc, this.sourceStart);
110 public StringBuffer printExpression(int indent, StringBuffer output) {
113 if (expressions != null) {
115 for (int i = 0 ; i < expressions.length ; i++) {
116 if (i > 0) output.append(", "); //$NON-NLS-1$
117 expressions[i].printExpression(0, output);
121 printIndent(indent+1, output);
126 return output.append('}');
129 public TypeBinding resolveTypeExpecting(BlockScope scope, TypeBinding expectedTb) {
130 // Array initializers can only occur on the right hand side of an assignment
131 // expression, therefore the expected type contains the valid information
132 // concerning the type that must be enforced by the elements of the array initializer.
134 // this method is recursive... (the test on isArrayType is the stop case)
136 constant = NotAConstant;
137 if (expectedTb.isArrayType()) {
138 binding = (ArrayBinding) expectedTb;
139 if (expressions == null)
141 TypeBinding expectedElementsTb = binding.elementsType(scope);
142 if (expectedElementsTb.isBaseType()) {
143 for (int i = 0, length = expressions.length; i < length; i++) {
144 Expression expression = expressions[i];
145 TypeBinding expressionTb =
146 (expression instanceof ArrayInitializer)
147 ? expression.resolveTypeExpecting(scope, expectedElementsTb)
148 : expression.resolveType(scope);
149 if (expressionTb == null)
152 // Compile-time conversion required?
153 if (expression.isConstantValueOfTypeAssignableToType(expressionTb, expectedElementsTb)) {
154 expression.implicitWidening(expectedElementsTb, expressionTb);
155 } else if (BaseTypeBinding.isWidening(expectedElementsTb.id, expressionTb.id)) {
156 expression.implicitWidening(expectedElementsTb, expressionTb);
158 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionTb, expectedElementsTb);
163 for (int i = 0, length = expressions.length; i < length; i++)
164 if (expressions[i].resolveTypeExpecting(scope, expectedElementsTb) == null)
170 // infer initializer type for error reporting based on first element
171 TypeBinding leafElementType = null;
173 if (expressions == null) {
174 leafElementType = scope.getJavaLangObject();
176 Expression currentExpression = expressions[0];
177 while(currentExpression != null && currentExpression instanceof ArrayInitializer) {
179 Expression[] subExprs = ((ArrayInitializer) currentExpression).expressions;
180 if (subExprs == null){
181 leafElementType = scope.getJavaLangObject();
182 currentExpression = null;
185 currentExpression = ((ArrayInitializer) currentExpression).expressions[0];
187 if (currentExpression != null) {
188 leafElementType = currentExpression.resolveType(scope);
191 if (leafElementType != null) {
192 TypeBinding probableTb = scope.createArray(leafElementType, dim);
193 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(this, probableTb, expectedTb);
198 public void traverse(ASTVisitor visitor, BlockScope scope) {
200 if (visitor.visit(this, scope)) {
201 if (expressions != null) {
202 int expressionsLength = expressions.length;
203 for (int i = 0; i < expressionsLength; i++)
204 expressions[i].traverse(visitor, scope);
207 visitor.endVisit(this, scope);