src/share/classes/com/sun/tools/javac/comp/Lower.java

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2789
36ed04994588
parent 2702
9ca8d8713094
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.comp;
aoqi@0 27
aoqi@0 28 import java.util.*;
aoqi@0 29
aoqi@0 30 import com.sun.tools.javac.code.*;
aoqi@0 31 import com.sun.tools.javac.code.Type.AnnotatedType;
aoqi@0 32 import com.sun.tools.javac.jvm.*;
aoqi@0 33 import com.sun.tools.javac.main.Option.PkgInfo;
aoqi@0 34 import com.sun.tools.javac.tree.*;
aoqi@0 35 import com.sun.tools.javac.util.*;
aoqi@0 36 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
aoqi@0 37 import com.sun.tools.javac.util.List;
aoqi@0 38
aoqi@0 39 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 40 import com.sun.tools.javac.tree.JCTree.*;
aoqi@0 41 import com.sun.tools.javac.code.Type.*;
aoqi@0 42
aoqi@0 43 import com.sun.tools.javac.jvm.Target;
aoqi@0 44 import com.sun.tools.javac.tree.EndPosTable;
aoqi@0 45
aoqi@0 46 import static com.sun.tools.javac.code.Flags.*;
aoqi@0 47 import static com.sun.tools.javac.code.Flags.BLOCK;
aoqi@0 48 import static com.sun.tools.javac.code.Kinds.*;
aoqi@0 49 import static com.sun.tools.javac.code.TypeTag.*;
aoqi@0 50 import static com.sun.tools.javac.jvm.ByteCodes.*;
aoqi@0 51 import static com.sun.tools.javac.tree.JCTree.Tag.*;
aoqi@0 52
aoqi@0 53 /** This pass translates away some syntactic sugar: inner classes,
aoqi@0 54 * class literals, assertions, foreach loops, etc.
aoqi@0 55 *
aoqi@0 56 * <p><b>This is NOT part of any supported API.
aoqi@0 57 * If you write code that depends on this, you do so at your own risk.
aoqi@0 58 * This code and its internal interfaces are subject to change or
aoqi@0 59 * deletion without notice.</b>
aoqi@0 60 */
aoqi@0 61 public class Lower extends TreeTranslator {
aoqi@0 62 protected static final Context.Key<Lower> lowerKey =
aoqi@0 63 new Context.Key<Lower>();
aoqi@0 64
aoqi@0 65 public static Lower instance(Context context) {
aoqi@0 66 Lower instance = context.get(lowerKey);
aoqi@0 67 if (instance == null)
aoqi@0 68 instance = new Lower(context);
aoqi@0 69 return instance;
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 private Names names;
aoqi@0 73 private Log log;
aoqi@0 74 private Symtab syms;
aoqi@0 75 private Resolve rs;
aoqi@0 76 private Check chk;
aoqi@0 77 private Attr attr;
aoqi@0 78 private TreeMaker make;
aoqi@0 79 private DiagnosticPosition make_pos;
aoqi@0 80 private ClassWriter writer;
aoqi@0 81 private ClassReader reader;
aoqi@0 82 private ConstFold cfolder;
aoqi@0 83 private Target target;
aoqi@0 84 private Source source;
aoqi@0 85 private final TypeEnvs typeEnvs;
aoqi@0 86 private boolean allowEnums;
aoqi@0 87 private final Name dollarAssertionsDisabled;
aoqi@0 88 private final Name classDollar;
aoqi@0 89 private Types types;
aoqi@0 90 private boolean debugLower;
aoqi@0 91 private PkgInfo pkginfoOpt;
aoqi@0 92
aoqi@0 93 protected Lower(Context context) {
aoqi@0 94 context.put(lowerKey, this);
aoqi@0 95 names = Names.instance(context);
aoqi@0 96 log = Log.instance(context);
aoqi@0 97 syms = Symtab.instance(context);
aoqi@0 98 rs = Resolve.instance(context);
aoqi@0 99 chk = Check.instance(context);
aoqi@0 100 attr = Attr.instance(context);
aoqi@0 101 make = TreeMaker.instance(context);
aoqi@0 102 writer = ClassWriter.instance(context);
aoqi@0 103 reader = ClassReader.instance(context);
aoqi@0 104 cfolder = ConstFold.instance(context);
aoqi@0 105 target = Target.instance(context);
aoqi@0 106 source = Source.instance(context);
aoqi@0 107 typeEnvs = TypeEnvs.instance(context);
aoqi@0 108 allowEnums = source.allowEnums();
aoqi@0 109 dollarAssertionsDisabled = names.
aoqi@0 110 fromString(target.syntheticNameChar() + "assertionsDisabled");
aoqi@0 111 classDollar = names.
aoqi@0 112 fromString("class" + target.syntheticNameChar());
aoqi@0 113
aoqi@0 114 types = Types.instance(context);
aoqi@0 115 Options options = Options.instance(context);
aoqi@0 116 debugLower = options.isSet("debuglower");
aoqi@0 117 pkginfoOpt = PkgInfo.get(options);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 /** The currently enclosing class.
aoqi@0 121 */
aoqi@0 122 ClassSymbol currentClass;
aoqi@0 123
aoqi@0 124 /** A queue of all translated classes.
aoqi@0 125 */
aoqi@0 126 ListBuffer<JCTree> translated;
aoqi@0 127
aoqi@0 128 /** Environment for symbol lookup, set by translateTopLevelClass.
aoqi@0 129 */
aoqi@0 130 Env<AttrContext> attrEnv;
aoqi@0 131
aoqi@0 132 /** A hash table mapping syntax trees to their ending source positions.
aoqi@0 133 */
aoqi@0 134 EndPosTable endPosTable;
aoqi@0 135
aoqi@0 136 /**************************************************************************
aoqi@0 137 * Global mappings
aoqi@0 138 *************************************************************************/
aoqi@0 139
aoqi@0 140 /** A hash table mapping local classes to their definitions.
aoqi@0 141 */
aoqi@0 142 Map<ClassSymbol, JCClassDecl> classdefs;
aoqi@0 143
aoqi@0 144 /** A hash table mapping local classes to a list of pruned trees.
aoqi@0 145 */
aoqi@0 146 public Map<ClassSymbol, List<JCTree>> prunedTree = new WeakHashMap<ClassSymbol, List<JCTree>>();
aoqi@0 147
aoqi@0 148 /** A hash table mapping virtual accessed symbols in outer subclasses
aoqi@0 149 * to the actually referred symbol in superclasses.
aoqi@0 150 */
aoqi@0 151 Map<Symbol,Symbol> actualSymbols;
aoqi@0 152
aoqi@0 153 /** The current method definition.
aoqi@0 154 */
aoqi@0 155 JCMethodDecl currentMethodDef;
aoqi@0 156
aoqi@0 157 /** The current method symbol.
aoqi@0 158 */
aoqi@0 159 MethodSymbol currentMethodSym;
aoqi@0 160
aoqi@0 161 /** The currently enclosing outermost class definition.
aoqi@0 162 */
aoqi@0 163 JCClassDecl outermostClassDef;
aoqi@0 164
aoqi@0 165 /** The currently enclosing outermost member definition.
aoqi@0 166 */
aoqi@0 167 JCTree outermostMemberDef;
aoqi@0 168
aoqi@0 169 /** A map from local variable symbols to their translation (as per LambdaToMethod).
aoqi@0 170 * This is required when a capturing local class is created from a lambda (in which
aoqi@0 171 * case the captured symbols should be replaced with the translated lambda symbols).
aoqi@0 172 */
aoqi@0 173 Map<Symbol, Symbol> lambdaTranslationMap = null;
aoqi@0 174
aoqi@0 175 /** A navigator class for assembling a mapping from local class symbols
aoqi@0 176 * to class definition trees.
aoqi@0 177 * There is only one case; all other cases simply traverse down the tree.
aoqi@0 178 */
aoqi@0 179 class ClassMap extends TreeScanner {
aoqi@0 180
aoqi@0 181 /** All encountered class defs are entered into classdefs table.
aoqi@0 182 */
aoqi@0 183 public void visitClassDef(JCClassDecl tree) {
aoqi@0 184 classdefs.put(tree.sym, tree);
aoqi@0 185 super.visitClassDef(tree);
aoqi@0 186 }
aoqi@0 187 }
aoqi@0 188 ClassMap classMap = new ClassMap();
aoqi@0 189
aoqi@0 190 /** Map a class symbol to its definition.
aoqi@0 191 * @param c The class symbol of which we want to determine the definition.
aoqi@0 192 */
aoqi@0 193 JCClassDecl classDef(ClassSymbol c) {
aoqi@0 194 // First lookup the class in the classdefs table.
aoqi@0 195 JCClassDecl def = classdefs.get(c);
aoqi@0 196 if (def == null && outermostMemberDef != null) {
aoqi@0 197 // If this fails, traverse outermost member definition, entering all
aoqi@0 198 // local classes into classdefs, and try again.
aoqi@0 199 classMap.scan(outermostMemberDef);
aoqi@0 200 def = classdefs.get(c);
aoqi@0 201 }
aoqi@0 202 if (def == null) {
aoqi@0 203 // If this fails, traverse outermost class definition, entering all
aoqi@0 204 // local classes into classdefs, and try again.
aoqi@0 205 classMap.scan(outermostClassDef);
aoqi@0 206 def = classdefs.get(c);
aoqi@0 207 }
aoqi@0 208 return def;
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 /** A hash table mapping class symbols to lists of free variables.
aoqi@0 212 * accessed by them. Only free variables of the method immediately containing
aoqi@0 213 * a class are associated with that class.
aoqi@0 214 */
aoqi@0 215 Map<ClassSymbol,List<VarSymbol>> freevarCache;
aoqi@0 216
aoqi@0 217 /** A navigator class for collecting the free variables accessed
aoqi@0 218 * from a local class. There is only one case; all other cases simply
aoqi@0 219 * traverse down the tree. This class doesn't deal with the specific
aoqi@0 220 * of Lower - it's an abstract visitor that is meant to be reused in
aoqi@0 221 * order to share the local variable capture logic.
aoqi@0 222 */
aoqi@0 223 abstract class BasicFreeVarCollector extends TreeScanner {
aoqi@0 224
aoqi@0 225 /** Add all free variables of class c to fvs list
aoqi@0 226 * unless they are already there.
aoqi@0 227 */
aoqi@0 228 abstract void addFreeVars(ClassSymbol c);
aoqi@0 229
aoqi@0 230 /** If tree refers to a variable in owner of local class, add it to
aoqi@0 231 * free variables list.
aoqi@0 232 */
aoqi@0 233 public void visitIdent(JCIdent tree) {
aoqi@0 234 visitSymbol(tree.sym);
aoqi@0 235 }
aoqi@0 236 // where
aoqi@0 237 abstract void visitSymbol(Symbol _sym);
aoqi@0 238
aoqi@0 239 /** If tree refers to a class instance creation expression
aoqi@0 240 * add all free variables of the freshly created class.
aoqi@0 241 */
aoqi@0 242 public void visitNewClass(JCNewClass tree) {
aoqi@0 243 ClassSymbol c = (ClassSymbol)tree.constructor.owner;
aoqi@0 244 addFreeVars(c);
aoqi@0 245 super.visitNewClass(tree);
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 /** If tree refers to a superclass constructor call,
aoqi@0 249 * add all free variables of the superclass.
aoqi@0 250 */
aoqi@0 251 public void visitApply(JCMethodInvocation tree) {
aoqi@0 252 if (TreeInfo.name(tree.meth) == names._super) {
aoqi@0 253 addFreeVars((ClassSymbol) TreeInfo.symbol(tree.meth).owner);
aoqi@0 254 }
aoqi@0 255 super.visitApply(tree);
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 /**
aoqi@0 260 * Lower-specific subclass of {@code BasicFreeVarCollector}.
aoqi@0 261 */
aoqi@0 262 class FreeVarCollector extends BasicFreeVarCollector {
aoqi@0 263
aoqi@0 264 /** The owner of the local class.
aoqi@0 265 */
aoqi@0 266 Symbol owner;
aoqi@0 267
aoqi@0 268 /** The local class.
aoqi@0 269 */
aoqi@0 270 ClassSymbol clazz;
aoqi@0 271
aoqi@0 272 /** The list of owner's variables accessed from within the local class,
aoqi@0 273 * without any duplicates.
aoqi@0 274 */
aoqi@0 275 List<VarSymbol> fvs;
aoqi@0 276
aoqi@0 277 FreeVarCollector(ClassSymbol clazz) {
aoqi@0 278 this.clazz = clazz;
aoqi@0 279 this.owner = clazz.owner;
aoqi@0 280 this.fvs = List.nil();
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 /** Add free variable to fvs list unless it is already there.
aoqi@0 284 */
aoqi@0 285 private void addFreeVar(VarSymbol v) {
aoqi@0 286 for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail)
aoqi@0 287 if (l.head == v) return;
aoqi@0 288 fvs = fvs.prepend(v);
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 @Override
aoqi@0 292 void addFreeVars(ClassSymbol c) {
aoqi@0 293 List<VarSymbol> fvs = freevarCache.get(c);
aoqi@0 294 if (fvs != null) {
aoqi@0 295 for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail) {
aoqi@0 296 addFreeVar(l.head);
aoqi@0 297 }
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 @Override
aoqi@0 302 void visitSymbol(Symbol _sym) {
aoqi@0 303 Symbol sym = _sym;
aoqi@0 304 if (sym.kind == VAR || sym.kind == MTH) {
aoqi@0 305 while (sym != null && sym.owner != owner)
aoqi@0 306 sym = proxies.lookup(proxyName(sym.name)).sym;
aoqi@0 307 if (sym != null && sym.owner == owner) {
aoqi@0 308 VarSymbol v = (VarSymbol)sym;
aoqi@0 309 if (v.getConstValue() == null) {
aoqi@0 310 addFreeVar(v);
aoqi@0 311 }
aoqi@0 312 } else {
aoqi@0 313 if (outerThisStack.head != null &&
aoqi@0 314 outerThisStack.head != _sym)
aoqi@0 315 visitSymbol(outerThisStack.head);
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 /** If tree refers to a class instance creation expression
aoqi@0 321 * add all free variables of the freshly created class.
aoqi@0 322 */
aoqi@0 323 public void visitNewClass(JCNewClass tree) {
aoqi@0 324 ClassSymbol c = (ClassSymbol)tree.constructor.owner;
aoqi@0 325 if (tree.encl == null &&
aoqi@0 326 c.hasOuterInstance() &&
aoqi@0 327 outerThisStack.head != null)
aoqi@0 328 visitSymbol(outerThisStack.head);
aoqi@0 329 super.visitNewClass(tree);
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 /** If tree refers to a qualified this or super expression
aoqi@0 333 * for anything but the current class, add the outer this
aoqi@0 334 * stack as a free variable.
aoqi@0 335 */
aoqi@0 336 public void visitSelect(JCFieldAccess tree) {
aoqi@0 337 if ((tree.name == names._this || tree.name == names._super) &&
aoqi@0 338 tree.selected.type.tsym != clazz &&
aoqi@0 339 outerThisStack.head != null)
aoqi@0 340 visitSymbol(outerThisStack.head);
aoqi@0 341 super.visitSelect(tree);
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 /** If tree refers to a superclass constructor call,
aoqi@0 345 * add all free variables of the superclass.
aoqi@0 346 */
aoqi@0 347 public void visitApply(JCMethodInvocation tree) {
aoqi@0 348 if (TreeInfo.name(tree.meth) == names._super) {
aoqi@0 349 Symbol constructor = TreeInfo.symbol(tree.meth);
aoqi@0 350 ClassSymbol c = (ClassSymbol)constructor.owner;
aoqi@0 351 if (c.hasOuterInstance() &&
aoqi@0 352 !tree.meth.hasTag(SELECT) &&
aoqi@0 353 outerThisStack.head != null)
aoqi@0 354 visitSymbol(outerThisStack.head);
aoqi@0 355 }
aoqi@0 356 super.visitApply(tree);
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 ClassSymbol ownerToCopyFreeVarsFrom(ClassSymbol c) {
aoqi@0 361 if (!c.isLocal()) {
aoqi@0 362 return null;
aoqi@0 363 }
aoqi@0 364 Symbol currentOwner = c.owner;
aoqi@0 365 while ((currentOwner.owner.kind & TYP) != 0 && currentOwner.isLocal()) {
aoqi@0 366 currentOwner = currentOwner.owner;
aoqi@0 367 }
aoqi@0 368 if ((currentOwner.owner.kind & (VAR | MTH)) != 0 && c.isSubClass(currentOwner, types)) {
aoqi@0 369 return (ClassSymbol)currentOwner;
aoqi@0 370 }
aoqi@0 371 return null;
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 /** Return the variables accessed from within a local class, which
aoqi@0 375 * are declared in the local class' owner.
aoqi@0 376 * (in reverse order of first access).
aoqi@0 377 */
aoqi@0 378 List<VarSymbol> freevars(ClassSymbol c) {
aoqi@0 379 List<VarSymbol> fvs = freevarCache.get(c);
aoqi@0 380 if (fvs != null) {
aoqi@0 381 return fvs;
aoqi@0 382 }
aoqi@0 383 if ((c.owner.kind & (VAR | MTH)) != 0) {
aoqi@0 384 FreeVarCollector collector = new FreeVarCollector(c);
aoqi@0 385 collector.scan(classDef(c));
aoqi@0 386 fvs = collector.fvs;
aoqi@0 387 freevarCache.put(c, fvs);
aoqi@0 388 return fvs;
aoqi@0 389 } else {
aoqi@0 390 ClassSymbol owner = ownerToCopyFreeVarsFrom(c);
aoqi@0 391 if (owner != null) {
aoqi@0 392 fvs = freevarCache.get(owner);
aoqi@0 393 freevarCache.put(c, fvs);
aoqi@0 394 return fvs;
aoqi@0 395 } else {
aoqi@0 396 return List.nil();
aoqi@0 397 }
aoqi@0 398 }
aoqi@0 399 }
aoqi@0 400
aoqi@0 401 Map<TypeSymbol,EnumMapping> enumSwitchMap = new LinkedHashMap<TypeSymbol,EnumMapping>();
aoqi@0 402
aoqi@0 403 EnumMapping mapForEnum(DiagnosticPosition pos, TypeSymbol enumClass) {
aoqi@0 404 EnumMapping map = enumSwitchMap.get(enumClass);
aoqi@0 405 if (map == null)
aoqi@0 406 enumSwitchMap.put(enumClass, map = new EnumMapping(pos, enumClass));
aoqi@0 407 return map;
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 /** This map gives a translation table to be used for enum
aoqi@0 411 * switches.
aoqi@0 412 *
aoqi@0 413 * <p>For each enum that appears as the type of a switch
aoqi@0 414 * expression, we maintain an EnumMapping to assist in the
aoqi@0 415 * translation, as exemplified by the following example:
aoqi@0 416 *
aoqi@0 417 * <p>we translate
aoqi@0 418 * <pre>
aoqi@0 419 * switch(colorExpression) {
aoqi@0 420 * case red: stmt1;
aoqi@0 421 * case green: stmt2;
aoqi@0 422 * }
aoqi@0 423 * </pre>
aoqi@0 424 * into
aoqi@0 425 * <pre>
aoqi@0 426 * switch(Outer$0.$EnumMap$Color[colorExpression.ordinal()]) {
aoqi@0 427 * case 1: stmt1;
aoqi@0 428 * case 2: stmt2
aoqi@0 429 * }
aoqi@0 430 * </pre>
aoqi@0 431 * with the auxiliary table initialized as follows:
aoqi@0 432 * <pre>
aoqi@0 433 * class Outer$0 {
aoqi@0 434 * synthetic final int[] $EnumMap$Color = new int[Color.values().length];
aoqi@0 435 * static {
aoqi@0 436 * try { $EnumMap$Color[red.ordinal()] = 1; } catch (NoSuchFieldError ex) {}
aoqi@0 437 * try { $EnumMap$Color[green.ordinal()] = 2; } catch (NoSuchFieldError ex) {}
aoqi@0 438 * }
aoqi@0 439 * }
aoqi@0 440 * </pre>
aoqi@0 441 * class EnumMapping provides mapping data and support methods for this translation.
aoqi@0 442 */
aoqi@0 443 class EnumMapping {
aoqi@0 444 EnumMapping(DiagnosticPosition pos, TypeSymbol forEnum) {
aoqi@0 445 this.forEnum = forEnum;
aoqi@0 446 this.values = new LinkedHashMap<VarSymbol,Integer>();
aoqi@0 447 this.pos = pos;
aoqi@0 448 Name varName = names
aoqi@0 449 .fromString(target.syntheticNameChar() +
aoqi@0 450 "SwitchMap" +
aoqi@0 451 target.syntheticNameChar() +
aoqi@0 452 writer.xClassName(forEnum.type).toString()
aoqi@0 453 .replace('/', '.')
aoqi@0 454 .replace('.', target.syntheticNameChar()));
aoqi@0 455 ClassSymbol outerCacheClass = outerCacheClass();
aoqi@0 456 this.mapVar = new VarSymbol(STATIC | SYNTHETIC | FINAL,
aoqi@0 457 varName,
aoqi@0 458 new ArrayType(syms.intType, syms.arrayClass),
aoqi@0 459 outerCacheClass);
aoqi@0 460 enterSynthetic(pos, mapVar, outerCacheClass.members());
aoqi@0 461 }
aoqi@0 462
aoqi@0 463 DiagnosticPosition pos = null;
aoqi@0 464
aoqi@0 465 // the next value to use
aoqi@0 466 int next = 1; // 0 (unused map elements) go to the default label
aoqi@0 467
aoqi@0 468 // the enum for which this is a map
aoqi@0 469 final TypeSymbol forEnum;
aoqi@0 470
aoqi@0 471 // the field containing the map
aoqi@0 472 final VarSymbol mapVar;
aoqi@0 473
aoqi@0 474 // the mapped values
aoqi@0 475 final Map<VarSymbol,Integer> values;
aoqi@0 476
aoqi@0 477 JCLiteral forConstant(VarSymbol v) {
aoqi@0 478 Integer result = values.get(v);
aoqi@0 479 if (result == null)
aoqi@0 480 values.put(v, result = next++);
aoqi@0 481 return make.Literal(result);
aoqi@0 482 }
aoqi@0 483
aoqi@0 484 // generate the field initializer for the map
aoqi@0 485 void translate() {
aoqi@0 486 make.at(pos.getStartPosition());
aoqi@0 487 JCClassDecl owner = classDef((ClassSymbol)mapVar.owner);
aoqi@0 488
aoqi@0 489 // synthetic static final int[] $SwitchMap$Color = new int[Color.values().length];
aoqi@0 490 MethodSymbol valuesMethod = lookupMethod(pos,
aoqi@0 491 names.values,
aoqi@0 492 forEnum.type,
aoqi@0 493 List.<Type>nil());
aoqi@0 494 JCExpression size = make // Color.values().length
aoqi@0 495 .Select(make.App(make.QualIdent(valuesMethod)),
aoqi@0 496 syms.lengthVar);
aoqi@0 497 JCExpression mapVarInit = make
aoqi@0 498 .NewArray(make.Type(syms.intType), List.of(size), null)
aoqi@0 499 .setType(new ArrayType(syms.intType, syms.arrayClass));
aoqi@0 500
aoqi@0 501 // try { $SwitchMap$Color[red.ordinal()] = 1; } catch (java.lang.NoSuchFieldError ex) {}
aoqi@0 502 ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
aoqi@0 503 Symbol ordinalMethod = lookupMethod(pos,
aoqi@0 504 names.ordinal,
aoqi@0 505 forEnum.type,
aoqi@0 506 List.<Type>nil());
aoqi@0 507 List<JCCatch> catcher = List.<JCCatch>nil()
aoqi@0 508 .prepend(make.Catch(make.VarDef(new VarSymbol(PARAMETER, names.ex,
aoqi@0 509 syms.noSuchFieldErrorType,
aoqi@0 510 syms.noSymbol),
aoqi@0 511 null),
aoqi@0 512 make.Block(0, List.<JCStatement>nil())));
aoqi@0 513 for (Map.Entry<VarSymbol,Integer> e : values.entrySet()) {
aoqi@0 514 VarSymbol enumerator = e.getKey();
aoqi@0 515 Integer mappedValue = e.getValue();
aoqi@0 516 JCExpression assign = make
aoqi@0 517 .Assign(make.Indexed(mapVar,
aoqi@0 518 make.App(make.Select(make.QualIdent(enumerator),
aoqi@0 519 ordinalMethod))),
aoqi@0 520 make.Literal(mappedValue))
aoqi@0 521 .setType(syms.intType);
aoqi@0 522 JCStatement exec = make.Exec(assign);
aoqi@0 523 JCStatement _try = make.Try(make.Block(0, List.of(exec)), catcher, null);
aoqi@0 524 stmts.append(_try);
aoqi@0 525 }
aoqi@0 526
aoqi@0 527 owner.defs = owner.defs
aoqi@0 528 .prepend(make.Block(STATIC, stmts.toList()))
aoqi@0 529 .prepend(make.VarDef(mapVar, mapVarInit));
aoqi@0 530 }
aoqi@0 531 }
aoqi@0 532
aoqi@0 533
aoqi@0 534 /**************************************************************************
aoqi@0 535 * Tree building blocks
aoqi@0 536 *************************************************************************/
aoqi@0 537
aoqi@0 538 /** Equivalent to make.at(pos.getStartPosition()) with side effect of caching
aoqi@0 539 * pos as make_pos, for use in diagnostics.
aoqi@0 540 **/
aoqi@0 541 TreeMaker make_at(DiagnosticPosition pos) {
aoqi@0 542 make_pos = pos;
aoqi@0 543 return make.at(pos);
aoqi@0 544 }
aoqi@0 545
aoqi@0 546 /** Make an attributed tree representing a literal. This will be an
aoqi@0 547 * Ident node in the case of boolean literals, a Literal node in all
aoqi@0 548 * other cases.
aoqi@0 549 * @param type The literal's type.
aoqi@0 550 * @param value The literal's value.
aoqi@0 551 */
aoqi@0 552 JCExpression makeLit(Type type, Object value) {
aoqi@0 553 return make.Literal(type.getTag(), value).setType(type.constType(value));
aoqi@0 554 }
aoqi@0 555
aoqi@0 556 /** Make an attributed tree representing null.
aoqi@0 557 */
aoqi@0 558 JCExpression makeNull() {
aoqi@0 559 return makeLit(syms.botType, null);
aoqi@0 560 }
aoqi@0 561
aoqi@0 562 /** Make an attributed class instance creation expression.
aoqi@0 563 * @param ctype The class type.
aoqi@0 564 * @param args The constructor arguments.
aoqi@0 565 */
aoqi@0 566 JCNewClass makeNewClass(Type ctype, List<JCExpression> args) {
aoqi@0 567 JCNewClass tree = make.NewClass(null,
aoqi@0 568 null, make.QualIdent(ctype.tsym), args, null);
aoqi@0 569 tree.constructor = rs.resolveConstructor(
aoqi@0 570 make_pos, attrEnv, ctype, TreeInfo.types(args), List.<Type>nil());
aoqi@0 571 tree.type = ctype;
aoqi@0 572 return tree;
aoqi@0 573 }
aoqi@0 574
aoqi@0 575 /** Make an attributed unary expression.
aoqi@0 576 * @param optag The operators tree tag.
aoqi@0 577 * @param arg The operator's argument.
aoqi@0 578 */
aoqi@0 579 JCUnary makeUnary(JCTree.Tag optag, JCExpression arg) {
aoqi@0 580 JCUnary tree = make.Unary(optag, arg);
aoqi@0 581 tree.operator = rs.resolveUnaryOperator(
aoqi@0 582 make_pos, optag, attrEnv, arg.type);
aoqi@0 583 tree.type = tree.operator.type.getReturnType();
aoqi@0 584 return tree;
aoqi@0 585 }
aoqi@0 586
aoqi@0 587 /** Make an attributed binary expression.
aoqi@0 588 * @param optag The operators tree tag.
aoqi@0 589 * @param lhs The operator's left argument.
aoqi@0 590 * @param rhs The operator's right argument.
aoqi@0 591 */
aoqi@0 592 JCBinary makeBinary(JCTree.Tag optag, JCExpression lhs, JCExpression rhs) {
aoqi@0 593 JCBinary tree = make.Binary(optag, lhs, rhs);
aoqi@0 594 tree.operator = rs.resolveBinaryOperator(
aoqi@0 595 make_pos, optag, attrEnv, lhs.type, rhs.type);
aoqi@0 596 tree.type = tree.operator.type.getReturnType();
aoqi@0 597 return tree;
aoqi@0 598 }
aoqi@0 599
aoqi@0 600 /** Make an attributed assignop expression.
aoqi@0 601 * @param optag The operators tree tag.
aoqi@0 602 * @param lhs The operator's left argument.
aoqi@0 603 * @param rhs The operator's right argument.
aoqi@0 604 */
aoqi@0 605 JCAssignOp makeAssignop(JCTree.Tag optag, JCTree lhs, JCTree rhs) {
aoqi@0 606 JCAssignOp tree = make.Assignop(optag, lhs, rhs);
aoqi@0 607 tree.operator = rs.resolveBinaryOperator(
aoqi@0 608 make_pos, tree.getTag().noAssignOp(), attrEnv, lhs.type, rhs.type);
aoqi@0 609 tree.type = lhs.type;
aoqi@0 610 return tree;
aoqi@0 611 }
aoqi@0 612
aoqi@0 613 /** Convert tree into string object, unless it has already a
aoqi@0 614 * reference type..
aoqi@0 615 */
aoqi@0 616 JCExpression makeString(JCExpression tree) {
aoqi@0 617 if (!tree.type.isPrimitiveOrVoid()) {
aoqi@0 618 return tree;
aoqi@0 619 } else {
aoqi@0 620 Symbol valueOfSym = lookupMethod(tree.pos(),
aoqi@0 621 names.valueOf,
aoqi@0 622 syms.stringType,
aoqi@0 623 List.of(tree.type));
aoqi@0 624 return make.App(make.QualIdent(valueOfSym), List.of(tree));
aoqi@0 625 }
aoqi@0 626 }
aoqi@0 627
aoqi@0 628 /** Create an empty anonymous class definition and enter and complete
aoqi@0 629 * its symbol. Return the class definition's symbol.
aoqi@0 630 * and create
aoqi@0 631 * @param flags The class symbol's flags
aoqi@0 632 * @param owner The class symbol's owner
aoqi@0 633 */
aoqi@0 634 JCClassDecl makeEmptyClass(long flags, ClassSymbol owner) {
aoqi@0 635 return makeEmptyClass(flags, owner, null, true);
aoqi@0 636 }
aoqi@0 637
aoqi@0 638 JCClassDecl makeEmptyClass(long flags, ClassSymbol owner, Name flatname,
aoqi@0 639 boolean addToDefs) {
aoqi@0 640 // Create class symbol.
aoqi@0 641 ClassSymbol c = reader.defineClass(names.empty, owner);
aoqi@0 642 if (flatname != null) {
aoqi@0 643 c.flatname = flatname;
aoqi@0 644 } else {
aoqi@0 645 c.flatname = chk.localClassName(c);
aoqi@0 646 }
aoqi@0 647 c.sourcefile = owner.sourcefile;
aoqi@0 648 c.completer = null;
aoqi@0 649 c.members_field = new Scope(c);
aoqi@0 650 c.flags_field = flags;
aoqi@0 651 ClassType ctype = (ClassType) c.type;
aoqi@0 652 ctype.supertype_field = syms.objectType;
aoqi@0 653 ctype.interfaces_field = List.nil();
aoqi@0 654
aoqi@0 655 JCClassDecl odef = classDef(owner);
aoqi@0 656
aoqi@0 657 // Enter class symbol in owner scope and compiled table.
aoqi@0 658 enterSynthetic(odef.pos(), c, owner.members());
aoqi@0 659 chk.compiled.put(c.flatname, c);
aoqi@0 660
aoqi@0 661 // Create class definition tree.
aoqi@0 662 JCClassDecl cdef = make.ClassDef(
aoqi@0 663 make.Modifiers(flags), names.empty,
aoqi@0 664 List.<JCTypeParameter>nil(),
aoqi@0 665 null, List.<JCExpression>nil(), List.<JCTree>nil());
aoqi@0 666 cdef.sym = c;
aoqi@0 667 cdef.type = c.type;
aoqi@0 668
aoqi@0 669 // Append class definition tree to owner's definitions.
aoqi@0 670 if (addToDefs) odef.defs = odef.defs.prepend(cdef);
aoqi@0 671 return cdef;
aoqi@0 672 }
aoqi@0 673
aoqi@0 674 /**************************************************************************
aoqi@0 675 * Symbol manipulation utilities
aoqi@0 676 *************************************************************************/
aoqi@0 677
aoqi@0 678 /** Enter a synthetic symbol in a given scope, but complain if there was already one there.
aoqi@0 679 * @param pos Position for error reporting.
aoqi@0 680 * @param sym The symbol.
aoqi@0 681 * @param s The scope.
aoqi@0 682 */
aoqi@0 683 private void enterSynthetic(DiagnosticPosition pos, Symbol sym, Scope s) {
aoqi@0 684 s.enter(sym);
aoqi@0 685 }
aoqi@0 686
aoqi@0 687 /** Create a fresh synthetic name within a given scope - the unique name is
aoqi@0 688 * obtained by appending '$' chars at the end of the name until no match
aoqi@0 689 * is found.
aoqi@0 690 *
aoqi@0 691 * @param name base name
aoqi@0 692 * @param s scope in which the name has to be unique
aoqi@0 693 * @return fresh synthetic name
aoqi@0 694 */
aoqi@0 695 private Name makeSyntheticName(Name name, Scope s) {
aoqi@0 696 do {
aoqi@0 697 name = name.append(
aoqi@0 698 target.syntheticNameChar(),
aoqi@0 699 names.empty);
aoqi@0 700 } while (lookupSynthetic(name, s) != null);
aoqi@0 701 return name;
aoqi@0 702 }
aoqi@0 703
aoqi@0 704 /** Check whether synthetic symbols generated during lowering conflict
aoqi@0 705 * with user-defined symbols.
aoqi@0 706 *
aoqi@0 707 * @param translatedTrees lowered class trees
aoqi@0 708 */
aoqi@0 709 void checkConflicts(List<JCTree> translatedTrees) {
aoqi@0 710 for (JCTree t : translatedTrees) {
aoqi@0 711 t.accept(conflictsChecker);
aoqi@0 712 }
aoqi@0 713 }
aoqi@0 714
aoqi@0 715 JCTree.Visitor conflictsChecker = new TreeScanner() {
aoqi@0 716
aoqi@0 717 TypeSymbol currentClass;
aoqi@0 718
aoqi@0 719 @Override
aoqi@0 720 public void visitMethodDef(JCMethodDecl that) {
aoqi@0 721 chk.checkConflicts(that.pos(), that.sym, currentClass);
aoqi@0 722 super.visitMethodDef(that);
aoqi@0 723 }
aoqi@0 724
aoqi@0 725 @Override
aoqi@0 726 public void visitVarDef(JCVariableDecl that) {
aoqi@0 727 if (that.sym.owner.kind == TYP) {
aoqi@0 728 chk.checkConflicts(that.pos(), that.sym, currentClass);
aoqi@0 729 }
aoqi@0 730 super.visitVarDef(that);
aoqi@0 731 }
aoqi@0 732
aoqi@0 733 @Override
aoqi@0 734 public void visitClassDef(JCClassDecl that) {
aoqi@0 735 TypeSymbol prevCurrentClass = currentClass;
aoqi@0 736 currentClass = that.sym;
aoqi@0 737 try {
aoqi@0 738 super.visitClassDef(that);
aoqi@0 739 }
aoqi@0 740 finally {
aoqi@0 741 currentClass = prevCurrentClass;
aoqi@0 742 }
aoqi@0 743 }
aoqi@0 744 };
aoqi@0 745
aoqi@0 746 /** Look up a synthetic name in a given scope.
aoqi@0 747 * @param s The scope.
aoqi@0 748 * @param name The name.
aoqi@0 749 */
aoqi@0 750 private Symbol lookupSynthetic(Name name, Scope s) {
aoqi@0 751 Symbol sym = s.lookup(name).sym;
aoqi@0 752 return (sym==null || (sym.flags()&SYNTHETIC)==0) ? null : sym;
aoqi@0 753 }
aoqi@0 754
aoqi@0 755 /** Look up a method in a given scope.
aoqi@0 756 */
aoqi@0 757 private MethodSymbol lookupMethod(DiagnosticPosition pos, Name name, Type qual, List<Type> args) {
aoqi@0 758 return rs.resolveInternalMethod(pos, attrEnv, qual, name, args, List.<Type>nil());
aoqi@0 759 }
aoqi@0 760
aoqi@0 761 /** Look up a constructor.
aoqi@0 762 */
aoqi@0 763 private MethodSymbol lookupConstructor(DiagnosticPosition pos, Type qual, List<Type> args) {
aoqi@0 764 return rs.resolveInternalConstructor(pos, attrEnv, qual, args, null);
aoqi@0 765 }
aoqi@0 766
aoqi@0 767 /** Look up a field.
aoqi@0 768 */
aoqi@0 769 private VarSymbol lookupField(DiagnosticPosition pos, Type qual, Name name) {
aoqi@0 770 return rs.resolveInternalField(pos, attrEnv, qual, name);
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 /** Anon inner classes are used as access constructor tags.
aoqi@0 774 * accessConstructorTag will use an existing anon class if one is available,
aoqi@0 775 * and synthethise a class (with makeEmptyClass) if one is not available.
aoqi@0 776 * However, there is a small possibility that an existing class will not
aoqi@0 777 * be generated as expected if it is inside a conditional with a constant
aoqi@0 778 * expression. If that is found to be the case, create an empty class tree here.
aoqi@0 779 */
aoqi@0 780 private void checkAccessConstructorTags() {
aoqi@0 781 for (List<ClassSymbol> l = accessConstrTags; l.nonEmpty(); l = l.tail) {
aoqi@0 782 ClassSymbol c = l.head;
aoqi@0 783 if (isTranslatedClassAvailable(c))
aoqi@0 784 continue;
aoqi@0 785 // Create class definition tree.
aoqi@0 786 JCClassDecl cdec = makeEmptyClass(STATIC | SYNTHETIC,
aoqi@0 787 c.outermostClass(), c.flatname, false);
aoqi@0 788 swapAccessConstructorTag(c, cdec.sym);
aoqi@0 789 translated.append(cdec);
aoqi@0 790 }
aoqi@0 791 }
aoqi@0 792 // where
aoqi@0 793 private boolean isTranslatedClassAvailable(ClassSymbol c) {
aoqi@0 794 for (JCTree tree: translated) {
aoqi@0 795 if (tree.hasTag(CLASSDEF)
aoqi@0 796 && ((JCClassDecl) tree).sym == c) {
aoqi@0 797 return true;
aoqi@0 798 }
aoqi@0 799 }
aoqi@0 800 return false;
aoqi@0 801 }
aoqi@0 802
aoqi@0 803 void swapAccessConstructorTag(ClassSymbol oldCTag, ClassSymbol newCTag) {
aoqi@0 804 for (MethodSymbol methodSymbol : accessConstrs.values()) {
aoqi@0 805 Assert.check(methodSymbol.type.hasTag(METHOD));
aoqi@0 806 MethodType oldMethodType =
aoqi@0 807 (MethodType)methodSymbol.type;
aoqi@0 808 if (oldMethodType.argtypes.head.tsym == oldCTag)
aoqi@0 809 methodSymbol.type =
aoqi@0 810 types.createMethodTypeWithParameters(oldMethodType,
aoqi@0 811 oldMethodType.getParameterTypes().tail
aoqi@0 812 .prepend(newCTag.erasure(types)));
aoqi@0 813 }
aoqi@0 814 }
aoqi@0 815
aoqi@0 816 /**************************************************************************
aoqi@0 817 * Access methods
aoqi@0 818 *************************************************************************/
aoqi@0 819
aoqi@0 820 /** Access codes for dereferencing, assignment,
aoqi@0 821 * and pre/post increment/decrement.
aoqi@0 822 * Access codes for assignment operations are determined by method accessCode
aoqi@0 823 * below.
aoqi@0 824 *
aoqi@0 825 * All access codes for accesses to the current class are even.
aoqi@0 826 * If a member of the superclass should be accessed instead (because
aoqi@0 827 * access was via a qualified super), add one to the corresponding code
aoqi@0 828 * for the current class, making the number odd.
aoqi@0 829 * This numbering scheme is used by the backend to decide whether
aoqi@0 830 * to issue an invokevirtual or invokespecial call.
aoqi@0 831 *
aoqi@0 832 * @see Gen#visitSelect(JCFieldAccess tree)
aoqi@0 833 */
aoqi@0 834 private static final int
aoqi@0 835 DEREFcode = 0,
aoqi@0 836 ASSIGNcode = 2,
aoqi@0 837 PREINCcode = 4,
aoqi@0 838 PREDECcode = 6,
aoqi@0 839 POSTINCcode = 8,
aoqi@0 840 POSTDECcode = 10,
aoqi@0 841 FIRSTASGOPcode = 12;
aoqi@0 842
aoqi@0 843 /** Number of access codes
aoqi@0 844 */
aoqi@0 845 private static final int NCODES = accessCode(ByteCodes.lushrl) + 2;
aoqi@0 846
aoqi@0 847 /** A mapping from symbols to their access numbers.
aoqi@0 848 */
aoqi@0 849 private Map<Symbol,Integer> accessNums;
aoqi@0 850
aoqi@0 851 /** A mapping from symbols to an array of access symbols, indexed by
aoqi@0 852 * access code.
aoqi@0 853 */
aoqi@0 854 private Map<Symbol,MethodSymbol[]> accessSyms;
aoqi@0 855
aoqi@0 856 /** A mapping from (constructor) symbols to access constructor symbols.
aoqi@0 857 */
aoqi@0 858 private Map<Symbol,MethodSymbol> accessConstrs;
aoqi@0 859
aoqi@0 860 /** A list of all class symbols used for access constructor tags.
aoqi@0 861 */
aoqi@0 862 private List<ClassSymbol> accessConstrTags;
aoqi@0 863
aoqi@0 864 /** A queue for all accessed symbols.
aoqi@0 865 */
aoqi@0 866 private ListBuffer<Symbol> accessed;
aoqi@0 867
aoqi@0 868 /** Map bytecode of binary operation to access code of corresponding
aoqi@0 869 * assignment operation. This is always an even number.
aoqi@0 870 */
aoqi@0 871 private static int accessCode(int bytecode) {
aoqi@0 872 if (ByteCodes.iadd <= bytecode && bytecode <= ByteCodes.lxor)
aoqi@0 873 return (bytecode - iadd) * 2 + FIRSTASGOPcode;
aoqi@0 874 else if (bytecode == ByteCodes.string_add)
aoqi@0 875 return (ByteCodes.lxor + 1 - iadd) * 2 + FIRSTASGOPcode;
aoqi@0 876 else if (ByteCodes.ishll <= bytecode && bytecode <= ByteCodes.lushrl)
aoqi@0 877 return (bytecode - ishll + ByteCodes.lxor + 2 - iadd) * 2 + FIRSTASGOPcode;
aoqi@0 878 else
aoqi@0 879 return -1;
aoqi@0 880 }
aoqi@0 881
aoqi@0 882 /** return access code for identifier,
aoqi@0 883 * @param tree The tree representing the identifier use.
aoqi@0 884 * @param enclOp The closest enclosing operation node of tree,
aoqi@0 885 * null if tree is not a subtree of an operation.
aoqi@0 886 */
aoqi@0 887 private static int accessCode(JCTree tree, JCTree enclOp) {
aoqi@0 888 if (enclOp == null)
aoqi@0 889 return DEREFcode;
aoqi@0 890 else if (enclOp.hasTag(ASSIGN) &&
aoqi@0 891 tree == TreeInfo.skipParens(((JCAssign) enclOp).lhs))
aoqi@0 892 return ASSIGNcode;
aoqi@0 893 else if (enclOp.getTag().isIncOrDecUnaryOp() &&
aoqi@0 894 tree == TreeInfo.skipParens(((JCUnary) enclOp).arg))
aoqi@0 895 return mapTagToUnaryOpCode(enclOp.getTag());
aoqi@0 896 else if (enclOp.getTag().isAssignop() &&
aoqi@0 897 tree == TreeInfo.skipParens(((JCAssignOp) enclOp).lhs))
aoqi@0 898 return accessCode(((OperatorSymbol) ((JCAssignOp) enclOp).operator).opcode);
aoqi@0 899 else
aoqi@0 900 return DEREFcode;
aoqi@0 901 }
aoqi@0 902
aoqi@0 903 /** Return binary operator that corresponds to given access code.
aoqi@0 904 */
aoqi@0 905 private OperatorSymbol binaryAccessOperator(int acode) {
aoqi@0 906 for (Scope.Entry e = syms.predefClass.members().elems;
aoqi@0 907 e != null;
aoqi@0 908 e = e.sibling) {
aoqi@0 909 if (e.sym instanceof OperatorSymbol) {
aoqi@0 910 OperatorSymbol op = (OperatorSymbol)e.sym;
aoqi@0 911 if (accessCode(op.opcode) == acode) return op;
aoqi@0 912 }
aoqi@0 913 }
aoqi@0 914 return null;
aoqi@0 915 }
aoqi@0 916
aoqi@0 917 /** Return tree tag for assignment operation corresponding
aoqi@0 918 * to given binary operator.
aoqi@0 919 */
aoqi@0 920 private static JCTree.Tag treeTag(OperatorSymbol operator) {
aoqi@0 921 switch (operator.opcode) {
aoqi@0 922 case ByteCodes.ior: case ByteCodes.lor:
aoqi@0 923 return BITOR_ASG;
aoqi@0 924 case ByteCodes.ixor: case ByteCodes.lxor:
aoqi@0 925 return BITXOR_ASG;
aoqi@0 926 case ByteCodes.iand: case ByteCodes.land:
aoqi@0 927 return BITAND_ASG;
aoqi@0 928 case ByteCodes.ishl: case ByteCodes.lshl:
aoqi@0 929 case ByteCodes.ishll: case ByteCodes.lshll:
aoqi@0 930 return SL_ASG;
aoqi@0 931 case ByteCodes.ishr: case ByteCodes.lshr:
aoqi@0 932 case ByteCodes.ishrl: case ByteCodes.lshrl:
aoqi@0 933 return SR_ASG;
aoqi@0 934 case ByteCodes.iushr: case ByteCodes.lushr:
aoqi@0 935 case ByteCodes.iushrl: case ByteCodes.lushrl:
aoqi@0 936 return USR_ASG;
aoqi@0 937 case ByteCodes.iadd: case ByteCodes.ladd:
aoqi@0 938 case ByteCodes.fadd: case ByteCodes.dadd:
aoqi@0 939 case ByteCodes.string_add:
aoqi@0 940 return PLUS_ASG;
aoqi@0 941 case ByteCodes.isub: case ByteCodes.lsub:
aoqi@0 942 case ByteCodes.fsub: case ByteCodes.dsub:
aoqi@0 943 return MINUS_ASG;
aoqi@0 944 case ByteCodes.imul: case ByteCodes.lmul:
aoqi@0 945 case ByteCodes.fmul: case ByteCodes.dmul:
aoqi@0 946 return MUL_ASG;
aoqi@0 947 case ByteCodes.idiv: case ByteCodes.ldiv:
aoqi@0 948 case ByteCodes.fdiv: case ByteCodes.ddiv:
aoqi@0 949 return DIV_ASG;
aoqi@0 950 case ByteCodes.imod: case ByteCodes.lmod:
aoqi@0 951 case ByteCodes.fmod: case ByteCodes.dmod:
aoqi@0 952 return MOD_ASG;
aoqi@0 953 default:
aoqi@0 954 throw new AssertionError();
aoqi@0 955 }
aoqi@0 956 }
aoqi@0 957
aoqi@0 958 /** The name of the access method with number `anum' and access code `acode'.
aoqi@0 959 */
aoqi@0 960 Name accessName(int anum, int acode) {
aoqi@0 961 return names.fromString(
aoqi@0 962 "access" + target.syntheticNameChar() + anum + acode / 10 + acode % 10);
aoqi@0 963 }
aoqi@0 964
aoqi@0 965 /** Return access symbol for a private or protected symbol from an inner class.
aoqi@0 966 * @param sym The accessed private symbol.
aoqi@0 967 * @param tree The accessing tree.
aoqi@0 968 * @param enclOp The closest enclosing operation node of tree,
aoqi@0 969 * null if tree is not a subtree of an operation.
aoqi@0 970 * @param protAccess Is access to a protected symbol in another
aoqi@0 971 * package?
aoqi@0 972 * @param refSuper Is access via a (qualified) C.super?
aoqi@0 973 */
aoqi@0 974 MethodSymbol accessSymbol(Symbol sym, JCTree tree, JCTree enclOp,
aoqi@0 975 boolean protAccess, boolean refSuper) {
aoqi@0 976 ClassSymbol accOwner = refSuper && protAccess
aoqi@0 977 // For access via qualified super (T.super.x), place the
aoqi@0 978 // access symbol on T.
aoqi@0 979 ? (ClassSymbol)((JCFieldAccess) tree).selected.type.tsym
aoqi@0 980 // Otherwise pretend that the owner of an accessed
aoqi@0 981 // protected symbol is the enclosing class of the current
aoqi@0 982 // class which is a subclass of the symbol's owner.
aoqi@0 983 : accessClass(sym, protAccess, tree);
aoqi@0 984
aoqi@0 985 Symbol vsym = sym;
aoqi@0 986 if (sym.owner != accOwner) {
aoqi@0 987 vsym = sym.clone(accOwner);
aoqi@0 988 actualSymbols.put(vsym, sym);
aoqi@0 989 }
aoqi@0 990
aoqi@0 991 Integer anum // The access number of the access method.
aoqi@0 992 = accessNums.get(vsym);
aoqi@0 993 if (anum == null) {
aoqi@0 994 anum = accessed.length();
aoqi@0 995 accessNums.put(vsym, anum);
aoqi@0 996 accessSyms.put(vsym, new MethodSymbol[NCODES]);
aoqi@0 997 accessed.append(vsym);
aoqi@0 998 // System.out.println("accessing " + vsym + " in " + vsym.location());
aoqi@0 999 }
aoqi@0 1000
aoqi@0 1001 int acode; // The access code of the access method.
aoqi@0 1002 List<Type> argtypes; // The argument types of the access method.
aoqi@0 1003 Type restype; // The result type of the access method.
aoqi@0 1004 List<Type> thrown; // The thrown exceptions of the access method.
aoqi@0 1005 switch (vsym.kind) {
aoqi@0 1006 case VAR:
aoqi@0 1007 acode = accessCode(tree, enclOp);
aoqi@0 1008 if (acode >= FIRSTASGOPcode) {
aoqi@0 1009 OperatorSymbol operator = binaryAccessOperator(acode);
aoqi@0 1010 if (operator.opcode == string_add)
aoqi@0 1011 argtypes = List.of(syms.objectType);
aoqi@0 1012 else
aoqi@0 1013 argtypes = operator.type.getParameterTypes().tail;
aoqi@0 1014 } else if (acode == ASSIGNcode)
aoqi@0 1015 argtypes = List.of(vsym.erasure(types));
aoqi@0 1016 else
aoqi@0 1017 argtypes = List.nil();
aoqi@0 1018 restype = vsym.erasure(types);
aoqi@0 1019 thrown = List.nil();
aoqi@0 1020 break;
aoqi@0 1021 case MTH:
aoqi@0 1022 acode = DEREFcode;
aoqi@0 1023 argtypes = vsym.erasure(types).getParameterTypes();
aoqi@0 1024 restype = vsym.erasure(types).getReturnType();
aoqi@0 1025 thrown = vsym.type.getThrownTypes();
aoqi@0 1026 break;
aoqi@0 1027 default:
aoqi@0 1028 throw new AssertionError();
aoqi@0 1029 }
aoqi@0 1030
aoqi@0 1031 // For references via qualified super, increment acode by one,
aoqi@0 1032 // making it odd.
aoqi@0 1033 if (protAccess && refSuper) acode++;
aoqi@0 1034
aoqi@0 1035 // Instance access methods get instance as first parameter.
aoqi@0 1036 // For protected symbols this needs to be the instance as a member
aoqi@0 1037 // of the type containing the accessed symbol, not the class
aoqi@0 1038 // containing the access method.
aoqi@0 1039 if ((vsym.flags() & STATIC) == 0) {
aoqi@0 1040 argtypes = argtypes.prepend(vsym.owner.erasure(types));
aoqi@0 1041 }
aoqi@0 1042 MethodSymbol[] accessors = accessSyms.get(vsym);
aoqi@0 1043 MethodSymbol accessor = accessors[acode];
aoqi@0 1044 if (accessor == null) {
aoqi@0 1045 accessor = new MethodSymbol(
aoqi@0 1046 STATIC | SYNTHETIC,
aoqi@0 1047 accessName(anum.intValue(), acode),
aoqi@0 1048 new MethodType(argtypes, restype, thrown, syms.methodClass),
aoqi@0 1049 accOwner);
aoqi@0 1050 enterSynthetic(tree.pos(), accessor, accOwner.members());
aoqi@0 1051 accessors[acode] = accessor;
aoqi@0 1052 }
aoqi@0 1053 return accessor;
aoqi@0 1054 }
aoqi@0 1055
aoqi@0 1056 /** The qualifier to be used for accessing a symbol in an outer class.
aoqi@0 1057 * This is either C.sym or C.this.sym, depending on whether or not
aoqi@0 1058 * sym is static.
aoqi@0 1059 * @param sym The accessed symbol.
aoqi@0 1060 */
aoqi@0 1061 JCExpression accessBase(DiagnosticPosition pos, Symbol sym) {
aoqi@0 1062 return (sym.flags() & STATIC) != 0
aoqi@0 1063 ? access(make.at(pos.getStartPosition()).QualIdent(sym.owner))
aoqi@0 1064 : makeOwnerThis(pos, sym, true);
aoqi@0 1065 }
aoqi@0 1066
aoqi@0 1067 /** Do we need an access method to reference private symbol?
aoqi@0 1068 */
aoqi@0 1069 boolean needsPrivateAccess(Symbol sym) {
aoqi@0 1070 if ((sym.flags() & PRIVATE) == 0 || sym.owner == currentClass) {
aoqi@0 1071 return false;
aoqi@0 1072 } else if (sym.name == names.init && sym.owner.isLocal()) {
aoqi@0 1073 // private constructor in local class: relax protection
aoqi@0 1074 sym.flags_field &= ~PRIVATE;
aoqi@0 1075 return false;
aoqi@0 1076 } else {
aoqi@0 1077 return true;
aoqi@0 1078 }
aoqi@0 1079 }
aoqi@0 1080
aoqi@0 1081 /** Do we need an access method to reference symbol in other package?
aoqi@0 1082 */
aoqi@0 1083 boolean needsProtectedAccess(Symbol sym, JCTree tree) {
aoqi@0 1084 if ((sym.flags() & PROTECTED) == 0 ||
aoqi@0 1085 sym.owner.owner == currentClass.owner || // fast special case
aoqi@0 1086 sym.packge() == currentClass.packge())
aoqi@0 1087 return false;
aoqi@0 1088 if (!currentClass.isSubClass(sym.owner, types))
aoqi@0 1089 return true;
aoqi@0 1090 if ((sym.flags() & STATIC) != 0 ||
aoqi@0 1091 !tree.hasTag(SELECT) ||
aoqi@0 1092 TreeInfo.name(((JCFieldAccess) tree).selected) == names._super)
aoqi@0 1093 return false;
aoqi@0 1094 return !((JCFieldAccess) tree).selected.type.tsym.isSubClass(currentClass, types);
aoqi@0 1095 }
aoqi@0 1096
aoqi@0 1097 /** The class in which an access method for given symbol goes.
aoqi@0 1098 * @param sym The access symbol
aoqi@0 1099 * @param protAccess Is access to a protected symbol in another
aoqi@0 1100 * package?
aoqi@0 1101 */
aoqi@0 1102 ClassSymbol accessClass(Symbol sym, boolean protAccess, JCTree tree) {
aoqi@0 1103 if (protAccess) {
aoqi@0 1104 Symbol qualifier = null;
aoqi@0 1105 ClassSymbol c = currentClass;
aoqi@0 1106 if (tree.hasTag(SELECT) && (sym.flags() & STATIC) == 0) {
aoqi@0 1107 qualifier = ((JCFieldAccess) tree).selected.type.tsym;
aoqi@0 1108 while (!qualifier.isSubClass(c, types)) {
aoqi@0 1109 c = c.owner.enclClass();
aoqi@0 1110 }
aoqi@0 1111 return c;
aoqi@0 1112 } else {
aoqi@0 1113 while (!c.isSubClass(sym.owner, types)) {
aoqi@0 1114 c = c.owner.enclClass();
aoqi@0 1115 }
aoqi@0 1116 }
aoqi@0 1117 return c;
aoqi@0 1118 } else {
aoqi@0 1119 // the symbol is private
aoqi@0 1120 return sym.owner.enclClass();
aoqi@0 1121 }
aoqi@0 1122 }
aoqi@0 1123
aoqi@0 1124 private void addPrunedInfo(JCTree tree) {
aoqi@0 1125 List<JCTree> infoList = prunedTree.get(currentClass);
aoqi@0 1126 infoList = (infoList == null) ? List.of(tree) : infoList.prepend(tree);
aoqi@0 1127 prunedTree.put(currentClass, infoList);
aoqi@0 1128 }
aoqi@0 1129
aoqi@0 1130 /** Ensure that identifier is accessible, return tree accessing the identifier.
aoqi@0 1131 * @param sym The accessed symbol.
aoqi@0 1132 * @param tree The tree referring to the symbol.
aoqi@0 1133 * @param enclOp The closest enclosing operation node of tree,
aoqi@0 1134 * null if tree is not a subtree of an operation.
aoqi@0 1135 * @param refSuper Is access via a (qualified) C.super?
aoqi@0 1136 */
aoqi@0 1137 JCExpression access(Symbol sym, JCExpression tree, JCExpression enclOp, boolean refSuper) {
aoqi@0 1138 // Access a free variable via its proxy, or its proxy's proxy
aoqi@0 1139 while (sym.kind == VAR && sym.owner.kind == MTH &&
aoqi@0 1140 sym.owner.enclClass() != currentClass) {
aoqi@0 1141 // A constant is replaced by its constant value.
aoqi@0 1142 Object cv = ((VarSymbol)sym).getConstValue();
aoqi@0 1143 if (cv != null) {
aoqi@0 1144 make.at(tree.pos);
aoqi@0 1145 return makeLit(sym.type, cv);
aoqi@0 1146 }
aoqi@0 1147 // Otherwise replace the variable by its proxy.
aoqi@0 1148 sym = proxies.lookup(proxyName(sym.name)).sym;
aoqi@0 1149 Assert.check(sym != null && (sym.flags_field & FINAL) != 0);
aoqi@0 1150 tree = make.at(tree.pos).Ident(sym);
aoqi@0 1151 }
aoqi@0 1152 JCExpression base = (tree.hasTag(SELECT)) ? ((JCFieldAccess) tree).selected : null;
aoqi@0 1153 switch (sym.kind) {
aoqi@0 1154 case TYP:
aoqi@0 1155 if (sym.owner.kind != PCK) {
aoqi@0 1156 // Convert type idents to
aoqi@0 1157 // <flat name> or <package name> . <flat name>
aoqi@0 1158 Name flatname = Convert.shortName(sym.flatName());
aoqi@0 1159 while (base != null &&
aoqi@0 1160 TreeInfo.symbol(base) != null &&
aoqi@0 1161 TreeInfo.symbol(base).kind != PCK) {
aoqi@0 1162 base = (base.hasTag(SELECT))
aoqi@0 1163 ? ((JCFieldAccess) base).selected
aoqi@0 1164 : null;
aoqi@0 1165 }
aoqi@0 1166 if (tree.hasTag(IDENT)) {
aoqi@0 1167 ((JCIdent) tree).name = flatname;
aoqi@0 1168 } else if (base == null) {
aoqi@0 1169 tree = make.at(tree.pos).Ident(sym);
aoqi@0 1170 ((JCIdent) tree).name = flatname;
aoqi@0 1171 } else {
aoqi@0 1172 ((JCFieldAccess) tree).selected = base;
aoqi@0 1173 ((JCFieldAccess) tree).name = flatname;
aoqi@0 1174 }
aoqi@0 1175 }
aoqi@0 1176 break;
aoqi@0 1177 case MTH: case VAR:
aoqi@0 1178 if (sym.owner.kind == TYP) {
aoqi@0 1179
aoqi@0 1180 // Access methods are required for
aoqi@0 1181 // - private members,
aoqi@0 1182 // - protected members in a superclass of an
aoqi@0 1183 // enclosing class contained in another package.
aoqi@0 1184 // - all non-private members accessed via a qualified super.
aoqi@0 1185 boolean protAccess = refSuper && !needsPrivateAccess(sym)
aoqi@0 1186 || needsProtectedAccess(sym, tree);
aoqi@0 1187 boolean accReq = protAccess || needsPrivateAccess(sym);
aoqi@0 1188
aoqi@0 1189 // A base has to be supplied for
aoqi@0 1190 // - simple identifiers accessing variables in outer classes.
aoqi@0 1191 boolean baseReq =
aoqi@0 1192 base == null &&
aoqi@0 1193 sym.owner != syms.predefClass &&
aoqi@0 1194 !sym.isMemberOf(currentClass, types);
aoqi@0 1195
aoqi@0 1196 if (accReq || baseReq) {
aoqi@0 1197 make.at(tree.pos);
aoqi@0 1198
aoqi@0 1199 // Constants are replaced by their constant value.
aoqi@0 1200 if (sym.kind == VAR) {
aoqi@0 1201 Object cv = ((VarSymbol)sym).getConstValue();
aoqi@0 1202 if (cv != null) {
aoqi@0 1203 addPrunedInfo(tree);
aoqi@0 1204 return makeLit(sym.type, cv);
aoqi@0 1205 }
aoqi@0 1206 }
aoqi@0 1207
aoqi@0 1208 // Private variables and methods are replaced by calls
aoqi@0 1209 // to their access methods.
aoqi@0 1210 if (accReq) {
aoqi@0 1211 List<JCExpression> args = List.nil();
aoqi@0 1212 if ((sym.flags() & STATIC) == 0) {
aoqi@0 1213 // Instance access methods get instance
aoqi@0 1214 // as first parameter.
aoqi@0 1215 if (base == null)
aoqi@0 1216 base = makeOwnerThis(tree.pos(), sym, true);
aoqi@0 1217 args = args.prepend(base);
aoqi@0 1218 base = null; // so we don't duplicate code
aoqi@0 1219 }
aoqi@0 1220 Symbol access = accessSymbol(sym, tree,
aoqi@0 1221 enclOp, protAccess,
aoqi@0 1222 refSuper);
aoqi@0 1223 JCExpression receiver = make.Select(
aoqi@0 1224 base != null ? base : make.QualIdent(access.owner),
aoqi@0 1225 access);
aoqi@0 1226 return make.App(receiver, args);
aoqi@0 1227
aoqi@0 1228 // Other accesses to members of outer classes get a
aoqi@0 1229 // qualifier.
aoqi@0 1230 } else if (baseReq) {
aoqi@0 1231 return make.at(tree.pos).Select(
aoqi@0 1232 accessBase(tree.pos(), sym), sym).setType(tree.type);
aoqi@0 1233 }
aoqi@0 1234 }
aoqi@0 1235 } else if (sym.owner.kind == MTH && lambdaTranslationMap != null) {
aoqi@0 1236 //sym is a local variable - check the lambda translation map to
aoqi@0 1237 //see if sym has been translated to something else in the current
aoqi@0 1238 //scope (by LambdaToMethod)
aoqi@0 1239 Symbol translatedSym = lambdaTranslationMap.get(sym);
aoqi@0 1240 if (translatedSym != null) {
aoqi@0 1241 tree = make.at(tree.pos).Ident(translatedSym);
aoqi@0 1242 }
aoqi@0 1243 }
aoqi@0 1244 }
aoqi@0 1245 return tree;
aoqi@0 1246 }
aoqi@0 1247
aoqi@0 1248 /** Ensure that identifier is accessible, return tree accessing the identifier.
aoqi@0 1249 * @param tree The identifier tree.
aoqi@0 1250 */
aoqi@0 1251 JCExpression access(JCExpression tree) {
aoqi@0 1252 Symbol sym = TreeInfo.symbol(tree);
aoqi@0 1253 return sym == null ? tree : access(sym, tree, null, false);
aoqi@0 1254 }
aoqi@0 1255
aoqi@0 1256 /** Return access constructor for a private constructor,
aoqi@0 1257 * or the constructor itself, if no access constructor is needed.
aoqi@0 1258 * @param pos The position to report diagnostics, if any.
aoqi@0 1259 * @param constr The private constructor.
aoqi@0 1260 */
aoqi@0 1261 Symbol accessConstructor(DiagnosticPosition pos, Symbol constr) {
aoqi@0 1262 if (needsPrivateAccess(constr)) {
aoqi@0 1263 ClassSymbol accOwner = constr.owner.enclClass();
aoqi@0 1264 MethodSymbol aconstr = accessConstrs.get(constr);
aoqi@0 1265 if (aconstr == null) {
aoqi@0 1266 List<Type> argtypes = constr.type.getParameterTypes();
aoqi@0 1267 if ((accOwner.flags_field & ENUM) != 0)
aoqi@0 1268 argtypes = argtypes
aoqi@0 1269 .prepend(syms.intType)
aoqi@0 1270 .prepend(syms.stringType);
aoqi@0 1271 aconstr = new MethodSymbol(
aoqi@0 1272 SYNTHETIC,
aoqi@0 1273 names.init,
aoqi@0 1274 new MethodType(
aoqi@0 1275 argtypes.append(
aoqi@0 1276 accessConstructorTag().erasure(types)),
aoqi@0 1277 constr.type.getReturnType(),
aoqi@0 1278 constr.type.getThrownTypes(),
aoqi@0 1279 syms.methodClass),
aoqi@0 1280 accOwner);
aoqi@0 1281 enterSynthetic(pos, aconstr, accOwner.members());
aoqi@0 1282 accessConstrs.put(constr, aconstr);
aoqi@0 1283 accessed.append(constr);
aoqi@0 1284 }
aoqi@0 1285 return aconstr;
aoqi@0 1286 } else {
aoqi@0 1287 return constr;
aoqi@0 1288 }
aoqi@0 1289 }
aoqi@0 1290
aoqi@0 1291 /** Return an anonymous class nested in this toplevel class.
aoqi@0 1292 */
aoqi@0 1293 ClassSymbol accessConstructorTag() {
aoqi@0 1294 ClassSymbol topClass = currentClass.outermostClass();
aoqi@0 1295 Name flatname = names.fromString("" + topClass.getQualifiedName() +
aoqi@0 1296 target.syntheticNameChar() +
aoqi@0 1297 "1");
aoqi@0 1298 ClassSymbol ctag = chk.compiled.get(flatname);
aoqi@0 1299 if (ctag == null)
aoqi@0 1300 ctag = makeEmptyClass(STATIC | SYNTHETIC, topClass).sym;
aoqi@0 1301 // keep a record of all tags, to verify that all are generated as required
aoqi@0 1302 accessConstrTags = accessConstrTags.prepend(ctag);
aoqi@0 1303 return ctag;
aoqi@0 1304 }
aoqi@0 1305
aoqi@0 1306 /** Add all required access methods for a private symbol to enclosing class.
aoqi@0 1307 * @param sym The symbol.
aoqi@0 1308 */
aoqi@0 1309 void makeAccessible(Symbol sym) {
aoqi@0 1310 JCClassDecl cdef = classDef(sym.owner.enclClass());
aoqi@0 1311 if (cdef == null) Assert.error("class def not found: " + sym + " in " + sym.owner);
aoqi@0 1312 if (sym.name == names.init) {
aoqi@0 1313 cdef.defs = cdef.defs.prepend(
aoqi@0 1314 accessConstructorDef(cdef.pos, sym, accessConstrs.get(sym)));
aoqi@0 1315 } else {
aoqi@0 1316 MethodSymbol[] accessors = accessSyms.get(sym);
aoqi@0 1317 for (int i = 0; i < NCODES; i++) {
aoqi@0 1318 if (accessors[i] != null)
aoqi@0 1319 cdef.defs = cdef.defs.prepend(
aoqi@0 1320 accessDef(cdef.pos, sym, accessors[i], i));
aoqi@0 1321 }
aoqi@0 1322 }
aoqi@0 1323 }
aoqi@0 1324
aoqi@0 1325 /** Maps unary operator integer codes to JCTree.Tag objects
aoqi@0 1326 * @param unaryOpCode the unary operator code
aoqi@0 1327 */
aoqi@0 1328 private static Tag mapUnaryOpCodeToTag(int unaryOpCode){
aoqi@0 1329 switch (unaryOpCode){
aoqi@0 1330 case PREINCcode:
aoqi@0 1331 return PREINC;
aoqi@0 1332 case PREDECcode:
aoqi@0 1333 return PREDEC;
aoqi@0 1334 case POSTINCcode:
aoqi@0 1335 return POSTINC;
aoqi@0 1336 case POSTDECcode:
aoqi@0 1337 return POSTDEC;
aoqi@0 1338 default:
aoqi@0 1339 return NO_TAG;
aoqi@0 1340 }
aoqi@0 1341 }
aoqi@0 1342
aoqi@0 1343 /** Maps JCTree.Tag objects to unary operator integer codes
aoqi@0 1344 * @param tag the JCTree.Tag
aoqi@0 1345 */
aoqi@0 1346 private static int mapTagToUnaryOpCode(Tag tag){
aoqi@0 1347 switch (tag){
aoqi@0 1348 case PREINC:
aoqi@0 1349 return PREINCcode;
aoqi@0 1350 case PREDEC:
aoqi@0 1351 return PREDECcode;
aoqi@0 1352 case POSTINC:
aoqi@0 1353 return POSTINCcode;
aoqi@0 1354 case POSTDEC:
aoqi@0 1355 return POSTDECcode;
aoqi@0 1356 default:
aoqi@0 1357 return -1;
aoqi@0 1358 }
aoqi@0 1359 }
aoqi@0 1360
aoqi@0 1361 /** Construct definition of an access method.
aoqi@0 1362 * @param pos The source code position of the definition.
aoqi@0 1363 * @param vsym The private or protected symbol.
aoqi@0 1364 * @param accessor The access method for the symbol.
aoqi@0 1365 * @param acode The access code.
aoqi@0 1366 */
aoqi@0 1367 JCTree accessDef(int pos, Symbol vsym, MethodSymbol accessor, int acode) {
aoqi@0 1368 // System.err.println("access " + vsym + " with " + accessor);//DEBUG
aoqi@0 1369 currentClass = vsym.owner.enclClass();
aoqi@0 1370 make.at(pos);
aoqi@0 1371 JCMethodDecl md = make.MethodDef(accessor, null);
aoqi@0 1372
aoqi@0 1373 // Find actual symbol
aoqi@0 1374 Symbol sym = actualSymbols.get(vsym);
aoqi@0 1375 if (sym == null) sym = vsym;
aoqi@0 1376
aoqi@0 1377 JCExpression ref; // The tree referencing the private symbol.
aoqi@0 1378 List<JCExpression> args; // Any additional arguments to be passed along.
aoqi@0 1379 if ((sym.flags() & STATIC) != 0) {
aoqi@0 1380 ref = make.Ident(sym);
aoqi@0 1381 args = make.Idents(md.params);
aoqi@0 1382 } else {
aoqi@0 1383 JCExpression site = make.Ident(md.params.head);
aoqi@0 1384 if (acode % 2 != 0) {
aoqi@0 1385 //odd access codes represent qualified super accesses - need to
aoqi@0 1386 //emit reference to the direct superclass, even if the refered
aoqi@0 1387 //member is from an indirect superclass (JLS 13.1)
aoqi@0 1388 site.setType(types.erasure(types.supertype(vsym.owner.enclClass().type)));
aoqi@0 1389 }
aoqi@0 1390 ref = make.Select(site, sym);
aoqi@0 1391 args = make.Idents(md.params.tail);
aoqi@0 1392 }
aoqi@0 1393 JCStatement stat; // The statement accessing the private symbol.
aoqi@0 1394 if (sym.kind == VAR) {
aoqi@0 1395 // Normalize out all odd access codes by taking floor modulo 2:
aoqi@0 1396 int acode1 = acode - (acode & 1);
aoqi@0 1397
aoqi@0 1398 JCExpression expr; // The access method's return value.
aoqi@0 1399 switch (acode1) {
aoqi@0 1400 case DEREFcode:
aoqi@0 1401 expr = ref;
aoqi@0 1402 break;
aoqi@0 1403 case ASSIGNcode:
aoqi@0 1404 expr = make.Assign(ref, args.head);
aoqi@0 1405 break;
aoqi@0 1406 case PREINCcode: case POSTINCcode: case PREDECcode: case POSTDECcode:
aoqi@0 1407 expr = makeUnary(mapUnaryOpCodeToTag(acode1), ref);
aoqi@0 1408 break;
aoqi@0 1409 default:
aoqi@0 1410 expr = make.Assignop(
aoqi@0 1411 treeTag(binaryAccessOperator(acode1)), ref, args.head);
aoqi@0 1412 ((JCAssignOp) expr).operator = binaryAccessOperator(acode1);
aoqi@0 1413 }
aoqi@0 1414 stat = make.Return(expr.setType(sym.type));
aoqi@0 1415 } else {
aoqi@0 1416 stat = make.Call(make.App(ref, args));
aoqi@0 1417 }
aoqi@0 1418 md.body = make.Block(0, List.of(stat));
aoqi@0 1419
aoqi@0 1420 // Make sure all parameters, result types and thrown exceptions
aoqi@0 1421 // are accessible.
aoqi@0 1422 for (List<JCVariableDecl> l = md.params; l.nonEmpty(); l = l.tail)
aoqi@0 1423 l.head.vartype = access(l.head.vartype);
aoqi@0 1424 md.restype = access(md.restype);
aoqi@0 1425 for (List<JCExpression> l = md.thrown; l.nonEmpty(); l = l.tail)
aoqi@0 1426 l.head = access(l.head);
aoqi@0 1427
aoqi@0 1428 return md;
aoqi@0 1429 }
aoqi@0 1430
aoqi@0 1431 /** Construct definition of an access constructor.
aoqi@0 1432 * @param pos The source code position of the definition.
aoqi@0 1433 * @param constr The private constructor.
aoqi@0 1434 * @param accessor The access method for the constructor.
aoqi@0 1435 */
aoqi@0 1436 JCTree accessConstructorDef(int pos, Symbol constr, MethodSymbol accessor) {
aoqi@0 1437 make.at(pos);
aoqi@0 1438 JCMethodDecl md = make.MethodDef(accessor,
aoqi@0 1439 accessor.externalType(types),
aoqi@0 1440 null);
aoqi@0 1441 JCIdent callee = make.Ident(names._this);
aoqi@0 1442 callee.sym = constr;
aoqi@0 1443 callee.type = constr.type;
aoqi@0 1444 md.body =
aoqi@0 1445 make.Block(0, List.<JCStatement>of(
aoqi@0 1446 make.Call(
aoqi@0 1447 make.App(
aoqi@0 1448 callee,
aoqi@0 1449 make.Idents(md.params.reverse().tail.reverse())))));
aoqi@0 1450 return md;
aoqi@0 1451 }
aoqi@0 1452
aoqi@0 1453 /**************************************************************************
aoqi@0 1454 * Free variables proxies and this$n
aoqi@0 1455 *************************************************************************/
aoqi@0 1456
aoqi@0 1457 /** A scope containing all free variable proxies for currently translated
aoqi@0 1458 * class, as well as its this$n symbol (if needed).
aoqi@0 1459 * Proxy scopes are nested in the same way classes are.
aoqi@0 1460 * Inside a constructor, proxies and any this$n symbol are duplicated
aoqi@0 1461 * in an additional innermost scope, where they represent the constructor
aoqi@0 1462 * parameters.
aoqi@0 1463 */
aoqi@0 1464 Scope proxies;
aoqi@0 1465
aoqi@0 1466 /** A scope containing all unnamed resource variables/saved
aoqi@0 1467 * exception variables for translated TWR blocks
aoqi@0 1468 */
aoqi@0 1469 Scope twrVars;
aoqi@0 1470
aoqi@0 1471 /** A stack containing the this$n field of the currently translated
aoqi@0 1472 * classes (if needed) in innermost first order.
aoqi@0 1473 * Inside a constructor, proxies and any this$n symbol are duplicated
aoqi@0 1474 * in an additional innermost scope, where they represent the constructor
aoqi@0 1475 * parameters.
aoqi@0 1476 */
aoqi@0 1477 List<VarSymbol> outerThisStack;
aoqi@0 1478
aoqi@0 1479 /** The name of a free variable proxy.
aoqi@0 1480 */
aoqi@0 1481 Name proxyName(Name name) {
aoqi@0 1482 return names.fromString("val" + target.syntheticNameChar() + name);
aoqi@0 1483 }
aoqi@0 1484
aoqi@0 1485 /** Proxy definitions for all free variables in given list, in reverse order.
aoqi@0 1486 * @param pos The source code position of the definition.
aoqi@0 1487 * @param freevars The free variables.
aoqi@0 1488 * @param owner The class in which the definitions go.
aoqi@0 1489 */
aoqi@0 1490 List<JCVariableDecl> freevarDefs(int pos, List<VarSymbol> freevars, Symbol owner) {
aoqi@0 1491 return freevarDefs(pos, freevars, owner, 0);
aoqi@0 1492 }
aoqi@0 1493
aoqi@0 1494 List<JCVariableDecl> freevarDefs(int pos, List<VarSymbol> freevars, Symbol owner,
aoqi@0 1495 long additionalFlags) {
aoqi@0 1496 long flags = FINAL | SYNTHETIC | additionalFlags;
aoqi@0 1497 if (owner.kind == TYP &&
aoqi@0 1498 target.usePrivateSyntheticFields())
aoqi@0 1499 flags |= PRIVATE;
aoqi@0 1500 List<JCVariableDecl> defs = List.nil();
aoqi@0 1501 for (List<VarSymbol> l = freevars; l.nonEmpty(); l = l.tail) {
aoqi@0 1502 VarSymbol v = l.head;
aoqi@0 1503 VarSymbol proxy = new VarSymbol(
aoqi@0 1504 flags, proxyName(v.name), v.erasure(types), owner);
aoqi@0 1505 proxies.enter(proxy);
aoqi@0 1506 JCVariableDecl vd = make.at(pos).VarDef(proxy, null);
aoqi@0 1507 vd.vartype = access(vd.vartype);
aoqi@0 1508 defs = defs.prepend(vd);
aoqi@0 1509 }
aoqi@0 1510 return defs;
aoqi@0 1511 }
aoqi@0 1512
aoqi@0 1513 /** The name of a this$n field
aoqi@0 1514 * @param type The class referenced by the this$n field
aoqi@0 1515 */
aoqi@0 1516 Name outerThisName(Type type, Symbol owner) {
aoqi@0 1517 Type t = type.getEnclosingType();
aoqi@0 1518 int nestingLevel = 0;
aoqi@0 1519 while (t.hasTag(CLASS)) {
aoqi@0 1520 t = t.getEnclosingType();
aoqi@0 1521 nestingLevel++;
aoqi@0 1522 }
aoqi@0 1523 Name result = names.fromString("this" + target.syntheticNameChar() + nestingLevel);
aoqi@0 1524 while (owner.kind == TYP && ((ClassSymbol)owner).members().lookup(result).scope != null)
aoqi@0 1525 result = names.fromString(result.toString() + target.syntheticNameChar());
aoqi@0 1526 return result;
aoqi@0 1527 }
aoqi@0 1528
aoqi@0 1529 private VarSymbol makeOuterThisVarSymbol(Symbol owner, long flags) {
aoqi@0 1530 if (owner.kind == TYP &&
aoqi@0 1531 target.usePrivateSyntheticFields())
aoqi@0 1532 flags |= PRIVATE;
aoqi@0 1533 Type target = types.erasure(owner.enclClass().type.getEnclosingType());
aoqi@0 1534 VarSymbol outerThis =
aoqi@0 1535 new VarSymbol(flags, outerThisName(target, owner), target, owner);
aoqi@0 1536 outerThisStack = outerThisStack.prepend(outerThis);
aoqi@0 1537 return outerThis;
aoqi@0 1538 }
aoqi@0 1539
aoqi@0 1540 private JCVariableDecl makeOuterThisVarDecl(int pos, VarSymbol sym) {
aoqi@0 1541 JCVariableDecl vd = make.at(pos).VarDef(sym, null);
aoqi@0 1542 vd.vartype = access(vd.vartype);
aoqi@0 1543 return vd;
aoqi@0 1544 }
aoqi@0 1545
aoqi@0 1546 /** Definition for this$n field.
aoqi@0 1547 * @param pos The source code position of the definition.
aoqi@0 1548 * @param owner The method in which the definition goes.
aoqi@0 1549 */
aoqi@0 1550 JCVariableDecl outerThisDef(int pos, MethodSymbol owner) {
aoqi@0 1551 ClassSymbol c = owner.enclClass();
aoqi@0 1552 boolean isMandated =
aoqi@0 1553 // Anonymous constructors
aoqi@0 1554 (owner.isConstructor() && owner.isAnonymous()) ||
aoqi@0 1555 // Constructors of non-private inner member classes
aoqi@0 1556 (owner.isConstructor() && c.isInner() &&
aoqi@0 1557 !c.isPrivate() && !c.isStatic());
aoqi@0 1558 long flags =
aoqi@0 1559 FINAL | (isMandated ? MANDATED : SYNTHETIC) | PARAMETER;
aoqi@0 1560 VarSymbol outerThis = makeOuterThisVarSymbol(owner, flags);
aoqi@0 1561 owner.extraParams = owner.extraParams.prepend(outerThis);
aoqi@0 1562 return makeOuterThisVarDecl(pos, outerThis);
aoqi@0 1563 }
aoqi@0 1564
aoqi@0 1565 /** Definition for this$n field.
aoqi@0 1566 * @param pos The source code position of the definition.
aoqi@0 1567 * @param owner The class in which the definition goes.
aoqi@0 1568 */
aoqi@0 1569 JCVariableDecl outerThisDef(int pos, ClassSymbol owner) {
aoqi@0 1570 VarSymbol outerThis = makeOuterThisVarSymbol(owner, FINAL | SYNTHETIC);
aoqi@0 1571 return makeOuterThisVarDecl(pos, outerThis);
aoqi@0 1572 }
aoqi@0 1573
aoqi@0 1574 /** Return a list of trees that load the free variables in given list,
aoqi@0 1575 * in reverse order.
aoqi@0 1576 * @param pos The source code position to be used for the trees.
aoqi@0 1577 * @param freevars The list of free variables.
aoqi@0 1578 */
aoqi@0 1579 List<JCExpression> loadFreevars(DiagnosticPosition pos, List<VarSymbol> freevars) {
aoqi@0 1580 List<JCExpression> args = List.nil();
aoqi@0 1581 for (List<VarSymbol> l = freevars; l.nonEmpty(); l = l.tail)
aoqi@0 1582 args = args.prepend(loadFreevar(pos, l.head));
aoqi@0 1583 return args;
aoqi@0 1584 }
aoqi@0 1585 //where
aoqi@0 1586 JCExpression loadFreevar(DiagnosticPosition pos, VarSymbol v) {
aoqi@0 1587 return access(v, make.at(pos).Ident(v), null, false);
aoqi@0 1588 }
aoqi@0 1589
aoqi@0 1590 /** Construct a tree simulating the expression {@code C.this}.
aoqi@0 1591 * @param pos The source code position to be used for the tree.
aoqi@0 1592 * @param c The qualifier class.
aoqi@0 1593 */
aoqi@0 1594 JCExpression makeThis(DiagnosticPosition pos, TypeSymbol c) {
aoqi@0 1595 if (currentClass == c) {
aoqi@0 1596 // in this case, `this' works fine
aoqi@0 1597 return make.at(pos).This(c.erasure(types));
aoqi@0 1598 } else {
aoqi@0 1599 // need to go via this$n
aoqi@0 1600 return makeOuterThis(pos, c);
aoqi@0 1601 }
aoqi@0 1602 }
aoqi@0 1603
aoqi@0 1604 /**
aoqi@0 1605 * Optionally replace a try statement with the desugaring of a
aoqi@0 1606 * try-with-resources statement. The canonical desugaring of
aoqi@0 1607 *
aoqi@0 1608 * try ResourceSpecification
aoqi@0 1609 * Block
aoqi@0 1610 *
aoqi@0 1611 * is
aoqi@0 1612 *
aoqi@0 1613 * {
aoqi@0 1614 * final VariableModifiers_minus_final R #resource = Expression;
aoqi@0 1615 * Throwable #primaryException = null;
aoqi@0 1616 *
aoqi@0 1617 * try ResourceSpecificationtail
aoqi@0 1618 * Block
aoqi@0 1619 * catch (Throwable #t) {
aoqi@0 1620 * #primaryException = t;
aoqi@0 1621 * throw #t;
aoqi@0 1622 * } finally {
aoqi@0 1623 * if (#resource != null) {
aoqi@0 1624 * if (#primaryException != null) {
aoqi@0 1625 * try {
aoqi@0 1626 * #resource.close();
aoqi@0 1627 * } catch(Throwable #suppressedException) {
aoqi@0 1628 * #primaryException.addSuppressed(#suppressedException);
aoqi@0 1629 * }
aoqi@0 1630 * } else {
aoqi@0 1631 * #resource.close();
aoqi@0 1632 * }
aoqi@0 1633 * }
aoqi@0 1634 * }
aoqi@0 1635 *
aoqi@0 1636 * @param tree The try statement to inspect.
aoqi@0 1637 * @return A a desugared try-with-resources tree, or the original
aoqi@0 1638 * try block if there are no resources to manage.
aoqi@0 1639 */
aoqi@0 1640 JCTree makeTwrTry(JCTry tree) {
aoqi@0 1641 make_at(tree.pos());
aoqi@0 1642 twrVars = twrVars.dup();
aoqi@0 1643 JCBlock twrBlock = makeTwrBlock(tree.resources, tree.body,
aoqi@0 1644 tree.finallyCanCompleteNormally, 0);
aoqi@0 1645 if (tree.catchers.isEmpty() && tree.finalizer == null)
aoqi@0 1646 result = translate(twrBlock);
aoqi@0 1647 else
aoqi@0 1648 result = translate(make.Try(twrBlock, tree.catchers, tree.finalizer));
aoqi@0 1649 twrVars = twrVars.leave();
aoqi@0 1650 return result;
aoqi@0 1651 }
aoqi@0 1652
aoqi@0 1653 private JCBlock makeTwrBlock(List<JCTree> resources, JCBlock block,
aoqi@0 1654 boolean finallyCanCompleteNormally, int depth) {
aoqi@0 1655 if (resources.isEmpty())
aoqi@0 1656 return block;
aoqi@0 1657
aoqi@0 1658 // Add resource declaration or expression to block statements
aoqi@0 1659 ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>();
aoqi@0 1660 JCTree resource = resources.head;
aoqi@0 1661 JCExpression expr = null;
aoqi@0 1662 if (resource instanceof JCVariableDecl) {
aoqi@0 1663 JCVariableDecl var = (JCVariableDecl) resource;
aoqi@0 1664 expr = make.Ident(var.sym).setType(resource.type);
aoqi@0 1665 stats.add(var);
aoqi@0 1666 } else {
aoqi@0 1667 Assert.check(resource instanceof JCExpression);
aoqi@0 1668 VarSymbol syntheticTwrVar =
aoqi@0 1669 new VarSymbol(SYNTHETIC | FINAL,
aoqi@0 1670 makeSyntheticName(names.fromString("twrVar" +
aoqi@0 1671 depth), twrVars),
aoqi@0 1672 (resource.type.hasTag(BOT)) ?
aoqi@0 1673 syms.autoCloseableType : resource.type,
aoqi@0 1674 currentMethodSym);
aoqi@0 1675 twrVars.enter(syntheticTwrVar);
aoqi@0 1676 JCVariableDecl syntheticTwrVarDecl =
aoqi@0 1677 make.VarDef(syntheticTwrVar, (JCExpression)resource);
aoqi@0 1678 expr = (JCExpression)make.Ident(syntheticTwrVar);
aoqi@0 1679 stats.add(syntheticTwrVarDecl);
aoqi@0 1680 }
aoqi@0 1681
aoqi@0 1682 // Add primaryException declaration
aoqi@0 1683 VarSymbol primaryException =
aoqi@0 1684 new VarSymbol(SYNTHETIC,
aoqi@0 1685 makeSyntheticName(names.fromString("primaryException" +
aoqi@0 1686 depth), twrVars),
aoqi@0 1687 syms.throwableType,
aoqi@0 1688 currentMethodSym);
aoqi@0 1689 twrVars.enter(primaryException);
aoqi@0 1690 JCVariableDecl primaryExceptionTreeDecl = make.VarDef(primaryException, makeNull());
aoqi@0 1691 stats.add(primaryExceptionTreeDecl);
aoqi@0 1692
aoqi@0 1693 // Create catch clause that saves exception and then rethrows it
aoqi@0 1694 VarSymbol param =
aoqi@0 1695 new VarSymbol(FINAL|SYNTHETIC,
aoqi@0 1696 names.fromString("t" +
aoqi@0 1697 target.syntheticNameChar()),
aoqi@0 1698 syms.throwableType,
aoqi@0 1699 currentMethodSym);
aoqi@0 1700 JCVariableDecl paramTree = make.VarDef(param, null);
aoqi@0 1701 JCStatement assign = make.Assignment(primaryException, make.Ident(param));
aoqi@0 1702 JCStatement rethrowStat = make.Throw(make.Ident(param));
aoqi@0 1703 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(assign, rethrowStat));
aoqi@0 1704 JCCatch catchClause = make.Catch(paramTree, catchBlock);
aoqi@0 1705
aoqi@0 1706 int oldPos = make.pos;
aoqi@0 1707 make.at(TreeInfo.endPos(block));
aoqi@0 1708 JCBlock finallyClause = makeTwrFinallyClause(primaryException, expr);
aoqi@0 1709 make.at(oldPos);
aoqi@0 1710 JCTry outerTry = make.Try(makeTwrBlock(resources.tail, block,
aoqi@0 1711 finallyCanCompleteNormally, depth + 1),
aoqi@0 1712 List.<JCCatch>of(catchClause),
aoqi@0 1713 finallyClause);
aoqi@0 1714 outerTry.finallyCanCompleteNormally = finallyCanCompleteNormally;
aoqi@0 1715 stats.add(outerTry);
aoqi@0 1716 JCBlock newBlock = make.Block(0L, stats.toList());
aoqi@0 1717 return newBlock;
aoqi@0 1718 }
aoqi@0 1719
aoqi@0 1720 private JCBlock makeTwrFinallyClause(Symbol primaryException, JCExpression resource) {
aoqi@0 1721 // primaryException.addSuppressed(catchException);
aoqi@0 1722 VarSymbol catchException =
aoqi@0 1723 new VarSymbol(SYNTHETIC, make.paramName(2),
aoqi@0 1724 syms.throwableType,
aoqi@0 1725 currentMethodSym);
aoqi@0 1726 JCStatement addSuppressionStatement =
aoqi@0 1727 make.Exec(makeCall(make.Ident(primaryException),
aoqi@0 1728 names.addSuppressed,
aoqi@0 1729 List.<JCExpression>of(make.Ident(catchException))));
aoqi@0 1730
aoqi@0 1731 // try { resource.close(); } catch (e) { primaryException.addSuppressed(e); }
aoqi@0 1732 JCBlock tryBlock =
aoqi@0 1733 make.Block(0L, List.<JCStatement>of(makeResourceCloseInvocation(resource)));
aoqi@0 1734 JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null);
aoqi@0 1735 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(addSuppressionStatement));
aoqi@0 1736 List<JCCatch> catchClauses = List.<JCCatch>of(make.Catch(catchExceptionDecl, catchBlock));
aoqi@0 1737 JCTry tryTree = make.Try(tryBlock, catchClauses, null);
aoqi@0 1738 tryTree.finallyCanCompleteNormally = true;
aoqi@0 1739
aoqi@0 1740 // if (primaryException != null) {try...} else resourceClose;
aoqi@0 1741 JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)),
aoqi@0 1742 tryTree,
aoqi@0 1743 makeResourceCloseInvocation(resource));
aoqi@0 1744
aoqi@0 1745 // if (#resource != null) { if (primaryException ... }
aoqi@0 1746 return make.Block(0L,
aoqi@0 1747 List.<JCStatement>of(make.If(makeNonNullCheck(resource),
aoqi@0 1748 closeIfStatement,
aoqi@0 1749 null)));
aoqi@0 1750 }
aoqi@0 1751
aoqi@0 1752 private JCStatement makeResourceCloseInvocation(JCExpression resource) {
aoqi@0 1753 // convert to AutoCloseable if needed
aoqi@0 1754 if (types.asSuper(resource.type, syms.autoCloseableType.tsym) == null) {
aoqi@0 1755 resource = (JCExpression) convert(resource, syms.autoCloseableType);
aoqi@0 1756 }
aoqi@0 1757
aoqi@0 1758 // create resource.close() method invocation
aoqi@0 1759 JCExpression resourceClose = makeCall(resource,
aoqi@0 1760 names.close,
aoqi@0 1761 List.<JCExpression>nil());
aoqi@0 1762 return make.Exec(resourceClose);
aoqi@0 1763 }
aoqi@0 1764
aoqi@0 1765 private JCExpression makeNonNullCheck(JCExpression expression) {
aoqi@0 1766 return makeBinary(NE, expression, makeNull());
aoqi@0 1767 }
aoqi@0 1768
aoqi@0 1769 /** Construct a tree that represents the outer instance
aoqi@0 1770 * {@code C.this}. Never pick the current `this'.
aoqi@0 1771 * @param pos The source code position to be used for the tree.
aoqi@0 1772 * @param c The qualifier class.
aoqi@0 1773 */
aoqi@0 1774 JCExpression makeOuterThis(DiagnosticPosition pos, TypeSymbol c) {
aoqi@0 1775 List<VarSymbol> ots = outerThisStack;
aoqi@0 1776 if (ots.isEmpty()) {
aoqi@0 1777 log.error(pos, "no.encl.instance.of.type.in.scope", c);
aoqi@0 1778 Assert.error();
aoqi@0 1779 return makeNull();
aoqi@0 1780 }
aoqi@0 1781 VarSymbol ot = ots.head;
aoqi@0 1782 JCExpression tree = access(make.at(pos).Ident(ot));
aoqi@0 1783 TypeSymbol otc = ot.type.tsym;
aoqi@0 1784 while (otc != c) {
aoqi@0 1785 do {
aoqi@0 1786 ots = ots.tail;
aoqi@0 1787 if (ots.isEmpty()) {
aoqi@0 1788 log.error(pos,
aoqi@0 1789 "no.encl.instance.of.type.in.scope",
aoqi@0 1790 c);
aoqi@0 1791 Assert.error(); // should have been caught in Attr
aoqi@0 1792 return tree;
aoqi@0 1793 }
aoqi@0 1794 ot = ots.head;
aoqi@0 1795 } while (ot.owner != otc);
aoqi@0 1796 if (otc.owner.kind != PCK && !otc.hasOuterInstance()) {
aoqi@0 1797 chk.earlyRefError(pos, c);
aoqi@0 1798 Assert.error(); // should have been caught in Attr
aoqi@0 1799 return makeNull();
aoqi@0 1800 }
aoqi@0 1801 tree = access(make.at(pos).Select(tree, ot));
aoqi@0 1802 otc = ot.type.tsym;
aoqi@0 1803 }
aoqi@0 1804 return tree;
aoqi@0 1805 }
aoqi@0 1806
aoqi@0 1807 /** Construct a tree that represents the closest outer instance
aoqi@0 1808 * {@code C.this} such that the given symbol is a member of C.
aoqi@0 1809 * @param pos The source code position to be used for the tree.
aoqi@0 1810 * @param sym The accessed symbol.
aoqi@0 1811 * @param preciseMatch should we accept a type that is a subtype of
aoqi@0 1812 * sym's owner, even if it doesn't contain sym
aoqi@0 1813 * due to hiding, overriding, or non-inheritance
aoqi@0 1814 * due to protection?
aoqi@0 1815 */
aoqi@0 1816 JCExpression makeOwnerThis(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) {
aoqi@0 1817 Symbol c = sym.owner;
aoqi@0 1818 if (preciseMatch ? sym.isMemberOf(currentClass, types)
aoqi@0 1819 : currentClass.isSubClass(sym.owner, types)) {
aoqi@0 1820 // in this case, `this' works fine
aoqi@0 1821 return make.at(pos).This(c.erasure(types));
aoqi@0 1822 } else {
aoqi@0 1823 // need to go via this$n
aoqi@0 1824 return makeOwnerThisN(pos, sym, preciseMatch);
aoqi@0 1825 }
aoqi@0 1826 }
aoqi@0 1827
aoqi@0 1828 /**
aoqi@0 1829 * Similar to makeOwnerThis but will never pick "this".
aoqi@0 1830 */
aoqi@0 1831 JCExpression makeOwnerThisN(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) {
aoqi@0 1832 Symbol c = sym.owner;
aoqi@0 1833 List<VarSymbol> ots = outerThisStack;
aoqi@0 1834 if (ots.isEmpty()) {
aoqi@0 1835 log.error(pos, "no.encl.instance.of.type.in.scope", c);
aoqi@0 1836 Assert.error();
aoqi@0 1837 return makeNull();
aoqi@0 1838 }
aoqi@0 1839 VarSymbol ot = ots.head;
aoqi@0 1840 JCExpression tree = access(make.at(pos).Ident(ot));
aoqi@0 1841 TypeSymbol otc = ot.type.tsym;
aoqi@0 1842 while (!(preciseMatch ? sym.isMemberOf(otc, types) : otc.isSubClass(sym.owner, types))) {
aoqi@0 1843 do {
aoqi@0 1844 ots = ots.tail;
aoqi@0 1845 if (ots.isEmpty()) {
aoqi@0 1846 log.error(pos,
aoqi@0 1847 "no.encl.instance.of.type.in.scope",
aoqi@0 1848 c);
aoqi@0 1849 Assert.error();
aoqi@0 1850 return tree;
aoqi@0 1851 }
aoqi@0 1852 ot = ots.head;
aoqi@0 1853 } while (ot.owner != otc);
aoqi@0 1854 tree = access(make.at(pos).Select(tree, ot));
aoqi@0 1855 otc = ot.type.tsym;
aoqi@0 1856 }
aoqi@0 1857 return tree;
aoqi@0 1858 }
aoqi@0 1859
aoqi@0 1860 /** Return tree simulating the assignment {@code this.name = name}, where
aoqi@0 1861 * name is the name of a free variable.
aoqi@0 1862 */
aoqi@0 1863 JCStatement initField(int pos, Name name) {
aoqi@0 1864 Scope.Entry e = proxies.lookup(name);
aoqi@0 1865 Symbol rhs = e.sym;
aoqi@0 1866 Assert.check(rhs.owner.kind == MTH);
aoqi@0 1867 Symbol lhs = e.next().sym;
aoqi@0 1868 Assert.check(rhs.owner.owner == lhs.owner);
aoqi@0 1869 make.at(pos);
aoqi@0 1870 return
aoqi@0 1871 make.Exec(
aoqi@0 1872 make.Assign(
aoqi@0 1873 make.Select(make.This(lhs.owner.erasure(types)), lhs),
aoqi@0 1874 make.Ident(rhs)).setType(lhs.erasure(types)));
aoqi@0 1875 }
aoqi@0 1876
aoqi@0 1877 /** Return tree simulating the assignment {@code this.this$n = this$n}.
aoqi@0 1878 */
aoqi@0 1879 JCStatement initOuterThis(int pos) {
aoqi@0 1880 VarSymbol rhs = outerThisStack.head;
aoqi@0 1881 Assert.check(rhs.owner.kind == MTH);
aoqi@0 1882 VarSymbol lhs = outerThisStack.tail.head;
aoqi@0 1883 Assert.check(rhs.owner.owner == lhs.owner);
aoqi@0 1884 make.at(pos);
aoqi@0 1885 return
aoqi@0 1886 make.Exec(
aoqi@0 1887 make.Assign(
aoqi@0 1888 make.Select(make.This(lhs.owner.erasure(types)), lhs),
aoqi@0 1889 make.Ident(rhs)).setType(lhs.erasure(types)));
aoqi@0 1890 }
aoqi@0 1891
aoqi@0 1892 /**************************************************************************
aoqi@0 1893 * Code for .class
aoqi@0 1894 *************************************************************************/
aoqi@0 1895
aoqi@0 1896 /** Return the symbol of a class to contain a cache of
aoqi@0 1897 * compiler-generated statics such as class$ and the
aoqi@0 1898 * $assertionsDisabled flag. We create an anonymous nested class
aoqi@0 1899 * (unless one already exists) and return its symbol. However,
aoqi@0 1900 * for backward compatibility in 1.4 and earlier we use the
aoqi@0 1901 * top-level class itself.
aoqi@0 1902 */
aoqi@0 1903 private ClassSymbol outerCacheClass() {
aoqi@0 1904 ClassSymbol clazz = outermostClassDef.sym;
aoqi@0 1905 if ((clazz.flags() & INTERFACE) == 0 &&
aoqi@0 1906 !target.useInnerCacheClass()) return clazz;
aoqi@0 1907 Scope s = clazz.members();
aoqi@0 1908 for (Scope.Entry e = s.elems; e != null; e = e.sibling)
aoqi@0 1909 if (e.sym.kind == TYP &&
aoqi@0 1910 e.sym.name == names.empty &&
aoqi@0 1911 (e.sym.flags() & INTERFACE) == 0) return (ClassSymbol) e.sym;
aoqi@0 1912 return makeEmptyClass(STATIC | SYNTHETIC, clazz).sym;
aoqi@0 1913 }
aoqi@0 1914
aoqi@0 1915 /** Return symbol for "class$" method. If there is no method definition
aoqi@0 1916 * for class$, construct one as follows:
aoqi@0 1917 *
aoqi@0 1918 * class class$(String x0) {
aoqi@0 1919 * try {
aoqi@0 1920 * return Class.forName(x0);
aoqi@0 1921 * } catch (ClassNotFoundException x1) {
aoqi@0 1922 * throw new NoClassDefFoundError(x1.getMessage());
aoqi@0 1923 * }
aoqi@0 1924 * }
aoqi@0 1925 */
aoqi@0 1926 private MethodSymbol classDollarSym(DiagnosticPosition pos) {
aoqi@0 1927 ClassSymbol outerCacheClass = outerCacheClass();
aoqi@0 1928 MethodSymbol classDollarSym =
aoqi@0 1929 (MethodSymbol)lookupSynthetic(classDollar,
aoqi@0 1930 outerCacheClass.members());
aoqi@0 1931 if (classDollarSym == null) {
aoqi@0 1932 classDollarSym = new MethodSymbol(
aoqi@0 1933 STATIC | SYNTHETIC,
aoqi@0 1934 classDollar,
aoqi@0 1935 new MethodType(
aoqi@0 1936 List.of(syms.stringType),
aoqi@0 1937 types.erasure(syms.classType),
aoqi@0 1938 List.<Type>nil(),
aoqi@0 1939 syms.methodClass),
aoqi@0 1940 outerCacheClass);
aoqi@0 1941 enterSynthetic(pos, classDollarSym, outerCacheClass.members());
aoqi@0 1942
aoqi@0 1943 JCMethodDecl md = make.MethodDef(classDollarSym, null);
aoqi@0 1944 try {
aoqi@0 1945 md.body = classDollarSymBody(pos, md);
aoqi@0 1946 } catch (CompletionFailure ex) {
aoqi@0 1947 md.body = make.Block(0, List.<JCStatement>nil());
aoqi@0 1948 chk.completionError(pos, ex);
aoqi@0 1949 }
aoqi@0 1950 JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
aoqi@0 1951 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(md);
aoqi@0 1952 }
aoqi@0 1953 return classDollarSym;
aoqi@0 1954 }
aoqi@0 1955
aoqi@0 1956 /** Generate code for class$(String name). */
aoqi@0 1957 JCBlock classDollarSymBody(DiagnosticPosition pos, JCMethodDecl md) {
aoqi@0 1958 MethodSymbol classDollarSym = md.sym;
aoqi@0 1959 ClassSymbol outerCacheClass = (ClassSymbol)classDollarSym.owner;
aoqi@0 1960
aoqi@0 1961 JCBlock returnResult;
aoqi@0 1962
aoqi@0 1963 // in 1.4.2 and above, we use
aoqi@0 1964 // Class.forName(String name, boolean init, ClassLoader loader);
aoqi@0 1965 // which requires we cache the current loader in cl$
aoqi@0 1966 if (target.classLiteralsNoInit()) {
aoqi@0 1967 // clsym = "private static ClassLoader cl$"
aoqi@0 1968 VarSymbol clsym = new VarSymbol(STATIC|SYNTHETIC,
aoqi@0 1969 names.fromString("cl" + target.syntheticNameChar()),
aoqi@0 1970 syms.classLoaderType,
aoqi@0 1971 outerCacheClass);
aoqi@0 1972 enterSynthetic(pos, clsym, outerCacheClass.members());
aoqi@0 1973
aoqi@0 1974 // emit "private static ClassLoader cl$;"
aoqi@0 1975 JCVariableDecl cldef = make.VarDef(clsym, null);
aoqi@0 1976 JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
aoqi@0 1977 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cldef);
aoqi@0 1978
aoqi@0 1979 // newcache := "new cache$1[0]"
aoqi@0 1980 JCNewArray newcache = make.
aoqi@0 1981 NewArray(make.Type(outerCacheClass.type),
aoqi@0 1982 List.<JCExpression>of(make.Literal(INT, 0).setType(syms.intType)),
aoqi@0 1983 null);
aoqi@0 1984 newcache.type = new ArrayType(types.erasure(outerCacheClass.type),
aoqi@0 1985 syms.arrayClass);
aoqi@0 1986
aoqi@0 1987 // forNameSym := java.lang.Class.forName(
aoqi@0 1988 // String s,boolean init,ClassLoader loader)
aoqi@0 1989 Symbol forNameSym = lookupMethod(make_pos, names.forName,
aoqi@0 1990 types.erasure(syms.classType),
aoqi@0 1991 List.of(syms.stringType,
aoqi@0 1992 syms.booleanType,
aoqi@0 1993 syms.classLoaderType));
aoqi@0 1994 // clvalue := "(cl$ == null) ?
aoqi@0 1995 // $newcache.getClass().getComponentType().getClassLoader() : cl$"
aoqi@0 1996 JCExpression clvalue =
aoqi@0 1997 make.Conditional(
aoqi@0 1998 makeBinary(EQ, make.Ident(clsym), makeNull()),
aoqi@0 1999 make.Assign(
aoqi@0 2000 make.Ident(clsym),
aoqi@0 2001 makeCall(
aoqi@0 2002 makeCall(makeCall(newcache,
aoqi@0 2003 names.getClass,
aoqi@0 2004 List.<JCExpression>nil()),
aoqi@0 2005 names.getComponentType,
aoqi@0 2006 List.<JCExpression>nil()),
aoqi@0 2007 names.getClassLoader,
aoqi@0 2008 List.<JCExpression>nil())).setType(syms.classLoaderType),
aoqi@0 2009 make.Ident(clsym)).setType(syms.classLoaderType);
aoqi@0 2010
aoqi@0 2011 // returnResult := "{ return Class.forName(param1, false, cl$); }"
aoqi@0 2012 List<JCExpression> args = List.of(make.Ident(md.params.head.sym),
aoqi@0 2013 makeLit(syms.booleanType, 0),
aoqi@0 2014 clvalue);
aoqi@0 2015 returnResult = make.
aoqi@0 2016 Block(0, List.<JCStatement>of(make.
aoqi@0 2017 Call(make. // return
aoqi@0 2018 App(make.
aoqi@0 2019 Ident(forNameSym), args))));
aoqi@0 2020 } else {
aoqi@0 2021 // forNameSym := java.lang.Class.forName(String s)
aoqi@0 2022 Symbol forNameSym = lookupMethod(make_pos,
aoqi@0 2023 names.forName,
aoqi@0 2024 types.erasure(syms.classType),
aoqi@0 2025 List.of(syms.stringType));
aoqi@0 2026 // returnResult := "{ return Class.forName(param1); }"
aoqi@0 2027 returnResult = make.
aoqi@0 2028 Block(0, List.of(make.
aoqi@0 2029 Call(make. // return
aoqi@0 2030 App(make.
aoqi@0 2031 QualIdent(forNameSym),
aoqi@0 2032 List.<JCExpression>of(make.
aoqi@0 2033 Ident(md.params.
aoqi@0 2034 head.sym))))));
aoqi@0 2035 }
aoqi@0 2036
aoqi@0 2037 // catchParam := ClassNotFoundException e1
aoqi@0 2038 VarSymbol catchParam =
aoqi@0 2039 new VarSymbol(SYNTHETIC, make.paramName(1),
aoqi@0 2040 syms.classNotFoundExceptionType,
aoqi@0 2041 classDollarSym);
aoqi@0 2042
aoqi@0 2043 JCStatement rethrow;
aoqi@0 2044 if (target.hasInitCause()) {
aoqi@0 2045 // rethrow = "throw new NoClassDefFoundError().initCause(e);
aoqi@0 2046 JCExpression throwExpr =
aoqi@0 2047 makeCall(makeNewClass(syms.noClassDefFoundErrorType,
aoqi@0 2048 List.<JCExpression>nil()),
aoqi@0 2049 names.initCause,
aoqi@0 2050 List.<JCExpression>of(make.Ident(catchParam)));
aoqi@0 2051 rethrow = make.Throw(throwExpr);
aoqi@0 2052 } else {
aoqi@0 2053 // getMessageSym := ClassNotFoundException.getMessage()
aoqi@0 2054 Symbol getMessageSym = lookupMethod(make_pos,
aoqi@0 2055 names.getMessage,
aoqi@0 2056 syms.classNotFoundExceptionType,
aoqi@0 2057 List.<Type>nil());
aoqi@0 2058 // rethrow = "throw new NoClassDefFoundError(e.getMessage());"
aoqi@0 2059 rethrow = make.
aoqi@0 2060 Throw(makeNewClass(syms.noClassDefFoundErrorType,
aoqi@0 2061 List.<JCExpression>of(make.App(make.Select(make.Ident(catchParam),
aoqi@0 2062 getMessageSym),
aoqi@0 2063 List.<JCExpression>nil()))));
aoqi@0 2064 }
aoqi@0 2065
aoqi@0 2066 // rethrowStmt := "( $rethrow )"
aoqi@0 2067 JCBlock rethrowStmt = make.Block(0, List.of(rethrow));
aoqi@0 2068
aoqi@0 2069 // catchBlock := "catch ($catchParam) $rethrowStmt"
aoqi@0 2070 JCCatch catchBlock = make.Catch(make.VarDef(catchParam, null),
aoqi@0 2071 rethrowStmt);
aoqi@0 2072
aoqi@0 2073 // tryCatch := "try $returnResult $catchBlock"
aoqi@0 2074 JCStatement tryCatch = make.Try(returnResult,
aoqi@0 2075 List.of(catchBlock), null);
aoqi@0 2076
aoqi@0 2077 return make.Block(0, List.of(tryCatch));
aoqi@0 2078 }
aoqi@0 2079 // where
aoqi@0 2080 /** Create an attributed tree of the form left.name(). */
aoqi@0 2081 private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) {
aoqi@0 2082 Assert.checkNonNull(left.type);
aoqi@0 2083 Symbol funcsym = lookupMethod(make_pos, name, left.type,
aoqi@0 2084 TreeInfo.types(args));
aoqi@0 2085 return make.App(make.Select(left, funcsym), args);
aoqi@0 2086 }
aoqi@0 2087
aoqi@0 2088 /** The Name Of The variable to cache T.class values.
aoqi@0 2089 * @param sig The signature of type T.
aoqi@0 2090 */
aoqi@0 2091 private Name cacheName(String sig) {
aoqi@0 2092 StringBuilder buf = new StringBuilder();
aoqi@0 2093 if (sig.startsWith("[")) {
aoqi@0 2094 buf = buf.append("array");
aoqi@0 2095 while (sig.startsWith("[")) {
aoqi@0 2096 buf = buf.append(target.syntheticNameChar());
aoqi@0 2097 sig = sig.substring(1);
aoqi@0 2098 }
aoqi@0 2099 if (sig.startsWith("L")) {
aoqi@0 2100 sig = sig.substring(0, sig.length() - 1);
aoqi@0 2101 }
aoqi@0 2102 } else {
aoqi@0 2103 buf = buf.append("class" + target.syntheticNameChar());
aoqi@0 2104 }
aoqi@0 2105 buf = buf.append(sig.replace('.', target.syntheticNameChar()));
aoqi@0 2106 return names.fromString(buf.toString());
aoqi@0 2107 }
aoqi@0 2108
aoqi@0 2109 /** The variable symbol that caches T.class values.
aoqi@0 2110 * If none exists yet, create a definition.
aoqi@0 2111 * @param sig The signature of type T.
aoqi@0 2112 * @param pos The position to report diagnostics, if any.
aoqi@0 2113 */
aoqi@0 2114 private VarSymbol cacheSym(DiagnosticPosition pos, String sig) {
aoqi@0 2115 ClassSymbol outerCacheClass = outerCacheClass();
aoqi@0 2116 Name cname = cacheName(sig);
aoqi@0 2117 VarSymbol cacheSym =
aoqi@0 2118 (VarSymbol)lookupSynthetic(cname, outerCacheClass.members());
aoqi@0 2119 if (cacheSym == null) {
aoqi@0 2120 cacheSym = new VarSymbol(
aoqi@0 2121 STATIC | SYNTHETIC, cname, types.erasure(syms.classType), outerCacheClass);
aoqi@0 2122 enterSynthetic(pos, cacheSym, outerCacheClass.members());
aoqi@0 2123
aoqi@0 2124 JCVariableDecl cacheDef = make.VarDef(cacheSym, null);
aoqi@0 2125 JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
aoqi@0 2126 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cacheDef);
aoqi@0 2127 }
aoqi@0 2128 return cacheSym;
aoqi@0 2129 }
aoqi@0 2130
aoqi@0 2131 /** The tree simulating a T.class expression.
aoqi@0 2132 * @param clazz The tree identifying type T.
aoqi@0 2133 */
aoqi@0 2134 private JCExpression classOf(JCTree clazz) {
aoqi@0 2135 return classOfType(clazz.type, clazz.pos());
aoqi@0 2136 }
aoqi@0 2137
aoqi@0 2138 private JCExpression classOfType(Type type, DiagnosticPosition pos) {
aoqi@0 2139 switch (type.getTag()) {
aoqi@0 2140 case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
aoqi@0 2141 case DOUBLE: case BOOLEAN: case VOID:
aoqi@0 2142 // replace with <BoxedClass>.TYPE
aoqi@0 2143 ClassSymbol c = types.boxedClass(type);
aoqi@0 2144 Symbol typeSym =
aoqi@0 2145 rs.accessBase(
aoqi@0 2146 rs.findIdentInType(attrEnv, c.type, names.TYPE, VAR),
aoqi@0 2147 pos, c.type, names.TYPE, true);
aoqi@0 2148 if (typeSym.kind == VAR)
aoqi@0 2149 ((VarSymbol)typeSym).getConstValue(); // ensure initializer is evaluated
aoqi@0 2150 return make.QualIdent(typeSym);
aoqi@0 2151 case CLASS: case ARRAY:
aoqi@0 2152 if (target.hasClassLiterals()) {
aoqi@0 2153 VarSymbol sym = new VarSymbol(
aoqi@0 2154 STATIC | PUBLIC | FINAL, names._class,
aoqi@0 2155 syms.classType, type.tsym);
aoqi@0 2156 return make_at(pos).Select(make.Type(type), sym);
aoqi@0 2157 }
aoqi@0 2158 // replace with <cache == null ? cache = class$(tsig) : cache>
aoqi@0 2159 // where
aoqi@0 2160 // - <tsig> is the type signature of T,
aoqi@0 2161 // - <cache> is the cache variable for tsig.
aoqi@0 2162 String sig =
aoqi@0 2163 writer.xClassName(type).toString().replace('/', '.');
aoqi@0 2164 Symbol cs = cacheSym(pos, sig);
aoqi@0 2165 return make_at(pos).Conditional(
aoqi@0 2166 makeBinary(EQ, make.Ident(cs), makeNull()),
aoqi@0 2167 make.Assign(
aoqi@0 2168 make.Ident(cs),
aoqi@0 2169 make.App(
aoqi@0 2170 make.Ident(classDollarSym(pos)),
aoqi@0 2171 List.<JCExpression>of(make.Literal(CLASS, sig)
aoqi@0 2172 .setType(syms.stringType))))
aoqi@0 2173 .setType(types.erasure(syms.classType)),
aoqi@0 2174 make.Ident(cs)).setType(types.erasure(syms.classType));
aoqi@0 2175 default:
aoqi@0 2176 throw new AssertionError();
aoqi@0 2177 }
aoqi@0 2178 }
aoqi@0 2179
aoqi@0 2180 /**************************************************************************
aoqi@0 2181 * Code for enabling/disabling assertions.
aoqi@0 2182 *************************************************************************/
aoqi@0 2183
aoqi@0 2184 private ClassSymbol assertionsDisabledClassCache;
aoqi@0 2185
aoqi@0 2186 /**Used to create an auxiliary class to hold $assertionsDisabled for interfaces.
aoqi@0 2187 */
aoqi@0 2188 private ClassSymbol assertionsDisabledClass() {
aoqi@0 2189 if (assertionsDisabledClassCache != null) return assertionsDisabledClassCache;
aoqi@0 2190
aoqi@0 2191 assertionsDisabledClassCache = makeEmptyClass(STATIC | SYNTHETIC, outermostClassDef.sym).sym;
aoqi@0 2192
aoqi@0 2193 return assertionsDisabledClassCache;
aoqi@0 2194 }
aoqi@0 2195
aoqi@0 2196 // This code is not particularly robust if the user has
aoqi@0 2197 // previously declared a member named '$assertionsDisabled'.
aoqi@0 2198 // The same faulty idiom also appears in the translation of
aoqi@0 2199 // class literals above. We should report an error if a
aoqi@0 2200 // previous declaration is not synthetic.
aoqi@0 2201
aoqi@0 2202 private JCExpression assertFlagTest(DiagnosticPosition pos) {
aoqi@0 2203 // Outermost class may be either true class or an interface.
aoqi@0 2204 ClassSymbol outermostClass = outermostClassDef.sym;
aoqi@0 2205
aoqi@0 2206 //only classes can hold a non-public field, look for a usable one:
aoqi@0 2207 ClassSymbol container = !currentClass.isInterface() ? currentClass :
aoqi@0 2208 assertionsDisabledClass();
aoqi@0 2209
aoqi@0 2210 VarSymbol assertDisabledSym =
aoqi@0 2211 (VarSymbol)lookupSynthetic(dollarAssertionsDisabled,
aoqi@0 2212 container.members());
aoqi@0 2213 if (assertDisabledSym == null) {
aoqi@0 2214 assertDisabledSym =
aoqi@0 2215 new VarSymbol(STATIC | FINAL | SYNTHETIC,
aoqi@0 2216 dollarAssertionsDisabled,
aoqi@0 2217 syms.booleanType,
aoqi@0 2218 container);
aoqi@0 2219 enterSynthetic(pos, assertDisabledSym, container.members());
aoqi@0 2220 Symbol desiredAssertionStatusSym = lookupMethod(pos,
aoqi@0 2221 names.desiredAssertionStatus,
aoqi@0 2222 types.erasure(syms.classType),
aoqi@0 2223 List.<Type>nil());
aoqi@0 2224 JCClassDecl containerDef = classDef(container);
aoqi@0 2225 make_at(containerDef.pos());
aoqi@0 2226 JCExpression notStatus = makeUnary(NOT, make.App(make.Select(
aoqi@0 2227 classOfType(types.erasure(outermostClass.type),
aoqi@0 2228 containerDef.pos()),
aoqi@0 2229 desiredAssertionStatusSym)));
aoqi@0 2230 JCVariableDecl assertDisabledDef = make.VarDef(assertDisabledSym,
aoqi@0 2231 notStatus);
aoqi@0 2232 containerDef.defs = containerDef.defs.prepend(assertDisabledDef);
aoqi@0 2233
aoqi@0 2234 if (currentClass.isInterface()) {
aoqi@0 2235 //need to load the assertions enabled/disabled state while
aoqi@0 2236 //initializing the interface:
aoqi@0 2237 JCClassDecl currentClassDef = classDef(currentClass);
aoqi@0 2238 make_at(currentClassDef.pos());
aoqi@0 2239 JCStatement dummy = make.If(make.QualIdent(assertDisabledSym), make.Skip(), null);
aoqi@0 2240 JCBlock clinit = make.Block(STATIC, List.<JCStatement>of(dummy));
aoqi@0 2241 currentClassDef.defs = currentClassDef.defs.prepend(clinit);
aoqi@0 2242 }
aoqi@0 2243 }
aoqi@0 2244 make_at(pos);
aoqi@0 2245 return makeUnary(NOT, make.Ident(assertDisabledSym));
aoqi@0 2246 }
aoqi@0 2247
aoqi@0 2248
aoqi@0 2249 /**************************************************************************
aoqi@0 2250 * Building blocks for let expressions
aoqi@0 2251 *************************************************************************/
aoqi@0 2252
aoqi@0 2253 interface TreeBuilder {
aoqi@0 2254 JCTree build(JCTree arg);
aoqi@0 2255 }
aoqi@0 2256
aoqi@0 2257 /** Construct an expression using the builder, with the given rval
aoqi@0 2258 * expression as an argument to the builder. However, the rval
aoqi@0 2259 * expression must be computed only once, even if used multiple
aoqi@0 2260 * times in the result of the builder. We do that by
aoqi@0 2261 * constructing a "let" expression that saves the rvalue into a
aoqi@0 2262 * temporary variable and then uses the temporary variable in
aoqi@0 2263 * place of the expression built by the builder. The complete
aoqi@0 2264 * resulting expression is of the form
aoqi@0 2265 * <pre>
aoqi@0 2266 * (let <b>TYPE</b> <b>TEMP</b> = <b>RVAL</b>;
aoqi@0 2267 * in (<b>BUILDER</b>(<b>TEMP</b>)))
aoqi@0 2268 * </pre>
aoqi@0 2269 * where <code><b>TEMP</b></code> is a newly declared variable
aoqi@0 2270 * in the let expression.
aoqi@0 2271 */
aoqi@0 2272 JCTree abstractRval(JCTree rval, Type type, TreeBuilder builder) {
aoqi@0 2273 rval = TreeInfo.skipParens(rval);
aoqi@0 2274 switch (rval.getTag()) {
aoqi@0 2275 case LITERAL:
aoqi@0 2276 return builder.build(rval);
aoqi@0 2277 case IDENT:
aoqi@0 2278 JCIdent id = (JCIdent) rval;
aoqi@0 2279 if ((id.sym.flags() & FINAL) != 0 && id.sym.owner.kind == MTH)
aoqi@0 2280 return builder.build(rval);
aoqi@0 2281 }
aoqi@0 2282 VarSymbol var =
aoqi@0 2283 new VarSymbol(FINAL|SYNTHETIC,
aoqi@0 2284 names.fromString(
aoqi@0 2285 target.syntheticNameChar()
aoqi@0 2286 + "" + rval.hashCode()),
aoqi@0 2287 type,
aoqi@0 2288 currentMethodSym);
aoqi@0 2289 rval = convert(rval,type);
aoqi@0 2290 JCVariableDecl def = make.VarDef(var, (JCExpression)rval); // XXX cast
aoqi@0 2291 JCTree built = builder.build(make.Ident(var));
aoqi@0 2292 JCTree res = make.LetExpr(def, built);
aoqi@0 2293 res.type = built.type;
aoqi@0 2294 return res;
aoqi@0 2295 }
aoqi@0 2296
aoqi@0 2297 // same as above, with the type of the temporary variable computed
aoqi@0 2298 JCTree abstractRval(JCTree rval, TreeBuilder builder) {
aoqi@0 2299 return abstractRval(rval, rval.type, builder);
aoqi@0 2300 }
aoqi@0 2301
aoqi@0 2302 // same as above, but for an expression that may be used as either
aoqi@0 2303 // an rvalue or an lvalue. This requires special handling for
aoqi@0 2304 // Select expressions, where we place the left-hand-side of the
aoqi@0 2305 // select in a temporary, and for Indexed expressions, where we
aoqi@0 2306 // place both the indexed expression and the index value in temps.
aoqi@0 2307 JCTree abstractLval(JCTree lval, final TreeBuilder builder) {
aoqi@0 2308 lval = TreeInfo.skipParens(lval);
aoqi@0 2309 switch (lval.getTag()) {
aoqi@0 2310 case IDENT:
aoqi@0 2311 return builder.build(lval);
aoqi@0 2312 case SELECT: {
aoqi@0 2313 final JCFieldAccess s = (JCFieldAccess)lval;
aoqi@0 2314 JCTree selected = TreeInfo.skipParens(s.selected);
aoqi@0 2315 Symbol lid = TreeInfo.symbol(s.selected);
aoqi@0 2316 if (lid != null && lid.kind == TYP) return builder.build(lval);
aoqi@0 2317 return abstractRval(s.selected, new TreeBuilder() {
aoqi@0 2318 public JCTree build(final JCTree selected) {
aoqi@0 2319 return builder.build(make.Select((JCExpression)selected, s.sym));
aoqi@0 2320 }
aoqi@0 2321 });
aoqi@0 2322 }
aoqi@0 2323 case INDEXED: {
aoqi@0 2324 final JCArrayAccess i = (JCArrayAccess)lval;
aoqi@0 2325 return abstractRval(i.indexed, new TreeBuilder() {
aoqi@0 2326 public JCTree build(final JCTree indexed) {
aoqi@0 2327 return abstractRval(i.index, syms.intType, new TreeBuilder() {
aoqi@0 2328 public JCTree build(final JCTree index) {
aoqi@0 2329 JCTree newLval = make.Indexed((JCExpression)indexed,
aoqi@0 2330 (JCExpression)index);
aoqi@0 2331 newLval.setType(i.type);
aoqi@0 2332 return builder.build(newLval);
aoqi@0 2333 }
aoqi@0 2334 });
aoqi@0 2335 }
aoqi@0 2336 });
aoqi@0 2337 }
aoqi@0 2338 case TYPECAST: {
aoqi@0 2339 return abstractLval(((JCTypeCast)lval).expr, builder);
aoqi@0 2340 }
aoqi@0 2341 }
aoqi@0 2342 throw new AssertionError(lval);
aoqi@0 2343 }
aoqi@0 2344
aoqi@0 2345 // evaluate and discard the first expression, then evaluate the second.
aoqi@0 2346 JCTree makeComma(final JCTree expr1, final JCTree expr2) {
aoqi@0 2347 return abstractRval(expr1, new TreeBuilder() {
aoqi@0 2348 public JCTree build(final JCTree discarded) {
aoqi@0 2349 return expr2;
aoqi@0 2350 }
aoqi@0 2351 });
aoqi@0 2352 }
aoqi@0 2353
aoqi@0 2354 /**************************************************************************
aoqi@0 2355 * Translation methods
aoqi@0 2356 *************************************************************************/
aoqi@0 2357
aoqi@0 2358 /** Visitor argument: enclosing operator node.
aoqi@0 2359 */
aoqi@0 2360 private JCExpression enclOp;
aoqi@0 2361
aoqi@0 2362 /** Visitor method: Translate a single node.
aoqi@0 2363 * Attach the source position from the old tree to its replacement tree.
aoqi@0 2364 */
aoqi@0 2365 @Override
aoqi@0 2366 public <T extends JCTree> T translate(T tree) {
aoqi@0 2367 if (tree == null) {
aoqi@0 2368 return null;
aoqi@0 2369 } else {
aoqi@0 2370 make_at(tree.pos());
aoqi@0 2371 T result = super.translate(tree);
aoqi@0 2372 if (endPosTable != null && result != tree) {
aoqi@0 2373 endPosTable.replaceTree(tree, result);
aoqi@0 2374 }
aoqi@0 2375 return result;
aoqi@0 2376 }
aoqi@0 2377 }
aoqi@0 2378
aoqi@0 2379 /** Visitor method: Translate a single node, boxing or unboxing if needed.
aoqi@0 2380 */
aoqi@0 2381 public <T extends JCTree> T translate(T tree, Type type) {
aoqi@0 2382 return (tree == null) ? null : boxIfNeeded(translate(tree), type);
aoqi@0 2383 }
aoqi@0 2384
aoqi@0 2385 /** Visitor method: Translate tree.
aoqi@0 2386 */
aoqi@0 2387 public <T extends JCTree> T translate(T tree, JCExpression enclOp) {
aoqi@0 2388 JCExpression prevEnclOp = this.enclOp;
aoqi@0 2389 this.enclOp = enclOp;
aoqi@0 2390 T res = translate(tree);
aoqi@0 2391 this.enclOp = prevEnclOp;
aoqi@0 2392 return res;
aoqi@0 2393 }
aoqi@0 2394
aoqi@0 2395 /** Visitor method: Translate list of trees.
aoqi@0 2396 */
aoqi@0 2397 public <T extends JCTree> List<T> translate(List<T> trees, JCExpression enclOp) {
aoqi@0 2398 JCExpression prevEnclOp = this.enclOp;
aoqi@0 2399 this.enclOp = enclOp;
aoqi@0 2400 List<T> res = translate(trees);
aoqi@0 2401 this.enclOp = prevEnclOp;
aoqi@0 2402 return res;
aoqi@0 2403 }
aoqi@0 2404
aoqi@0 2405 /** Visitor method: Translate list of trees.
aoqi@0 2406 */
aoqi@0 2407 public <T extends JCTree> List<T> translate(List<T> trees, Type type) {
aoqi@0 2408 if (trees == null) return null;
aoqi@0 2409 for (List<T> l = trees; l.nonEmpty(); l = l.tail)
aoqi@0 2410 l.head = translate(l.head, type);
aoqi@0 2411 return trees;
aoqi@0 2412 }
aoqi@0 2413
aoqi@0 2414 public void visitTopLevel(JCCompilationUnit tree) {
aoqi@0 2415 if (needPackageInfoClass(tree)) {
aoqi@0 2416 Name name = names.package_info;
aoqi@0 2417 long flags = Flags.ABSTRACT | Flags.INTERFACE;
aoqi@0 2418 if (target.isPackageInfoSynthetic())
aoqi@0 2419 // package-info is marked SYNTHETIC in JDK 1.6 and later releases
aoqi@0 2420 flags = flags | Flags.SYNTHETIC;
aoqi@0 2421 JCClassDecl packageAnnotationsClass
aoqi@0 2422 = make.ClassDef(make.Modifiers(flags,
aoqi@0 2423 tree.packageAnnotations),
aoqi@0 2424 name, List.<JCTypeParameter>nil(),
aoqi@0 2425 null, List.<JCExpression>nil(), List.<JCTree>nil());
aoqi@0 2426 ClassSymbol c = tree.packge.package_info;
aoqi@0 2427 c.flags_field |= flags;
aoqi@0 2428 c.setAttributes(tree.packge);
aoqi@0 2429 ClassType ctype = (ClassType) c.type;
aoqi@0 2430 ctype.supertype_field = syms.objectType;
aoqi@0 2431 ctype.interfaces_field = List.nil();
aoqi@0 2432 packageAnnotationsClass.sym = c;
aoqi@0 2433
aoqi@0 2434 translated.append(packageAnnotationsClass);
aoqi@0 2435 }
aoqi@0 2436 }
aoqi@0 2437 // where
aoqi@0 2438 private boolean needPackageInfoClass(JCCompilationUnit tree) {
aoqi@0 2439 switch (pkginfoOpt) {
aoqi@0 2440 case ALWAYS:
aoqi@0 2441 return true;
aoqi@0 2442 case LEGACY:
aoqi@0 2443 return tree.packageAnnotations.nonEmpty();
aoqi@0 2444 case NONEMPTY:
aoqi@0 2445 for (Attribute.Compound a :
aoqi@0 2446 tree.packge.getDeclarationAttributes()) {
aoqi@0 2447 Attribute.RetentionPolicy p = types.getRetention(a);
aoqi@0 2448 if (p != Attribute.RetentionPolicy.SOURCE)
aoqi@0 2449 return true;
aoqi@0 2450 }
aoqi@0 2451 return false;
aoqi@0 2452 }
aoqi@0 2453 throw new AssertionError();
aoqi@0 2454 }
aoqi@0 2455
aoqi@0 2456 public void visitClassDef(JCClassDecl tree) {
aoqi@0 2457 Env<AttrContext> prevEnv = attrEnv;
aoqi@0 2458 ClassSymbol currentClassPrev = currentClass;
aoqi@0 2459 MethodSymbol currentMethodSymPrev = currentMethodSym;
aoqi@0 2460
aoqi@0 2461 currentClass = tree.sym;
aoqi@0 2462 currentMethodSym = null;
aoqi@0 2463 attrEnv = typeEnvs.remove(currentClass);
aoqi@0 2464 if (attrEnv == null)
aoqi@0 2465 attrEnv = prevEnv;
aoqi@0 2466
aoqi@0 2467 classdefs.put(currentClass, tree);
aoqi@0 2468
aoqi@0 2469 proxies = proxies.dup(currentClass);
aoqi@0 2470 List<VarSymbol> prevOuterThisStack = outerThisStack;
aoqi@0 2471
aoqi@0 2472 // If this is an enum definition
aoqi@0 2473 if ((tree.mods.flags & ENUM) != 0 &&
aoqi@0 2474 (types.supertype(currentClass.type).tsym.flags() & ENUM) == 0)
aoqi@0 2475 visitEnumDef(tree);
aoqi@0 2476
aoqi@0 2477 // If this is a nested class, define a this$n field for
aoqi@0 2478 // it and add to proxies.
aoqi@0 2479 JCVariableDecl otdef = null;
aoqi@0 2480 if (currentClass.hasOuterInstance())
aoqi@0 2481 otdef = outerThisDef(tree.pos, currentClass);
aoqi@0 2482
aoqi@0 2483 // If this is a local class, define proxies for all its free variables.
aoqi@0 2484 List<JCVariableDecl> fvdefs = freevarDefs(
aoqi@0 2485 tree.pos, freevars(currentClass), currentClass);
aoqi@0 2486
aoqi@0 2487 // Recursively translate superclass, interfaces.
aoqi@0 2488 tree.extending = translate(tree.extending);
aoqi@0 2489 tree.implementing = translate(tree.implementing);
aoqi@0 2490
aoqi@0 2491 if (currentClass.isLocal()) {
aoqi@0 2492 ClassSymbol encl = currentClass.owner.enclClass();
aoqi@0 2493 if (encl.trans_local == null) {
aoqi@0 2494 encl.trans_local = List.nil();
aoqi@0 2495 }
aoqi@0 2496 encl.trans_local = encl.trans_local.prepend(currentClass);
aoqi@0 2497 }
aoqi@0 2498
aoqi@0 2499 // Recursively translate members, taking into account that new members
aoqi@0 2500 // might be created during the translation and prepended to the member
aoqi@0 2501 // list `tree.defs'.
aoqi@0 2502 List<JCTree> seen = List.nil();
aoqi@0 2503 while (tree.defs != seen) {
aoqi@0 2504 List<JCTree> unseen = tree.defs;
aoqi@0 2505 for (List<JCTree> l = unseen; l.nonEmpty() && l != seen; l = l.tail) {
aoqi@0 2506 JCTree outermostMemberDefPrev = outermostMemberDef;
aoqi@0 2507 if (outermostMemberDefPrev == null) outermostMemberDef = l.head;
aoqi@0 2508 l.head = translate(l.head);
aoqi@0 2509 outermostMemberDef = outermostMemberDefPrev;
aoqi@0 2510 }
aoqi@0 2511 seen = unseen;
aoqi@0 2512 }
aoqi@0 2513
aoqi@0 2514 // Convert a protected modifier to public, mask static modifier.
aoqi@0 2515 if ((tree.mods.flags & PROTECTED) != 0) tree.mods.flags |= PUBLIC;
aoqi@0 2516 tree.mods.flags &= ClassFlags;
aoqi@0 2517
aoqi@0 2518 // Convert name to flat representation, replacing '.' by '$'.
aoqi@0 2519 tree.name = Convert.shortName(currentClass.flatName());
aoqi@0 2520
aoqi@0 2521 // Add this$n and free variables proxy definitions to class.
aoqi@0 2522
aoqi@0 2523 for (List<JCVariableDecl> l = fvdefs; l.nonEmpty(); l = l.tail) {
aoqi@0 2524 tree.defs = tree.defs.prepend(l.head);
aoqi@0 2525 enterSynthetic(tree.pos(), l.head.sym, currentClass.members());
aoqi@0 2526 }
aoqi@0 2527 if (currentClass.hasOuterInstance()) {
aoqi@0 2528 tree.defs = tree.defs.prepend(otdef);
aoqi@0 2529 enterSynthetic(tree.pos(), otdef.sym, currentClass.members());
aoqi@0 2530 }
aoqi@0 2531
aoqi@0 2532 proxies = proxies.leave();
aoqi@0 2533 outerThisStack = prevOuterThisStack;
aoqi@0 2534
aoqi@0 2535 // Append translated tree to `translated' queue.
aoqi@0 2536 translated.append(tree);
aoqi@0 2537
aoqi@0 2538 attrEnv = prevEnv;
aoqi@0 2539 currentClass = currentClassPrev;
aoqi@0 2540 currentMethodSym = currentMethodSymPrev;
aoqi@0 2541
aoqi@0 2542 // Return empty block {} as a placeholder for an inner class.
mcimadamore@2789 2543 result = make_at(tree.pos()).Block(SYNTHETIC, List.<JCStatement>nil());
aoqi@0 2544 }
aoqi@0 2545
aoqi@0 2546 /** Translate an enum class. */
aoqi@0 2547 private void visitEnumDef(JCClassDecl tree) {
aoqi@0 2548 make_at(tree.pos());
aoqi@0 2549
aoqi@0 2550 // add the supertype, if needed
aoqi@0 2551 if (tree.extending == null)
aoqi@0 2552 tree.extending = make.Type(types.supertype(tree.type));
aoqi@0 2553
aoqi@0 2554 // classOfType adds a cache field to tree.defs unless
aoqi@0 2555 // target.hasClassLiterals().
aoqi@0 2556 JCExpression e_class = classOfType(tree.sym.type, tree.pos()).
aoqi@0 2557 setType(types.erasure(syms.classType));
aoqi@0 2558
aoqi@0 2559 // process each enumeration constant, adding implicit constructor parameters
aoqi@0 2560 int nextOrdinal = 0;
aoqi@0 2561 ListBuffer<JCExpression> values = new ListBuffer<JCExpression>();
aoqi@0 2562 ListBuffer<JCTree> enumDefs = new ListBuffer<JCTree>();
aoqi@0 2563 ListBuffer<JCTree> otherDefs = new ListBuffer<JCTree>();
aoqi@0 2564 for (List<JCTree> defs = tree.defs;
aoqi@0 2565 defs.nonEmpty();
aoqi@0 2566 defs=defs.tail) {
aoqi@0 2567 if (defs.head.hasTag(VARDEF) && (((JCVariableDecl) defs.head).mods.flags & ENUM) != 0) {
aoqi@0 2568 JCVariableDecl var = (JCVariableDecl)defs.head;
aoqi@0 2569 visitEnumConstantDef(var, nextOrdinal++);
aoqi@0 2570 values.append(make.QualIdent(var.sym));
aoqi@0 2571 enumDefs.append(var);
aoqi@0 2572 } else {
aoqi@0 2573 otherDefs.append(defs.head);
aoqi@0 2574 }
aoqi@0 2575 }
aoqi@0 2576
aoqi@0 2577 // private static final T[] #VALUES = { a, b, c };
aoqi@0 2578 Name valuesName = names.fromString(target.syntheticNameChar() + "VALUES");
aoqi@0 2579 while (tree.sym.members().lookup(valuesName).scope != null) // avoid name clash
aoqi@0 2580 valuesName = names.fromString(valuesName + "" + target.syntheticNameChar());
aoqi@0 2581 Type arrayType = new ArrayType(types.erasure(tree.type), syms.arrayClass);
aoqi@0 2582 VarSymbol valuesVar = new VarSymbol(PRIVATE|FINAL|STATIC|SYNTHETIC,
aoqi@0 2583 valuesName,
aoqi@0 2584 arrayType,
aoqi@0 2585 tree.type.tsym);
aoqi@0 2586 JCNewArray newArray = make.NewArray(make.Type(types.erasure(tree.type)),
aoqi@0 2587 List.<JCExpression>nil(),
aoqi@0 2588 values.toList());
aoqi@0 2589 newArray.type = arrayType;
aoqi@0 2590 enumDefs.append(make.VarDef(valuesVar, newArray));
aoqi@0 2591 tree.sym.members().enter(valuesVar);
aoqi@0 2592
aoqi@0 2593 Symbol valuesSym = lookupMethod(tree.pos(), names.values,
aoqi@0 2594 tree.type, List.<Type>nil());
aoqi@0 2595 List<JCStatement> valuesBody;
aoqi@0 2596 if (useClone()) {
aoqi@0 2597 // return (T[]) $VALUES.clone();
aoqi@0 2598 JCTypeCast valuesResult =
aoqi@0 2599 make.TypeCast(valuesSym.type.getReturnType(),
aoqi@0 2600 make.App(make.Select(make.Ident(valuesVar),
aoqi@0 2601 syms.arrayCloneMethod)));
aoqi@0 2602 valuesBody = List.<JCStatement>of(make.Return(valuesResult));
aoqi@0 2603 } else {
aoqi@0 2604 // template: T[] $result = new T[$values.length];
aoqi@0 2605 Name resultName = names.fromString(target.syntheticNameChar() + "result");
aoqi@0 2606 while (tree.sym.members().lookup(resultName).scope != null) // avoid name clash
aoqi@0 2607 resultName = names.fromString(resultName + "" + target.syntheticNameChar());
aoqi@0 2608 VarSymbol resultVar = new VarSymbol(FINAL|SYNTHETIC,
aoqi@0 2609 resultName,
aoqi@0 2610 arrayType,
aoqi@0 2611 valuesSym);
aoqi@0 2612 JCNewArray resultArray = make.NewArray(make.Type(types.erasure(tree.type)),
aoqi@0 2613 List.of(make.Select(make.Ident(valuesVar), syms.lengthVar)),
aoqi@0 2614 null);
aoqi@0 2615 resultArray.type = arrayType;
aoqi@0 2616 JCVariableDecl decl = make.VarDef(resultVar, resultArray);
aoqi@0 2617
aoqi@0 2618 // template: System.arraycopy($VALUES, 0, $result, 0, $VALUES.length);
aoqi@0 2619 if (systemArraycopyMethod == null) {
aoqi@0 2620 systemArraycopyMethod =
aoqi@0 2621 new MethodSymbol(PUBLIC | STATIC,
aoqi@0 2622 names.fromString("arraycopy"),
aoqi@0 2623 new MethodType(List.<Type>of(syms.objectType,
aoqi@0 2624 syms.intType,
aoqi@0 2625 syms.objectType,
aoqi@0 2626 syms.intType,
aoqi@0 2627 syms.intType),
aoqi@0 2628 syms.voidType,
aoqi@0 2629 List.<Type>nil(),
aoqi@0 2630 syms.methodClass),
aoqi@0 2631 syms.systemType.tsym);
aoqi@0 2632 }
aoqi@0 2633 JCStatement copy =
aoqi@0 2634 make.Exec(make.App(make.Select(make.Ident(syms.systemType.tsym),
aoqi@0 2635 systemArraycopyMethod),
aoqi@0 2636 List.of(make.Ident(valuesVar), make.Literal(0),
aoqi@0 2637 make.Ident(resultVar), make.Literal(0),
aoqi@0 2638 make.Select(make.Ident(valuesVar), syms.lengthVar))));
aoqi@0 2639
aoqi@0 2640 // template: return $result;
aoqi@0 2641 JCStatement ret = make.Return(make.Ident(resultVar));
aoqi@0 2642 valuesBody = List.<JCStatement>of(decl, copy, ret);
aoqi@0 2643 }
aoqi@0 2644
aoqi@0 2645 JCMethodDecl valuesDef =
aoqi@0 2646 make.MethodDef((MethodSymbol)valuesSym, make.Block(0, valuesBody));
aoqi@0 2647
aoqi@0 2648 enumDefs.append(valuesDef);
aoqi@0 2649
aoqi@0 2650 if (debugLower)
aoqi@0 2651 System.err.println(tree.sym + ".valuesDef = " + valuesDef);
aoqi@0 2652
aoqi@0 2653 /** The template for the following code is:
aoqi@0 2654 *
aoqi@0 2655 * public static E valueOf(String name) {
aoqi@0 2656 * return (E)Enum.valueOf(E.class, name);
aoqi@0 2657 * }
aoqi@0 2658 *
aoqi@0 2659 * where E is tree.sym
aoqi@0 2660 */
aoqi@0 2661 MethodSymbol valueOfSym = lookupMethod(tree.pos(),
aoqi@0 2662 names.valueOf,
aoqi@0 2663 tree.sym.type,
aoqi@0 2664 List.of(syms.stringType));
aoqi@0 2665 Assert.check((valueOfSym.flags() & STATIC) != 0);
aoqi@0 2666 VarSymbol nameArgSym = valueOfSym.params.head;
aoqi@0 2667 JCIdent nameVal = make.Ident(nameArgSym);
aoqi@0 2668 JCStatement enum_ValueOf =
aoqi@0 2669 make.Return(make.TypeCast(tree.sym.type,
aoqi@0 2670 makeCall(make.Ident(syms.enumSym),
aoqi@0 2671 names.valueOf,
aoqi@0 2672 List.of(e_class, nameVal))));
aoqi@0 2673 JCMethodDecl valueOf = make.MethodDef(valueOfSym,
aoqi@0 2674 make.Block(0, List.of(enum_ValueOf)));
aoqi@0 2675 nameVal.sym = valueOf.params.head.sym;
aoqi@0 2676 if (debugLower)
aoqi@0 2677 System.err.println(tree.sym + ".valueOf = " + valueOf);
aoqi@0 2678 enumDefs.append(valueOf);
aoqi@0 2679
aoqi@0 2680 enumDefs.appendList(otherDefs.toList());
aoqi@0 2681 tree.defs = enumDefs.toList();
aoqi@0 2682 }
aoqi@0 2683 // where
aoqi@0 2684 private MethodSymbol systemArraycopyMethod;
aoqi@0 2685 private boolean useClone() {
aoqi@0 2686 try {
aoqi@0 2687 Scope.Entry e = syms.objectType.tsym.members().lookup(names.clone);
aoqi@0 2688 return (e.sym != null);
aoqi@0 2689 }
aoqi@0 2690 catch (CompletionFailure e) {
aoqi@0 2691 return false;
aoqi@0 2692 }
aoqi@0 2693 }
aoqi@0 2694
aoqi@0 2695 /** Translate an enumeration constant and its initializer. */
aoqi@0 2696 private void visitEnumConstantDef(JCVariableDecl var, int ordinal) {
aoqi@0 2697 JCNewClass varDef = (JCNewClass)var.init;
aoqi@0 2698 varDef.args = varDef.args.
aoqi@0 2699 prepend(makeLit(syms.intType, ordinal)).
aoqi@0 2700 prepend(makeLit(syms.stringType, var.name.toString()));
aoqi@0 2701 }
aoqi@0 2702
aoqi@0 2703 public void visitMethodDef(JCMethodDecl tree) {
aoqi@0 2704 if (tree.name == names.init && (currentClass.flags_field&ENUM) != 0) {
aoqi@0 2705 // Add "String $enum$name, int $enum$ordinal" to the beginning of the
aoqi@0 2706 // argument list for each constructor of an enum.
aoqi@0 2707 JCVariableDecl nameParam = make_at(tree.pos()).
aoqi@0 2708 Param(names.fromString(target.syntheticNameChar() +
aoqi@0 2709 "enum" + target.syntheticNameChar() + "name"),
aoqi@0 2710 syms.stringType, tree.sym);
aoqi@0 2711 nameParam.mods.flags |= SYNTHETIC; nameParam.sym.flags_field |= SYNTHETIC;
aoqi@0 2712 JCVariableDecl ordParam = make.
aoqi@0 2713 Param(names.fromString(target.syntheticNameChar() +
aoqi@0 2714 "enum" + target.syntheticNameChar() +
aoqi@0 2715 "ordinal"),
aoqi@0 2716 syms.intType, tree.sym);
aoqi@0 2717 ordParam.mods.flags |= SYNTHETIC; ordParam.sym.flags_field |= SYNTHETIC;
aoqi@0 2718
aoqi@0 2719 tree.params = tree.params.prepend(ordParam).prepend(nameParam);
aoqi@0 2720
aoqi@0 2721 MethodSymbol m = tree.sym;
aoqi@0 2722 m.extraParams = m.extraParams.prepend(ordParam.sym);
aoqi@0 2723 m.extraParams = m.extraParams.prepend(nameParam.sym);
aoqi@0 2724 Type olderasure = m.erasure(types);
aoqi@0 2725 m.erasure_field = new MethodType(
aoqi@0 2726 olderasure.getParameterTypes().prepend(syms.intType).prepend(syms.stringType),
aoqi@0 2727 olderasure.getReturnType(),
aoqi@0 2728 olderasure.getThrownTypes(),
aoqi@0 2729 syms.methodClass);
aoqi@0 2730 }
aoqi@0 2731
aoqi@0 2732 JCMethodDecl prevMethodDef = currentMethodDef;
aoqi@0 2733 MethodSymbol prevMethodSym = currentMethodSym;
aoqi@0 2734 try {
aoqi@0 2735 currentMethodDef = tree;
aoqi@0 2736 currentMethodSym = tree.sym;
aoqi@0 2737 visitMethodDefInternal(tree);
aoqi@0 2738 } finally {
aoqi@0 2739 currentMethodDef = prevMethodDef;
aoqi@0 2740 currentMethodSym = prevMethodSym;
aoqi@0 2741 }
aoqi@0 2742 }
aoqi@0 2743 //where
aoqi@0 2744 private void visitMethodDefInternal(JCMethodDecl tree) {
aoqi@0 2745 if (tree.name == names.init &&
aoqi@0 2746 (currentClass.isInner() || currentClass.isLocal())) {
aoqi@0 2747 // We are seeing a constructor of an inner class.
aoqi@0 2748 MethodSymbol m = tree.sym;
aoqi@0 2749
aoqi@0 2750 // Push a new proxy scope for constructor parameters.
aoqi@0 2751 // and create definitions for any this$n and proxy parameters.
aoqi@0 2752 proxies = proxies.dup(m);
aoqi@0 2753 List<VarSymbol> prevOuterThisStack = outerThisStack;
aoqi@0 2754 List<VarSymbol> fvs = freevars(currentClass);
aoqi@0 2755 JCVariableDecl otdef = null;
aoqi@0 2756 if (currentClass.hasOuterInstance())
aoqi@0 2757 otdef = outerThisDef(tree.pos, m);
aoqi@0 2758 List<JCVariableDecl> fvdefs = freevarDefs(tree.pos, fvs, m, PARAMETER);
aoqi@0 2759
aoqi@0 2760 // Recursively translate result type, parameters and thrown list.
aoqi@0 2761 tree.restype = translate(tree.restype);
aoqi@0 2762 tree.params = translateVarDefs(tree.params);
aoqi@0 2763 tree.thrown = translate(tree.thrown);
aoqi@0 2764
aoqi@0 2765 // when compiling stubs, don't process body
aoqi@0 2766 if (tree.body == null) {
aoqi@0 2767 result = tree;
aoqi@0 2768 return;
aoqi@0 2769 }
aoqi@0 2770
aoqi@0 2771 // Add this$n (if needed) in front of and free variables behind
aoqi@0 2772 // constructor parameter list.
aoqi@0 2773 tree.params = tree.params.appendList(fvdefs);
aoqi@0 2774 if (currentClass.hasOuterInstance())
aoqi@0 2775 tree.params = tree.params.prepend(otdef);
aoqi@0 2776
aoqi@0 2777 // If this is an initial constructor, i.e., it does not start with
aoqi@0 2778 // this(...), insert initializers for this$n and proxies
aoqi@0 2779 // before (pre-1.4, after) the call to superclass constructor.
aoqi@0 2780 JCStatement selfCall = translate(tree.body.stats.head);
aoqi@0 2781
aoqi@0 2782 List<JCStatement> added = List.nil();
aoqi@0 2783 if (fvs.nonEmpty()) {
aoqi@0 2784 List<Type> addedargtypes = List.nil();
aoqi@0 2785 for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail) {
aoqi@0 2786 if (TreeInfo.isInitialConstructor(tree)) {
aoqi@0 2787 final Name pName = proxyName(l.head.name);
aoqi@0 2788 m.capturedLocals =
aoqi@0 2789 m.capturedLocals.append((VarSymbol)
aoqi@0 2790 (proxies.lookup(pName).sym));
aoqi@0 2791 added = added.prepend(
aoqi@0 2792 initField(tree.body.pos, pName));
aoqi@0 2793 }
aoqi@0 2794 addedargtypes = addedargtypes.prepend(l.head.erasure(types));
aoqi@0 2795 }
aoqi@0 2796 Type olderasure = m.erasure(types);
aoqi@0 2797 m.erasure_field = new MethodType(
aoqi@0 2798 olderasure.getParameterTypes().appendList(addedargtypes),
aoqi@0 2799 olderasure.getReturnType(),
aoqi@0 2800 olderasure.getThrownTypes(),
aoqi@0 2801 syms.methodClass);
aoqi@0 2802 }
aoqi@0 2803 if (currentClass.hasOuterInstance() &&
aoqi@0 2804 TreeInfo.isInitialConstructor(tree))
aoqi@0 2805 {
aoqi@0 2806 added = added.prepend(initOuterThis(tree.body.pos));
aoqi@0 2807 }
aoqi@0 2808
aoqi@0 2809 // pop local variables from proxy stack
aoqi@0 2810 proxies = proxies.leave();
aoqi@0 2811
aoqi@0 2812 // recursively translate following local statements and
aoqi@0 2813 // combine with this- or super-call
aoqi@0 2814 List<JCStatement> stats = translate(tree.body.stats.tail);
aoqi@0 2815 if (target.initializeFieldsBeforeSuper())
aoqi@0 2816 tree.body.stats = stats.prepend(selfCall).prependList(added);
aoqi@0 2817 else
aoqi@0 2818 tree.body.stats = stats.prependList(added).prepend(selfCall);
aoqi@0 2819
aoqi@0 2820 outerThisStack = prevOuterThisStack;
aoqi@0 2821 } else {
aoqi@0 2822 Map<Symbol, Symbol> prevLambdaTranslationMap =
aoqi@0 2823 lambdaTranslationMap;
aoqi@0 2824 try {
aoqi@0 2825 lambdaTranslationMap = (tree.sym.flags() & SYNTHETIC) != 0 &&
aoqi@0 2826 tree.sym.name.startsWith(names.lambda) ?
aoqi@0 2827 makeTranslationMap(tree) : null;
aoqi@0 2828 super.visitMethodDef(tree);
aoqi@0 2829 } finally {
aoqi@0 2830 lambdaTranslationMap = prevLambdaTranslationMap;
aoqi@0 2831 }
aoqi@0 2832 }
aoqi@0 2833 result = tree;
aoqi@0 2834 }
aoqi@0 2835 //where
aoqi@0 2836 private Map<Symbol, Symbol> makeTranslationMap(JCMethodDecl tree) {
aoqi@0 2837 Map<Symbol, Symbol> translationMap = new HashMap<Symbol,Symbol>();
aoqi@0 2838 for (JCVariableDecl vd : tree.params) {
aoqi@0 2839 Symbol p = vd.sym;
aoqi@0 2840 if (p != p.baseSymbol()) {
aoqi@0 2841 translationMap.put(p.baseSymbol(), p);
aoqi@0 2842 }
aoqi@0 2843 }
aoqi@0 2844 return translationMap;
aoqi@0 2845 }
aoqi@0 2846
aoqi@0 2847 public void visitAnnotatedType(JCAnnotatedType tree) {
aoqi@0 2848 // No need to retain type annotations in the tree
aoqi@0 2849 // tree.annotations = translate(tree.annotations);
aoqi@0 2850 tree.annotations = List.nil();
aoqi@0 2851 tree.underlyingType = translate(tree.underlyingType);
aoqi@0 2852 // but maintain type annotations in the type.
aoqi@0 2853 if (tree.type.isAnnotated()) {
aoqi@0 2854 tree.type = tree.underlyingType.type.unannotatedType().annotatedType(tree.type.getAnnotationMirrors());
aoqi@0 2855 } else if (tree.underlyingType.type.isAnnotated()) {
aoqi@0 2856 tree.type = tree.underlyingType.type;
aoqi@0 2857 }
aoqi@0 2858 result = tree;
aoqi@0 2859 }
aoqi@0 2860
aoqi@0 2861 public void visitTypeCast(JCTypeCast tree) {
aoqi@0 2862 tree.clazz = translate(tree.clazz);
aoqi@0 2863 if (tree.type.isPrimitive() != tree.expr.type.isPrimitive())
aoqi@0 2864 tree.expr = translate(tree.expr, tree.type);
aoqi@0 2865 else
aoqi@0 2866 tree.expr = translate(tree.expr);
aoqi@0 2867 result = tree;
aoqi@0 2868 }
aoqi@0 2869
aoqi@0 2870 public void visitNewClass(JCNewClass tree) {
aoqi@0 2871 ClassSymbol c = (ClassSymbol)tree.constructor.owner;
aoqi@0 2872
aoqi@0 2873 // Box arguments, if necessary
aoqi@0 2874 boolean isEnum = (tree.constructor.owner.flags() & ENUM) != 0;
aoqi@0 2875 List<Type> argTypes = tree.constructor.type.getParameterTypes();
aoqi@0 2876 if (isEnum) argTypes = argTypes.prepend(syms.intType).prepend(syms.stringType);
aoqi@0 2877 tree.args = boxArgs(argTypes, tree.args, tree.varargsElement);
aoqi@0 2878 tree.varargsElement = null;
aoqi@0 2879
aoqi@0 2880 // If created class is local, add free variables after
aoqi@0 2881 // explicit constructor arguments.
aoqi@0 2882 if (c.isLocal()) {
aoqi@0 2883 tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
aoqi@0 2884 }
aoqi@0 2885
aoqi@0 2886 // If an access constructor is used, append null as a last argument.
aoqi@0 2887 Symbol constructor = accessConstructor(tree.pos(), tree.constructor);
aoqi@0 2888 if (constructor != tree.constructor) {
aoqi@0 2889 tree.args = tree.args.append(makeNull());
aoqi@0 2890 tree.constructor = constructor;
aoqi@0 2891 }
aoqi@0 2892
aoqi@0 2893 // If created class has an outer instance, and new is qualified, pass
aoqi@0 2894 // qualifier as first argument. If new is not qualified, pass the
aoqi@0 2895 // correct outer instance as first argument.
aoqi@0 2896 if (c.hasOuterInstance()) {
aoqi@0 2897 JCExpression thisArg;
aoqi@0 2898 if (tree.encl != null) {
aoqi@0 2899 thisArg = attr.makeNullCheck(translate(tree.encl));
aoqi@0 2900 thisArg.type = tree.encl.type;
aoqi@0 2901 } else if (c.isLocal()) {
aoqi@0 2902 // local class
aoqi@0 2903 thisArg = makeThis(tree.pos(), c.type.getEnclosingType().tsym);
aoqi@0 2904 } else {
aoqi@0 2905 // nested class
aoqi@0 2906 thisArg = makeOwnerThis(tree.pos(), c, false);
aoqi@0 2907 }
aoqi@0 2908 tree.args = tree.args.prepend(thisArg);
aoqi@0 2909 }
aoqi@0 2910 tree.encl = null;
aoqi@0 2911
aoqi@0 2912 // If we have an anonymous class, create its flat version, rather
aoqi@0 2913 // than the class or interface following new.
aoqi@0 2914 if (tree.def != null) {
aoqi@0 2915 translate(tree.def);
aoqi@0 2916 tree.clazz = access(make_at(tree.clazz.pos()).Ident(tree.def.sym));
aoqi@0 2917 tree.def = null;
aoqi@0 2918 } else {
aoqi@0 2919 tree.clazz = access(c, tree.clazz, enclOp, false);
aoqi@0 2920 }
aoqi@0 2921 result = tree;
aoqi@0 2922 }
aoqi@0 2923
aoqi@0 2924 // Simplify conditionals with known constant controlling expressions.
aoqi@0 2925 // This allows us to avoid generating supporting declarations for
aoqi@0 2926 // the dead code, which will not be eliminated during code generation.
aoqi@0 2927 // Note that Flow.isFalse and Flow.isTrue only return true
aoqi@0 2928 // for constant expressions in the sense of JLS 15.27, which
aoqi@0 2929 // are guaranteed to have no side-effects. More aggressive
aoqi@0 2930 // constant propagation would require that we take care to
aoqi@0 2931 // preserve possible side-effects in the condition expression.
aoqi@0 2932
aoqi@0 2933 /** Visitor method for conditional expressions.
aoqi@0 2934 */
aoqi@0 2935 @Override
aoqi@0 2936 public void visitConditional(JCConditional tree) {
aoqi@0 2937 JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
aoqi@0 2938 if (cond.type.isTrue()) {
aoqi@0 2939 result = convert(translate(tree.truepart, tree.type), tree.type);
aoqi@0 2940 addPrunedInfo(cond);
aoqi@0 2941 } else if (cond.type.isFalse()) {
aoqi@0 2942 result = convert(translate(tree.falsepart, tree.type), tree.type);
aoqi@0 2943 addPrunedInfo(cond);
aoqi@0 2944 } else {
aoqi@0 2945 // Condition is not a compile-time constant.
aoqi@0 2946 tree.truepart = translate(tree.truepart, tree.type);
aoqi@0 2947 tree.falsepart = translate(tree.falsepart, tree.type);
aoqi@0 2948 result = tree;
aoqi@0 2949 }
aoqi@0 2950 }
aoqi@0 2951 //where
aoqi@0 2952 private JCTree convert(JCTree tree, Type pt) {
aoqi@0 2953 if (tree.type == pt || tree.type.hasTag(BOT))
aoqi@0 2954 return tree;
aoqi@0 2955 JCTree result = make_at(tree.pos()).TypeCast(make.Type(pt), (JCExpression)tree);
aoqi@0 2956 result.type = (tree.type.constValue() != null) ? cfolder.coerce(tree.type, pt)
aoqi@0 2957 : pt;
aoqi@0 2958 return result;
aoqi@0 2959 }
aoqi@0 2960
aoqi@0 2961 /** Visitor method for if statements.
aoqi@0 2962 */
aoqi@0 2963 public void visitIf(JCIf tree) {
aoqi@0 2964 JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
aoqi@0 2965 if (cond.type.isTrue()) {
aoqi@0 2966 result = translate(tree.thenpart);
aoqi@0 2967 addPrunedInfo(cond);
aoqi@0 2968 } else if (cond.type.isFalse()) {
aoqi@0 2969 if (tree.elsepart != null) {
aoqi@0 2970 result = translate(tree.elsepart);
aoqi@0 2971 } else {
aoqi@0 2972 result = make.Skip();
aoqi@0 2973 }
aoqi@0 2974 addPrunedInfo(cond);
aoqi@0 2975 } else {
aoqi@0 2976 // Condition is not a compile-time constant.
aoqi@0 2977 tree.thenpart = translate(tree.thenpart);
aoqi@0 2978 tree.elsepart = translate(tree.elsepart);
aoqi@0 2979 result = tree;
aoqi@0 2980 }
aoqi@0 2981 }
aoqi@0 2982
aoqi@0 2983 /** Visitor method for assert statements. Translate them away.
aoqi@0 2984 */
aoqi@0 2985 public void visitAssert(JCAssert tree) {
aoqi@0 2986 DiagnosticPosition detailPos = (tree.detail == null) ? tree.pos() : tree.detail.pos();
aoqi@0 2987 tree.cond = translate(tree.cond, syms.booleanType);
aoqi@0 2988 if (!tree.cond.type.isTrue()) {
aoqi@0 2989 JCExpression cond = assertFlagTest(tree.pos());
aoqi@0 2990 List<JCExpression> exnArgs = (tree.detail == null) ?
aoqi@0 2991 List.<JCExpression>nil() : List.of(translate(tree.detail));
aoqi@0 2992 if (!tree.cond.type.isFalse()) {
aoqi@0 2993 cond = makeBinary
aoqi@0 2994 (AND,
aoqi@0 2995 cond,
aoqi@0 2996 makeUnary(NOT, tree.cond));
aoqi@0 2997 }
aoqi@0 2998 result =
aoqi@0 2999 make.If(cond,
aoqi@0 3000 make_at(tree).
aoqi@0 3001 Throw(makeNewClass(syms.assertionErrorType, exnArgs)),
aoqi@0 3002 null);
aoqi@0 3003 } else {
aoqi@0 3004 result = make.Skip();
aoqi@0 3005 }
aoqi@0 3006 }
aoqi@0 3007
aoqi@0 3008 public void visitApply(JCMethodInvocation tree) {
aoqi@0 3009 Symbol meth = TreeInfo.symbol(tree.meth);
aoqi@0 3010 List<Type> argtypes = meth.type.getParameterTypes();
aoqi@0 3011 if (allowEnums &&
aoqi@0 3012 meth.name==names.init &&
aoqi@0 3013 meth.owner == syms.enumSym)
aoqi@0 3014 argtypes = argtypes.tail.tail;
aoqi@0 3015 tree.args = boxArgs(argtypes, tree.args, tree.varargsElement);
aoqi@0 3016 tree.varargsElement = null;
aoqi@0 3017 Name methName = TreeInfo.name(tree.meth);
aoqi@0 3018 if (meth.name==names.init) {
aoqi@0 3019 // We are seeing a this(...) or super(...) constructor call.
aoqi@0 3020 // If an access constructor is used, append null as a last argument.
aoqi@0 3021 Symbol constructor = accessConstructor(tree.pos(), meth);
aoqi@0 3022 if (constructor != meth) {
aoqi@0 3023 tree.args = tree.args.append(makeNull());
aoqi@0 3024 TreeInfo.setSymbol(tree.meth, constructor);
aoqi@0 3025 }
aoqi@0 3026
aoqi@0 3027 // If we are calling a constructor of a local class, add
aoqi@0 3028 // free variables after explicit constructor arguments.
aoqi@0 3029 ClassSymbol c = (ClassSymbol)constructor.owner;
aoqi@0 3030 if (c.isLocal()) {
aoqi@0 3031 tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
aoqi@0 3032 }
aoqi@0 3033
aoqi@0 3034 // If we are calling a constructor of an enum class, pass
aoqi@0 3035 // along the name and ordinal arguments
aoqi@0 3036 if ((c.flags_field&ENUM) != 0 || c.getQualifiedName() == names.java_lang_Enum) {
aoqi@0 3037 List<JCVariableDecl> params = currentMethodDef.params;
aoqi@0 3038 if (currentMethodSym.owner.hasOuterInstance())
aoqi@0 3039 params = params.tail; // drop this$n
aoqi@0 3040 tree.args = tree.args
aoqi@0 3041 .prepend(make_at(tree.pos()).Ident(params.tail.head.sym)) // ordinal
aoqi@0 3042 .prepend(make.Ident(params.head.sym)); // name
aoqi@0 3043 }
aoqi@0 3044
aoqi@0 3045 // If we are calling a constructor of a class with an outer
aoqi@0 3046 // instance, and the call
aoqi@0 3047 // is qualified, pass qualifier as first argument in front of
aoqi@0 3048 // the explicit constructor arguments. If the call
aoqi@0 3049 // is not qualified, pass the correct outer instance as
aoqi@0 3050 // first argument.
aoqi@0 3051 if (c.hasOuterInstance()) {
aoqi@0 3052 JCExpression thisArg;
aoqi@0 3053 if (tree.meth.hasTag(SELECT)) {
aoqi@0 3054 thisArg = attr.
aoqi@0 3055 makeNullCheck(translate(((JCFieldAccess) tree.meth).selected));
aoqi@0 3056 tree.meth = make.Ident(constructor);
aoqi@0 3057 ((JCIdent) tree.meth).name = methName;
aoqi@0 3058 } else if (c.isLocal() || methName == names._this){
aoqi@0 3059 // local class or this() call
aoqi@0 3060 thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
aoqi@0 3061 } else {
aoqi@0 3062 // super() call of nested class - never pick 'this'
aoqi@0 3063 thisArg = makeOwnerThisN(tree.meth.pos(), c, false);
aoqi@0 3064 }
aoqi@0 3065 tree.args = tree.args.prepend(thisArg);
aoqi@0 3066 }
aoqi@0 3067 } else {
aoqi@0 3068 // We are seeing a normal method invocation; translate this as usual.
aoqi@0 3069 tree.meth = translate(tree.meth);
aoqi@0 3070
aoqi@0 3071 // If the translated method itself is an Apply tree, we are
aoqi@0 3072 // seeing an access method invocation. In this case, append
aoqi@0 3073 // the method arguments to the arguments of the access method.
aoqi@0 3074 if (tree.meth.hasTag(APPLY)) {
aoqi@0 3075 JCMethodInvocation app = (JCMethodInvocation)tree.meth;
aoqi@0 3076 app.args = tree.args.prependList(app.args);
aoqi@0 3077 result = app;
aoqi@0 3078 return;
aoqi@0 3079 }
aoqi@0 3080 }
aoqi@0 3081 result = tree;
aoqi@0 3082 }
aoqi@0 3083
aoqi@0 3084 List<JCExpression> boxArgs(List<Type> parameters, List<JCExpression> _args, Type varargsElement) {
aoqi@0 3085 List<JCExpression> args = _args;
aoqi@0 3086 if (parameters.isEmpty()) return args;
aoqi@0 3087 boolean anyChanges = false;
aoqi@0 3088 ListBuffer<JCExpression> result = new ListBuffer<JCExpression>();
aoqi@0 3089 while (parameters.tail.nonEmpty()) {
aoqi@0 3090 JCExpression arg = translate(args.head, parameters.head);
aoqi@0 3091 anyChanges |= (arg != args.head);
aoqi@0 3092 result.append(arg);
aoqi@0 3093 args = args.tail;
aoqi@0 3094 parameters = parameters.tail;
aoqi@0 3095 }
aoqi@0 3096 Type parameter = parameters.head;
aoqi@0 3097 if (varargsElement != null) {
aoqi@0 3098 anyChanges = true;
aoqi@0 3099 ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>();
aoqi@0 3100 while (args.nonEmpty()) {
aoqi@0 3101 JCExpression arg = translate(args.head, varargsElement);
aoqi@0 3102 elems.append(arg);
aoqi@0 3103 args = args.tail;
aoqi@0 3104 }
aoqi@0 3105 JCNewArray boxedArgs = make.NewArray(make.Type(varargsElement),
aoqi@0 3106 List.<JCExpression>nil(),
aoqi@0 3107 elems.toList());
aoqi@0 3108 boxedArgs.type = new ArrayType(varargsElement, syms.arrayClass);
aoqi@0 3109 result.append(boxedArgs);
aoqi@0 3110 } else {
aoqi@0 3111 if (args.length() != 1) throw new AssertionError(args);
aoqi@0 3112 JCExpression arg = translate(args.head, parameter);
aoqi@0 3113 anyChanges |= (arg != args.head);
aoqi@0 3114 result.append(arg);
aoqi@0 3115 if (!anyChanges) return _args;
aoqi@0 3116 }
aoqi@0 3117 return result.toList();
aoqi@0 3118 }
aoqi@0 3119
aoqi@0 3120 /** Expand a boxing or unboxing conversion if needed. */
aoqi@0 3121 @SuppressWarnings("unchecked") // XXX unchecked
aoqi@0 3122 <T extends JCTree> T boxIfNeeded(T tree, Type type) {
aoqi@0 3123 boolean havePrimitive = tree.type.isPrimitive();
aoqi@0 3124 if (havePrimitive == type.isPrimitive())
aoqi@0 3125 return tree;
aoqi@0 3126 if (havePrimitive) {
aoqi@0 3127 Type unboxedTarget = types.unboxedType(type);
aoqi@0 3128 if (!unboxedTarget.hasTag(NONE)) {
aoqi@0 3129 if (!types.isSubtype(tree.type, unboxedTarget)) //e.g. Character c = 89;
aoqi@0 3130 tree.type = unboxedTarget.constType(tree.type.constValue());
aoqi@0 3131 return (T)boxPrimitive((JCExpression)tree, type);
aoqi@0 3132 } else {
aoqi@0 3133 tree = (T)boxPrimitive((JCExpression)tree);
aoqi@0 3134 }
aoqi@0 3135 } else {
aoqi@0 3136 tree = (T)unbox((JCExpression)tree, type);
aoqi@0 3137 }
aoqi@0 3138 return tree;
aoqi@0 3139 }
aoqi@0 3140
aoqi@0 3141 /** Box up a single primitive expression. */
aoqi@0 3142 JCExpression boxPrimitive(JCExpression tree) {
aoqi@0 3143 return boxPrimitive(tree, types.boxedClass(tree.type).type);
aoqi@0 3144 }
aoqi@0 3145
aoqi@0 3146 /** Box up a single primitive expression. */
aoqi@0 3147 JCExpression boxPrimitive(JCExpression tree, Type box) {
aoqi@0 3148 make_at(tree.pos());
aoqi@0 3149 if (target.boxWithConstructors()) {
aoqi@0 3150 Symbol ctor = lookupConstructor(tree.pos(),
aoqi@0 3151 box,
aoqi@0 3152 List.<Type>nil()
aoqi@0 3153 .prepend(tree.type));
aoqi@0 3154 return make.Create(ctor, List.of(tree));
aoqi@0 3155 } else {
aoqi@0 3156 Symbol valueOfSym = lookupMethod(tree.pos(),
aoqi@0 3157 names.valueOf,
aoqi@0 3158 box,
aoqi@0 3159 List.<Type>nil()
aoqi@0 3160 .prepend(tree.type));
aoqi@0 3161 return make.App(make.QualIdent(valueOfSym), List.of(tree));
aoqi@0 3162 }
aoqi@0 3163 }
aoqi@0 3164
aoqi@0 3165 /** Unbox an object to a primitive value. */
aoqi@0 3166 JCExpression unbox(JCExpression tree, Type primitive) {
aoqi@0 3167 Type unboxedType = types.unboxedType(tree.type);
aoqi@0 3168 if (unboxedType.hasTag(NONE)) {
aoqi@0 3169 unboxedType = primitive;
aoqi@0 3170 if (!unboxedType.isPrimitive())
aoqi@0 3171 throw new AssertionError(unboxedType);
aoqi@0 3172 make_at(tree.pos());
aoqi@0 3173 tree = make.TypeCast(types.boxedClass(unboxedType).type, tree);
aoqi@0 3174 } else {
aoqi@0 3175 // There must be a conversion from unboxedType to primitive.
aoqi@0 3176 if (!types.isSubtype(unboxedType, primitive))
aoqi@0 3177 throw new AssertionError(tree);
aoqi@0 3178 }
aoqi@0 3179 make_at(tree.pos());
aoqi@0 3180 Symbol valueSym = lookupMethod(tree.pos(),
aoqi@0 3181 unboxedType.tsym.name.append(names.Value), // x.intValue()
aoqi@0 3182 tree.type,
aoqi@0 3183 List.<Type>nil());
aoqi@0 3184 return make.App(make.Select(tree, valueSym));
aoqi@0 3185 }
aoqi@0 3186
aoqi@0 3187 /** Visitor method for parenthesized expressions.
aoqi@0 3188 * If the subexpression has changed, omit the parens.
aoqi@0 3189 */
aoqi@0 3190 public void visitParens(JCParens tree) {
aoqi@0 3191 JCTree expr = translate(tree.expr);
aoqi@0 3192 result = ((expr == tree.expr) ? tree : expr);
aoqi@0 3193 }
aoqi@0 3194
aoqi@0 3195 public void visitIndexed(JCArrayAccess tree) {
aoqi@0 3196 tree.indexed = translate(tree.indexed);
aoqi@0 3197 tree.index = translate(tree.index, syms.intType);
aoqi@0 3198 result = tree;
aoqi@0 3199 }
aoqi@0 3200
aoqi@0 3201 public void visitAssign(JCAssign tree) {
aoqi@0 3202 tree.lhs = translate(tree.lhs, tree);
aoqi@0 3203 tree.rhs = translate(tree.rhs, tree.lhs.type);
aoqi@0 3204
aoqi@0 3205 // If translated left hand side is an Apply, we are
aoqi@0 3206 // seeing an access method invocation. In this case, append
aoqi@0 3207 // right hand side as last argument of the access method.
aoqi@0 3208 if (tree.lhs.hasTag(APPLY)) {
aoqi@0 3209 JCMethodInvocation app = (JCMethodInvocation)tree.lhs;
aoqi@0 3210 app.args = List.of(tree.rhs).prependList(app.args);
aoqi@0 3211 result = app;
aoqi@0 3212 } else {
aoqi@0 3213 result = tree;
aoqi@0 3214 }
aoqi@0 3215 }
aoqi@0 3216
aoqi@0 3217 public void visitAssignop(final JCAssignOp tree) {
aoqi@0 3218 JCTree lhsAccess = access(TreeInfo.skipParens(tree.lhs));
aoqi@0 3219 final boolean boxingReq = !tree.lhs.type.isPrimitive() &&
aoqi@0 3220 tree.operator.type.getReturnType().isPrimitive();
aoqi@0 3221
aoqi@0 3222 if (boxingReq || lhsAccess.hasTag(APPLY)) {
aoqi@0 3223 // boxing required; need to rewrite as x = (unbox typeof x)(x op y);
aoqi@0 3224 // or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y)
aoqi@0 3225 // (but without recomputing x)
aoqi@0 3226 JCTree newTree = abstractLval(tree.lhs, new TreeBuilder() {
aoqi@0 3227 public JCTree build(final JCTree lhs) {
aoqi@0 3228 JCTree.Tag newTag = tree.getTag().noAssignOp();
aoqi@0 3229 // Erasure (TransTypes) can change the type of
aoqi@0 3230 // tree.lhs. However, we can still get the
aoqi@0 3231 // unerased type of tree.lhs as it is stored
aoqi@0 3232 // in tree.type in Attr.
aoqi@0 3233 Symbol newOperator = rs.resolveBinaryOperator(tree.pos(),
aoqi@0 3234 newTag,
aoqi@0 3235 attrEnv,
aoqi@0 3236 tree.type,
aoqi@0 3237 tree.rhs.type);
aoqi@0 3238 JCExpression expr = (JCExpression)lhs;
aoqi@0 3239 if (expr.type != tree.type)
aoqi@0 3240 expr = make.TypeCast(tree.type, expr);
aoqi@0 3241 JCBinary opResult = make.Binary(newTag, expr, tree.rhs);
aoqi@0 3242 opResult.operator = newOperator;
aoqi@0 3243 opResult.type = newOperator.type.getReturnType();
aoqi@0 3244 JCExpression newRhs = boxingReq ?
aoqi@0 3245 make.TypeCast(types.unboxedType(tree.type), opResult) :
aoqi@0 3246 opResult;
aoqi@0 3247 return make.Assign((JCExpression)lhs, newRhs).setType(tree.type);
aoqi@0 3248 }
aoqi@0 3249 });
aoqi@0 3250 result = translate(newTree);
aoqi@0 3251 return;
aoqi@0 3252 }
aoqi@0 3253 tree.lhs = translate(tree.lhs, tree);
aoqi@0 3254 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
aoqi@0 3255
aoqi@0 3256 // If translated left hand side is an Apply, we are
aoqi@0 3257 // seeing an access method invocation. In this case, append
aoqi@0 3258 // right hand side as last argument of the access method.
aoqi@0 3259 if (tree.lhs.hasTag(APPLY)) {
aoqi@0 3260 JCMethodInvocation app = (JCMethodInvocation)tree.lhs;
aoqi@0 3261 // if operation is a += on strings,
aoqi@0 3262 // make sure to convert argument to string
aoqi@0 3263 JCExpression rhs = (((OperatorSymbol)tree.operator).opcode == string_add)
aoqi@0 3264 ? makeString(tree.rhs)
aoqi@0 3265 : tree.rhs;
aoqi@0 3266 app.args = List.of(rhs).prependList(app.args);
aoqi@0 3267 result = app;
aoqi@0 3268 } else {
aoqi@0 3269 result = tree;
aoqi@0 3270 }
aoqi@0 3271 }
aoqi@0 3272
aoqi@0 3273 /** Lower a tree of the form e++ or e-- where e is an object type */
aoqi@0 3274 JCTree lowerBoxedPostop(final JCUnary tree) {
aoqi@0 3275 // translate to tmp1=lval(e); tmp2=tmp1; tmp1 OP 1; tmp2
aoqi@0 3276 // or
aoqi@0 3277 // translate to tmp1=lval(e); tmp2=tmp1; (typeof tree)tmp1 OP 1; tmp2
aoqi@0 3278 // where OP is += or -=
aoqi@0 3279 final boolean cast = TreeInfo.skipParens(tree.arg).hasTag(TYPECAST);
aoqi@0 3280 return abstractLval(tree.arg, new TreeBuilder() {
aoqi@0 3281 public JCTree build(final JCTree tmp1) {
aoqi@0 3282 return abstractRval(tmp1, tree.arg.type, new TreeBuilder() {
aoqi@0 3283 public JCTree build(final JCTree tmp2) {
aoqi@0 3284 JCTree.Tag opcode = (tree.hasTag(POSTINC))
aoqi@0 3285 ? PLUS_ASG : MINUS_ASG;
aoqi@0 3286 JCTree lhs = cast
aoqi@0 3287 ? make.TypeCast(tree.arg.type, (JCExpression)tmp1)
aoqi@0 3288 : tmp1;
aoqi@0 3289 JCTree update = makeAssignop(opcode,
aoqi@0 3290 lhs,
aoqi@0 3291 make.Literal(1));
aoqi@0 3292 return makeComma(update, tmp2);
aoqi@0 3293 }
aoqi@0 3294 });
aoqi@0 3295 }
aoqi@0 3296 });
aoqi@0 3297 }
aoqi@0 3298
aoqi@0 3299 public void visitUnary(JCUnary tree) {
aoqi@0 3300 boolean isUpdateOperator = tree.getTag().isIncOrDecUnaryOp();
aoqi@0 3301 if (isUpdateOperator && !tree.arg.type.isPrimitive()) {
aoqi@0 3302 switch(tree.getTag()) {
aoqi@0 3303 case PREINC: // ++ e
aoqi@0 3304 // translate to e += 1
aoqi@0 3305 case PREDEC: // -- e
aoqi@0 3306 // translate to e -= 1
aoqi@0 3307 {
aoqi@0 3308 JCTree.Tag opcode = (tree.hasTag(PREINC))
aoqi@0 3309 ? PLUS_ASG : MINUS_ASG;
aoqi@0 3310 JCAssignOp newTree = makeAssignop(opcode,
aoqi@0 3311 tree.arg,
aoqi@0 3312 make.Literal(1));
aoqi@0 3313 result = translate(newTree, tree.type);
aoqi@0 3314 return;
aoqi@0 3315 }
aoqi@0 3316 case POSTINC: // e ++
aoqi@0 3317 case POSTDEC: // e --
aoqi@0 3318 {
aoqi@0 3319 result = translate(lowerBoxedPostop(tree), tree.type);
aoqi@0 3320 return;
aoqi@0 3321 }
aoqi@0 3322 }
aoqi@0 3323 throw new AssertionError(tree);
aoqi@0 3324 }
aoqi@0 3325
aoqi@0 3326 tree.arg = boxIfNeeded(translate(tree.arg, tree), tree.type);
aoqi@0 3327
aoqi@0 3328 if (tree.hasTag(NOT) && tree.arg.type.constValue() != null) {
aoqi@0 3329 tree.type = cfolder.fold1(bool_not, tree.arg.type);
aoqi@0 3330 }
aoqi@0 3331
aoqi@0 3332 // If translated left hand side is an Apply, we are
aoqi@0 3333 // seeing an access method invocation. In this case, return
aoqi@0 3334 // that access method invocation as result.
aoqi@0 3335 if (isUpdateOperator && tree.arg.hasTag(APPLY)) {
aoqi@0 3336 result = tree.arg;
aoqi@0 3337 } else {
aoqi@0 3338 result = tree;
aoqi@0 3339 }
aoqi@0 3340 }
aoqi@0 3341
aoqi@0 3342 public void visitBinary(JCBinary tree) {
aoqi@0 3343 List<Type> formals = tree.operator.type.getParameterTypes();
aoqi@0 3344 JCTree lhs = tree.lhs = translate(tree.lhs, formals.head);
aoqi@0 3345 switch (tree.getTag()) {
aoqi@0 3346 case OR:
aoqi@0 3347 if (lhs.type.isTrue()) {
aoqi@0 3348 result = lhs;
aoqi@0 3349 return;
aoqi@0 3350 }
aoqi@0 3351 if (lhs.type.isFalse()) {
aoqi@0 3352 result = translate(tree.rhs, formals.tail.head);
aoqi@0 3353 return;
aoqi@0 3354 }
aoqi@0 3355 break;
aoqi@0 3356 case AND:
aoqi@0 3357 if (lhs.type.isFalse()) {
aoqi@0 3358 result = lhs;
aoqi@0 3359 return;
aoqi@0 3360 }
aoqi@0 3361 if (lhs.type.isTrue()) {
aoqi@0 3362 result = translate(tree.rhs, formals.tail.head);
aoqi@0 3363 return;
aoqi@0 3364 }
aoqi@0 3365 break;
aoqi@0 3366 }
aoqi@0 3367 tree.rhs = translate(tree.rhs, formals.tail.head);
aoqi@0 3368 result = tree;
aoqi@0 3369 }
aoqi@0 3370
aoqi@0 3371 public void visitIdent(JCIdent tree) {
aoqi@0 3372 result = access(tree.sym, tree, enclOp, false);
aoqi@0 3373 }
aoqi@0 3374
aoqi@0 3375 /** Translate away the foreach loop. */
aoqi@0 3376 public void visitForeachLoop(JCEnhancedForLoop tree) {
aoqi@0 3377 if (types.elemtype(tree.expr.type) == null)
aoqi@0 3378 visitIterableForeachLoop(tree);
aoqi@0 3379 else
aoqi@0 3380 visitArrayForeachLoop(tree);
aoqi@0 3381 }
aoqi@0 3382 // where
aoqi@0 3383 /**
aoqi@0 3384 * A statement of the form
aoqi@0 3385 *
aoqi@0 3386 * <pre>
aoqi@0 3387 * for ( T v : arrayexpr ) stmt;
aoqi@0 3388 * </pre>
aoqi@0 3389 *
aoqi@0 3390 * (where arrayexpr is of an array type) gets translated to
aoqi@0 3391 *
aoqi@0 3392 * <pre>{@code
aoqi@0 3393 * for ( { arraytype #arr = arrayexpr;
aoqi@0 3394 * int #len = array.length;
aoqi@0 3395 * int #i = 0; };
aoqi@0 3396 * #i < #len; i$++ ) {
aoqi@0 3397 * T v = arr$[#i];
aoqi@0 3398 * stmt;
aoqi@0 3399 * }
aoqi@0 3400 * }</pre>
aoqi@0 3401 *
aoqi@0 3402 * where #arr, #len, and #i are freshly named synthetic local variables.
aoqi@0 3403 */
aoqi@0 3404 private void visitArrayForeachLoop(JCEnhancedForLoop tree) {
aoqi@0 3405 make_at(tree.expr.pos());
aoqi@0 3406 VarSymbol arraycache = new VarSymbol(SYNTHETIC,
aoqi@0 3407 names.fromString("arr" + target.syntheticNameChar()),
aoqi@0 3408 tree.expr.type,
aoqi@0 3409 currentMethodSym);
aoqi@0 3410 JCStatement arraycachedef = make.VarDef(arraycache, tree.expr);
aoqi@0 3411 VarSymbol lencache = new VarSymbol(SYNTHETIC,
aoqi@0 3412 names.fromString("len" + target.syntheticNameChar()),
aoqi@0 3413 syms.intType,
aoqi@0 3414 currentMethodSym);
aoqi@0 3415 JCStatement lencachedef = make.
aoqi@0 3416 VarDef(lencache, make.Select(make.Ident(arraycache), syms.lengthVar));
aoqi@0 3417 VarSymbol index = new VarSymbol(SYNTHETIC,
aoqi@0 3418 names.fromString("i" + target.syntheticNameChar()),
aoqi@0 3419 syms.intType,
aoqi@0 3420 currentMethodSym);
aoqi@0 3421
aoqi@0 3422 JCVariableDecl indexdef = make.VarDef(index, make.Literal(INT, 0));
aoqi@0 3423 indexdef.init.type = indexdef.type = syms.intType.constType(0);
aoqi@0 3424
aoqi@0 3425 List<JCStatement> loopinit = List.of(arraycachedef, lencachedef, indexdef);
aoqi@0 3426 JCBinary cond = makeBinary(LT, make.Ident(index), make.Ident(lencache));
aoqi@0 3427
aoqi@0 3428 JCExpressionStatement step = make.Exec(makeUnary(PREINC, make.Ident(index)));
aoqi@0 3429
aoqi@0 3430 Type elemtype = types.elemtype(tree.expr.type);
aoqi@0 3431 JCExpression loopvarinit = make.Indexed(make.Ident(arraycache),
aoqi@0 3432 make.Ident(index)).setType(elemtype);
aoqi@0 3433 JCVariableDecl loopvardef = (JCVariableDecl)make.VarDef(tree.var.mods,
aoqi@0 3434 tree.var.name,
aoqi@0 3435 tree.var.vartype,
aoqi@0 3436 loopvarinit).setType(tree.var.type);
aoqi@0 3437 loopvardef.sym = tree.var.sym;
aoqi@0 3438 JCBlock body = make.
aoqi@0 3439 Block(0, List.of(loopvardef, tree.body));
aoqi@0 3440
aoqi@0 3441 result = translate(make.
aoqi@0 3442 ForLoop(loopinit,
aoqi@0 3443 cond,
aoqi@0 3444 List.of(step),
aoqi@0 3445 body));
aoqi@0 3446 patchTargets(body, tree, result);
aoqi@0 3447 }
aoqi@0 3448 /** Patch up break and continue targets. */
aoqi@0 3449 private void patchTargets(JCTree body, final JCTree src, final JCTree dest) {
aoqi@0 3450 class Patcher extends TreeScanner {
aoqi@0 3451 public void visitBreak(JCBreak tree) {
aoqi@0 3452 if (tree.target == src)
aoqi@0 3453 tree.target = dest;
aoqi@0 3454 }
aoqi@0 3455 public void visitContinue(JCContinue tree) {
aoqi@0 3456 if (tree.target == src)
aoqi@0 3457 tree.target = dest;
aoqi@0 3458 }
aoqi@0 3459 public void visitClassDef(JCClassDecl tree) {}
aoqi@0 3460 }
aoqi@0 3461 new Patcher().scan(body);
aoqi@0 3462 }
aoqi@0 3463 /**
aoqi@0 3464 * A statement of the form
aoqi@0 3465 *
aoqi@0 3466 * <pre>
aoqi@0 3467 * for ( T v : coll ) stmt ;
aoqi@0 3468 * </pre>
aoqi@0 3469 *
aoqi@0 3470 * (where coll implements {@code Iterable<? extends T>}) gets translated to
aoqi@0 3471 *
aoqi@0 3472 * <pre>{@code
aoqi@0 3473 * for ( Iterator<? extends T> #i = coll.iterator(); #i.hasNext(); ) {
aoqi@0 3474 * T v = (T) #i.next();
aoqi@0 3475 * stmt;
aoqi@0 3476 * }
aoqi@0 3477 * }</pre>
aoqi@0 3478 *
aoqi@0 3479 * where #i is a freshly named synthetic local variable.
aoqi@0 3480 */
aoqi@0 3481 private void visitIterableForeachLoop(JCEnhancedForLoop tree) {
aoqi@0 3482 make_at(tree.expr.pos());
aoqi@0 3483 Type iteratorTarget = syms.objectType;
aoqi@0 3484 Type iterableType = types.asSuper(types.cvarUpperBound(tree.expr.type),
aoqi@0 3485 syms.iterableType.tsym);
aoqi@0 3486 if (iterableType.getTypeArguments().nonEmpty())
aoqi@0 3487 iteratorTarget = types.erasure(iterableType.getTypeArguments().head);
aoqi@0 3488 Type eType = tree.expr.type;
aoqi@0 3489 while (eType.hasTag(TYPEVAR)) {
aoqi@0 3490 eType = eType.getUpperBound();
aoqi@0 3491 }
aoqi@0 3492 tree.expr.type = types.erasure(eType);
aoqi@0 3493 if (eType.isCompound())
aoqi@0 3494 tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr);
aoqi@0 3495 Symbol iterator = lookupMethod(tree.expr.pos(),
aoqi@0 3496 names.iterator,
aoqi@0 3497 eType,
aoqi@0 3498 List.<Type>nil());
aoqi@0 3499 VarSymbol itvar = new VarSymbol(SYNTHETIC, names.fromString("i" + target.syntheticNameChar()),
aoqi@0 3500 types.erasure(types.asSuper(iterator.type.getReturnType(), syms.iteratorType.tsym)),
aoqi@0 3501 currentMethodSym);
aoqi@0 3502
aoqi@0 3503 JCStatement init = make.
aoqi@0 3504 VarDef(itvar, make.App(make.Select(tree.expr, iterator)
aoqi@0 3505 .setType(types.erasure(iterator.type))));
aoqi@0 3506
aoqi@0 3507 Symbol hasNext = lookupMethod(tree.expr.pos(),
aoqi@0 3508 names.hasNext,
aoqi@0 3509 itvar.type,
aoqi@0 3510 List.<Type>nil());
aoqi@0 3511 JCMethodInvocation cond = make.App(make.Select(make.Ident(itvar), hasNext));
aoqi@0 3512 Symbol next = lookupMethod(tree.expr.pos(),
aoqi@0 3513 names.next,
aoqi@0 3514 itvar.type,
aoqi@0 3515 List.<Type>nil());
aoqi@0 3516 JCExpression vardefinit = make.App(make.Select(make.Ident(itvar), next));
aoqi@0 3517 if (tree.var.type.isPrimitive())
aoqi@0 3518 vardefinit = make.TypeCast(types.cvarUpperBound(iteratorTarget), vardefinit);
aoqi@0 3519 else
aoqi@0 3520 vardefinit = make.TypeCast(tree.var.type, vardefinit);
aoqi@0 3521 JCVariableDecl indexDef = (JCVariableDecl)make.VarDef(tree.var.mods,
aoqi@0 3522 tree.var.name,
aoqi@0 3523 tree.var.vartype,
aoqi@0 3524 vardefinit).setType(tree.var.type);
aoqi@0 3525 indexDef.sym = tree.var.sym;
aoqi@0 3526 JCBlock body = make.Block(0, List.of(indexDef, tree.body));
aoqi@0 3527 body.endpos = TreeInfo.endPos(tree.body);
aoqi@0 3528 result = translate(make.
aoqi@0 3529 ForLoop(List.of(init),
aoqi@0 3530 cond,
aoqi@0 3531 List.<JCExpressionStatement>nil(),
aoqi@0 3532 body));
aoqi@0 3533 patchTargets(body, tree, result);
aoqi@0 3534 }
aoqi@0 3535
aoqi@0 3536 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 3537 MethodSymbol oldMethodSym = currentMethodSym;
aoqi@0 3538 tree.mods = translate(tree.mods);
aoqi@0 3539 tree.vartype = translate(tree.vartype);
aoqi@0 3540 if (currentMethodSym == null) {
aoqi@0 3541 // A class or instance field initializer.
aoqi@0 3542 currentMethodSym =
aoqi@0 3543 new MethodSymbol((tree.mods.flags&STATIC) | BLOCK,
aoqi@0 3544 names.empty, null,
aoqi@0 3545 currentClass);
aoqi@0 3546 }
aoqi@0 3547 if (tree.init != null) tree.init = translate(tree.init, tree.type);
aoqi@0 3548 result = tree;
aoqi@0 3549 currentMethodSym = oldMethodSym;
aoqi@0 3550 }
aoqi@0 3551
aoqi@0 3552 public void visitBlock(JCBlock tree) {
aoqi@0 3553 MethodSymbol oldMethodSym = currentMethodSym;
aoqi@0 3554 if (currentMethodSym == null) {
aoqi@0 3555 // Block is a static or instance initializer.
aoqi@0 3556 currentMethodSym =
aoqi@0 3557 new MethodSymbol(tree.flags | BLOCK,
aoqi@0 3558 names.empty, null,
aoqi@0 3559 currentClass);
aoqi@0 3560 }
aoqi@0 3561 super.visitBlock(tree);
aoqi@0 3562 currentMethodSym = oldMethodSym;
aoqi@0 3563 }
aoqi@0 3564
aoqi@0 3565 public void visitDoLoop(JCDoWhileLoop tree) {
aoqi@0 3566 tree.body = translate(tree.body);
aoqi@0 3567 tree.cond = translate(tree.cond, syms.booleanType);
aoqi@0 3568 result = tree;
aoqi@0 3569 }
aoqi@0 3570
aoqi@0 3571 public void visitWhileLoop(JCWhileLoop tree) {
aoqi@0 3572 tree.cond = translate(tree.cond, syms.booleanType);
aoqi@0 3573 tree.body = translate(tree.body);
aoqi@0 3574 result = tree;
aoqi@0 3575 }
aoqi@0 3576
aoqi@0 3577 public void visitForLoop(JCForLoop tree) {
aoqi@0 3578 tree.init = translate(tree.init);
aoqi@0 3579 if (tree.cond != null)
aoqi@0 3580 tree.cond = translate(tree.cond, syms.booleanType);
aoqi@0 3581 tree.step = translate(tree.step);
aoqi@0 3582 tree.body = translate(tree.body);
aoqi@0 3583 result = tree;
aoqi@0 3584 }
aoqi@0 3585
aoqi@0 3586 public void visitReturn(JCReturn tree) {
aoqi@0 3587 if (tree.expr != null)
aoqi@0 3588 tree.expr = translate(tree.expr,
aoqi@0 3589 types.erasure(currentMethodDef
aoqi@0 3590 .restype.type));
aoqi@0 3591 result = tree;
aoqi@0 3592 }
aoqi@0 3593
aoqi@0 3594 public void visitSwitch(JCSwitch tree) {
aoqi@0 3595 Type selsuper = types.supertype(tree.selector.type);
aoqi@0 3596 boolean enumSwitch = selsuper != null &&
aoqi@0 3597 (tree.selector.type.tsym.flags() & ENUM) != 0;
aoqi@0 3598 boolean stringSwitch = selsuper != null &&
aoqi@0 3599 types.isSameType(tree.selector.type, syms.stringType);
aoqi@0 3600 Type target = enumSwitch ? tree.selector.type :
aoqi@0 3601 (stringSwitch? syms.stringType : syms.intType);
aoqi@0 3602 tree.selector = translate(tree.selector, target);
aoqi@0 3603 tree.cases = translateCases(tree.cases);
aoqi@0 3604 if (enumSwitch) {
aoqi@0 3605 result = visitEnumSwitch(tree);
aoqi@0 3606 } else if (stringSwitch) {
aoqi@0 3607 result = visitStringSwitch(tree);
aoqi@0 3608 } else {
aoqi@0 3609 result = tree;
aoqi@0 3610 }
aoqi@0 3611 }
aoqi@0 3612
aoqi@0 3613 public JCTree visitEnumSwitch(JCSwitch tree) {
aoqi@0 3614 TypeSymbol enumSym = tree.selector.type.tsym;
aoqi@0 3615 EnumMapping map = mapForEnum(tree.pos(), enumSym);
aoqi@0 3616 make_at(tree.pos());
aoqi@0 3617 Symbol ordinalMethod = lookupMethod(tree.pos(),
aoqi@0 3618 names.ordinal,
aoqi@0 3619 tree.selector.type,
aoqi@0 3620 List.<Type>nil());
aoqi@0 3621 JCArrayAccess selector = make.Indexed(map.mapVar,
aoqi@0 3622 make.App(make.Select(tree.selector,
aoqi@0 3623 ordinalMethod)));
aoqi@0 3624 ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
aoqi@0 3625 for (JCCase c : tree.cases) {
aoqi@0 3626 if (c.pat != null) {
aoqi@0 3627 VarSymbol label = (VarSymbol)TreeInfo.symbol(c.pat);
aoqi@0 3628 JCLiteral pat = map.forConstant(label);
aoqi@0 3629 cases.append(make.Case(pat, c.stats));
aoqi@0 3630 } else {
aoqi@0 3631 cases.append(c);
aoqi@0 3632 }
aoqi@0 3633 }
aoqi@0 3634 JCSwitch enumSwitch = make.Switch(selector, cases.toList());
aoqi@0 3635 patchTargets(enumSwitch, tree, enumSwitch);
aoqi@0 3636 return enumSwitch;
aoqi@0 3637 }
aoqi@0 3638
aoqi@0 3639 public JCTree visitStringSwitch(JCSwitch tree) {
aoqi@0 3640 List<JCCase> caseList = tree.getCases();
aoqi@0 3641 int alternatives = caseList.size();
aoqi@0 3642
aoqi@0 3643 if (alternatives == 0) { // Strange but legal possibility
aoqi@0 3644 return make.at(tree.pos()).Exec(attr.makeNullCheck(tree.getExpression()));
aoqi@0 3645 } else {
aoqi@0 3646 /*
aoqi@0 3647 * The general approach used is to translate a single
aoqi@0 3648 * string switch statement into a series of two chained
aoqi@0 3649 * switch statements: the first a synthesized statement
aoqi@0 3650 * switching on the argument string's hash value and
aoqi@0 3651 * computing a string's position in the list of original
aoqi@0 3652 * case labels, if any, followed by a second switch on the
aoqi@0 3653 * computed integer value. The second switch has the same
aoqi@0 3654 * code structure as the original string switch statement
aoqi@0 3655 * except that the string case labels are replaced with
aoqi@0 3656 * positional integer constants starting at 0.
aoqi@0 3657 *
aoqi@0 3658 * The first switch statement can be thought of as an
aoqi@0 3659 * inlined map from strings to their position in the case
aoqi@0 3660 * label list. An alternate implementation would use an
aoqi@0 3661 * actual Map for this purpose, as done for enum switches.
aoqi@0 3662 *
aoqi@0 3663 * With some additional effort, it would be possible to
aoqi@0 3664 * use a single switch statement on the hash code of the
aoqi@0 3665 * argument, but care would need to be taken to preserve
aoqi@0 3666 * the proper control flow in the presence of hash
aoqi@0 3667 * collisions and other complications, such as
aoqi@0 3668 * fallthroughs. Switch statements with one or two
aoqi@0 3669 * alternatives could also be specially translated into
aoqi@0 3670 * if-then statements to omit the computation of the hash
aoqi@0 3671 * code.
aoqi@0 3672 *
aoqi@0 3673 * The generated code assumes that the hashing algorithm
aoqi@0 3674 * of String is the same in the compilation environment as
aoqi@0 3675 * in the environment the code will run in. The string
aoqi@0 3676 * hashing algorithm in the SE JDK has been unchanged
aoqi@0 3677 * since at least JDK 1.2. Since the algorithm has been
aoqi@0 3678 * specified since that release as well, it is very
aoqi@0 3679 * unlikely to be changed in the future.
aoqi@0 3680 *
aoqi@0 3681 * Different hashing algorithms, such as the length of the
aoqi@0 3682 * strings or a perfect hashing algorithm over the
aoqi@0 3683 * particular set of case labels, could potentially be
aoqi@0 3684 * used instead of String.hashCode.
aoqi@0 3685 */
aoqi@0 3686
aoqi@0 3687 ListBuffer<JCStatement> stmtList = new ListBuffer<JCStatement>();
aoqi@0 3688
aoqi@0 3689 // Map from String case labels to their original position in
aoqi@0 3690 // the list of case labels.
aoqi@0 3691 Map<String, Integer> caseLabelToPosition =
aoqi@0 3692 new LinkedHashMap<String, Integer>(alternatives + 1, 1.0f);
aoqi@0 3693
aoqi@0 3694 // Map of hash codes to the string case labels having that hashCode.
aoqi@0 3695 Map<Integer, Set<String>> hashToString =
aoqi@0 3696 new LinkedHashMap<Integer, Set<String>>(alternatives + 1, 1.0f);
aoqi@0 3697
aoqi@0 3698 int casePosition = 0;
aoqi@0 3699 for(JCCase oneCase : caseList) {
aoqi@0 3700 JCExpression expression = oneCase.getExpression();
aoqi@0 3701
aoqi@0 3702 if (expression != null) { // expression for a "default" case is null
aoqi@0 3703 String labelExpr = (String) expression.type.constValue();
aoqi@0 3704 Integer mapping = caseLabelToPosition.put(labelExpr, casePosition);
aoqi@0 3705 Assert.checkNull(mapping);
aoqi@0 3706 int hashCode = labelExpr.hashCode();
aoqi@0 3707
aoqi@0 3708 Set<String> stringSet = hashToString.get(hashCode);
aoqi@0 3709 if (stringSet == null) {
aoqi@0 3710 stringSet = new LinkedHashSet<String>(1, 1.0f);
aoqi@0 3711 stringSet.add(labelExpr);
aoqi@0 3712 hashToString.put(hashCode, stringSet);
aoqi@0 3713 } else {
aoqi@0 3714 boolean added = stringSet.add(labelExpr);
aoqi@0 3715 Assert.check(added);
aoqi@0 3716 }
aoqi@0 3717 }
aoqi@0 3718 casePosition++;
aoqi@0 3719 }
aoqi@0 3720
aoqi@0 3721 // Synthesize a switch statement that has the effect of
aoqi@0 3722 // mapping from a string to the integer position of that
aoqi@0 3723 // string in the list of case labels. This is done by
aoqi@0 3724 // switching on the hashCode of the string followed by an
aoqi@0 3725 // if-then-else chain comparing the input for equality
aoqi@0 3726 // with all the case labels having that hash value.
aoqi@0 3727
aoqi@0 3728 /*
aoqi@0 3729 * s$ = top of stack;
aoqi@0 3730 * tmp$ = -1;
aoqi@0 3731 * switch($s.hashCode()) {
aoqi@0 3732 * case caseLabel.hashCode:
aoqi@0 3733 * if (s$.equals("caseLabel_1")
aoqi@0 3734 * tmp$ = caseLabelToPosition("caseLabel_1");
aoqi@0 3735 * else if (s$.equals("caseLabel_2"))
aoqi@0 3736 * tmp$ = caseLabelToPosition("caseLabel_2");
aoqi@0 3737 * ...
aoqi@0 3738 * break;
aoqi@0 3739 * ...
aoqi@0 3740 * }
aoqi@0 3741 */
aoqi@0 3742
aoqi@0 3743 VarSymbol dollar_s = new VarSymbol(FINAL|SYNTHETIC,
aoqi@0 3744 names.fromString("s" + tree.pos + target.syntheticNameChar()),
aoqi@0 3745 syms.stringType,
aoqi@0 3746 currentMethodSym);
aoqi@0 3747 stmtList.append(make.at(tree.pos()).VarDef(dollar_s, tree.getExpression()).setType(dollar_s.type));
aoqi@0 3748
aoqi@0 3749 VarSymbol dollar_tmp = new VarSymbol(SYNTHETIC,
aoqi@0 3750 names.fromString("tmp" + tree.pos + target.syntheticNameChar()),
aoqi@0 3751 syms.intType,
aoqi@0 3752 currentMethodSym);
aoqi@0 3753 JCVariableDecl dollar_tmp_def =
aoqi@0 3754 (JCVariableDecl)make.VarDef(dollar_tmp, make.Literal(INT, -1)).setType(dollar_tmp.type);
aoqi@0 3755 dollar_tmp_def.init.type = dollar_tmp.type = syms.intType;
aoqi@0 3756 stmtList.append(dollar_tmp_def);
aoqi@0 3757 ListBuffer<JCCase> caseBuffer = new ListBuffer<>();
aoqi@0 3758 // hashCode will trigger nullcheck on original switch expression
aoqi@0 3759 JCMethodInvocation hashCodeCall = makeCall(make.Ident(dollar_s),
aoqi@0 3760 names.hashCode,
aoqi@0 3761 List.<JCExpression>nil()).setType(syms.intType);
aoqi@0 3762 JCSwitch switch1 = make.Switch(hashCodeCall,
aoqi@0 3763 caseBuffer.toList());
aoqi@0 3764 for(Map.Entry<Integer, Set<String>> entry : hashToString.entrySet()) {
aoqi@0 3765 int hashCode = entry.getKey();
aoqi@0 3766 Set<String> stringsWithHashCode = entry.getValue();
aoqi@0 3767 Assert.check(stringsWithHashCode.size() >= 1);
aoqi@0 3768
aoqi@0 3769 JCStatement elsepart = null;
aoqi@0 3770 for(String caseLabel : stringsWithHashCode ) {
aoqi@0 3771 JCMethodInvocation stringEqualsCall = makeCall(make.Ident(dollar_s),
aoqi@0 3772 names.equals,
aoqi@0 3773 List.<JCExpression>of(make.Literal(caseLabel)));
aoqi@0 3774 elsepart = make.If(stringEqualsCall,
aoqi@0 3775 make.Exec(make.Assign(make.Ident(dollar_tmp),
aoqi@0 3776 make.Literal(caseLabelToPosition.get(caseLabel))).
aoqi@0 3777 setType(dollar_tmp.type)),
aoqi@0 3778 elsepart);
aoqi@0 3779 }
aoqi@0 3780
aoqi@0 3781 ListBuffer<JCStatement> lb = new ListBuffer<>();
aoqi@0 3782 JCBreak breakStmt = make.Break(null);
aoqi@0 3783 breakStmt.target = switch1;
aoqi@0 3784 lb.append(elsepart).append(breakStmt);
aoqi@0 3785
aoqi@0 3786 caseBuffer.append(make.Case(make.Literal(hashCode), lb.toList()));
aoqi@0 3787 }
aoqi@0 3788
aoqi@0 3789 switch1.cases = caseBuffer.toList();
aoqi@0 3790 stmtList.append(switch1);
aoqi@0 3791
aoqi@0 3792 // Make isomorphic switch tree replacing string labels
aoqi@0 3793 // with corresponding integer ones from the label to
aoqi@0 3794 // position map.
aoqi@0 3795
aoqi@0 3796 ListBuffer<JCCase> lb = new ListBuffer<>();
aoqi@0 3797 JCSwitch switch2 = make.Switch(make.Ident(dollar_tmp), lb.toList());
aoqi@0 3798 for(JCCase oneCase : caseList ) {
aoqi@0 3799 // Rewire up old unlabeled break statements to the
aoqi@0 3800 // replacement switch being created.
aoqi@0 3801 patchTargets(oneCase, tree, switch2);
aoqi@0 3802
aoqi@0 3803 boolean isDefault = (oneCase.getExpression() == null);
aoqi@0 3804 JCExpression caseExpr;
aoqi@0 3805 if (isDefault)
aoqi@0 3806 caseExpr = null;
aoqi@0 3807 else {
aoqi@0 3808 caseExpr = make.Literal(caseLabelToPosition.get((String)TreeInfo.skipParens(oneCase.
aoqi@0 3809 getExpression()).
aoqi@0 3810 type.constValue()));
aoqi@0 3811 }
aoqi@0 3812
aoqi@0 3813 lb.append(make.Case(caseExpr,
aoqi@0 3814 oneCase.getStatements()));
aoqi@0 3815 }
aoqi@0 3816
aoqi@0 3817 switch2.cases = lb.toList();
aoqi@0 3818 stmtList.append(switch2);
aoqi@0 3819
aoqi@0 3820 return make.Block(0L, stmtList.toList());
aoqi@0 3821 }
aoqi@0 3822 }
aoqi@0 3823
aoqi@0 3824 public void visitNewArray(JCNewArray tree) {
aoqi@0 3825 tree.elemtype = translate(tree.elemtype);
aoqi@0 3826 for (List<JCExpression> t = tree.dims; t.tail != null; t = t.tail)
aoqi@0 3827 if (t.head != null) t.head = translate(t.head, syms.intType);
aoqi@0 3828 tree.elems = translate(tree.elems, types.elemtype(tree.type));
aoqi@0 3829 result = tree;
aoqi@0 3830 }
aoqi@0 3831
aoqi@0 3832 public void visitSelect(JCFieldAccess tree) {
aoqi@0 3833 // need to special case-access of the form C.super.x
aoqi@0 3834 // these will always need an access method, unless C
aoqi@0 3835 // is a default interface subclassed by the current class.
aoqi@0 3836 boolean qualifiedSuperAccess =
aoqi@0 3837 tree.selected.hasTag(SELECT) &&
aoqi@0 3838 TreeInfo.name(tree.selected) == names._super &&
aoqi@0 3839 !types.isDirectSuperInterface(((JCFieldAccess)tree.selected).selected.type.tsym, currentClass);
aoqi@0 3840 tree.selected = translate(tree.selected);
aoqi@0 3841 if (tree.name == names._class) {
aoqi@0 3842 result = classOf(tree.selected);
aoqi@0 3843 }
aoqi@0 3844 else if (tree.name == names._super &&
aoqi@0 3845 types.isDirectSuperInterface(tree.selected.type.tsym, currentClass)) {
aoqi@0 3846 //default super call!! Not a classic qualified super call
aoqi@0 3847 TypeSymbol supSym = tree.selected.type.tsym;
aoqi@0 3848 Assert.checkNonNull(types.asSuper(currentClass.type, supSym));
aoqi@0 3849 result = tree;
aoqi@0 3850 }
aoqi@0 3851 else if (tree.name == names._this || tree.name == names._super) {
aoqi@0 3852 result = makeThis(tree.pos(), tree.selected.type.tsym);
aoqi@0 3853 }
aoqi@0 3854 else
aoqi@0 3855 result = access(tree.sym, tree, enclOp, qualifiedSuperAccess);
aoqi@0 3856 }
aoqi@0 3857
aoqi@0 3858 public void visitLetExpr(LetExpr tree) {
aoqi@0 3859 tree.defs = translateVarDefs(tree.defs);
aoqi@0 3860 tree.expr = translate(tree.expr, tree.type);
aoqi@0 3861 result = tree;
aoqi@0 3862 }
aoqi@0 3863
aoqi@0 3864 // There ought to be nothing to rewrite here;
aoqi@0 3865 // we don't generate code.
aoqi@0 3866 public void visitAnnotation(JCAnnotation tree) {
aoqi@0 3867 result = tree;
aoqi@0 3868 }
aoqi@0 3869
aoqi@0 3870 @Override
aoqi@0 3871 public void visitTry(JCTry tree) {
aoqi@0 3872 if (tree.resources.nonEmpty()) {
aoqi@0 3873 result = makeTwrTry(tree);
aoqi@0 3874 return;
aoqi@0 3875 }
aoqi@0 3876
aoqi@0 3877 boolean hasBody = tree.body.getStatements().nonEmpty();
aoqi@0 3878 boolean hasCatchers = tree.catchers.nonEmpty();
aoqi@0 3879 boolean hasFinally = tree.finalizer != null &&
aoqi@0 3880 tree.finalizer.getStatements().nonEmpty();
aoqi@0 3881
aoqi@0 3882 if (!hasCatchers && !hasFinally) {
aoqi@0 3883 result = translate(tree.body);
aoqi@0 3884 return;
aoqi@0 3885 }
aoqi@0 3886
aoqi@0 3887 if (!hasBody) {
aoqi@0 3888 if (hasFinally) {
aoqi@0 3889 result = translate(tree.finalizer);
aoqi@0 3890 } else {
aoqi@0 3891 result = translate(tree.body);
aoqi@0 3892 }
aoqi@0 3893 return;
aoqi@0 3894 }
aoqi@0 3895
aoqi@0 3896 // no optimizations possible
aoqi@0 3897 super.visitTry(tree);
aoqi@0 3898 }
aoqi@0 3899
aoqi@0 3900 /**************************************************************************
aoqi@0 3901 * main method
aoqi@0 3902 *************************************************************************/
aoqi@0 3903
aoqi@0 3904 /** Translate a toplevel class and return a list consisting of
aoqi@0 3905 * the translated class and translated versions of all inner classes.
aoqi@0 3906 * @param env The attribution environment current at the class definition.
aoqi@0 3907 * We need this for resolving some additional symbols.
aoqi@0 3908 * @param cdef The tree representing the class definition.
aoqi@0 3909 */
aoqi@0 3910 public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
aoqi@0 3911 ListBuffer<JCTree> translated = null;
aoqi@0 3912 try {
aoqi@0 3913 attrEnv = env;
aoqi@0 3914 this.make = make;
aoqi@0 3915 endPosTable = env.toplevel.endPositions;
aoqi@0 3916 currentClass = null;
aoqi@0 3917 currentMethodDef = null;
aoqi@0 3918 outermostClassDef = (cdef.hasTag(CLASSDEF)) ? (JCClassDecl)cdef : null;
aoqi@0 3919 outermostMemberDef = null;
aoqi@0 3920 this.translated = new ListBuffer<JCTree>();
aoqi@0 3921 classdefs = new HashMap<ClassSymbol,JCClassDecl>();
aoqi@0 3922 actualSymbols = new HashMap<Symbol,Symbol>();
aoqi@0 3923 freevarCache = new HashMap<ClassSymbol,List<VarSymbol>>();
aoqi@0 3924 proxies = new Scope(syms.noSymbol);
aoqi@0 3925 twrVars = new Scope(syms.noSymbol);
aoqi@0 3926 outerThisStack = List.nil();
aoqi@0 3927 accessNums = new HashMap<Symbol,Integer>();
aoqi@0 3928 accessSyms = new HashMap<Symbol,MethodSymbol[]>();
aoqi@0 3929 accessConstrs = new HashMap<Symbol,MethodSymbol>();
aoqi@0 3930 accessConstrTags = List.nil();
aoqi@0 3931 accessed = new ListBuffer<Symbol>();
aoqi@0 3932 translate(cdef, (JCExpression)null);
aoqi@0 3933 for (List<Symbol> l = accessed.toList(); l.nonEmpty(); l = l.tail)
aoqi@0 3934 makeAccessible(l.head);
aoqi@0 3935 for (EnumMapping map : enumSwitchMap.values())
aoqi@0 3936 map.translate();
aoqi@0 3937 checkConflicts(this.translated.toList());
aoqi@0 3938 checkAccessConstructorTags();
aoqi@0 3939 translated = this.translated;
aoqi@0 3940 } finally {
aoqi@0 3941 // note that recursive invocations of this method fail hard
aoqi@0 3942 attrEnv = null;
aoqi@0 3943 this.make = null;
aoqi@0 3944 endPosTable = null;
aoqi@0 3945 currentClass = null;
aoqi@0 3946 currentMethodDef = null;
aoqi@0 3947 outermostClassDef = null;
aoqi@0 3948 outermostMemberDef = null;
aoqi@0 3949 this.translated = null;
aoqi@0 3950 classdefs = null;
aoqi@0 3951 actualSymbols = null;
aoqi@0 3952 freevarCache = null;
aoqi@0 3953 proxies = null;
aoqi@0 3954 outerThisStack = null;
aoqi@0 3955 accessNums = null;
aoqi@0 3956 accessSyms = null;
aoqi@0 3957 accessConstrs = null;
aoqi@0 3958 accessConstrTags = null;
aoqi@0 3959 accessed = null;
aoqi@0 3960 enumSwitchMap.clear();
aoqi@0 3961 assertionsDisabledClassCache = null;
aoqi@0 3962 }
aoqi@0 3963 return translated.toList();
aoqi@0 3964 }
aoqi@0 3965 }

mercurial