J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.tools.tree; |
| 27 | |
| 28 | import sun.tools.java.*; |
| 29 | import sun.tools.asm.Assembler; |
| 30 | import sun.tools.asm.ArrayData; |
| 31 | import java.io.PrintStream; |
| 32 | import java.util.Hashtable; |
| 33 | |
| 34 | /** |
| 35 | * WARNING: The contents of this source file are not part of any |
| 36 | * supported API. Code that depends on them does so at its own risk: |
| 37 | * they are subject to change or removal without notice. |
| 38 | */ |
| 39 | public |
| 40 | class NewArrayExpression extends NaryExpression { |
| 41 | Expression init; |
| 42 | |
| 43 | /** |
| 44 | * Constructor |
| 45 | */ |
| 46 | public NewArrayExpression(long where, Expression right, Expression args[]) { |
| 47 | super(NEWARRAY, where, Type.tError, right, args); |
| 48 | } |
| 49 | |
| 50 | public NewArrayExpression(long where, Expression right, Expression args[], Expression init) { |
| 51 | this(where, right, args); |
| 52 | this.init = init; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Check |
| 57 | */ |
| 58 | public Vset checkValue(Environment env, Context ctx, Vset vset, Hashtable exp) { |
| 59 | type = right.toType(env, ctx); |
| 60 | |
| 61 | boolean flag = (init != null); // flag says that dims are forbidden |
| 62 | for (int i = 0 ; i < args.length ; i++) { |
| 63 | Expression dim = args[i]; |
| 64 | if (dim == null) { |
| 65 | if (i == 0 && !flag) { |
| 66 | env.error(where, "array.dim.missing"); |
| 67 | } |
| 68 | flag = true; |
| 69 | } else { |
| 70 | if (flag) { |
| 71 | env.error(dim.where, "invalid.array.dim"); |
| 72 | } |
| 73 | vset = dim.checkValue(env, ctx, vset, exp); |
| 74 | args[i] = convert(env, ctx, Type.tInt, dim); |
| 75 | } |
| 76 | type = Type.tArray(type); |
| 77 | } |
| 78 | if (init != null) { |
| 79 | vset = init.checkInitializer(env, ctx, vset, type, exp); |
| 80 | init = convert(env, ctx, type, init); |
| 81 | } |
| 82 | return vset; |
| 83 | } |
| 84 | |
| 85 | public Expression copyInline(Context ctx) { |
| 86 | NewArrayExpression e = (NewArrayExpression)super.copyInline(ctx); |
| 87 | if (init != null) { |
| 88 | e.init = init.copyInline(ctx); |
| 89 | } |
| 90 | return e; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Inline |
| 95 | */ |
| 96 | public Expression inline(Environment env, Context ctx) { |
| 97 | Expression e = null; |
| 98 | for (int i = 0 ; i < args.length ; i++) { |
| 99 | if (args[i] != null) { |
| 100 | e = (e != null) ? new CommaExpression(where, e, args[i]) : args[i]; |
| 101 | } |
| 102 | } |
| 103 | if (init != null) |
| 104 | e = (e != null) ? new CommaExpression(where, e, init) : init; |
| 105 | return (e != null) ? e.inline(env, ctx) : null; |
| 106 | } |
| 107 | public Expression inlineValue(Environment env, Context ctx) { |
| 108 | if (init != null) |
| 109 | return init.inlineValue(env, ctx); // args are all null |
| 110 | for (int i = 0 ; i < args.length ; i++) { |
| 111 | if (args[i] != null) { |
| 112 | args[i] = args[i].inlineValue(env, ctx); |
| 113 | } |
| 114 | } |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Code |
| 120 | */ |
| 121 | public void codeValue(Environment env, Context ctx, Assembler asm) { |
| 122 | int t = 0; |
| 123 | for (int i = 0 ; i < args.length ; i++) { |
| 124 | if (args[i] != null) { |
| 125 | args[i].codeValue(env, ctx, asm); |
| 126 | t++; |
| 127 | } |
| 128 | } |
| 129 | if (args.length > 1) { |
| 130 | asm.add(where, opc_multianewarray, new ArrayData(type, t)); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | switch (type.getElementType().getTypeCode()) { |
| 135 | case TC_BOOLEAN: |
| 136 | asm.add(where, opc_newarray, new Integer(T_BOOLEAN)); break; |
| 137 | case TC_BYTE: |
| 138 | asm.add(where, opc_newarray, new Integer(T_BYTE)); break; |
| 139 | case TC_SHORT: |
| 140 | asm.add(where, opc_newarray, new Integer(T_SHORT)); break; |
| 141 | case TC_CHAR: |
| 142 | asm.add(where, opc_newarray, new Integer(T_CHAR)); break; |
| 143 | case TC_INT: |
| 144 | asm.add(where, opc_newarray, new Integer(T_INT)); break; |
| 145 | case TC_LONG: |
| 146 | asm.add(where, opc_newarray, new Integer(T_LONG)); break; |
| 147 | case TC_FLOAT: |
| 148 | asm.add(where, opc_newarray, new Integer(T_FLOAT)); break; |
| 149 | case TC_DOUBLE: |
| 150 | asm.add(where, opc_newarray, new Integer(T_DOUBLE)); break; |
| 151 | case TC_ARRAY: |
| 152 | asm.add(where, opc_anewarray, type.getElementType()); break; |
| 153 | case TC_CLASS: |
| 154 | asm.add(where, opc_anewarray, |
| 155 | env.getClassDeclaration(type.getElementType())); |
| 156 | break; |
| 157 | default: |
| 158 | throw new CompilerError("codeValue"); |
| 159 | } |
| 160 | } |
| 161 | } |