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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*
* Copyright (c) 1997, 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.tree.*;
import sun.tools.asm.Assembler;
/**
* A reference from one scope to another.
*
* 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 UplevelReference implements Constants {
/**
* The class in which the reference occurs.
*/
ClassDefinition client;
/**
* The field being referenced.
* It is always a final argument or a final local variable.
* (An uplevel reference to a field of a class C is fetched
* through an implicit uplevel reference to C.this, which is
* an argument.)
*/
LocalMember target;
/**
* The local variable which bears a copy of the target's value,
* for all methods of the client class.
* Its name is "this$C" for <code>this.C</code> or
* "val$x" for other target variables <code>x</code>.
* <p>
* This local variable is always a constructor argument,
* and is therefore usable only in the constructor and in initializers.
* All other methods use the local field.
* @see #localField
*/
LocalMember localArgument;
/**
* A private synthetic field of the client class which
* bears a copy of the target's value.
* The compiler tries to avoid creating it if possible.
* The field has the same name and type as the localArgument.
* @see #localArgument
*/
MemberDefinition localField;
/**
* The next item on the references list of the client.
*/
UplevelReference next;
/**
* constructor
*/
public UplevelReference(ClassDefinition client, LocalMember target) {
this.client = client;
this.target = target;
// Choose a name and build a variable declaration node.
Identifier valName;
if (target.getName().equals(idThis)) {
ClassDefinition tc = target.getClassDefinition();
// It should always be true that tc.enclosingClassOf(client).
// If it were false, the numbering scheme would fail
// to produce unique names, since we'd be trying
// to number classes which were not in the sequence
// of enclosing scopes. The next paragraph of this
// code robustly deals with that possibility, however,
// by detecting name collisions and perturbing the names.
int depth = 0;
for (ClassDefinition pd = tc; !pd.isTopLevel(); pd = pd.getOuterClass()) {
// The inner classes specification states that the name of
// a private field containing a reference to the outermost
// enclosing instance is named "this$0". That outermost
// enclosing instance is always the innermost toplevel class.
depth += 1;
}
// In this example, T1,T2,T3 are all top-level (static),
// while I4,I5,I6,I7 are all inner. Each of the inner classes
// will have a single up-level "this$N" reference to the next
// class out. Only the outermost "this$0" will refer to a
// top-level class, T3.
//
// class T1 {
// static class T2 {
// static class T3 {
// class I4 {
// class I5 {
// class I6 {
// // at this point we have these fields in various places:
// // I4 this$0; I5 this$1; I6 this$2;
// }
// }
// class I7 {
// // I4 this$0; I7 this$1;
// }
// }
// }
// }
// }
valName = Identifier.lookup(prefixThis + depth);
} else {
valName = Identifier.lookup(prefixVal + target.getName());
}
// Make reasonably certain that valName is unique to this client.
// (This check can be fooled by malicious naming of explicit
// constructor arguments, or of inherited fields.)
Identifier base = valName;
int tick = 0;
while (true) {
boolean failed = (client.getFirstMatch(valName) != null);
for (UplevelReference r = client.getReferences();
r != null; r = r.next) {
if (r.target.getName().equals(valName)) {
failed = true;
}
}
if (!failed) {
break;
}
// try another name
valName = Identifier.lookup(base + "$" + (++tick));
}
// Build the constructor argument.
// Like "this", it wil be shared equally by all constructors of client.
localArgument = new LocalMember(target.getWhere(),
client,
M_FINAL | M_SYNTHETIC,
target.getType(),
valName);
}
/**
* Insert self into a list of references.
* Maintain "isEarlierThan" as an invariant of the list.
* This is important (a) to maximize stability of signatures,
* and (b) to allow uplevel "this" parameters to come at the
* front of every argument list they appear in.
*/
public UplevelReference insertInto(UplevelReference references) {
if (references == null || isEarlierThan(references)) {
next = references;
return this;
} else {
UplevelReference prev = references;
while (!(prev.next == null || isEarlierThan(prev.next))) {
prev = prev.next;
}
next = prev.next;
prev.next = this;
return references;
}
}
/**
* Tells if self precedes the other in the canonical ordering.
*/
public final boolean isEarlierThan(UplevelReference other) {
// Outer fields always come first.
if (isClientOuterField()) {
return true;
} else if (other.isClientOuterField()) {
return false;
}
// Now it doesn't matter what the order is; use string comparison.
LocalMember target2 = other.target;
Identifier name = target.getName();
Identifier name2 = target2.getName();
int cmp = name.toString().compareTo(name2.toString());
if (cmp != 0) {
return cmp < 0;
}
Identifier cname = target.getClassDefinition().getName();
Identifier cname2 = target2.getClassDefinition().getName();
int ccmp = cname.toString().compareTo(cname2.toString());
return ccmp < 0;
}
/**
* the target of this reference
*/
public final LocalMember getTarget() {
return target;
}
/**
* the local argument for this reference
*/
public final LocalMember getLocalArgument() {
return localArgument;
}
/**
* the field allocated in the client for this reference
*/
public final MemberDefinition getLocalField() {
return localField;
}
/**
* Get the local field, creating one if necessary.
* The client class must not be frozen.
*/
public final MemberDefinition getLocalField(Environment env) {
if (localField == null) {
makeLocalField(env);
}
return localField;
}
/**
* the client class
*/
public final ClassDefinition getClient() {
return client;
}
/**
* the next reference in the client's list
*/
public final UplevelReference getNext() {
return next;
}
/**
* Tell if this uplevel reference is the up-level "this" pointer
* of an inner class. Such references are treated differently
* than others, because they affect constructor calls across
* compilation units.
*/
public boolean isClientOuterField() {
MemberDefinition outerf = client.findOuterMember();
return (outerf != null) && (localField == outerf);
}
/**
* Tell if my local argument is directly available in this context.
* If not, the uplevel reference will have to be via a class field.
* <p>
* This must be called in a context which is local
* to the client of the uplevel reference.
*/
public boolean localArgumentAvailable(Environment env, Context ctx) {
MemberDefinition reff = ctx.field;
if (reff.getClassDefinition() != client) {
throw new CompilerError("localArgumentAvailable");
}
return ( reff.isConstructor()
|| reff.isVariable()
|| reff.isInitializer() );
}
/**
* Process an uplevel reference.
* The only decision to make at this point is whether
* to build a "localField" instance variable, which
* is done (lazily) when localArgumentAvailable() proves false.
*/
public void noteReference(Environment env, Context ctx) {
if (localField == null && !localArgumentAvailable(env, ctx)) {
// We need an instance variable unless client is a constructor.
makeLocalField(env);
}
}
private void makeLocalField(Environment env) {
// Cannot alter decisions like this one at a late date.
client.referencesMustNotBeFrozen();
int mod = M_PRIVATE | M_FINAL | M_SYNTHETIC;
localField = env.makeMemberDefinition(env,
localArgument.getWhere(),
client, null,
mod,
localArgument.getType(),
localArgument.getName(),
null, null, null);
}
/**
* Assuming noteReference() is all taken care of,
* build an uplevel reference.
* <p>
* This must be called in a context which is local
* to the client of the uplevel reference.
*/
public Expression makeLocalReference(Environment env, Context ctx) {
if (ctx.field.getClassDefinition() != client) {
throw new CompilerError("makeLocalReference");
}
if (localArgumentAvailable(env, ctx)) {
return new IdentifierExpression(0, localArgument);
} else {
return makeFieldReference(env, ctx);
}
}
/**
* As with makeLocalReference(), build a locally-usable reference.
* Ignore the availability of local arguments; always use a class field.
*/
public Expression makeFieldReference(Environment env, Context ctx) {
Expression e = ctx.findOuterLink(env, 0, localField);
return new FieldExpression(0, e, localField);
}
/**
* During the inline phase, call this on a list of references
* for which the code phase will later emit arguments.
* It will make sure that any "double-uplevel" values
* needed by the callee are also present at the call site.
* <p>
* If any reference is a "ClientOuterField", it is skipped
* by this method (and by willCodeArguments). This is because
*/
public void willCodeArguments(Environment env, Context ctx) {
if (!isClientOuterField()) {
ctx.noteReference(env, target);
}
if (next != null) {
next.willCodeArguments(env, ctx);
}
}
/**
* Code is being generated for a call to a constructor of
* the client class. Push an argument for the constructor.
*/
public void codeArguments(Environment env, Context ctx, Assembler asm,
long where, MemberDefinition conField) {
if (!isClientOuterField()) {
Expression e = ctx.makeReference(env, target);
e.codeValue(env, ctx, asm);
}
if (next != null) {
next.codeArguments(env, ctx, asm, where, conField);
}
}
/**
* Code is being generated for a constructor of the client class.
* Emit code which initializes the instance.
*/
public void codeInitialization(Environment env, Context ctx, Assembler asm,
long where, MemberDefinition conField) {
// If the reference is a clientOuterField, then the initialization
// code is generated in MethodExpression.makeVarInits().
// (Fix for bug 4075063.)
if (localField != null && !isClientOuterField()) {
Expression e = ctx.makeReference(env, target);
Expression f = makeFieldReference(env, ctx);
e = new AssignExpression(e.getWhere(), f, e);
e.type = localField.getType();
e.code(env, ctx, asm);
}
if (next != null) {
next.codeInitialization(env, ctx, asm, where, conField);
}
}
public String toString() {
return "[" + localArgument + " in " + client + "]";
}
}
|