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