1 /*
2  * Copyright (c) 2010, 2013, 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 
26 package jdk.nashorn.internal.codegen.types;
27 
28 import static jdk.internal.org.objectweb.asm.Opcodes.AALOAD;
29 import static jdk.internal.org.objectweb.asm.Opcodes.AASTORE;
30 import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;
31 import static jdk.internal.org.objectweb.asm.Opcodes.ANEWARRAY;
32 import static jdk.internal.org.objectweb.asm.Opcodes.ARRAYLENGTH;
33 
34 import jdk.internal.org.objectweb.asm.MethodVisitor;
35 
36 /**
37  * This is an array type, i.e. OBJECT_ARRAY, NUMBER_ARRAY.
38  */
39 public class ArrayType extends ObjectType implements BytecodeArrayOps {
40     private static final long serialVersionUID = 1L;
41 
42     /**
43      * Constructor
44      *
45      * @param clazz the Java class representation of the array
46      */
ArrayType(final Class<?> clazz)47     protected ArrayType(final Class<?> clazz) {
48         super(clazz);
49     }
50 
51     /**
52      * Get the element type of the array elements e.g. for OBJECT_ARRAY, this is OBJECT
53      *
54      * @return the element type
55      */
getElementType()56     public Type getElementType() {
57         return Type.typeFor(getTypeClass().getComponentType());
58     }
59 
60     @Override
astore(final MethodVisitor method)61     public void astore(final MethodVisitor method) {
62         method.visitInsn(AASTORE);
63     }
64 
65     @Override
aload(final MethodVisitor method)66     public Type aload(final MethodVisitor method) {
67         method.visitInsn(AALOAD);
68         return getElementType();
69     }
70 
71     @Override
arraylength(final MethodVisitor method)72     public Type arraylength(final MethodVisitor method) {
73         method.visitInsn(ARRAYLENGTH);
74         return INT;
75     }
76 
77     @Override
newarray(final MethodVisitor method)78     public Type newarray(final MethodVisitor method) {
79         method.visitTypeInsn(ANEWARRAY, getElementType().getInternalName());
80         return this;
81     }
82 
83     @Override
newarray(final MethodVisitor method, final int dims)84     public Type newarray(final MethodVisitor method, final int dims) {
85         method.visitMultiANewArrayInsn(getInternalName(), dims);
86         return this;
87     }
88 
89     @Override
load(final MethodVisitor method, final int slot)90     public Type load(final MethodVisitor method, final int slot) {
91         assert slot != -1;
92         method.visitVarInsn(ALOAD, slot);
93         return this;
94     }
95 
96     @Override
toString()97     public String toString() {
98         return "array<elementType=" + getElementType().getTypeClass().getSimpleName() + '>';
99     }
100 
101     @Override
convert(final MethodVisitor method, final Type to)102     public Type convert(final MethodVisitor method, final Type to) {
103         assert to.isObject();
104         assert !to.isArray() || ((ArrayType)to).getElementType() == getElementType();
105         return to;
106     }
107 
108 }
109