1/*
2 * Copyright (c) 1994, 2003, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.tools.tree;
27
28import sun.tools.java.*;
29import sun.tools.asm.*;
30import java.io.PrintStream;
31import java.util.Hashtable;
32
33/**
34 * WARNING: The contents of this source file are not part of any
35 * supported API.  Code that depends on them does so at its own risk:
36 * they are subject to change or removal without notice.
37 */
38public
39class ArrayExpression extends NaryExpression {
40    /**
41     * Constructor
42     */
43    public ArrayExpression(long where, Expression args[]) {
44        super(ARRAY, where, Type.tError, null, args);
45    }
46
47    /**
48     * Check expression type
49     */
50    public Vset checkValue(Environment env, Context ctx, Vset vset, Hashtable<Object, Object> exp) {
51        env.error(where, "invalid.array.expr");
52        return vset;
53    }
54    public Vset checkInitializer(Environment env, Context ctx, Vset vset, Type t, Hashtable<Object, Object> exp) {
55        if (!t.isType(TC_ARRAY)) {
56            if (!t.isType(TC_ERROR)) {
57                env.error(where, "invalid.array.init", t);
58            }
59            return vset;
60        }
61        type = t;
62        t = t.getElementType();
63        for (int i = 0 ; i < args.length ; i++) {
64            vset = args[i].checkInitializer(env, ctx, vset, t, exp);
65            args[i] = convert(env, ctx, t, args[i]);
66        }
67        return vset;
68    }
69
70    /**
71     * Inline
72     */
73    public Expression inline(Environment env, Context ctx) {
74        Expression e = null;
75        for (int i = 0 ; i < args.length ; i++) {
76            args[i] = args[i].inline(env, ctx);
77            if (args[i] != null) {
78                e = (e == null) ? args[i] : new CommaExpression(where, e, args[i]);
79            }
80        }
81        return e;
82    }
83    public Expression inlineValue(Environment env, Context ctx) {
84        for (int i = 0 ; i < args.length ; i++) {
85            args[i] = args[i].inlineValue(env, ctx);
86        }
87        return this;
88    }
89
90    /**
91     * Code
92     */
93    public void codeValue(Environment env, Context ctx, Assembler asm) {
94        int t = 0;
95        asm.add(where, opc_ldc, args.length);
96        switch (type.getElementType().getTypeCode()) {
97          case TC_BOOLEAN:      asm.add(where, opc_newarray, T_BOOLEAN);   break;
98          case TC_BYTE:         asm.add(where, opc_newarray, T_BYTE);      break;
99          case TC_SHORT:        asm.add(where, opc_newarray, T_SHORT);     break;
100          case TC_CHAR:         asm.add(where, opc_newarray, T_CHAR);      break;
101          case TC_INT:          asm.add(where, opc_newarray, T_INT);       break;
102          case TC_LONG:         asm.add(where, opc_newarray, T_LONG);      break;
103          case TC_FLOAT:        asm.add(where, opc_newarray, T_FLOAT);     break;
104          case TC_DOUBLE:       asm.add(where, opc_newarray, T_DOUBLE);    break;
105
106          case TC_ARRAY:
107            asm.add(where, opc_anewarray, type.getElementType());
108            break;
109
110          case TC_CLASS:
111            asm.add(where, opc_anewarray, env.getClassDeclaration(type.getElementType()));
112            break;
113
114          default:
115            throw new CompilerError("codeValue");
116        }
117
118        for (int i = 0 ; i < args.length ; i++) {
119
120            // If the array element is the default initial value,
121            // then don't bother generating code for this element.
122            if (args[i].equalsDefault()) continue;
123
124            asm.add(where, opc_dup);
125            asm.add(where, opc_ldc, i);
126            args[i].codeValue(env, ctx, asm);
127            switch (type.getElementType().getTypeCode()) {
128              case TC_BOOLEAN:
129              case TC_BYTE:
130                asm.add(where, opc_bastore);
131                break;
132              case TC_CHAR:
133                asm.add(where, opc_castore);
134                break;
135              case TC_SHORT:
136                asm.add(where, opc_sastore);
137                break;
138              default:
139                asm.add(where, opc_iastore + type.getElementType().getTypeCodeOffset());
140            }
141        }
142    }
143}
144