001: /*
002: * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.tree;
027:
028: import sun.tools.java.*;
029: import sun.tools.asm.Assembler;
030: import sun.tools.asm.ArrayData;
031: import java.io.PrintStream;
032: import java.util.Hashtable;
033:
034: /**
035: * WARNING: The contents of this source file are not part of any
036: * supported API. Code that depends on them does so at its own risk:
037: * they are subject to change or removal without notice.
038: */
039: public class NewArrayExpression extends NaryExpression {
040: Expression init;
041:
042: /**
043: * Constructor
044: */
045: public NewArrayExpression(long where, Expression right,
046: Expression args[]) {
047: super (NEWARRAY, where, Type.tError, right, args);
048: }
049:
050: public NewArrayExpression(long where, Expression right,
051: Expression args[], Expression init) {
052: this (where, right, args);
053: this .init = init;
054: }
055:
056: /**
057: * Check
058: */
059: public Vset checkValue(Environment env, Context ctx, Vset vset,
060: Hashtable exp) {
061: type = right.toType(env, ctx);
062:
063: boolean flag = (init != null); // flag says that dims are forbidden
064: for (int i = 0; i < args.length; i++) {
065: Expression dim = args[i];
066: if (dim == null) {
067: if (i == 0 && !flag) {
068: env.error(where, "array.dim.missing");
069: }
070: flag = true;
071: } else {
072: if (flag) {
073: env.error(dim.where, "invalid.array.dim");
074: }
075: vset = dim.checkValue(env, ctx, vset, exp);
076: args[i] = convert(env, ctx, Type.tInt, dim);
077: }
078: type = Type.tArray(type);
079: }
080: if (init != null) {
081: vset = init.checkInitializer(env, ctx, vset, type, exp);
082: init = convert(env, ctx, type, init);
083: }
084: return vset;
085: }
086:
087: public Expression copyInline(Context ctx) {
088: NewArrayExpression e = (NewArrayExpression) super
089: .copyInline(ctx);
090: if (init != null) {
091: e.init = init.copyInline(ctx);
092: }
093: return e;
094: }
095:
096: /**
097: * Inline
098: */
099: public Expression inline(Environment env, Context ctx) {
100: Expression e = null;
101: for (int i = 0; i < args.length; i++) {
102: if (args[i] != null) {
103: e = (e != null) ? new CommaExpression(where, e, args[i])
104: : args[i];
105: }
106: }
107: if (init != null)
108: e = (e != null) ? new CommaExpression(where, e, init)
109: : init;
110: return (e != null) ? e.inline(env, ctx) : null;
111: }
112:
113: public Expression inlineValue(Environment env, Context ctx) {
114: if (init != null)
115: return init.inlineValue(env, ctx); // args are all null
116: for (int i = 0; i < args.length; i++) {
117: if (args[i] != null) {
118: args[i] = args[i].inlineValue(env, ctx);
119: }
120: }
121: return this ;
122: }
123:
124: /**
125: * Code
126: */
127: public void codeValue(Environment env, Context ctx, Assembler asm) {
128: int t = 0;
129: for (int i = 0; i < args.length; i++) {
130: if (args[i] != null) {
131: args[i].codeValue(env, ctx, asm);
132: t++;
133: }
134: }
135: if (args.length > 1) {
136: asm.add(where, opc_multianewarray, new ArrayData(type, t));
137: return;
138: }
139:
140: switch (type.getElementType().getTypeCode()) {
141: case TC_BOOLEAN:
142: asm.add(where, opc_newarray, new Integer(T_BOOLEAN));
143: break;
144: case TC_BYTE:
145: asm.add(where, opc_newarray, new Integer(T_BYTE));
146: break;
147: case TC_SHORT:
148: asm.add(where, opc_newarray, new Integer(T_SHORT));
149: break;
150: case TC_CHAR:
151: asm.add(where, opc_newarray, new Integer(T_CHAR));
152: break;
153: case TC_INT:
154: asm.add(where, opc_newarray, new Integer(T_INT));
155: break;
156: case TC_LONG:
157: asm.add(where, opc_newarray, new Integer(T_LONG));
158: break;
159: case TC_FLOAT:
160: asm.add(where, opc_newarray, new Integer(T_FLOAT));
161: break;
162: case TC_DOUBLE:
163: asm.add(where, opc_newarray, new Integer(T_DOUBLE));
164: break;
165: case TC_ARRAY:
166: asm.add(where, opc_anewarray, type.getElementType());
167: break;
168: case TC_CLASS:
169: asm.add(where, opc_anewarray, env.getClassDeclaration(type
170: .getElementType()));
171: break;
172: default:
173: throw new CompilerError("codeValue");
174: }
175: }
176: }
|