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.*;
030: import java.io.PrintStream;
031: import java.util.Hashtable;
032:
033: /**
034: * WARNING: The contents of this source file are not part of any
035: * supported API. Code that depends on them does so at its own risk:
036: * they are subject to change or removal without notice.
037: */
038: public class ArrayExpression extends NaryExpression {
039: /**
040: * Constructor
041: */
042: public ArrayExpression(long where, Expression args[]) {
043: super (ARRAY, where, Type.tError, null, args);
044: }
045:
046: /**
047: * Check expression type
048: */
049: public Vset checkValue(Environment env, Context ctx, Vset vset,
050: Hashtable exp) {
051: env.error(where, "invalid.array.expr");
052: return vset;
053: }
054:
055: public Vset checkInitializer(Environment env, Context ctx,
056: Vset vset, Type t, Hashtable exp) {
057: if (!t.isType(TC_ARRAY)) {
058: if (!t.isType(TC_ERROR)) {
059: env.error(where, "invalid.array.init", t);
060: }
061: return vset;
062: }
063: type = t;
064: t = t.getElementType();
065: for (int i = 0; i < args.length; i++) {
066: vset = args[i].checkInitializer(env, ctx, vset, t, exp);
067: args[i] = convert(env, ctx, t, args[i]);
068: }
069: return vset;
070: }
071:
072: /**
073: * Inline
074: */
075: public Expression inline(Environment env, Context ctx) {
076: Expression e = null;
077: for (int i = 0; i < args.length; i++) {
078: args[i] = args[i].inline(env, ctx);
079: if (args[i] != null) {
080: e = (e == null) ? args[i] : new CommaExpression(where,
081: e, args[i]);
082: }
083: }
084: return e;
085: }
086:
087: public Expression inlineValue(Environment env, Context ctx) {
088: for (int i = 0; i < args.length; i++) {
089: args[i] = args[i].inlineValue(env, ctx);
090: }
091: return this ;
092: }
093:
094: /**
095: * Code
096: */
097: public void codeValue(Environment env, Context ctx, Assembler asm) {
098: int t = 0;
099: asm.add(where, opc_ldc, new Integer(args.length));
100: switch (type.getElementType().getTypeCode()) {
101: case TC_BOOLEAN:
102: asm.add(where, opc_newarray, new Integer(T_BOOLEAN));
103: break;
104: case TC_BYTE:
105: asm.add(where, opc_newarray, new Integer(T_BYTE));
106: break;
107: case TC_SHORT:
108: asm.add(where, opc_newarray, new Integer(T_SHORT));
109: break;
110: case TC_CHAR:
111: asm.add(where, opc_newarray, new Integer(T_CHAR));
112: break;
113: case TC_INT:
114: asm.add(where, opc_newarray, new Integer(T_INT));
115: break;
116: case TC_LONG:
117: asm.add(where, opc_newarray, new Integer(T_LONG));
118: break;
119: case TC_FLOAT:
120: asm.add(where, opc_newarray, new Integer(T_FLOAT));
121: break;
122: case TC_DOUBLE:
123: asm.add(where, opc_newarray, new Integer(T_DOUBLE));
124: break;
125:
126: case TC_ARRAY:
127: asm.add(where, opc_anewarray, type.getElementType());
128: break;
129:
130: case TC_CLASS:
131: asm.add(where, opc_anewarray, env.getClassDeclaration(type
132: .getElementType()));
133: break;
134:
135: default:
136: throw new CompilerError("codeValue");
137: }
138:
139: for (int i = 0; i < args.length; i++) {
140:
141: // If the array element is the default initial value,
142: // then don't bother generating code for this element.
143: if (args[i].equalsDefault())
144: continue;
145:
146: asm.add(where, opc_dup);
147: asm.add(where, opc_ldc, new Integer(i));
148: args[i].codeValue(env, ctx, asm);
149: switch (type.getElementType().getTypeCode()) {
150: case TC_BOOLEAN:
151: case TC_BYTE:
152: asm.add(where, opc_bastore);
153: break;
154: case TC_CHAR:
155: asm.add(where, opc_castore);
156: break;
157: case TC_SHORT:
158: asm.add(where, opc_sastore);
159: break;
160: default:
161: asm.add(where, opc_iastore
162: + type.getElementType().getTypeCodeOffset());
163: }
164: }
165: }
166: }
|