1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - initial API and implementation
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.ast;
10 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
11 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
12 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
13 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
14 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypeBinding;
15 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
16 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
18 public class ArrayInitializer extends Expression {
19 public Expression[] expressions;
21 public ArrayBinding binding; //the type of the { , , , }
24 * ArrayInitializer constructor comment.
26 public ArrayInitializer() {
30 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
31 if (expressions != null) {
32 for (int i = 0, max = expressions.length; i < max; i++) {
33 flowInfo = expressions[i].analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
40 * Code generation for a array initializer
42 //public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
43 // // Flatten the values and compute the dimensions, by iterating in depth into nested array initializers
45 // int pc = codeStream.position;
46 // int expressionLength = (expressions == null) ? 0: expressions.length;
47 // codeStream.generateInlinedValue(expressionLength);
48 // codeStream.newArray(currentScope, binding);
49 // if (expressions != null) {
50 // // binding is an ArrayType, so I can just deal with the dimension
51 // int elementsTypeID = binding.dimensions > 1 ? -1 : binding.leafComponentType.id;
52 // for (int i = 0; i < expressionLength; i++) {
54 // if ((expr = expressions[i]).constant != NotAConstant) {
55 // switch (elementsTypeID) { // filter out initializations to default values
61 // if (expr.constant.longValue() != 0) {
63 // codeStream.generateInlinedValue(i);
64 // expr.generateCode(currentScope, codeStream, true);
65 // codeStream.arrayAtPut(elementsTypeID, false);
70 // double constantValue = expr.constant.doubleValue();
71 // if (constantValue == -0.0 || constantValue != 0) {
73 // codeStream.generateInlinedValue(i);
74 // expr.generateCode(currentScope, codeStream, true);
75 // codeStream.arrayAtPut(elementsTypeID, false);
79 // if (expr.constant.booleanValue() != false) {
81 // codeStream.generateInlinedValue(i);
82 // expr.generateCode(currentScope, codeStream, true);
83 // codeStream.arrayAtPut(elementsTypeID, false);
87 // if (!(expr instanceof NullLiteral)) {
89 // codeStream.generateInlinedValue(i);
90 // expr.generateCode(currentScope, codeStream, true);
91 // codeStream.arrayAtPut(elementsTypeID, false);
94 // } else if (!(expr instanceof NullLiteral)) {
96 // codeStream.generateInlinedValue(i);
97 // expr.generateCode(currentScope, codeStream, true);
98 // codeStream.arrayAtPut(elementsTypeID, false);
102 // if (!valueRequired) {
105 // codeStream.recordPositionsFrom(pc, this.sourceStart);
107 public StringBuffer printExpression(int indent, StringBuffer output) {
110 if (expressions != null) {
112 for (int i = 0; i < expressions.length; i++) {
114 output.append(", "); //$NON-NLS-1$
115 expressions[i].printExpression(0, output);
119 printIndent(indent + 1, output);
124 return output.append('}');
127 public TypeBinding resolveTypeExpecting(BlockScope scope, TypeBinding expectedTb) {
128 // Array initializers can only occur on the right hand side of an assignment
129 // expression, therefore the expected type contains the valid information
130 // concerning the type that must be enforced by the elements of the array initializer.
132 // this method is recursive... (the test on isArrayType is the stop case)
134 constant = NotAConstant;
135 if (expectedTb.isArrayType()) {
136 binding = (ArrayBinding) expectedTb;
137 if (expressions == null)
139 TypeBinding expectedElementsTb = binding.elementsType(scope);
140 if (expectedElementsTb.isBaseType()) {
141 for (int i = 0, length = expressions.length; i < length; i++) {
142 Expression expression = expressions[i];
143 TypeBinding expressionTb = (expression instanceof ArrayInitializer) ? expression.resolveTypeExpecting(scope,
144 expectedElementsTb) : expression.resolveType(scope);
145 if (expressionTb == null)
148 // Compile-time conversion required?
149 if (expression.isConstantValueOfTypeAssignableToType(expressionTb, expectedElementsTb)) {
150 expression.implicitWidening(expectedElementsTb, expressionTb);
151 } else if (BaseTypeBinding.isWidening(expectedElementsTb.id, expressionTb.id)) {
152 expression.implicitWidening(expectedElementsTb, expressionTb);
154 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionTb, expectedElementsTb);
159 for (int i = 0, length = expressions.length; i < length; i++)
160 if (expressions[i].resolveTypeExpecting(scope, expectedElementsTb) == null)
166 // infer initializer type for error reporting based on first element
167 TypeBinding leafElementType = null;
169 if (expressions == null) {
170 leafElementType = scope.getJavaLangObject();
172 Expression currentExpression = expressions[0];
173 while (currentExpression != null && currentExpression instanceof ArrayInitializer) {
175 Expression[] subExprs = ((ArrayInitializer) currentExpression).expressions;
176 if (subExprs == null) {
177 leafElementType = scope.getJavaLangObject();
178 currentExpression = null;
181 currentExpression = ((ArrayInitializer) currentExpression).expressions[0];
183 if (currentExpression != null) {
184 leafElementType = currentExpression.resolveType(scope);
187 if (leafElementType != null) {
188 TypeBinding probableTb = scope.createArray(leafElementType, dim);
189 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(this, probableTb, expectedTb);
194 public String toStringExpression() {
196 String s = "{"; //$NON-NLS-1$
197 if (expressions != null) {
199 for (int i = 0; i < expressions.length; i++) {
200 s = s + expressions[i].toStringExpression() + ","; //$NON-NLS-1$
203 s = s + "\n ";j = 20;}}}; //$NON-NLS-1$
204 s = s + "}"; //$NON-NLS-1$
208 public void traverse(ASTVisitor visitor, BlockScope scope) {
209 if (visitor.visit(this, scope)) {
210 if (expressions != null) {
211 int expressionsLength = expressions.length;
212 for (int i = 0; i < expressionsLength; i++)
213 expressions[i].traverse(visitor, scope);
216 visitor.endVisit(this, scope);