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,
31 FlowContext flowContext, FlowInfo flowInfo) {
32 if (expressions != null) {
33 for (int i = 0, max = expressions.length; i < max; i++) {
34 flowInfo = expressions[i].analyseCode(currentScope,
35 flowContext, flowInfo).unconditionalInits();
42 * Code generation for a array initializer
44 // public void generateCode(BlockScope currentScope, CodeStream codeStream,
45 // boolean valueRequired) {
46 // // Flatten the values and compute the dimensions, by iterating in depth
47 // into nested array initializers
49 // int pc = codeStream.position;
50 // int expressionLength = (expressions == null) ? 0: expressions.length;
51 // codeStream.generateInlinedValue(expressionLength);
52 // codeStream.newArray(currentScope, binding);
53 // if (expressions != null) {
54 // // binding is an ArrayType, so I can just deal with the dimension
55 // int elementsTypeID = binding.dimensions > 1 ? -1 :
56 // binding.leafComponentType.id;
57 // for (int i = 0; i < expressionLength; i++) {
59 // if ((expr = expressions[i]).constant != NotAConstant) {
60 // switch (elementsTypeID) { // filter out initializations to default values
66 // if (expr.constant.longValue() != 0) {
68 // codeStream.generateInlinedValue(i);
69 // expr.generateCode(currentScope, codeStream, true);
70 // codeStream.arrayAtPut(elementsTypeID, false);
75 // double constantValue = expr.constant.doubleValue();
76 // if (constantValue == -0.0 || constantValue != 0) {
78 // codeStream.generateInlinedValue(i);
79 // expr.generateCode(currentScope, codeStream, true);
80 // codeStream.arrayAtPut(elementsTypeID, false);
84 // if (expr.constant.booleanValue() != false) {
86 // codeStream.generateInlinedValue(i);
87 // expr.generateCode(currentScope, codeStream, true);
88 // codeStream.arrayAtPut(elementsTypeID, false);
92 // if (!(expr instanceof NullLiteral)) {
94 // codeStream.generateInlinedValue(i);
95 // expr.generateCode(currentScope, codeStream, true);
96 // codeStream.arrayAtPut(elementsTypeID, false);
99 // } else if (!(expr instanceof NullLiteral)) {
101 // codeStream.generateInlinedValue(i);
102 // expr.generateCode(currentScope, codeStream, true);
103 // codeStream.arrayAtPut(elementsTypeID, false);
107 // if (!valueRequired) {
110 // codeStream.recordPositionsFrom(pc, this.sourceStart);
112 public StringBuffer printExpression(int indent, StringBuffer output) {
115 if (expressions != null) {
117 for (int i = 0; i < expressions.length; i++) {
119 output.append(", "); //$NON-NLS-1$
120 expressions[i].printExpression(0, output);
124 printIndent(indent + 1, output);
129 return output.append('}');
132 public TypeBinding resolveTypeExpecting(BlockScope scope,
133 TypeBinding expectedTb) {
134 // Array initializers can only occur on the right hand side of an
136 // expression, therefore the expected type contains the valid
138 // concerning the type that must be enforced by the elements of the
139 // array initializer.
141 // this method is recursive... (the test on isArrayType is the stop
144 constant = NotAConstant;
145 if (expectedTb.isArrayType()) {
146 binding = (ArrayBinding) expectedTb;
147 if (expressions == null)
149 TypeBinding expectedElementsTb = binding.elementsType(scope);
150 if (expectedElementsTb.isBaseType()) {
151 for (int i = 0, length = expressions.length; i < length; i++) {
152 Expression expression = expressions[i];
153 TypeBinding expressionTb = (expression instanceof ArrayInitializer) ? expression
154 .resolveTypeExpecting(scope, expectedElementsTb)
155 : expression.resolveType(scope);
156 if (expressionTb == null)
159 // Compile-time conversion required?
160 if (expression.isConstantValueOfTypeAssignableToType(
161 expressionTb, expectedElementsTb)) {
162 expression.implicitWidening(expectedElementsTb,
164 } else if (BaseTypeBinding.isWidening(
165 expectedElementsTb.id, expressionTb.id)) {
166 expression.implicitWidening(expectedElementsTb,
169 scope.problemReporter()
170 .typeMismatchErrorActualTypeExpectedType(
171 expression, expressionTb,
177 for (int i = 0, length = expressions.length; i < length; i++)
178 if (expressions[i].resolveTypeExpecting(scope,
179 expectedElementsTb) == null)
185 // infer initializer type for error reporting based on first element
186 TypeBinding leafElementType = null;
188 if (expressions == null) {
189 leafElementType = scope.getJavaLangObject();
191 Expression currentExpression = expressions[0];
192 while (currentExpression != null
193 && currentExpression instanceof ArrayInitializer) {
195 Expression[] subExprs = ((ArrayInitializer) currentExpression).expressions;
196 if (subExprs == null) {
197 leafElementType = scope.getJavaLangObject();
198 currentExpression = null;
201 currentExpression = ((ArrayInitializer) currentExpression).expressions[0];
203 if (currentExpression != null) {
204 leafElementType = currentExpression.resolveType(scope);
207 if (leafElementType != null) {
208 TypeBinding probableTb = scope.createArray(leafElementType, dim);
209 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
210 this, probableTb, expectedTb);
215 public String toStringExpression() {
217 String s = "{"; //$NON-NLS-1$
218 if (expressions != null) {
220 for (int i = 0; i < expressions.length; i++) {
221 s = s + expressions[i].toStringExpression() + ","; //$NON-NLS-1$
224 s = s + "\n ";j = 20;}}}; //$NON-NLS-1$
225 s = s + "}"; //$NON-NLS-1$
229 public void traverse(ASTVisitor visitor, BlockScope scope) {
230 if (visitor.visit(this, scope)) {
231 if (expressions != null) {
232 int expressionsLength = expressions.length;
233 for (int i = 0; i < expressionsLength; i++)
234 expressions[i].traverse(visitor, scope);
237 visitor.endVisit(this, scope);