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