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