1) Fixed issue #872.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ArrayInitializer.java
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
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.ast;
9
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;
17
18 public class ArrayInitializer extends Expression {
19         public Expression[] expressions;
20
21         public ArrayBinding binding; // the type of the { , , , }
22
23         /**
24          * ArrayInitializer constructor comment.
25          */
26         public ArrayInitializer() {
27                 super();
28         }
29
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();
36                         }
37                 }
38                 return flowInfo;
39         }
40
41         /**
42          * Code generation for a array initializer
43          */
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
48         //
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++) {
58         // Expression expr;
59         // if ((expr = expressions[i]).constant != NotAConstant) {
60         // switch (elementsTypeID) { // filter out initializations to default values
61         // case T_int :
62         // case T_short :
63         // case T_byte :
64         // case T_char :
65         // case T_long :
66         // if (expr.constant.longValue() != 0) {
67         // codeStream.dup();
68         // codeStream.generateInlinedValue(i);
69         // expr.generateCode(currentScope, codeStream, true);
70         // codeStream.arrayAtPut(elementsTypeID, false);
71         // }
72         // break;
73         // case T_float :
74         // case T_double :
75         // double constantValue = expr.constant.doubleValue();
76         // if (constantValue == -0.0 || constantValue != 0) {
77         // codeStream.dup();
78         // codeStream.generateInlinedValue(i);
79         // expr.generateCode(currentScope, codeStream, true);
80         // codeStream.arrayAtPut(elementsTypeID, false);
81         // }
82         // break;
83         // case T_boolean :
84         // if (expr.constant.booleanValue() != false) {
85         // codeStream.dup();
86         // codeStream.generateInlinedValue(i);
87         // expr.generateCode(currentScope, codeStream, true);
88         // codeStream.arrayAtPut(elementsTypeID, false);
89         // }
90         // break;
91         // default :
92         // if (!(expr instanceof NullLiteral)) {
93         // codeStream.dup();
94         // codeStream.generateInlinedValue(i);
95         // expr.generateCode(currentScope, codeStream, true);
96         // codeStream.arrayAtPut(elementsTypeID, false);
97         // }
98         // }
99         // } else if (!(expr instanceof NullLiteral)) {
100         // codeStream.dup();
101         // codeStream.generateInlinedValue(i);
102         // expr.generateCode(currentScope, codeStream, true);
103         // codeStream.arrayAtPut(elementsTypeID, false);
104         // }
105         // }
106         // }
107         // if (!valueRequired) {
108         // codeStream.pop();
109         // }
110         // codeStream.recordPositionsFrom(pc, this.sourceStart);
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)
119                                         output.append(", "); //$NON-NLS-1$
120                                 expressions[i].printExpression(0, output);
121                                 j--;
122                                 if (j == 0) {
123                                         output.append('\n');
124                                         printIndent(indent + 1, output);
125                                         j = 20;
126                                 }
127                         }
128                 }
129                 return output.append('}');
130         }
131
132         public TypeBinding resolveTypeExpecting(BlockScope scope,
133                         TypeBinding expectedTb) {
134                 // Array initializers can only occur on the right hand side of an
135                 // assignment
136                 // expression, therefore the expected type contains the valid
137                 // information
138                 // concerning the type that must be enforced by the elements of the
139                 // array initializer.
140
141                 // this method is recursive... (the test on isArrayType is the stop
142                 // case)
143
144                 constant = NotAConstant;
145                 if (expectedTb.isArrayType()) {
146                         binding = (ArrayBinding) expectedTb;
147                         if (expressions == null)
148                                 return binding;
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)
157                                                 return null;
158
159                                         // Compile-time conversion required?
160                                         if (expression.isConstantValueOfTypeAssignableToType(
161                                                         expressionTb, expectedElementsTb)) {
162                                                 expression.implicitWidening(expectedElementsTb,
163                                                                 expressionTb);
164                                         } else if (BaseTypeBinding.isWidening(
165                                                         expectedElementsTb.id, expressionTb.id)) {
166                                                 expression.implicitWidening(expectedElementsTb,
167                                                                 expressionTb);
168                                         } else {
169                                                 scope.problemReporter()
170                                                                 .typeMismatchErrorActualTypeExpectedType(
171                                                                                 expression, expressionTb,
172                                                                                 expectedElementsTb);
173                                                 return null;
174                                         }
175                                 }
176                         } else {
177                                 for (int i = 0, length = expressions.length; i < length; i++)
178                                         if (expressions[i].resolveTypeExpecting(scope,
179                                                         expectedElementsTb) == null)
180                                                 return null;
181                         }
182                         return binding;
183                 }
184
185                 // infer initializer type for error reporting based on first element
186                 TypeBinding leafElementType = null;
187                 int dim = 1;
188                 if (expressions == null) {
189                         leafElementType = scope.getJavaLangObject();
190                 } else {
191                         Expression currentExpression = expressions[0];
192                         while (currentExpression != null
193                                         && currentExpression instanceof ArrayInitializer) {
194                                 dim++;
195                                 Expression[] subExprs = ((ArrayInitializer) currentExpression).expressions;
196                                 if (subExprs == null) {
197                                         leafElementType = scope.getJavaLangObject();
198                                         currentExpression = null;
199                                         break;
200                                 }
201                                 currentExpression = ((ArrayInitializer) currentExpression).expressions[0];
202                         }
203                         if (currentExpression != null) {
204                                 leafElementType = currentExpression.resolveType(scope);
205                         }
206                 }
207                 if (leafElementType != null) {
208                         TypeBinding probableTb = scope.createArray(leafElementType, dim);
209                         scope.problemReporter().typeMismatchErrorActualTypeExpectedType(
210                                         this, probableTb, expectedTb);
211                 }
212                 return null;
213         }
214
215         public String toStringExpression() {
216
217                 String s = "{"; //$NON-NLS-1$
218                 if (expressions != null) {
219                         int j = 20;
220                         for (int i = 0; i < expressions.length; i++) {
221                                 s = s + expressions[i].toStringExpression() + ","; //$NON-NLS-1$
222                                 j--;
223                                 if (j == 0) {
224                                         s = s + "\n                ";j = 20;}}}; //$NON-NLS-1$
225                 s = s + "}"; //$NON-NLS-1$
226                 return s;
227         }
228
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);
235                         }
236                 }
237                 visitor.endVisit(this, scope);
238         }
239 }