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