langtools/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java
author jrose
Sat, 01 May 2010 15:05:39 -0700
changeset 5736 ee0850472ca1
parent 4877 b642d21c9f74
child 5738 c24b113fe4ac
permissions -rw-r--r--
6939134: JSR 292 adjustments to method handle invocation Summary: split MethodHandle.invoke into invokeExact and invokeGeneric Reviewed-by: twisti
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: 165
diff changeset
     2
 * Copyright 2003-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
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.jvm.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.code.Type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import static com.sun.tools.javac.code.Kinds.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
/** This is the second phase of Enter, in which classes are completed
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *  by entering their members into the class scope using
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  MemberEnter.complete().  See Enter for an overview.
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 MemberEnter extends JCTree.Visitor implements Completer {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    protected static final Context.Key<MemberEnter> memberEnterKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        new Context.Key<MemberEnter>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /** A switch to determine whether we check for package/class conflicts
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    final static boolean checkClash = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1045
diff changeset
    64
    private final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private final Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    private final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    private final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    private final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private final ClassReader reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private final Todo todo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    private final Annotate annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private final Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
    75
    private final JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private final Target target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    private final boolean skipAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public static MemberEnter instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        MemberEnter instance = context.get(memberEnterKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            instance = new MemberEnter(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    protected MemberEnter(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        context.put(memberEnterKey, this);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1045
diff changeset
    89
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        log = Log.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
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        reader = ClassReader.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        todo = Todo.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        annotate = Annotate.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        types = Types.instance(context);
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   100
        diags = JCDiagnostic.Factory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        target = Target.instance(context);
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
   102
        Options options = Options.instance(context);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
   103
        skipAnnotations = options.get("skipAnnotations") != null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /** A queue for classes whose members still need to be entered into the
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     *  symbol table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    ListBuffer<Env<AttrContext>> halfcompleted = new ListBuffer<Env<AttrContext>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /** Set to true only when the first of a set of classes is
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     *  processed from the halfcompleted queue.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    boolean isFirst = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    /** A flag to disable completion from time to time during member
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     *  enter, as we only need to look up types.  This avoids
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *  unnecessarily deep recursion.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    boolean completionEnabled = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    /* ---------- Processing import clauses ----------------
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    /** Import all classes of a class or package on demand.
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     *  @param pos           Position to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     *  @param tsym          The class or package the members of which are imported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     *  @param toScope   The (import) scope in which imported classes
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     *               are entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    private void importAll(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                           final TypeSymbol tsym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                           Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        // Check that packages imported from exist (JLS ???).
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            // If we can't find java.lang, exit immediately.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) {
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   138
                JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                throw new FatalError(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                log.error(pos, "doesnt.exist", tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        final Scope fromScope = tsym.members();
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        final Scope toScope = env.toplevel.starImportScope;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            if (e.sym.kind == TYP && !toScope.includes(e.sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                toScope.enter(e.sym, fromScope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    /** Import all static members of a class or package on demand.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     *  @param pos           Position to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     *  @param tsym          The class or package the members of which are imported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     *  @param toScope   The (import) scope in which imported classes
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     *               are entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    private void importStaticAll(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                                 final TypeSymbol tsym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                                 Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        final JavaFileObject sourcefile = env.toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        final Scope toScope = env.toplevel.starImportScope;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        final PackageSymbol packge = env.toplevel.packge;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        final TypeSymbol origin = tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        // enter imported types immediately
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        new Object() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            Set<Symbol> processed = new HashSet<Symbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            void importFrom(TypeSymbol tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                if (tsym == null || !processed.add(tsym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                // also import inherited names
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                importFrom(types.supertype(tsym.type).tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                for (Type t : types.interfaces(tsym.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                    importFrom(t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                final Scope fromScope = tsym.members();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                    Symbol sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                    if (sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                        (sym.flags() & STATIC) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                        staticImportAccessible(sym, packge) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                        sym.isMemberOf(origin, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                        !toScope.includes(sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                        toScope.enter(sym, fromScope, origin.members());
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        }.importFrom(tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        // enter non-types before annotations that might use them
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        annotate.earlier(new Annotate.Annotator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            Set<Symbol> processed = new HashSet<Symbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                return "import static " + tsym + ".*" + " in " + sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            void importFrom(TypeSymbol tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                if (tsym == null || !processed.add(tsym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                // also import inherited names
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                importFrom(types.supertype(tsym.type).tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                for (Type t : types.interfaces(tsym.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                    importFrom(t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                final Scope fromScope = tsym.members();
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                    Symbol sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                    if (sym.isStatic() && sym.kind != TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                        staticImportAccessible(sym, packge) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                        !toScope.includes(sym) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                        sym.isMemberOf(origin, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                        toScope.enter(sym, fromScope, origin.members());
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            public void enterAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                importFrom(tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    // is the sym accessible everywhere in packge?
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    boolean staticImportAccessible(Symbol sym, PackageSymbol packge) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        int flags = (int)(sym.flags() & AccessFlags);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        switch (flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        case PUBLIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            return sym.packge() == packge;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
    /** Import statics types of a given name.  Non-types are handled in Attr.
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     *  @param pos           Position to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     *  @param tsym          The class from which the name is imported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
     *  @param name          The (simple) name being imported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
     *  @param env           The environment containing the named import
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     *                  scope to add to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    private void importNamedStatic(final DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                                   final TypeSymbol tsym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                                   final Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                                   final Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        if (tsym.kind != TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            log.error(pos, "static.imp.only.classes.and.interfaces");
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        final Scope toScope = env.toplevel.namedImportScope;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        final PackageSymbol packge = env.toplevel.packge;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        final TypeSymbol origin = tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        // enter imported types immediately
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        new Object() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            Set<Symbol> processed = new HashSet<Symbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            void importFrom(TypeSymbol tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                if (tsym == null || !processed.add(tsym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                // also import inherited names
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                importFrom(types.supertype(tsym.type).tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                for (Type t : types.interfaces(tsym.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                    importFrom(t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                for (Scope.Entry e = tsym.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                     e.scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                     e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                    Symbol sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    if (sym.isStatic() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                        sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                        staticImportAccessible(sym, packge) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                        sym.isMemberOf(origin, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                        chk.checkUniqueStaticImport(pos, sym, toScope))
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                        toScope.enter(sym, sym.owner.members(), origin.members());
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }.importFrom(tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        // enter non-types before annotations that might use them
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        annotate.earlier(new Annotate.Annotator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            Set<Symbol> processed = new HashSet<Symbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            boolean found = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                return "import static " + tsym + "." + name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            void importFrom(TypeSymbol tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                if (tsym == null || !processed.add(tsym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                // also import inherited names
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                importFrom(types.supertype(tsym.type).tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                for (Type t : types.interfaces(tsym.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                    importFrom(t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                for (Scope.Entry e = tsym.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                     e.scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                     e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                    Symbol sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                    if (sym.isStatic() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                        staticImportAccessible(sym, packge) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                        sym.isMemberOf(origin, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                        found = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                        if (sym.kind == MTH ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                            sym.kind != TYP && chk.checkUniqueStaticImport(pos, sym, toScope))
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                            toScope.enter(sym, sym.owner.members(), origin.members());
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            public void enterAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                    importFrom(tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                    if (!found) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                        log.error(pos, "cant.resolve.location",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   322
                                  KindName.STATIC,
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   323
                                  name, List.<Type>nil(), List.<Type>nil(),
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   324
                                  Kinds.typeKindName(tsym.type),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                                  tsym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                    log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    /** Import given class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     *  @param pos           Position to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
     *  @param tsym          The class to be imported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
     *  @param env           The environment containing the named import
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
     *                  scope to add to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    private void importNamed(DiagnosticPosition pos, Symbol tsym, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        if (tsym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            chk.checkUniqueImport(pos, tsym, env.toplevel.namedImportScope))
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            env.toplevel.namedImportScope.enter(tsym, tsym.owner.members());
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
    /** Construct method type from method signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     *  @param typarams    The method's type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     *  @param params      The method's value parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
     *  @param res             The method's result type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
     *                 null if it is a constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
     *  @param thrown      The method's thrown exceptions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     *  @param env             The method's (local) environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    Type signature(List<JCTypeParameter> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                   List<JCVariableDecl> params,
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                   JCTree res,
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                   List<JCExpression> thrown,
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                   Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        // Enter and attribute type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        List<Type> tvars = enter.classEnter(typarams, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        attr.attribTypeVariables(typarams, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        // Enter and attribute value parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        ListBuffer<Type> argbuf = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            memberEnter(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            argbuf.append(l.head.vartype.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        // Attribute result type, if one is given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        Type restype = res == null ? syms.voidType : attr.attribType(res, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        // Attribute thrown exceptions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
        ListBuffer<Type> thrownbuf = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
        for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            Type exc = attr.attribType(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            if (exc.tag != TYPEVAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                exc = chk.checkClassType(l.head.pos(), exc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            thrownbuf.append(exc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
        Type mtype = new MethodType(argbuf.toList(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                                    restype,
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                                    thrownbuf.toList(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                                    syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
 * Visitor methods for member enter
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    /** Visitor argument: the current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    protected Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    /** Enter field and method definitions and process import
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
     *  clauses, catching any completion failure exceptions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    protected void memberEnter(JCTree tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        Env<AttrContext> prevEnv = this.env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        }  catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            this.env = prevEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
    /** Enter members from a list of trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
    void memberEnter(List<? extends JCTree> trees, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            memberEnter(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
    /** Enter members for a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
    void finishClass(JCClassDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        if ((tree.mods.flags & Flags.ENUM) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            addEnumMembers(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        memberEnter(tree.defs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
    /** Add the implicit members for an enum type
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
     *  to the symbol table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
    private void addEnumMembers(JCClassDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        JCExpression valuesType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass));
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        // public static T[] values() { return ???; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        JCMethodDecl values = make.
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                      names.values,
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                      valuesType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                      List.<JCTypeParameter>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                      List.<JCVariableDecl>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                      List.<JCExpression>nil(), // thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                      null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                      null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        memberEnter(values, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        // public static T valueOf(String name) { return ???; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        JCMethodDecl valueOf = make.
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                      names.valueOf,
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                      make.Type(tree.sym.type),
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                      List.<JCTypeParameter>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
                      List.of(make.VarDef(make.Modifiers(Flags.PARAMETER),
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                                            names.fromString("name"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                                            make.Type(syms.stringType), null)),
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                      List.<JCExpression>nil(), // thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                      null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                      null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        memberEnter(valueOf, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        // the remaining members are for bootstrapping only
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
        if (!target.compilerBootstrap(tree.sym)) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        // public final int ordinal() { return ???; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        JCMethodDecl ordinal = make.at(tree.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL),
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
                      names.ordinal,
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                      make.Type(syms.intType),
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                      List.<JCTypeParameter>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
                      List.<JCVariableDecl>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
                      List.<JCExpression>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                      null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                      null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        memberEnter(ordinal, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        // public final String name() { return ???; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
        JCMethodDecl name = make.
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL),
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                      names._name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                      make.Type(syms.stringType),
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                      List.<JCTypeParameter>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                      List.<JCVariableDecl>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                      List.<JCExpression>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                      null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                      null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        memberEnter(name, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        // public int compareTo(E other) { return ???; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        MethodSymbol compareTo = new
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            MethodSymbol(Flags.PUBLIC,
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                         names.compareTo,
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                         new MethodType(List.of(tree.sym.type),
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                                        syms.intType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                                        List.<Type>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                                        syms.methodClass),
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                         tree.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        memberEnter(make.MethodDef(compareTo, null), env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
    public void visitTopLevel(JCCompilationUnit tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        if (tree.starImportScope.elems != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            // we must have already processed this toplevel
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        // check that no class exists with same fully qualified name as
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        // toplevel package
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        if (checkClash && tree.pid != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            Symbol p = tree.packge;
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            while (p.owner != syms.rootPackage) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                p.owner.complete(); // enter all class members of p
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                if (syms.classes.get(p.getQualifiedName()) != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                    log.error(tree.pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                              "pkg.clashes.with.class.of.same.name",
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                              p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                p = p.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
        // process package annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        annotateLater(tree.packageAnnotations, env, tree.packge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        // Import-on-demand java.lang.
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        importAll(tree.pos, reader.enterPackage(names.java_lang), env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        // Process all import clauses.
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        memberEnter(tree.defs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    // process the non-static imports and the static imports of types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
    public void visitImport(JCImport tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        JCTree imp = tree.qualid;
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
        Name name = TreeInfo.name(imp);
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        TypeSymbol p;
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        // Create a local environment pointing to this tree to disable
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        // effects of other imports in Resolve.findGlobalType
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        Env<AttrContext> localEnv = env.dup(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
        // Attribute qualifying package or class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        JCFieldAccess s = (JCFieldAccess) imp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
        p = attr.
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            attribTree(s.selected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
                       localEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                       tree.staticImport ? TYP : (TYP | PCK),
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                       Type.noType).tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
        if (name == names.asterisk) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            // Import on demand.
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
            chk.checkCanonical(s.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            if (tree.staticImport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
                importStaticAll(tree.pos, p, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
                importAll(tree.pos, p, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            // Named type import.
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
            if (tree.staticImport) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
                importNamedStatic(tree.pos(), p, name, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
                chk.checkCanonical(s.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
                TypeSymbol c = attribImportType(imp, localEnv).tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
                chk.checkCanonical(imp);
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
                importNamed(tree.pos(), c, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
    public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        Scope enclScope = enter.enterScope(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
        MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
        tree.sym = m;
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
        Env<AttrContext> localEnv = methodEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        // Compute the method type
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        m.type = signature(tree.typarams, tree.params,
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                           tree.restype, tree.thrown,
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                           localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
        // Set m.params
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
        ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        JCVariableDecl lastParam = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
            JCVariableDecl param = lastParam = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
            assert param.sym != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
            params.append(param.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        m.params = params.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        // mark the method varargs, if necessary
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            m.flags_field |= Flags.VARARGS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        localEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
        if (chk.checkUnique(tree.pos(), m, enclScope)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
            enclScope.enter(m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        annotateLater(tree.mods.annotations, localEnv, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        if (tree.defaultValue != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            annotateDefaultValueLater(tree.defaultValue, localEnv, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
    /** Create a fresh environment for method bodies.
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
     *  @param tree     The method definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
     *  @param env      The environment current outside of the method definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
    Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        Env<AttrContext> localEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
            env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        localEnv.enclMethod = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
        localEnv.info.scope.owner = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
        if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
    public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        Env<AttrContext> localEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        if ((tree.mods.flags & STATIC) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            (env.info.scope.owner.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
            localEnv.info.staticLevel++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
        attr.attribType(tree.vartype, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        Scope enclScope = enter.enterScope(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
        VarSymbol v =
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
        v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        tree.sym = v;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        if (tree.init != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
            v.flags_field |= HASINIT;
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1043
diff changeset
   630
            if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) {
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1043
diff changeset
   631
                Env<AttrContext> initEnv = getInitEnv(tree, env);
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1043
diff changeset
   632
                initEnv.info.enclVar = v;
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1043
diff changeset
   633
                v.setLazyConstValue(initEnv(tree, initEnv), log, attr, tree.init);
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1043
diff changeset
   634
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
        if (chk.checkUnique(tree.pos(), v, enclScope)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
            chk.checkTransparentVar(tree.pos(), v, enclScope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
            enclScope.enter(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
        annotateLater(tree.mods.annotations, localEnv, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
        v.pos = tree.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
    /** Create a fresh environment for a variable's initializer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
     *  If the variable is a field, the owner of the environment's scope
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
     *  is be the variable itself, otherwise the owner is the method
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
     *  enclosing the variable definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
     *  @param tree     The variable definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
     *  @param env      The environment current outside of the variable definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
    Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
        if (tree.sym.owner.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
            localEnv.info.scope = new Scope.DelegatedScope(env.info.scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
            localEnv.info.scope.owner = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        if ((tree.mods.flags & STATIC) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
            (env.enclClass.sym.flags() & INTERFACE) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
            localEnv.info.staticLevel++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
    /** Default member enter visitor method: do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
    public void visitTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
    public void visitErroneous(JCErroneous tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
        memberEnter(tree.errs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
    public Env<AttrContext> getMethodEnv(JCMethodDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
        Env<AttrContext> mEnv = methodEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        mEnv.info.lint = mEnv.info.lint.augment(tree.sym.attributes_field, tree.sym.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
            mEnv.info.scope.enterIfAbsent(l.head.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
            mEnv.info.scope.enterIfAbsent(l.head.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        return mEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
    public Env<AttrContext> getInitEnv(JCVariableDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
        Env<AttrContext> iEnv = initEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
        return iEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
 * Type completion
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
    Type attribImportType(JCTree tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
        assert completionEnabled;
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
            // To prevent deep recursion, suppress completion of some
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
            // types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
            completionEnabled = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
            return attr.attribType(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            completionEnabled = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
 * Annotation processing
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
    /** Queue annotations for later processing. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
    void annotateLater(final List<JCAnnotation> annotations,
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                       final Env<AttrContext> localEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
                       final Symbol s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
        if (annotations.isEmpty()) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
        if (s.kind != PCK) s.attributes_field = null; // mark it incomplete for now
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
        annotate.later(new Annotate.Annotator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                    return "annotate " + annotations + " onto " + s + " in " + s.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                public void enterAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
                    assert s.kind == PCK || s.attributes_field == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                    JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                        if (s.attributes_field != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
                            s.attributes_field.nonEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
                            annotations.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
                            log.error(annotations.head.pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
                                      "already.annotated",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   728
                                      kindName(s), s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
                        enterAnnotations(annotations, localEnv, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
                        log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
            });
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
     * Check if a list of annotations contains a reference to
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
     * java.lang.Deprecated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    private boolean hasDeprecatedAnnotation(List<JCAnnotation> annotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
            JCAnnotation a = al.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
            if (a.annotationType.type == syms.deprecatedType && a.args.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
    /** Enter a set of annotations. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
    private void enterAnnotations(List<JCAnnotation> annotations,
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
                          Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
                          Symbol s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        ListBuffer<Attribute.Compound> buf =
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
            new ListBuffer<Attribute.Compound>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
        Set<TypeSymbol> annotated = new HashSet<TypeSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
        if (!skipAnnotations)
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
        for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            JCAnnotation a = al.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            Attribute.Compound c = annotate.enterAnnotation(a,
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
                                                            syms.annotationType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
                                                            env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
            if (c == null) continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            buf.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
            // Note: @Deprecated has no effect on local variables and parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
            if (!c.type.isErroneous()
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
                && s.owner.kind != MTH
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
                && types.isSameType(c.type, syms.deprecatedType))
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
                s.flags_field |= Flags.DEPRECATED;
5736
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4877
diff changeset
   771
            // Internally to java.dyn, a @PolymorphicSignature annotation
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4877
diff changeset
   772
            // translates to a classfile attribute.
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4877
diff changeset
   773
            if (!c.type.isErroneous()
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4877
diff changeset
   774
                && types.isSameType(c.type, syms.polymorphicSignatureType)) {
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4877
diff changeset
   775
                s.flags_field |= Flags.POLYMORPHIC_SIGNATURE;
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4877
diff changeset
   776
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
            if (!annotated.add(a.type.tsym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
                log.error(a.pos, "duplicate.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
        s.attributes_field = buf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
    /** Queue processing of an attribute default value. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
    void annotateDefaultValueLater(final JCExpression defaultValue,
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
                                   final Env<AttrContext> localEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
                                   final MethodSymbol m) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
        annotate.later(new Annotate.Annotator() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
                public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
                    return "annotate " + m.owner + "." +
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
                        m + " default " + defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
                public void enterAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
                    JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
                        enterDefaultValue(defaultValue, localEnv, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                        log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
            });
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
    /** Enter a default value for an attribute method. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
    private void enterDefaultValue(final JCExpression defaultValue,
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                                   final Env<AttrContext> localEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                                   final MethodSymbol m) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
        m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                                                      defaultValue,
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
                                                      localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
 * Source completer
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
    /** Complete entering a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
     *  @param sym         The symbol of the class to be completed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
    public void complete(Symbol sym) throws CompletionFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
        // Suppress some (recursive) MemberEnter invocations
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
        if (!completionEnabled) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
            // Re-install same completer for next time around and return.
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            assert (sym.flags() & Flags.COMPOUND) == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
            sym.completer = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
        ClassSymbol c = (ClassSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
        ClassType ct = (ClassType)c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        Env<AttrContext> env = enter.typeEnvs.get(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        JCClassDecl tree = (JCClassDecl)env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        boolean wasFirst = isFirst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        isFirst = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
            // Save class environment for later member enter (2) processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
            halfcompleted.append(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
1043
11ea4773b0af 6695838: javac does not detect cyclic inheritance involving static inner classes after import clause
mcimadamore
parents: 1040
diff changeset
   840
            // Mark class as not yet attributed.
11ea4773b0af 6695838: javac does not detect cyclic inheritance involving static inner classes after import clause
mcimadamore
parents: 1040
diff changeset
   841
            c.flags_field |= UNATTRIBUTED;
11ea4773b0af 6695838: javac does not detect cyclic inheritance involving static inner classes after import clause
mcimadamore
parents: 1040
diff changeset
   842
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            // If this is a toplevel-class, make sure any preceding import
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
            // clauses have been seen.
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            if (c.owner.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
                memberEnter(env.toplevel, env.enclosing(JCTree.TOPLEVEL));
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
                todo.append(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
            if (c.owner.kind == TYP)
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
                c.owner.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            // create an environment for evaluating the base clauses
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
            Env<AttrContext> baseEnv = baseEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
            // Determine supertype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
            Type supertype =
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
                (tree.extending != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
                ? attr.attribBase(tree.extending, baseEnv, true, false, true)
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                : ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c))
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                ? attr.attribBase(enumBase(tree.pos, c), baseEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
                                  true, false, false)
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                : (c.fullname == names.java_lang_Object)
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
                ? Type.noType
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                : syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
            ct.supertype_field = supertype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
            // Determine interfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
            ListBuffer<Type> interfaces = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
            Set<Type> interfaceSet = new HashSet<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            List<JCExpression> interfaceTrees = tree.implementing;
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
            if ((tree.mods.flags & Flags.ENUM) != 0 && target.compilerBootstrap(c)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
                // add interface Comparable<T>
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
                interfaceTrees =
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
                    interfaceTrees.prepend(make.Type(new ClassType(syms.comparableType.getEnclosingType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
                                                                   List.of(c.type),
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
                                                                   syms.comparableType.tsym)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
                // add interface Serializable
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
                interfaceTrees =
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
                    interfaceTrees.prepend(make.Type(syms.serializableType));
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
            for (JCExpression iface : interfaceTrees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
                Type i = attr.attribBase(iface, baseEnv, false, true, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
                if (i.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
                    interfaces.append(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
                    chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
            if ((c.flags_field & ANNOTATION) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
                ct.interfaces_field = List.of(syms.annotationType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
                ct.interfaces_field = interfaces.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
            if (c.fullname == names.java_lang_Object) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
                if (tree.extending != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
                    chk.checkNonCyclic(tree.extending.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
                                       supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
                    ct.supertype_field = Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
                else if (tree.implementing.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
                    chk.checkNonCyclic(tree.implementing.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
                                       ct.interfaces_field.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
                    ct.interfaces_field = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            // Annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
            // In general, we cannot fully process annotations yet,  but we
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
            // can attribute the annotation types and then check to see if the
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
            // @Deprecated annotation is present.
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
            attr.attribAnnotationTypes(tree.mods.annotations, baseEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
            if (hasDeprecatedAnnotation(tree.mods.annotations))
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
                c.flags_field |= DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
            annotateLater(tree.mods.annotations, baseEnv, c);
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
   915
            // class type parameters use baseEnv but everything uses env
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
   916
            for (JCTypeParameter tp : tree.typarams)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
   917
                tp.accept(new TypeAnnotate(baseEnv));
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
   918
            tree.accept(new TypeAnnotate(env));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
165
1875ac59896a 6663588: Compiler goes into infinite loop for Cyclic Inheritance test case
mcimadamore
parents: 10
diff changeset
   920
            chk.checkNonCyclic(tree.pos(), c.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
165
1875ac59896a 6663588: Compiler goes into infinite loop for Cyclic Inheritance test case
mcimadamore
parents: 10
diff changeset
   922
            attr.attribTypeVariables(tree.typarams, baseEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
            // Add default constructor if needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
            if ((c.flags() & INTERFACE) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
                !TreeInfo.hasConstructors(tree.defs)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
                List<Type> argtypes = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
                List<Type> typarams = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
                List<Type> thrown = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
                long ctorFlags = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
                boolean based = false;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1045
diff changeset
   932
                if (c.name.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
                    JCNewClass nc = (JCNewClass)env.next.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
                    if (nc.constructor != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
                        Type superConstrType = types.memberType(c.type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
                                                                nc.constructor);
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                        argtypes = superConstrType.getParameterTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
                        typarams = superConstrType.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
                        ctorFlags = nc.constructor.flags() & VARARGS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
                        if (nc.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
                            argtypes = argtypes.prepend(nc.encl.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
                            based = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                        thrown = superConstrType.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
                JCTree constrDef = DefaultConstructor(make.at(tree.pos), c,
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
                                                    typarams, argtypes, thrown,
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
                                                    ctorFlags, based);
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
                tree.defs = tree.defs.prepend(constrDef);
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
            // If this is a class, enter symbols for this and super into
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
            // current scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
            if ((c.flags_field & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
                VarSymbol thisSym =
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
                    new VarSymbol(FINAL | HASINIT, names._this, c.type, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
                thisSym.pos = Position.FIRSTPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
                env.info.scope.enter(thisSym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
                if (ct.supertype_field.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                    VarSymbol superSym =
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                        new VarSymbol(FINAL | HASINIT, names._super,
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                                      ct.supertype_field, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
                    superSym.pos = Position.FIRSTPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
                    env.info.scope.enter(superSym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
            // check that no package exists with same fully qualified name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
            // but admit classes in the unnamed package which have the same
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
            // name as a top-level package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
            if (checkClash &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                c.owner.kind == PCK && c.owner != syms.unnamedPackage &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                reader.packageExists(c.fullname))
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                    log.error(tree.pos, "clash.with.pkg.of.same.name", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
            chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
        // Enter all member fields and methods of a set of half completed
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
        // classes in a second phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
        if (wasFirst) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                while (halfcompleted.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                    finish(halfcompleted.next());
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                isFirst = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
            // commit pending annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
            annotate.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1001
    // A sub-phase that "compiles" annotations in annotated types.
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1002
    private class TypeAnnotate extends TreeScanner {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1003
        private Env<AttrContext> env;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1004
        public TypeAnnotate(Env<AttrContext> env) { this.env = env; }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1005
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1006
        private void enterTypeAnnotations(List<JCTypeAnnotation> annotations) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1007
            Set<TypeSymbol> annotated = new HashSet<TypeSymbol>();
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1008
            if (!skipAnnotations)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1009
                for (List<JCTypeAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1010
                    JCTypeAnnotation a = al.head;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1011
                    Attribute.Compound c = annotate.enterAnnotation(a,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1012
                            syms.annotationType,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1013
                            env);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1014
                    if (c == null) continue;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1015
                    Attribute.TypeCompound tc = new Attribute.TypeCompound(c.type, c.values, a.annotation_position);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1016
                    a.attribute_field = tc;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1017
                    // Note: @Deprecated has no effect on local variables and parameters
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1018
                    if (!annotated.add(a.type.tsym))
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1019
                        log.error(a.pos, "duplicate.annotation");
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1020
                }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1021
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1022
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1023
        // each class (including enclosed inner classes) should be visited
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1024
        // separately through MemberEnter.complete(Symbol)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1025
        // this flag is used to prevent from visiting inner classes.
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1026
        private boolean isEnclosingClass = false;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1027
        @Override
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1028
        public void visitClassDef(final JCClassDecl tree) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1029
            if (isEnclosingClass)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1030
                return;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1031
            isEnclosingClass = true;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1032
            scan(tree.mods);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1033
            // type parameter need to be visited with a separate env
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1034
            // scan(tree.typarams);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1035
            scan(tree.extending);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1036
            scan(tree.implementing);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1037
            scan(tree.defs);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1038
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1039
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1040
        private void annotate(final JCTree tree, final List<JCTypeAnnotation> annotations) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1041
            annotate.later(new Annotate.Annotator() {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1042
                public String toString() {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1043
                    return "annotate " + annotations + " onto " + tree;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1044
                }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1045
                public void enterAnnotation() {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1046
                    JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1047
                    try {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1048
                        enterTypeAnnotations(annotations);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1049
                    } finally {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1050
                        log.useSource(prev);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1051
                    }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1052
                }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1053
            });
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1054
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1055
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1056
        @Override
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1057
        public void visitAnnotatedType(final JCAnnotatedType tree) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1058
            annotate(tree, tree.annotations);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1059
            super.visitAnnotatedType(tree);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1060
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1061
        @Override
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1062
        public void visitTypeParameter(final JCTypeParameter tree) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1063
            annotate(tree, tree.annotations);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1064
            super.visitTypeParameter(tree);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1065
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1066
        @Override
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1067
        public void visitNewArray(final JCNewArray tree) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1068
            annotate(tree, tree.annotations);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1069
            for (List<JCTypeAnnotation> dimAnnos : tree.dimAnnotations)
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1070
                annotate(tree, dimAnnos);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1071
            super.visitNewArray(tree);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1072
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1073
        @Override
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1074
        public void visitMethodDef(JCMethodDecl tree) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1075
            annotate(tree, tree.receiverAnnotations);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1076
            super.visitMethodDef(tree);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1077
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1078
    }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1079
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1260
diff changeset
  1080
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
    private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
        Scope typaramScope = new Scope(tree.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
        if (tree.typarams != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
            for (List<JCTypeParameter> typarams = tree.typarams;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
                 typarams.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
                 typarams = typarams.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
                typaramScope.enter(typarams.head.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
        Env<AttrContext> outer = env.outer; // the base clause can't see members of this class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
        Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(typaramScope));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
        localEnv.baseClause = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
        localEnv.outer = outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
        localEnv.info.isSelfCall = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
    /** Enter member fields and methods of a class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
     *  @param env        the environment current for the class block.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
    private void finish(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
            JCClassDecl tree = (JCClassDecl)env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
            finishClass(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
            log.useSource(prev);
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
    /** Generate a base clause for an enum type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
     *  @param pos              The position for trees and diagnostics, if any
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
     *  @param c                The class symbol of the enum
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
    private JCExpression enumBase(int pos, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
        JCExpression result = make.at(pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
            TypeApply(make.QualIdent(syms.enumSym),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
                      List.<JCExpression>of(make.Type(c.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
 * tree building
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
    /** Generate default constructor for given class. For classes different
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
     *  from java.lang.Object, this is:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
     *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
     *      super(x_0, ..., x_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
     *    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
     *  or, if based == true:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
     *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
     *      x_0.super(x_1, ..., x_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
     *    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
     *  @param make     The tree factory.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
     *  @param c        The class owning the default constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
     *  @param argtypes The parameter types of the constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
     *  @param thrown   The thrown exceptions of the constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
     *  @param based    Is first parameter a this$n?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
    JCTree DefaultConstructor(TreeMaker make,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
                            ClassSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
                            List<Type> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
                            List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
                            List<Type> thrown,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
                            long flags,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
                            boolean based) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
        List<JCVariableDecl> params = make.Params(argtypes, syms.noSymbol);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
        List<JCStatement> stats = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
        if (c.type != syms.objectType)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
            stats = stats.prepend(SuperCall(make, typarams, params, based));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
        if ((c.flags() & ENUM) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            (types.supertype(c.type).tsym == syms.enumSym ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
             target.compilerBootstrap(c))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
            // constructors of true enums are private
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
            flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
            flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1045
diff changeset
  1161
        if (c.name.isEmpty()) flags |= ANONCONSTR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
        JCTree result = make.MethodDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
            make.Modifiers(flags),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
            names.init,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
            null,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            make.TypeParams(typarams),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
            params,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
            make.Types(thrown),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
            make.Block(0, stats),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
            null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
    /** Generate call to superclass constructor. This is:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
     *    super(id_0, ..., id_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
     * or, if based == true
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
     *    id_0.super(id_1,...,id_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
     *  where id_0, ..., id_n are the names of the given parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
     *  @param make    The tree factory
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
     *  @param params  The parameters that need to be passed to super
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
     *  @param typarams  The type parameters that need to be passed to super
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
     *  @param based   Is first parameter a this$n?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
    JCExpressionStatement SuperCall(TreeMaker make,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
                   List<Type> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
                   List<JCVariableDecl> params,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
                   boolean based) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
        JCExpression meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
        if (based) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
            meth = make.Select(make.Ident(params.head), names._super);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
            params = params.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
            meth = make.Ident(names._super);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
        List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
        return make.Exec(make.Apply(typeargs, meth, make.Idents(params)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
}