src/share/classes/com/sun/tools/javac/comp/MemberEnter.java

changeset 1
9a66ca7c79fa
child 8
38bd6375f37d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,1107 @@
     1.4 +/*
     1.5 + * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.comp;
    1.30 +
    1.31 +import java.util.*;
    1.32 +import java.util.Set;
    1.33 +import javax.tools.JavaFileObject;
    1.34 +
    1.35 +import com.sun.tools.javac.code.*;
    1.36 +import com.sun.tools.javac.jvm.*;
    1.37 +import com.sun.tools.javac.tree.*;
    1.38 +import com.sun.tools.javac.util.*;
    1.39 +import com.sun.tools.javac.util.List;
    1.40 +
    1.41 +import com.sun.tools.javac.code.Type.*;
    1.42 +import com.sun.tools.javac.code.Symbol.*;
    1.43 +import com.sun.tools.javac.tree.JCTree.*;
    1.44 +
    1.45 +import static com.sun.tools.javac.code.Flags.*;
    1.46 +import static com.sun.tools.javac.code.Kinds.*;
    1.47 +import static com.sun.tools.javac.code.TypeTags.*;
    1.48 +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    1.49 +
    1.50 +/** This is the second phase of Enter, in which classes are completed
    1.51 + *  by entering their members into the class scope using
    1.52 + *  MemberEnter.complete().  See Enter for an overview.
    1.53 + *
    1.54 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.55 + *  you write code that depends on this, you do so at your own risk.
    1.56 + *  This code and its internal interfaces are subject to change or
    1.57 + *  deletion without notice.</b>
    1.58 + */
    1.59 +public class MemberEnter extends JCTree.Visitor implements Completer {
    1.60 +    protected static final Context.Key<MemberEnter> memberEnterKey =
    1.61 +        new Context.Key<MemberEnter>();
    1.62 +
    1.63 +    /** A switch to determine whether we check for package/class conflicts
    1.64 +     */
    1.65 +    final static boolean checkClash = true;
    1.66 +
    1.67 +    private final Name.Table names;
    1.68 +    private final Enter enter;
    1.69 +    private final Log log;
    1.70 +    private final Check chk;
    1.71 +    private final Attr attr;
    1.72 +    private final Symtab syms;
    1.73 +    private final TreeMaker make;
    1.74 +    private final ClassReader reader;
    1.75 +    private final Todo todo;
    1.76 +    private final Annotate annotate;
    1.77 +    private final Types types;
    1.78 +    private final Target target;
    1.79 +
    1.80 +    private final boolean skipAnnotations;
    1.81 +
    1.82 +    public static MemberEnter instance(Context context) {
    1.83 +        MemberEnter instance = context.get(memberEnterKey);
    1.84 +        if (instance == null)
    1.85 +            instance = new MemberEnter(context);
    1.86 +        return instance;
    1.87 +    }
    1.88 +
    1.89 +    protected MemberEnter(Context context) {
    1.90 +        context.put(memberEnterKey, this);
    1.91 +        names = Name.Table.instance(context);
    1.92 +        enter = Enter.instance(context);
    1.93 +        log = Log.instance(context);
    1.94 +        chk = Check.instance(context);
    1.95 +        attr = Attr.instance(context);
    1.96 +        syms = Symtab.instance(context);
    1.97 +        make = TreeMaker.instance(context);
    1.98 +        reader = ClassReader.instance(context);
    1.99 +        todo = Todo.instance(context);
   1.100 +        annotate = Annotate.instance(context);
   1.101 +        types = Types.instance(context);
   1.102 +        target = Target.instance(context);
   1.103 +        skipAnnotations =
   1.104 +            Options.instance(context).get("skipAnnotations") != null;
   1.105 +    }
   1.106 +
   1.107 +    /** A queue for classes whose members still need to be entered into the
   1.108 +     *  symbol table.
   1.109 +     */
   1.110 +    ListBuffer<Env<AttrContext>> halfcompleted = new ListBuffer<Env<AttrContext>>();
   1.111 +
   1.112 +    /** Set to true only when the first of a set of classes is
   1.113 +     *  processed from the halfcompleted queue.
   1.114 +     */
   1.115 +    boolean isFirst = true;
   1.116 +
   1.117 +    /** A flag to disable completion from time to time during member
   1.118 +     *  enter, as we only need to look up types.  This avoids
   1.119 +     *  unnecessarily deep recursion.
   1.120 +     */
   1.121 +    boolean completionEnabled = true;
   1.122 +
   1.123 +    /* ---------- Processing import clauses ----------------
   1.124 +     */
   1.125 +
   1.126 +    /** Import all classes of a class or package on demand.
   1.127 +     *  @param pos           Position to be used for error reporting.
   1.128 +     *  @param tsym          The class or package the members of which are imported.
   1.129 +     *  @param toScope   The (import) scope in which imported classes
   1.130 +     *               are entered.
   1.131 +     */
   1.132 +    private void importAll(int pos,
   1.133 +                           final TypeSymbol tsym,
   1.134 +                           Env<AttrContext> env) {
   1.135 +        // Check that packages imported from exist (JLS ???).
   1.136 +        if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) {
   1.137 +            // If we can't find java.lang, exit immediately.
   1.138 +            if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) {
   1.139 +                JCDiagnostic msg = JCDiagnostic.fragment("fatal.err.no.java.lang");
   1.140 +                throw new FatalError(msg);
   1.141 +            } else {
   1.142 +                log.error(pos, "doesnt.exist", tsym);
   1.143 +            }
   1.144 +        }
   1.145 +        final Scope fromScope = tsym.members();
   1.146 +        final Scope toScope = env.toplevel.starImportScope;
   1.147 +        for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
   1.148 +            if (e.sym.kind == TYP && !toScope.includes(e.sym))
   1.149 +                toScope.enter(e.sym, fromScope);
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +    /** Import all static members of a class or package on demand.
   1.154 +     *  @param pos           Position to be used for error reporting.
   1.155 +     *  @param tsym          The class or package the members of which are imported.
   1.156 +     *  @param toScope   The (import) scope in which imported classes
   1.157 +     *               are entered.
   1.158 +     */
   1.159 +    private void importStaticAll(int pos,
   1.160 +                                 final TypeSymbol tsym,
   1.161 +                                 Env<AttrContext> env) {
   1.162 +        final JavaFileObject sourcefile = env.toplevel.sourcefile;
   1.163 +        final Scope toScope = env.toplevel.starImportScope;
   1.164 +        final PackageSymbol packge = env.toplevel.packge;
   1.165 +        final TypeSymbol origin = tsym;
   1.166 +
   1.167 +        // enter imported types immediately
   1.168 +        new Object() {
   1.169 +            Set<Symbol> processed = new HashSet<Symbol>();
   1.170 +            void importFrom(TypeSymbol tsym) {
   1.171 +                if (tsym == null || !processed.add(tsym))
   1.172 +                    return;
   1.173 +
   1.174 +                // also import inherited names
   1.175 +                importFrom(types.supertype(tsym.type).tsym);
   1.176 +                for (Type t : types.interfaces(tsym.type))
   1.177 +                    importFrom(t.tsym);
   1.178 +
   1.179 +                final Scope fromScope = tsym.members();
   1.180 +                for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
   1.181 +                    Symbol sym = e.sym;
   1.182 +                    if (sym.kind == TYP &&
   1.183 +                        (sym.flags() & STATIC) != 0 &&
   1.184 +                        staticImportAccessible(sym, packge) &&
   1.185 +                        sym.isMemberOf(origin, types) &&
   1.186 +                        !toScope.includes(sym))
   1.187 +                        toScope.enter(sym, fromScope, origin.members());
   1.188 +                }
   1.189 +            }
   1.190 +        }.importFrom(tsym);
   1.191 +
   1.192 +        // enter non-types before annotations that might use them
   1.193 +        annotate.earlier(new Annotate.Annotator() {
   1.194 +            Set<Symbol> processed = new HashSet<Symbol>();
   1.195 +
   1.196 +            public String toString() {
   1.197 +                return "import static " + tsym + ".*" + " in " + sourcefile;
   1.198 +            }
   1.199 +            void importFrom(TypeSymbol tsym) {
   1.200 +                if (tsym == null || !processed.add(tsym))
   1.201 +                    return;
   1.202 +
   1.203 +                // also import inherited names
   1.204 +                importFrom(types.supertype(tsym.type).tsym);
   1.205 +                for (Type t : types.interfaces(tsym.type))
   1.206 +                    importFrom(t.tsym);
   1.207 +
   1.208 +                final Scope fromScope = tsym.members();
   1.209 +                for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
   1.210 +                    Symbol sym = e.sym;
   1.211 +                    if (sym.isStatic() && sym.kind != TYP &&
   1.212 +                        staticImportAccessible(sym, packge) &&
   1.213 +                        !toScope.includes(sym) &&
   1.214 +                        sym.isMemberOf(origin, types)) {
   1.215 +                        toScope.enter(sym, fromScope, origin.members());
   1.216 +                    }
   1.217 +                }
   1.218 +            }
   1.219 +            public void enterAnnotation() {
   1.220 +                importFrom(tsym);
   1.221 +            }
   1.222 +        });
   1.223 +    }
   1.224 +
   1.225 +    // is the sym accessible everywhere in packge?
   1.226 +    boolean staticImportAccessible(Symbol sym, PackageSymbol packge) {
   1.227 +        int flags = (int)(sym.flags() & AccessFlags);
   1.228 +        switch (flags) {
   1.229 +        default:
   1.230 +        case PUBLIC:
   1.231 +            return true;
   1.232 +        case PRIVATE:
   1.233 +            return false;
   1.234 +        case 0:
   1.235 +        case PROTECTED:
   1.236 +            return sym.packge() == packge;
   1.237 +        }
   1.238 +    }
   1.239 +
   1.240 +    /** Import statics types of a given name.  Non-types are handled in Attr.
   1.241 +     *  @param pos           Position to be used for error reporting.
   1.242 +     *  @param tsym          The class from which the name is imported.
   1.243 +     *  @param name          The (simple) name being imported.
   1.244 +     *  @param env           The environment containing the named import
   1.245 +     *                  scope to add to.
   1.246 +     */
   1.247 +    private void importNamedStatic(final DiagnosticPosition pos,
   1.248 +                                   final TypeSymbol tsym,
   1.249 +                                   final Name name,
   1.250 +                                   final Env<AttrContext> env) {
   1.251 +        if (tsym.kind != TYP) {
   1.252 +            log.error(pos, "static.imp.only.classes.and.interfaces");
   1.253 +            return;
   1.254 +        }
   1.255 +
   1.256 +        final Scope toScope = env.toplevel.namedImportScope;
   1.257 +        final PackageSymbol packge = env.toplevel.packge;
   1.258 +        final TypeSymbol origin = tsym;
   1.259 +
   1.260 +        // enter imported types immediately
   1.261 +        new Object() {
   1.262 +            Set<Symbol> processed = new HashSet<Symbol>();
   1.263 +            void importFrom(TypeSymbol tsym) {
   1.264 +                if (tsym == null || !processed.add(tsym))
   1.265 +                    return;
   1.266 +
   1.267 +                // also import inherited names
   1.268 +                importFrom(types.supertype(tsym.type).tsym);
   1.269 +                for (Type t : types.interfaces(tsym.type))
   1.270 +                    importFrom(t.tsym);
   1.271 +
   1.272 +                for (Scope.Entry e = tsym.members().lookup(name);
   1.273 +                     e.scope != null;
   1.274 +                     e = e.next()) {
   1.275 +                    Symbol sym = e.sym;
   1.276 +                    if (sym.isStatic() &&
   1.277 +                        sym.kind == TYP &&
   1.278 +                        staticImportAccessible(sym, packge) &&
   1.279 +                        sym.isMemberOf(origin, types) &&
   1.280 +                        chk.checkUniqueStaticImport(pos, sym, toScope))
   1.281 +                        toScope.enter(sym, sym.owner.members(), origin.members());
   1.282 +                }
   1.283 +            }
   1.284 +        }.importFrom(tsym);
   1.285 +
   1.286 +        // enter non-types before annotations that might use them
   1.287 +        annotate.earlier(new Annotate.Annotator() {
   1.288 +            Set<Symbol> processed = new HashSet<Symbol>();
   1.289 +            boolean found = false;
   1.290 +
   1.291 +            public String toString() {
   1.292 +                return "import static " + tsym + "." + name;
   1.293 +            }
   1.294 +            void importFrom(TypeSymbol tsym) {
   1.295 +                if (tsym == null || !processed.add(tsym))
   1.296 +                    return;
   1.297 +
   1.298 +                // also import inherited names
   1.299 +                importFrom(types.supertype(tsym.type).tsym);
   1.300 +                for (Type t : types.interfaces(tsym.type))
   1.301 +                    importFrom(t.tsym);
   1.302 +
   1.303 +                for (Scope.Entry e = tsym.members().lookup(name);
   1.304 +                     e.scope != null;
   1.305 +                     e = e.next()) {
   1.306 +                    Symbol sym = e.sym;
   1.307 +                    if (sym.isStatic() &&
   1.308 +                        staticImportAccessible(sym, packge) &&
   1.309 +                        sym.isMemberOf(origin, types)) {
   1.310 +                        found = true;
   1.311 +                        if (sym.kind == MTH ||
   1.312 +                            sym.kind != TYP && chk.checkUniqueStaticImport(pos, sym, toScope))
   1.313 +                            toScope.enter(sym, sym.owner.members(), origin.members());
   1.314 +                    }
   1.315 +                }
   1.316 +            }
   1.317 +            public void enterAnnotation() {
   1.318 +                JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
   1.319 +                try {
   1.320 +                    importFrom(tsym);
   1.321 +                    if (!found) {
   1.322 +                        log.error(pos, "cant.resolve.location",
   1.323 +                                  JCDiagnostic.fragment("kindname.static"),
   1.324 +                                  name, "", "", Resolve.typeKindName(tsym.type),
   1.325 +                                  tsym.type);
   1.326 +                    }
   1.327 +                } finally {
   1.328 +                    log.useSource(prev);
   1.329 +                }
   1.330 +            }
   1.331 +        });
   1.332 +    }
   1.333 +    /** Import given class.
   1.334 +     *  @param pos           Position to be used for error reporting.
   1.335 +     *  @param tsym          The class to be imported.
   1.336 +     *  @param env           The environment containing the named import
   1.337 +     *                  scope to add to.
   1.338 +     */
   1.339 +    private void importNamed(DiagnosticPosition pos, Symbol tsym, Env<AttrContext> env) {
   1.340 +        if (tsym.kind == TYP &&
   1.341 +            chk.checkUniqueImport(pos, tsym, env.toplevel.namedImportScope))
   1.342 +            env.toplevel.namedImportScope.enter(tsym, tsym.owner.members());
   1.343 +    }
   1.344 +
   1.345 +    /** Construct method type from method signature.
   1.346 +     *  @param typarams    The method's type parameters.
   1.347 +     *  @param params      The method's value parameters.
   1.348 +     *  @param res             The method's result type,
   1.349 +     *                 null if it is a constructor.
   1.350 +     *  @param thrown      The method's thrown exceptions.
   1.351 +     *  @param env             The method's (local) environment.
   1.352 +     */
   1.353 +    Type signature(List<JCTypeParameter> typarams,
   1.354 +                   List<JCVariableDecl> params,
   1.355 +                   JCTree res,
   1.356 +                   List<JCExpression> thrown,
   1.357 +                   Env<AttrContext> env) {
   1.358 +
   1.359 +        // Enter and attribute type parameters.
   1.360 +        List<Type> tvars = enter.classEnter(typarams, env);
   1.361 +        attr.attribTypeVariables(typarams, env);
   1.362 +
   1.363 +        // Enter and attribute value parameters.
   1.364 +        ListBuffer<Type> argbuf = new ListBuffer<Type>();
   1.365 +        for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) {
   1.366 +            memberEnter(l.head, env);
   1.367 +            argbuf.append(l.head.vartype.type);
   1.368 +        }
   1.369 +
   1.370 +        // Attribute result type, if one is given.
   1.371 +        Type restype = res == null ? syms.voidType : attr.attribType(res, env);
   1.372 +
   1.373 +        // Attribute thrown exceptions.
   1.374 +        ListBuffer<Type> thrownbuf = new ListBuffer<Type>();
   1.375 +        for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) {
   1.376 +            Type exc = attr.attribType(l.head, env);
   1.377 +            if (exc.tag != TYPEVAR)
   1.378 +                exc = chk.checkClassType(l.head.pos(), exc);
   1.379 +            thrownbuf.append(exc);
   1.380 +        }
   1.381 +        Type mtype = new MethodType(argbuf.toList(),
   1.382 +                                    restype,
   1.383 +                                    thrownbuf.toList(),
   1.384 +                                    syms.methodClass);
   1.385 +        return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype);
   1.386 +    }
   1.387 +
   1.388 +/* ********************************************************************
   1.389 + * Visitor methods for member enter
   1.390 + *********************************************************************/
   1.391 +
   1.392 +    /** Visitor argument: the current environment
   1.393 +     */
   1.394 +    protected Env<AttrContext> env;
   1.395 +
   1.396 +    /** Enter field and method definitions and process import
   1.397 +     *  clauses, catching any completion failure exceptions.
   1.398 +     */
   1.399 +    protected void memberEnter(JCTree tree, Env<AttrContext> env) {
   1.400 +        Env<AttrContext> prevEnv = this.env;
   1.401 +        try {
   1.402 +            this.env = env;
   1.403 +            tree.accept(this);
   1.404 +        }  catch (CompletionFailure ex) {
   1.405 +            chk.completionError(tree.pos(), ex);
   1.406 +        } finally {
   1.407 +            this.env = prevEnv;
   1.408 +        }
   1.409 +    }
   1.410 +
   1.411 +    /** Enter members from a list of trees.
   1.412 +     */
   1.413 +    void memberEnter(List<? extends JCTree> trees, Env<AttrContext> env) {
   1.414 +        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
   1.415 +            memberEnter(l.head, env);
   1.416 +    }
   1.417 +
   1.418 +    /** Enter members for a class.
   1.419 +     */
   1.420 +    void finishClass(JCClassDecl tree, Env<AttrContext> env) {
   1.421 +        if ((tree.mods.flags & Flags.ENUM) != 0 &&
   1.422 +            (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
   1.423 +            addEnumMembers(tree, env);
   1.424 +        }
   1.425 +        memberEnter(tree.defs, env);
   1.426 +    }
   1.427 +
   1.428 +    /** Add the implicit members for an enum type
   1.429 +     *  to the symbol table.
   1.430 +     */
   1.431 +    private void addEnumMembers(JCClassDecl tree, Env<AttrContext> env) {
   1.432 +        JCExpression valuesType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass));
   1.433 +
   1.434 +        // public static T[] values() { return ???; }
   1.435 +        JCMethodDecl values = make.
   1.436 +            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
   1.437 +                      names.values,
   1.438 +                      valuesType,
   1.439 +                      List.<JCTypeParameter>nil(),
   1.440 +                      List.<JCVariableDecl>nil(),
   1.441 +                      List.<JCExpression>nil(), // thrown
   1.442 +                      null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
   1.443 +                      null);
   1.444 +        memberEnter(values, env);
   1.445 +
   1.446 +        // public static T valueOf(String name) { return ???; }
   1.447 +        JCMethodDecl valueOf = make.
   1.448 +            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
   1.449 +                      names.valueOf,
   1.450 +                      make.Type(tree.sym.type),
   1.451 +                      List.<JCTypeParameter>nil(),
   1.452 +                      List.of(make.VarDef(make.Modifiers(Flags.PARAMETER),
   1.453 +                                            names.fromString("name"),
   1.454 +                                            make.Type(syms.stringType), null)),
   1.455 +                      List.<JCExpression>nil(), // thrown
   1.456 +                      null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
   1.457 +                      null);
   1.458 +        memberEnter(valueOf, env);
   1.459 +
   1.460 +        // the remaining members are for bootstrapping only
   1.461 +        if (!target.compilerBootstrap(tree.sym)) return;
   1.462 +
   1.463 +        // public final int ordinal() { return ???; }
   1.464 +        JCMethodDecl ordinal = make.at(tree.pos).
   1.465 +            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL),
   1.466 +                      names.ordinal,
   1.467 +                      make.Type(syms.intType),
   1.468 +                      List.<JCTypeParameter>nil(),
   1.469 +                      List.<JCVariableDecl>nil(),
   1.470 +                      List.<JCExpression>nil(),
   1.471 +                      null,
   1.472 +                      null);
   1.473 +        memberEnter(ordinal, env);
   1.474 +
   1.475 +        // public final String name() { return ???; }
   1.476 +        JCMethodDecl name = make.
   1.477 +            MethodDef(make.Modifiers(Flags.PUBLIC|Flags.FINAL),
   1.478 +                      names._name,
   1.479 +                      make.Type(syms.stringType),
   1.480 +                      List.<JCTypeParameter>nil(),
   1.481 +                      List.<JCVariableDecl>nil(),
   1.482 +                      List.<JCExpression>nil(),
   1.483 +                      null,
   1.484 +                      null);
   1.485 +        memberEnter(name, env);
   1.486 +
   1.487 +        // public int compareTo(E other) { return ???; }
   1.488 +        MethodSymbol compareTo = new
   1.489 +            MethodSymbol(Flags.PUBLIC,
   1.490 +                         names.compareTo,
   1.491 +                         new MethodType(List.of(tree.sym.type),
   1.492 +                                        syms.intType,
   1.493 +                                        List.<Type>nil(),
   1.494 +                                        syms.methodClass),
   1.495 +                         tree.sym);
   1.496 +        memberEnter(make.MethodDef(compareTo, null), env);
   1.497 +    }
   1.498 +
   1.499 +    public void visitTopLevel(JCCompilationUnit tree) {
   1.500 +        if (tree.starImportScope.elems != null) {
   1.501 +            // we must have already processed this toplevel
   1.502 +            return;
   1.503 +        }
   1.504 +
   1.505 +        // check that no class exists with same fully qualified name as
   1.506 +        // toplevel package
   1.507 +        if (checkClash && tree.pid != null) {
   1.508 +            Symbol p = tree.packge;
   1.509 +            while (p.owner != syms.rootPackage) {
   1.510 +                p.owner.complete(); // enter all class members of p
   1.511 +                if (syms.classes.get(p.getQualifiedName()) != null) {
   1.512 +                    log.error(tree.pos,
   1.513 +                              "pkg.clashes.with.class.of.same.name",
   1.514 +                              p);
   1.515 +                }
   1.516 +                p = p.owner;
   1.517 +            }
   1.518 +        }
   1.519 +
   1.520 +        // process package annotations
   1.521 +        annotateLater(tree.packageAnnotations, env, tree.packge);
   1.522 +
   1.523 +        // Import-on-demand java.lang.
   1.524 +        importAll(tree.pos, reader.enterPackage(names.java_lang), env);
   1.525 +
   1.526 +        // Process all import clauses.
   1.527 +        memberEnter(tree.defs, env);
   1.528 +    }
   1.529 +
   1.530 +    // process the non-static imports and the static imports of types.
   1.531 +    public void visitImport(JCImport tree) {
   1.532 +        JCTree imp = tree.qualid;
   1.533 +        Name name = TreeInfo.name(imp);
   1.534 +        TypeSymbol p;
   1.535 +
   1.536 +        // Create a local environment pointing to this tree to disable
   1.537 +        // effects of other imports in Resolve.findGlobalType
   1.538 +        Env<AttrContext> localEnv = env.dup(tree);
   1.539 +
   1.540 +        // Attribute qualifying package or class.
   1.541 +        JCFieldAccess s = (JCFieldAccess) imp;
   1.542 +        p = attr.
   1.543 +            attribTree(s.selected,
   1.544 +                       localEnv,
   1.545 +                       tree.staticImport ? TYP : (TYP | PCK),
   1.546 +                       Type.noType).tsym;
   1.547 +        if (name == names.asterisk) {
   1.548 +            // Import on demand.
   1.549 +            chk.checkCanonical(s.selected);
   1.550 +            if (tree.staticImport)
   1.551 +                importStaticAll(tree.pos, p, env);
   1.552 +            else
   1.553 +                importAll(tree.pos, p, env);
   1.554 +        } else {
   1.555 +            // Named type import.
   1.556 +            if (tree.staticImport) {
   1.557 +                importNamedStatic(tree.pos(), p, name, localEnv);
   1.558 +                chk.checkCanonical(s.selected);
   1.559 +            } else {
   1.560 +                TypeSymbol c = attribImportType(imp, localEnv).tsym;
   1.561 +                chk.checkCanonical(imp);
   1.562 +                importNamed(tree.pos(), c, env);
   1.563 +            }
   1.564 +        }
   1.565 +    }
   1.566 +
   1.567 +    public void visitMethodDef(JCMethodDecl tree) {
   1.568 +        Scope enclScope = enter.enterScope(env);
   1.569 +        MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner);
   1.570 +        m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree);
   1.571 +        tree.sym = m;
   1.572 +        Env<AttrContext> localEnv = methodEnv(tree, env);
   1.573 +
   1.574 +        // Compute the method type
   1.575 +        m.type = signature(tree.typarams, tree.params,
   1.576 +                           tree.restype, tree.thrown,
   1.577 +                           localEnv);
   1.578 +
   1.579 +        // Set m.params
   1.580 +        ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>();
   1.581 +        JCVariableDecl lastParam = null;
   1.582 +        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
   1.583 +            JCVariableDecl param = lastParam = l.head;
   1.584 +            assert param.sym != null;
   1.585 +            params.append(param.sym);
   1.586 +        }
   1.587 +        m.params = params.toList();
   1.588 +
   1.589 +        // mark the method varargs, if necessary
   1.590 +        if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
   1.591 +            m.flags_field |= Flags.VARARGS;
   1.592 +
   1.593 +        localEnv.info.scope.leave();
   1.594 +        if (chk.checkUnique(tree.pos(), m, enclScope)) {
   1.595 +            enclScope.enter(m);
   1.596 +        }
   1.597 +        annotateLater(tree.mods.annotations, localEnv, m);
   1.598 +        if (tree.defaultValue != null)
   1.599 +            annotateDefaultValueLater(tree.defaultValue, localEnv, m);
   1.600 +    }
   1.601 +
   1.602 +    /** Create a fresh environment for method bodies.
   1.603 +     *  @param tree     The method definition.
   1.604 +     *  @param env      The environment current outside of the method definition.
   1.605 +     */
   1.606 +    Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) {
   1.607 +        Env<AttrContext> localEnv =
   1.608 +            env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
   1.609 +        localEnv.enclMethod = tree;
   1.610 +        localEnv.info.scope.owner = tree.sym;
   1.611 +        if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
   1.612 +        return localEnv;
   1.613 +    }
   1.614 +
   1.615 +    public void visitVarDef(JCVariableDecl tree) {
   1.616 +        Env<AttrContext> localEnv = env;
   1.617 +        if ((tree.mods.flags & STATIC) != 0 ||
   1.618 +            (env.info.scope.owner.flags() & INTERFACE) != 0) {
   1.619 +            localEnv = env.dup(tree, env.info.dup());
   1.620 +            localEnv.info.staticLevel++;
   1.621 +        }
   1.622 +        attr.attribType(tree.vartype, localEnv);
   1.623 +        Scope enclScope = enter.enterScope(env);
   1.624 +        VarSymbol v =
   1.625 +            new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
   1.626 +        v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
   1.627 +        tree.sym = v;
   1.628 +        if (tree.init != null) {
   1.629 +            v.flags_field |= HASINIT;
   1.630 +            if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS)
   1.631 +                v.setLazyConstValue(initEnv(tree, env), log, attr, tree.init);
   1.632 +        }
   1.633 +        if (chk.checkUnique(tree.pos(), v, enclScope)) {
   1.634 +            chk.checkTransparentVar(tree.pos(), v, enclScope);
   1.635 +            enclScope.enter(v);
   1.636 +        }
   1.637 +        annotateLater(tree.mods.annotations, localEnv, v);
   1.638 +        v.pos = tree.pos;
   1.639 +    }
   1.640 +
   1.641 +    /** Create a fresh environment for a variable's initializer.
   1.642 +     *  If the variable is a field, the owner of the environment's scope
   1.643 +     *  is be the variable itself, otherwise the owner is the method
   1.644 +     *  enclosing the variable definition.
   1.645 +     *
   1.646 +     *  @param tree     The variable definition.
   1.647 +     *  @param env      The environment current outside of the variable definition.
   1.648 +     */
   1.649 +    Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) {
   1.650 +        Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup()));
   1.651 +        if (tree.sym.owner.kind == TYP) {
   1.652 +            localEnv.info.scope = new Scope.DelegatedScope(env.info.scope);
   1.653 +            localEnv.info.scope.owner = tree.sym;
   1.654 +        }
   1.655 +        if ((tree.mods.flags & STATIC) != 0 ||
   1.656 +            (env.enclClass.sym.flags() & INTERFACE) != 0)
   1.657 +            localEnv.info.staticLevel++;
   1.658 +        return localEnv;
   1.659 +    }
   1.660 +
   1.661 +    /** Default member enter visitor method: do nothing
   1.662 +     */
   1.663 +    public void visitTree(JCTree tree) {
   1.664 +    }
   1.665 +
   1.666 +
   1.667 +    public void visitErroneous(JCErroneous tree) {
   1.668 +        memberEnter(tree.errs, env);
   1.669 +    }
   1.670 +
   1.671 +    public Env<AttrContext> getMethodEnv(JCMethodDecl tree, Env<AttrContext> env) {
   1.672 +        Env<AttrContext> mEnv = methodEnv(tree, env);
   1.673 +        mEnv.info.lint = mEnv.info.lint.augment(tree.sym.attributes_field, tree.sym.flags());
   1.674 +        for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail)
   1.675 +            mEnv.info.scope.enterIfAbsent(l.head.type.tsym);
   1.676 +        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail)
   1.677 +            mEnv.info.scope.enterIfAbsent(l.head.sym);
   1.678 +        return mEnv;
   1.679 +    }
   1.680 +
   1.681 +    public Env<AttrContext> getInitEnv(JCVariableDecl tree, Env<AttrContext> env) {
   1.682 +        Env<AttrContext> iEnv = initEnv(tree, env);
   1.683 +        return iEnv;
   1.684 +    }
   1.685 +
   1.686 +/* ********************************************************************
   1.687 + * Type completion
   1.688 + *********************************************************************/
   1.689 +
   1.690 +    Type attribImportType(JCTree tree, Env<AttrContext> env) {
   1.691 +        assert completionEnabled;
   1.692 +        try {
   1.693 +            // To prevent deep recursion, suppress completion of some
   1.694 +            // types.
   1.695 +            completionEnabled = false;
   1.696 +            return attr.attribType(tree, env);
   1.697 +        } finally {
   1.698 +            completionEnabled = true;
   1.699 +        }
   1.700 +    }
   1.701 +
   1.702 +/* ********************************************************************
   1.703 + * Annotation processing
   1.704 + *********************************************************************/
   1.705 +
   1.706 +    /** Queue annotations for later processing. */
   1.707 +    void annotateLater(final List<JCAnnotation> annotations,
   1.708 +                       final Env<AttrContext> localEnv,
   1.709 +                       final Symbol s) {
   1.710 +        if (annotations.isEmpty()) return;
   1.711 +        if (s.kind != PCK) s.attributes_field = null; // mark it incomplete for now
   1.712 +        annotate.later(new Annotate.Annotator() {
   1.713 +                public String toString() {
   1.714 +                    return "annotate " + annotations + " onto " + s + " in " + s.owner;
   1.715 +                }
   1.716 +                public void enterAnnotation() {
   1.717 +                    assert s.kind == PCK || s.attributes_field == null;
   1.718 +                    JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
   1.719 +                    try {
   1.720 +                        if (s.attributes_field != null &&
   1.721 +                            s.attributes_field.nonEmpty() &&
   1.722 +                            annotations.nonEmpty())
   1.723 +                            log.error(annotations.head.pos,
   1.724 +                                      "already.annotated",
   1.725 +                                      Resolve.kindName(s), s);
   1.726 +                        enterAnnotations(annotations, localEnv, s);
   1.727 +                    } finally {
   1.728 +                        log.useSource(prev);
   1.729 +                    }
   1.730 +                }
   1.731 +            });
   1.732 +    }
   1.733 +
   1.734 +    /**
   1.735 +     * Check if a list of annotations contains a reference to
   1.736 +     * java.lang.Deprecated.
   1.737 +     **/
   1.738 +    private boolean hasDeprecatedAnnotation(List<JCAnnotation> annotations) {
   1.739 +        for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
   1.740 +            JCAnnotation a = al.head;
   1.741 +            if (a.annotationType.type == syms.deprecatedType && a.args.isEmpty())
   1.742 +                return true;
   1.743 +        }
   1.744 +        return false;
   1.745 +    }
   1.746 +
   1.747 +
   1.748 +    /** Enter a set of annotations. */
   1.749 +    private void enterAnnotations(List<JCAnnotation> annotations,
   1.750 +                          Env<AttrContext> env,
   1.751 +                          Symbol s) {
   1.752 +        ListBuffer<Attribute.Compound> buf =
   1.753 +            new ListBuffer<Attribute.Compound>();
   1.754 +        Set<TypeSymbol> annotated = new HashSet<TypeSymbol>();
   1.755 +        if (!skipAnnotations)
   1.756 +        for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
   1.757 +            JCAnnotation a = al.head;
   1.758 +            Attribute.Compound c = annotate.enterAnnotation(a,
   1.759 +                                                            syms.annotationType,
   1.760 +                                                            env);
   1.761 +            if (c == null) continue;
   1.762 +            buf.append(c);
   1.763 +            // Note: @Deprecated has no effect on local variables and parameters
   1.764 +            if (!c.type.isErroneous()
   1.765 +                && s.owner.kind != MTH
   1.766 +                && types.isSameType(c.type, syms.deprecatedType))
   1.767 +                s.flags_field |= Flags.DEPRECATED;
   1.768 +            if (!annotated.add(a.type.tsym))
   1.769 +                log.error(a.pos, "duplicate.annotation");
   1.770 +        }
   1.771 +        s.attributes_field = buf.toList();
   1.772 +    }
   1.773 +
   1.774 +    /** Queue processing of an attribute default value. */
   1.775 +    void annotateDefaultValueLater(final JCExpression defaultValue,
   1.776 +                                   final Env<AttrContext> localEnv,
   1.777 +                                   final MethodSymbol m) {
   1.778 +        annotate.later(new Annotate.Annotator() {
   1.779 +                public String toString() {
   1.780 +                    return "annotate " + m.owner + "." +
   1.781 +                        m + " default " + defaultValue;
   1.782 +                }
   1.783 +                public void enterAnnotation() {
   1.784 +                    JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
   1.785 +                    try {
   1.786 +                        enterDefaultValue(defaultValue, localEnv, m);
   1.787 +                    } finally {
   1.788 +                        log.useSource(prev);
   1.789 +                    }
   1.790 +                }
   1.791 +            });
   1.792 +    }
   1.793 +
   1.794 +    /** Enter a default value for an attribute method. */
   1.795 +    private void enterDefaultValue(final JCExpression defaultValue,
   1.796 +                                   final Env<AttrContext> localEnv,
   1.797 +                                   final MethodSymbol m) {
   1.798 +        m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(),
   1.799 +                                                      defaultValue,
   1.800 +                                                      localEnv);
   1.801 +    }
   1.802 +
   1.803 +/* ********************************************************************
   1.804 + * Source completer
   1.805 + *********************************************************************/
   1.806 +
   1.807 +    /** Complete entering a class.
   1.808 +     *  @param sym         The symbol of the class to be completed.
   1.809 +     */
   1.810 +    public void complete(Symbol sym) throws CompletionFailure {
   1.811 +        // Suppress some (recursive) MemberEnter invocations
   1.812 +        if (!completionEnabled) {
   1.813 +            // Re-install same completer for next time around and return.
   1.814 +            assert (sym.flags() & Flags.COMPOUND) == 0;
   1.815 +            sym.completer = this;
   1.816 +            return;
   1.817 +        }
   1.818 +
   1.819 +        ClassSymbol c = (ClassSymbol)sym;
   1.820 +        ClassType ct = (ClassType)c.type;
   1.821 +        Env<AttrContext> env = enter.typeEnvs.get(c);
   1.822 +        JCClassDecl tree = (JCClassDecl)env.tree;
   1.823 +        boolean wasFirst = isFirst;
   1.824 +        isFirst = false;
   1.825 +
   1.826 +        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
   1.827 +        try {
   1.828 +            // Save class environment for later member enter (2) processing.
   1.829 +            halfcompleted.append(env);
   1.830 +
   1.831 +            // If this is a toplevel-class, make sure any preceding import
   1.832 +            // clauses have been seen.
   1.833 +            if (c.owner.kind == PCK) {
   1.834 +                memberEnter(env.toplevel, env.enclosing(JCTree.TOPLEVEL));
   1.835 +                todo.append(env);
   1.836 +            }
   1.837 +
   1.838 +            // Mark class as not yet attributed.
   1.839 +            c.flags_field |= UNATTRIBUTED;
   1.840 +
   1.841 +            if (c.owner.kind == TYP)
   1.842 +                c.owner.complete();
   1.843 +
   1.844 +            // create an environment for evaluating the base clauses
   1.845 +            Env<AttrContext> baseEnv = baseEnv(tree, env);
   1.846 +
   1.847 +            // Determine supertype.
   1.848 +            Type supertype =
   1.849 +                (tree.extending != null)
   1.850 +                ? attr.attribBase(tree.extending, baseEnv, true, false, true)
   1.851 +                : ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c))
   1.852 +                ? attr.attribBase(enumBase(tree.pos, c), baseEnv,
   1.853 +                                  true, false, false)
   1.854 +                : (c.fullname == names.java_lang_Object)
   1.855 +                ? Type.noType
   1.856 +                : syms.objectType;
   1.857 +            ct.supertype_field = supertype;
   1.858 +
   1.859 +            // Determine interfaces.
   1.860 +            ListBuffer<Type> interfaces = new ListBuffer<Type>();
   1.861 +            Set<Type> interfaceSet = new HashSet<Type>();
   1.862 +            List<JCExpression> interfaceTrees = tree.implementing;
   1.863 +            if ((tree.mods.flags & Flags.ENUM) != 0 && target.compilerBootstrap(c)) {
   1.864 +                // add interface Comparable<T>
   1.865 +                interfaceTrees =
   1.866 +                    interfaceTrees.prepend(make.Type(new ClassType(syms.comparableType.getEnclosingType(),
   1.867 +                                                                   List.of(c.type),
   1.868 +                                                                   syms.comparableType.tsym)));
   1.869 +                // add interface Serializable
   1.870 +                interfaceTrees =
   1.871 +                    interfaceTrees.prepend(make.Type(syms.serializableType));
   1.872 +            }
   1.873 +            for (JCExpression iface : interfaceTrees) {
   1.874 +                Type i = attr.attribBase(iface, baseEnv, false, true, true);
   1.875 +                if (i.tag == CLASS) {
   1.876 +                    interfaces.append(i);
   1.877 +                    chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet);
   1.878 +                }
   1.879 +            }
   1.880 +            if ((c.flags_field & ANNOTATION) != 0)
   1.881 +                ct.interfaces_field = List.of(syms.annotationType);
   1.882 +            else
   1.883 +                ct.interfaces_field = interfaces.toList();
   1.884 +
   1.885 +            if (c.fullname == names.java_lang_Object) {
   1.886 +                if (tree.extending != null) {
   1.887 +                    chk.checkNonCyclic(tree.extending.pos(),
   1.888 +                                       supertype);
   1.889 +                    ct.supertype_field = Type.noType;
   1.890 +                }
   1.891 +                else if (tree.implementing.nonEmpty()) {
   1.892 +                    chk.checkNonCyclic(tree.implementing.head.pos(),
   1.893 +                                       ct.interfaces_field.head);
   1.894 +                    ct.interfaces_field = List.nil();
   1.895 +                }
   1.896 +            }
   1.897 +
   1.898 +            // Annotations.
   1.899 +            // In general, we cannot fully process annotations yet,  but we
   1.900 +            // can attribute the annotation types and then check to see if the
   1.901 +            // @Deprecated annotation is present.
   1.902 +            attr.attribAnnotationTypes(tree.mods.annotations, baseEnv);
   1.903 +            if (hasDeprecatedAnnotation(tree.mods.annotations))
   1.904 +                c.flags_field |= DEPRECATED;
   1.905 +            annotateLater(tree.mods.annotations, baseEnv, c);
   1.906 +
   1.907 +            attr.attribTypeVariables(tree.typarams, baseEnv);
   1.908 +
   1.909 +            chk.checkNonCyclic(tree.pos(), c.type);
   1.910 +
   1.911 +            // Add default constructor if needed.
   1.912 +            if ((c.flags() & INTERFACE) == 0 &&
   1.913 +                !TreeInfo.hasConstructors(tree.defs)) {
   1.914 +                List<Type> argtypes = List.nil();
   1.915 +                List<Type> typarams = List.nil();
   1.916 +                List<Type> thrown = List.nil();
   1.917 +                long ctorFlags = 0;
   1.918 +                boolean based = false;
   1.919 +                if (c.name.len == 0) {
   1.920 +                    JCNewClass nc = (JCNewClass)env.next.tree;
   1.921 +                    if (nc.constructor != null) {
   1.922 +                        Type superConstrType = types.memberType(c.type,
   1.923 +                                                                nc.constructor);
   1.924 +                        argtypes = superConstrType.getParameterTypes();
   1.925 +                        typarams = superConstrType.getTypeArguments();
   1.926 +                        ctorFlags = nc.constructor.flags() & VARARGS;
   1.927 +                        if (nc.encl != null) {
   1.928 +                            argtypes = argtypes.prepend(nc.encl.type);
   1.929 +                            based = true;
   1.930 +                        }
   1.931 +                        thrown = superConstrType.getThrownTypes();
   1.932 +                    }
   1.933 +                }
   1.934 +                JCTree constrDef = DefaultConstructor(make.at(tree.pos), c,
   1.935 +                                                    typarams, argtypes, thrown,
   1.936 +                                                    ctorFlags, based);
   1.937 +                tree.defs = tree.defs.prepend(constrDef);
   1.938 +            }
   1.939 +
   1.940 +            // If this is a class, enter symbols for this and super into
   1.941 +            // current scope.
   1.942 +            if ((c.flags_field & INTERFACE) == 0) {
   1.943 +                VarSymbol thisSym =
   1.944 +                    new VarSymbol(FINAL | HASINIT, names._this, c.type, c);
   1.945 +                thisSym.pos = Position.FIRSTPOS;
   1.946 +                env.info.scope.enter(thisSym);
   1.947 +                if (ct.supertype_field.tag == CLASS) {
   1.948 +                    VarSymbol superSym =
   1.949 +                        new VarSymbol(FINAL | HASINIT, names._super,
   1.950 +                                      ct.supertype_field, c);
   1.951 +                    superSym.pos = Position.FIRSTPOS;
   1.952 +                    env.info.scope.enter(superSym);
   1.953 +                }
   1.954 +            }
   1.955 +
   1.956 +            // check that no package exists with same fully qualified name,
   1.957 +            // but admit classes in the unnamed package which have the same
   1.958 +            // name as a top-level package.
   1.959 +            if (checkClash &&
   1.960 +                c.owner.kind == PCK && c.owner != syms.unnamedPackage &&
   1.961 +                reader.packageExists(c.fullname))
   1.962 +                {
   1.963 +                    log.error(tree.pos, "clash.with.pkg.of.same.name", c);
   1.964 +                }
   1.965 +
   1.966 +        } catch (CompletionFailure ex) {
   1.967 +            chk.completionError(tree.pos(), ex);
   1.968 +        } finally {
   1.969 +            log.useSource(prev);
   1.970 +        }
   1.971 +
   1.972 +        // Enter all member fields and methods of a set of half completed
   1.973 +        // classes in a second phase.
   1.974 +        if (wasFirst) {
   1.975 +            try {
   1.976 +                while (halfcompleted.nonEmpty()) {
   1.977 +                    finish(halfcompleted.next());
   1.978 +                }
   1.979 +            } finally {
   1.980 +                isFirst = true;
   1.981 +            }
   1.982 +
   1.983 +            // commit pending annotations
   1.984 +            annotate.flush();
   1.985 +        }
   1.986 +    }
   1.987 +
   1.988 +    private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
   1.989 +        Scope typaramScope = new Scope(tree.sym);
   1.990 +        if (tree.typarams != null)
   1.991 +            for (List<JCTypeParameter> typarams = tree.typarams;
   1.992 +                 typarams.nonEmpty();
   1.993 +                 typarams = typarams.tail)
   1.994 +                typaramScope.enter(typarams.head.type.tsym);
   1.995 +        Env<AttrContext> outer = env.outer; // the base clause can't see members of this class
   1.996 +        Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(typaramScope));
   1.997 +        localEnv.baseClause = true;
   1.998 +        localEnv.outer = outer;
   1.999 +        localEnv.info.isSelfCall = false;
  1.1000 +        return localEnv;
  1.1001 +    }
  1.1002 +
  1.1003 +    /** Enter member fields and methods of a class
  1.1004 +     *  @param env        the environment current for the class block.
  1.1005 +     */
  1.1006 +    private void finish(Env<AttrContext> env) {
  1.1007 +        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
  1.1008 +        try {
  1.1009 +            JCClassDecl tree = (JCClassDecl)env.tree;
  1.1010 +            finishClass(tree, env);
  1.1011 +        } finally {
  1.1012 +            log.useSource(prev);
  1.1013 +        }
  1.1014 +    }
  1.1015 +
  1.1016 +    /** Generate a base clause for an enum type.
  1.1017 +     *  @param pos              The position for trees and diagnostics, if any
  1.1018 +     *  @param c                The class symbol of the enum
  1.1019 +     */
  1.1020 +    private JCExpression enumBase(int pos, ClassSymbol c) {
  1.1021 +        JCExpression result = make.at(pos).
  1.1022 +            TypeApply(make.QualIdent(syms.enumSym),
  1.1023 +                      List.<JCExpression>of(make.Type(c.type)));
  1.1024 +        return result;
  1.1025 +    }
  1.1026 +
  1.1027 +/* ***************************************************************************
  1.1028 + * tree building
  1.1029 + ****************************************************************************/
  1.1030 +
  1.1031 +    /** Generate default constructor for given class. For classes different
  1.1032 +     *  from java.lang.Object, this is:
  1.1033 +     *
  1.1034 +     *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
  1.1035 +     *      super(x_0, ..., x_n)
  1.1036 +     *    }
  1.1037 +     *
  1.1038 +     *  or, if based == true:
  1.1039 +     *
  1.1040 +     *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
  1.1041 +     *      x_0.super(x_1, ..., x_n)
  1.1042 +     *    }
  1.1043 +     *
  1.1044 +     *  @param make     The tree factory.
  1.1045 +     *  @param c        The class owning the default constructor.
  1.1046 +     *  @param argtypes The parameter types of the constructor.
  1.1047 +     *  @param thrown   The thrown exceptions of the constructor.
  1.1048 +     *  @param based    Is first parameter a this$n?
  1.1049 +     */
  1.1050 +    JCTree DefaultConstructor(TreeMaker make,
  1.1051 +                            ClassSymbol c,
  1.1052 +                            List<Type> typarams,
  1.1053 +                            List<Type> argtypes,
  1.1054 +                            List<Type> thrown,
  1.1055 +                            long flags,
  1.1056 +                            boolean based) {
  1.1057 +        List<JCVariableDecl> params = make.Params(argtypes, syms.noSymbol);
  1.1058 +        List<JCStatement> stats = List.nil();
  1.1059 +        if (c.type != syms.objectType)
  1.1060 +            stats = stats.prepend(SuperCall(make, typarams, params, based));
  1.1061 +        if ((c.flags() & ENUM) != 0 &&
  1.1062 +            (types.supertype(c.type).tsym == syms.enumSym ||
  1.1063 +             target.compilerBootstrap(c))) {
  1.1064 +            // constructors of true enums are private
  1.1065 +            flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR;
  1.1066 +        } else
  1.1067 +            flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR;
  1.1068 +        if (c.name.len == 0) flags |= ANONCONSTR;
  1.1069 +        JCTree result = make.MethodDef(
  1.1070 +            make.Modifiers(flags),
  1.1071 +            names.init,
  1.1072 +            null,
  1.1073 +            make.TypeParams(typarams),
  1.1074 +            params,
  1.1075 +            make.Types(thrown),
  1.1076 +            make.Block(0, stats),
  1.1077 +            null);
  1.1078 +        return result;
  1.1079 +    }
  1.1080 +
  1.1081 +    /** Generate call to superclass constructor. This is:
  1.1082 +     *
  1.1083 +     *    super(id_0, ..., id_n)
  1.1084 +     *
  1.1085 +     * or, if based == true
  1.1086 +     *
  1.1087 +     *    id_0.super(id_1,...,id_n)
  1.1088 +     *
  1.1089 +     *  where id_0, ..., id_n are the names of the given parameters.
  1.1090 +     *
  1.1091 +     *  @param make    The tree factory
  1.1092 +     *  @param params  The parameters that need to be passed to super
  1.1093 +     *  @param typarams  The type parameters that need to be passed to super
  1.1094 +     *  @param based   Is first parameter a this$n?
  1.1095 +     */
  1.1096 +    JCExpressionStatement SuperCall(TreeMaker make,
  1.1097 +                   List<Type> typarams,
  1.1098 +                   List<JCVariableDecl> params,
  1.1099 +                   boolean based) {
  1.1100 +        JCExpression meth;
  1.1101 +        if (based) {
  1.1102 +            meth = make.Select(make.Ident(params.head), names._super);
  1.1103 +            params = params.tail;
  1.1104 +        } else {
  1.1105 +            meth = make.Ident(names._super);
  1.1106 +        }
  1.1107 +        List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null;
  1.1108 +        return make.Exec(make.Apply(typeargs, meth, make.Idents(params)));
  1.1109 +    }
  1.1110 +}

mercurial