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