1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.tools.tree;
import sun.tools.java.*;
import sun.tools.asm.Assembler;
import java.io.PrintStream;
import java.util.Hashtable;
/**
* WARNING: The contents of this source file are not part of any
* supported API. Code that depends on them does so at its own risk:
* they are subject to change or removal without notice.
*/
public
class ArrayAccessExpression extends UnaryExpression {
/**
* The index expression for the array access. Note that
* ArrayAccessExpression also `moonlights' as a structure for
* storing array types (like Object[]) which are used as part
* of cast expressions. For properly formed array types, the
* value of index is null. We need to be on the lookout for
* null indices in true array accesses, and non-null indices
* in array types. We also need to make sure general purpose
* methods (like copyInline(), which is called for both) are
* prepared to handle either null or non-null indices.
*/
Expression index;
/**
* constructor
*/
public ArrayAccessExpression(long where, Expression right, Expression index) {
super(ARRAYACCESS, where, Type.tError, right);
this.index = index;
}
/**
* Check expression type
*/
public Vset checkValue(Environment env, Context ctx, Vset vset, Hashtable<Object, Object> exp) {
vset = right.checkValue(env, ctx, vset, exp);
if (index == null) {
env.error(where, "array.index.required");
return vset;
}
vset = index.checkValue(env, ctx, vset, exp);
index = convert(env, ctx, Type.tInt, index);
if (!right.type.isType(TC_ARRAY)) {
if (!right.type.isType(TC_ERROR)) {
env.error(where, "not.array", right.type);
}
return vset;
}
type = right.type.getElementType();
return vset;
}
public Vset checkAmbigName(Environment env, Context ctx,
Vset vset, Hashtable<Object, Object> exp,
UnaryExpression loc) {
if (index == null) {
vset = right.checkAmbigName(env, ctx, vset, exp, this);
if (right.type == Type.tPackage) {
FieldExpression.reportFailedPackagePrefix(env, right);
return vset;
}
// Nope. Is this field expression a type?
if (right instanceof TypeExpression) {
Type atype = Type.tArray(right.type);
loc.right = new TypeExpression(where, atype);
return vset;
}
env.error(where, "array.index.required");
return vset;
}
return super.checkAmbigName(env, ctx, vset, exp, loc);
}
/*
* Check the array if it appears on the LHS of an assignment
*/
public Vset checkLHS(Environment env, Context ctx,
Vset vset, Hashtable<Object, Object> exp) {
return checkValue(env, ctx, vset, exp);
}
/*
* Check the array if it appears on the LHS of an op= expression
*/
public Vset checkAssignOp(Environment env, Context ctx,
Vset vset, Hashtable<Object, Object> exp, Expression outside) {
return checkValue(env, ctx, vset, exp);
}
/**
* An array access expression never requires the use of an access method to perform
* an assignment to an array element, though an access method may be required to
* fetch the array object itself.
*/
public FieldUpdater getAssigner(Environment env, Context ctx) {
return null;
}
/**
* An array access expression never requires a field updater.
*/
public FieldUpdater getUpdater(Environment env, Context ctx) {
return null;
}
/**
* Convert to a type
*/
Type toType(Environment env, Context ctx) {
return toType(env, right.toType(env, ctx));
}
Type toType(Environment env, Type t) {
if (index != null) {
env.error(index.where, "array.dim.in.type");
}
return Type.tArray(t);
}
/**
* Inline
*/
public Expression inline(Environment env, Context ctx) {
// It isn't possible to simply replace an array access
// with a CommaExpression as happens with many binary
// operators, because array accesses may have side effects
// such as NullPointerException or IndexOutOfBoundsException.
right = right.inlineValue(env, ctx);
index = index.inlineValue(env, ctx);
return this;
}
public Expression inlineValue(Environment env, Context ctx) {
// inlineValue() should not end up being called when the index is
// null. If it is null, we let this method fail with a
// NullPointerException.
right = right.inlineValue(env, ctx);
index = index.inlineValue(env, ctx);
return this;
}
public Expression inlineLHS(Environment env, Context ctx) {
return inlineValue(env, ctx);
}
/**
* Create a copy of the expression for method inlining
*/
public Expression copyInline(Context ctx) {
ArrayAccessExpression e = (ArrayAccessExpression)clone();
e.right = right.copyInline(ctx);
if (index == null) {
// The index can be null when this node is being used to
// represent a type (e.g. Object[]) used in a cast expression.
// We need to copy such structures without complaint.
e.index = null;
} else {
e.index = index.copyInline(ctx);
}
return e;
}
/**
* The cost of inlining this expression
*/
public int costInline(int thresh, Environment env, Context ctx) {
// costInline() should not end up being called when the index is
// null. If it is null, we let this method fail with a
// NullPointerException.
return 1 + right.costInline(thresh, env, ctx)
+ index.costInline(thresh, env, ctx);
}
/**
* Code
*/
int codeLValue(Environment env, Context ctx, Assembler asm) {
// codeLValue() should not end up being called when the index is
// null. If it is null, we let this method fail with a
// NullPointerException.
right.codeValue(env, ctx, asm);
index.codeValue(env, ctx, asm);
return 2;
}
void codeLoad(Environment env, Context ctx, Assembler asm) {
switch (type.getTypeCode()) {
case TC_BOOLEAN:
case TC_BYTE:
asm.add(where, opc_baload);
break;
case TC_CHAR:
asm.add(where, opc_caload);
break;
case TC_SHORT:
asm.add(where, opc_saload);
break;
default:
asm.add(where, opc_iaload + type.getTypeCodeOffset());
}
}
void codeStore(Environment env, Context ctx, Assembler asm) {
switch (type.getTypeCode()) {
case TC_BOOLEAN:
case TC_BYTE:
asm.add(where, opc_bastore);
break;
case TC_CHAR:
asm.add(where, opc_castore);
break;
case TC_SHORT:
asm.add(where, opc_sastore);
break;
default:
asm.add(where, opc_iastore + type.getTypeCodeOffset());
}
}
public void codeValue(Environment env, Context ctx, Assembler asm) {
codeLValue(env, ctx, asm);
codeLoad(env, ctx, asm);
}
/**
* Print
*/
public void print(PrintStream out) {
out.print("(" + opNames[op] + " ");
right.print(out);
out.print(" ");
if (index != null) {
index.print(out);
} else {
out.print("<empty>");
}
out.print(")");
}
}
|