1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.impl.*;
15 import net.sourceforge.phpdt.internal.compiler.codegen.*;
16 import net.sourceforge.phpdt.internal.compiler.flow.*;
17 import net.sourceforge.phpdt.internal.compiler.lookup.*;
19 public class ArrayAllocationExpression extends Expression {
21 public TypeReference type;
23 //dimensions.length gives the number of dimensions, but the
24 // last ones may be nulled as in new int[4][5][][]
25 public Expression[] dimensions;
26 public ArrayInitializer initializer;
28 public ArrayBinding arrayTb;
31 * ArrayAllocationExpression constructor comment.
33 public ArrayAllocationExpression() {
37 public FlowInfo analyseCode(
38 BlockScope currentScope,
39 FlowContext flowContext,
41 for (int i = 0, max = dimensions.length; i < max; i++) {
43 if ((dim = dimensions[i]) != null) {
44 flowInfo = dim.analyseCode(currentScope, flowContext, flowInfo);
47 if (initializer != null) {
48 return initializer.analyseCode(currentScope, flowContext, flowInfo);
55 * Code generation for a array allocation expression
57 public void generateCode(
58 BlockScope currentScope,
59 CodeStream codeStream,
60 boolean valueRequired) {
62 int pc = codeStream.position;
63 ArrayBinding arrayBinding;
65 if (initializer != null) {
66 initializer.generateCode(currentScope, codeStream, valueRequired);
70 int nonNullDimensionsLength = 0;
71 for (int i = 0, max = dimensions.length; i < max; i++)
72 if (dimensions[i] != null) {
73 dimensions[i].generateCode(currentScope, codeStream, true);
74 nonNullDimensionsLength++;
77 // Generate a sequence of bytecodes corresponding to an array allocation
78 if ((arrayTb.isArrayType())
79 && ((arrayBinding = (ArrayBinding) arrayTb).dimensions == 1)) {
80 // Mono-dimensional array
81 codeStream.newArray(currentScope, arrayBinding);
83 // Multi-dimensional array
84 codeStream.multianewarray(arrayTb, nonNullDimensionsLength);
88 codeStream.generateImplicitConversion(implicitConversion);
93 codeStream.recordPositionsFrom(pc, this.sourceStart);
96 public TypeBinding resolveType(BlockScope scope) {
98 // Build an array type reference using the current dimensions
99 // The parser does not check for the fact that dimension may be null
100 // only at the -end- like new int [4][][]. The parser allows new int[][4][]
101 // so this must be checked here......(this comes from a reduction to LL1 grammar)
103 TypeBinding referenceTb = type.resolveType(scope);
104 // will check for null after dimensions are checked
105 constant = Constant.NotAConstant;
106 if (referenceTb == VoidBinding) {
107 scope.problemReporter().cannotAllocateVoidArray(this);
108 referenceTb = null; // will return below
111 // check the validity of the dimension syntax (and test for all null dimensions)
113 for (int i = dimensions.length; --i >= 0;) {
114 if (dimensions[i] != null) {
119 // should not have an empty dimension before an non-empty one
120 scope.problemReporter().incorrectLocationForEmptyDimension(this, i);
124 if (referenceTb == null)
127 // lengthDim == -1 says if all dimensions are nulled
128 // when an initializer is given, no dimension must be specified
129 if (initializer == null) {
130 if (lengthDim == -1) {
131 scope.problemReporter().mustDefineDimensionsOrInitializer(this);
134 } else if (lengthDim != -1) {
135 scope.problemReporter().cannotDefineDimensionsAndInitializer(this);
139 // dimensions resolution
140 for (int i = 0; i <= lengthDim; i++) {
141 TypeBinding dimTb = dimensions[i].resolveTypeExpecting(scope, IntBinding);
144 dimensions[i].implicitWidening(IntBinding, dimTb);
147 // building the array binding
148 arrayTb = scope.createArray(referenceTb, dimensions.length);
150 // check the initializer
151 if (initializer != null)
152 if ((initializer.resolveTypeExpecting(scope, arrayTb)) != null)
153 initializer.binding = arrayTb;
157 public String toStringExpression() {
159 String s = "new " + type.toString(0); //$NON-NLS-1$
160 for (int i = 0; i < dimensions.length; i++) {
161 if (dimensions[i] == null)
162 s = s + "[]"; //$NON-NLS-1$
164 s = s + "[" + dimensions[i].toStringExpression() + "]"; //$NON-NLS-2$ //$NON-NLS-1$
166 if (initializer != null)
167 s = s + initializer.toStringExpression();
171 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
173 if (visitor.visit(this, scope)) {
174 int dimensionsLength = dimensions.length;
175 type.traverse(visitor, scope);
176 for (int i = 0; i < dimensionsLength; i++) {
177 if (dimensions[i] != null)
178 dimensions[i].traverse(visitor, scope);
180 if (initializer != null)
181 initializer.traverse(visitor, scope);
183 visitor.endVisit(this, scope);