Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / ArrayInitializer.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12
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.*;
17
18 public class ArrayInitializer extends Expression {
19                 
20         public Expression[] expressions;
21         public ArrayBinding binding; //the type of the { , , , }
22         
23         /**
24          * ArrayInitializer constructor comment.
25          */
26         public ArrayInitializer() {
27
28                 super();
29         }
30
31         public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
32
33                 if (expressions != null) {
34                         for (int i = 0, max = expressions.length; i < max; i++) {
35                                 flowInfo = expressions[i].analyseCode(currentScope, 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, boolean valueRequired) {
45
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++) {
55                                 Expression expr;
56                                 if ((expr = expressions[i]).constant != NotAConstant) {
57                                         switch (elementsTypeID) { // filter out initializations to default values
58                                                 case T_int :
59                                                 case T_short :
60                                                 case T_byte :
61                                                 case T_char :
62                                                 case T_long :
63                                                         if (expr.constant.longValue() != 0) {
64                                                                 codeStream.dup();
65                                                                 codeStream.generateInlinedValue(i);
66                                                                 expr.generateCode(currentScope, codeStream, true);
67                                                                 codeStream.arrayAtPut(elementsTypeID, false);
68                                                         }
69                                                         break;
70                                                 case T_float :
71                                                 case T_double :
72                                                         double constantValue = expr.constant.doubleValue();
73                                                         if (constantValue == -0.0 || constantValue != 0) {
74                                                                 codeStream.dup();
75                                                                 codeStream.generateInlinedValue(i);
76                                                                 expr.generateCode(currentScope, codeStream, true);
77                                                                 codeStream.arrayAtPut(elementsTypeID, false);
78                                                         }
79                                                         break;
80                                                 case T_boolean :
81                                                         if (expr.constant.booleanValue() != false) {
82                                                                 codeStream.dup();
83                                                                 codeStream.generateInlinedValue(i);
84                                                                 expr.generateCode(currentScope, codeStream, true);
85                                                                 codeStream.arrayAtPut(elementsTypeID, false);
86                                                         }
87                                                         break;
88                                                 default :
89                                                         if (!(expr instanceof NullLiteral)) {
90                                                                 codeStream.dup();
91                                                                 codeStream.generateInlinedValue(i);
92                                                                 expr.generateCode(currentScope, codeStream, true);
93                                                                 codeStream.arrayAtPut(elementsTypeID, false);
94                                                         }
95                                         }
96                                 } else if (!(expr instanceof NullLiteral)) {
97                                         codeStream.dup();
98                                         codeStream.generateInlinedValue(i);
99                                         expr.generateCode(currentScope, codeStream, true);
100                                         codeStream.arrayAtPut(elementsTypeID, false);
101                                 }
102                         }
103                 }
104                 if (!valueRequired) {
105                         codeStream.pop();
106                 }
107                 codeStream.recordPositionsFrom(pc, this.sourceStart);
108         }
109
110         public StringBuffer printExpression(int indent, StringBuffer output) {
111         
112                 output.append('{');
113                 if (expressions != null) {      
114                         int j = 20 ; 
115                         for (int i = 0 ; i < expressions.length ; i++) {        
116                                 if (i > 0) output.append(", "); //$NON-NLS-1$
117                                 expressions[i].printExpression(0, output);
118                                 j -- ;
119                                 if (j == 0) {
120                                         output.append('\n');
121                                         printIndent(indent+1, output);
122                                         j = 20;
123                                 }
124                         }
125                 }
126                 return output.append('}');
127         }
128
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.
133         
134                 // this method is recursive... (the test on isArrayType is the stop case)
135         
136                 constant = NotAConstant;
137                 if (expectedTb.isArrayType()) {
138                         binding = (ArrayBinding) expectedTb;
139                         if (expressions == null)
140                                 return binding;
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)
150                                                 return null;
151         
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);
157                                         } else {
158                                                 scope.problemReporter().typeMismatchErrorActualTypeExpectedType(expression, expressionTb, expectedElementsTb);
159                                                 return null;
160                                         }
161                                 }
162                         } else {
163                                 for (int i = 0, length = expressions.length; i < length; i++)
164                                         if (expressions[i].resolveTypeExpecting(scope, expectedElementsTb) == null)
165                                                 return null;
166                         }
167                         return binding;
168                 }
169                 
170                 // infer initializer type for error reporting based on first element
171                 TypeBinding leafElementType = null;
172                 int dim = 1;
173                 if (expressions == null) {
174                         leafElementType = scope.getJavaLangObject();
175                 } else {
176                         Expression currentExpression = expressions[0];
177                         while(currentExpression != null && currentExpression instanceof ArrayInitializer) {
178                                 dim++;
179                                 Expression[] subExprs = ((ArrayInitializer) currentExpression).expressions;
180                                 if (subExprs == null){
181                                         leafElementType = scope.getJavaLangObject();
182                                         currentExpression = null;
183                                         break;
184                                 }
185                                 currentExpression = ((ArrayInitializer) currentExpression).expressions[0];
186                         }
187                         if (currentExpression != null) {
188                                 leafElementType = currentExpression.resolveType(scope);
189                         }
190                 }
191                 if (leafElementType != null) {
192                         TypeBinding probableTb = scope.createArray(leafElementType, dim);
193                         scope.problemReporter().typeMismatchErrorActualTypeExpectedType(this, probableTb, expectedTb);
194                 }
195                 return null;
196         }
197         
198         public void traverse(ASTVisitor visitor, BlockScope scope) {
199
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);
205                         }
206                 }
207                 visitor.endVisit(this, scope);
208         }
209 }