0N/A/*
2362N/A * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.tree;
0N/A
0N/Aimport sun.tools.java.*;
0N/Aimport sun.tools.asm.Assembler;
0N/Aimport java.io.PrintStream;
0N/Aimport java.util.Hashtable;
0N/A
0N/A/**
0N/A * WARNING: The contents of this source file are not part of any
0N/A * supported API. Code that depends on them does so at its own risk:
0N/A * they are subject to change or removal without notice.
0N/A */
0N/Apublic
0N/Aclass ArrayAccessExpression extends UnaryExpression {
0N/A
0N/A /**
0N/A * The index expression for the array access. Note that
0N/A * ArrayAccessExpression also `moonlights' as a structure for
0N/A * storing array types (like Object[]) which are used as part
0N/A * of cast expressions. For properly formed array types, the
0N/A * value of index is null. We need to be on the lookout for
0N/A * null indices in true array accesses, and non-null indices
0N/A * in array types. We also need to make sure general purpose
0N/A * methods (like copyInline(), which is called for both) are
0N/A * prepared to handle either null or non-null indices.
0N/A */
0N/A Expression index;
0N/A
0N/A /**
0N/A * constructor
0N/A */
0N/A public ArrayAccessExpression(long where, Expression right, Expression index) {
0N/A super(ARRAYACCESS, where, Type.tError, right);
0N/A this.index = index;
0N/A }
0N/A
0N/A /**
0N/A * Check expression type
0N/A */
0N/A public Vset checkValue(Environment env, Context ctx, Vset vset, Hashtable exp) {
0N/A vset = right.checkValue(env, ctx, vset, exp);
0N/A if (index == null) {
0N/A env.error(where, "array.index.required");
0N/A return vset;
0N/A }
0N/A vset = index.checkValue(env, ctx, vset, exp);
0N/A index = convert(env, ctx, Type.tInt, index);
0N/A
0N/A if (!right.type.isType(TC_ARRAY)) {
0N/A if (!right.type.isType(TC_ERROR)) {
0N/A env.error(where, "not.array", right.type);
0N/A }
0N/A return vset;
0N/A }
0N/A
0N/A type = right.type.getElementType();
0N/A return vset;
0N/A }
0N/A
0N/A public Vset checkAmbigName(Environment env, Context ctx,
0N/A Vset vset, Hashtable exp,
0N/A UnaryExpression loc) {
0N/A if (index == null) {
0N/A vset = right.checkAmbigName(env, ctx, vset, exp, this);
0N/A if (right.type == Type.tPackage) {
0N/A FieldExpression.reportFailedPackagePrefix(env, right);
0N/A return vset;
0N/A }
0N/A
0N/A // Nope. Is this field expression a type?
0N/A if (right instanceof TypeExpression) {
0N/A Type atype = Type.tArray(right.type);
0N/A loc.right = new TypeExpression(where, atype);
0N/A return vset;
0N/A }
0N/A
0N/A env.error(where, "array.index.required");
0N/A return vset;
0N/A }
0N/A return super.checkAmbigName(env, ctx, vset, exp, loc);
0N/A }
0N/A
0N/A /*
0N/A * Check the array if it appears on the LHS of an assignment
0N/A */
0N/A public Vset checkLHS(Environment env, Context ctx,
0N/A Vset vset, Hashtable exp) {
0N/A return checkValue(env, ctx, vset, exp);
0N/A }
0N/A
0N/A /*
0N/A * Check the array if it appears on the LHS of an op= expression
0N/A */
0N/A public Vset checkAssignOp(Environment env, Context ctx,
0N/A Vset vset, Hashtable exp, Expression outside) {
0N/A return checkValue(env, ctx, vset, exp);
0N/A }
0N/A
0N/A /**
0N/A * An array access expression never requires the use of an access method to perform
0N/A * an assignment to an array element, though an access method may be required to
0N/A * fetch the array object itself.
0N/A */
0N/A public FieldUpdater getAssigner(Environment env, Context ctx) {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * An array access expression never requires a field updater.
0N/A */
0N/A public FieldUpdater getUpdater(Environment env, Context ctx) {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Convert to a type
0N/A */
0N/A Type toType(Environment env, Context ctx) {
0N/A return toType(env, right.toType(env, ctx));
0N/A }
0N/A Type toType(Environment env, Type t) {
0N/A if (index != null) {
0N/A env.error(index.where, "array.dim.in.type");
0N/A }
0N/A return Type.tArray(t);
0N/A }
0N/A
0N/A /**
0N/A * Inline
0N/A */
0N/A public Expression inline(Environment env, Context ctx) {
0N/A // It isn't possible to simply replace an array access
0N/A // with a CommaExpression as happens with many binary
0N/A // operators, because array accesses may have side effects
0N/A // such as NullPointerException or IndexOutOfBoundsException.
0N/A right = right.inlineValue(env, ctx);
0N/A index = index.inlineValue(env, ctx);
0N/A return this;
0N/A }
0N/A public Expression inlineValue(Environment env, Context ctx) {
0N/A // inlineValue() should not end up being called when the index is
0N/A // null. If it is null, we let this method fail with a
0N/A // NullPointerException.
0N/A
0N/A right = right.inlineValue(env, ctx);
0N/A index = index.inlineValue(env, ctx);
0N/A return this;
0N/A }
0N/A public Expression inlineLHS(Environment env, Context ctx) {
0N/A return inlineValue(env, ctx);
0N/A }
0N/A
0N/A /**
0N/A * Create a copy of the expression for method inlining
0N/A */
0N/A public Expression copyInline(Context ctx) {
0N/A ArrayAccessExpression e = (ArrayAccessExpression)clone();
0N/A e.right = right.copyInline(ctx);
0N/A if (index == null) {
0N/A // The index can be null when this node is being used to
0N/A // represent a type (e.g. Object[]) used in a cast expression.
0N/A // We need to copy such structures without complaint.
0N/A e.index = null;
0N/A } else {
0N/A e.index = index.copyInline(ctx);
0N/A }
0N/A return e;
0N/A }
0N/A
0N/A /**
0N/A * The cost of inlining this expression
0N/A */
0N/A public int costInline(int thresh, Environment env, Context ctx) {
0N/A // costInline() should not end up being called when the index is
0N/A // null. If it is null, we let this method fail with a
0N/A // NullPointerException.
0N/A
0N/A return 1 + right.costInline(thresh, env, ctx)
0N/A + index.costInline(thresh, env, ctx);
0N/A }
0N/A
0N/A /**
0N/A * Code
0N/A */
0N/A int codeLValue(Environment env, Context ctx, Assembler asm) {
0N/A // codeLValue() should not end up being called when the index is
0N/A // null. If it is null, we let this method fail with a
0N/A // NullPointerException.
0N/A
0N/A right.codeValue(env, ctx, asm);
0N/A index.codeValue(env, ctx, asm);
0N/A return 2;
0N/A }
0N/A void codeLoad(Environment env, Context ctx, Assembler asm) {
0N/A switch (type.getTypeCode()) {
0N/A case TC_BOOLEAN:
0N/A case TC_BYTE:
0N/A asm.add(where, opc_baload);
0N/A break;
0N/A case TC_CHAR:
0N/A asm.add(where, opc_caload);
0N/A break;
0N/A case TC_SHORT:
0N/A asm.add(where, opc_saload);
0N/A break;
0N/A default:
0N/A asm.add(where, opc_iaload + type.getTypeCodeOffset());
0N/A }
0N/A }
0N/A void codeStore(Environment env, Context ctx, Assembler asm) {
0N/A switch (type.getTypeCode()) {
0N/A case TC_BOOLEAN:
0N/A case TC_BYTE:
0N/A asm.add(where, opc_bastore);
0N/A break;
0N/A case TC_CHAR:
0N/A asm.add(where, opc_castore);
0N/A break;
0N/A case TC_SHORT:
0N/A asm.add(where, opc_sastore);
0N/A break;
0N/A default:
0N/A asm.add(where, opc_iastore + type.getTypeCodeOffset());
0N/A }
0N/A }
0N/A public void codeValue(Environment env, Context ctx, Assembler asm) {
0N/A codeLValue(env, ctx, asm);
0N/A codeLoad(env, ctx, asm);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Print
0N/A */
0N/A public void print(PrintStream out) {
0N/A out.print("(" + opNames[op] + " ");
0N/A right.print(out);
0N/A out.print(" ");
0N/A if (index != null) {
0N/A index.print(out);
0N/A } else {
0N/A out.print("<empty>");
0N/A }
0N/A out.print(")");
0N/A }
0N/A}