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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2425
76b61848c9a4
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.comp;
aoqi@0 27
aoqi@0 28 import java.util.*;
aoqi@0 29 import javax.tools.JavaFileObject;
aoqi@0 30 import javax.tools.JavaFileManager;
aoqi@0 31
aoqi@0 32 import com.sun.tools.javac.code.*;
aoqi@0 33 import com.sun.tools.javac.code.Scope.*;
aoqi@0 34 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 35 import com.sun.tools.javac.code.Type.*;
aoqi@0 36 import com.sun.tools.javac.jvm.*;
aoqi@0 37 import com.sun.tools.javac.main.Option.PkgInfo;
aoqi@0 38 import com.sun.tools.javac.tree.*;
aoqi@0 39 import com.sun.tools.javac.tree.JCTree.*;
aoqi@0 40 import com.sun.tools.javac.util.*;
aoqi@0 41 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
aoqi@0 42 import com.sun.tools.javac.util.List;
aoqi@0 43
aoqi@0 44
aoqi@0 45 import static com.sun.tools.javac.code.Flags.*;
aoqi@0 46 import static com.sun.tools.javac.code.Kinds.*;
aoqi@0 47
aoqi@0 48 /** This class enters symbols for all encountered definitions into
aoqi@0 49 * the symbol table. The pass consists of two phases, organized as
aoqi@0 50 * follows:
aoqi@0 51 *
aoqi@0 52 * <p>In the first phase, all class symbols are entered into their
aoqi@0 53 * enclosing scope, descending recursively down the tree for classes
aoqi@0 54 * which are members of other classes. The class symbols are given a
aoqi@0 55 * MemberEnter object as completer.
aoqi@0 56 *
aoqi@0 57 * <p>In the second phase classes are completed using
aoqi@0 58 * MemberEnter.complete(). Completion might occur on demand, but
aoqi@0 59 * any classes that are not completed that way will be eventually
aoqi@0 60 * completed by processing the `uncompleted' queue. Completion
aoqi@0 61 * entails (1) determination of a class's parameters, supertype and
aoqi@0 62 * interfaces, as well as (2) entering all symbols defined in the
aoqi@0 63 * class into its scope, with the exception of class symbols which
aoqi@0 64 * have been entered in phase 1. (2) depends on (1) having been
aoqi@0 65 * completed for a class and all its superclasses and enclosing
aoqi@0 66 * classes. That's why, after doing (1), we put classes in a
aoqi@0 67 * `halfcompleted' queue. Only when we have performed (1) for a class
aoqi@0 68 * and all it's superclasses and enclosing classes, we proceed to
aoqi@0 69 * (2).
aoqi@0 70 *
aoqi@0 71 * <p>Whereas the first phase is organized as a sweep through all
aoqi@0 72 * compiled syntax trees, the second phase is demand. Members of a
aoqi@0 73 * class are entered when the contents of a class are first
aoqi@0 74 * accessed. This is accomplished by installing completer objects in
aoqi@0 75 * class symbols for compiled classes which invoke the member-enter
aoqi@0 76 * phase for the corresponding class tree.
aoqi@0 77 *
aoqi@0 78 * <p>Classes migrate from one phase to the next via queues:
aoqi@0 79 *
aoqi@0 80 * <pre>{@literal
aoqi@0 81 * class enter -> (Enter.uncompleted) --> member enter (1)
aoqi@0 82 * -> (MemberEnter.halfcompleted) --> member enter (2)
aoqi@0 83 * -> (Todo) --> attribute
aoqi@0 84 * (only for toplevel classes)
aoqi@0 85 * }</pre>
aoqi@0 86 *
aoqi@0 87 * <p><b>This is NOT part of any supported API.
aoqi@0 88 * If you write code that depends on this, you do so at your own risk.
aoqi@0 89 * This code and its internal interfaces are subject to change or
aoqi@0 90 * deletion without notice.</b>
aoqi@0 91 */
aoqi@0 92 public class Enter extends JCTree.Visitor {
aoqi@0 93 protected static final Context.Key<Enter> enterKey =
aoqi@0 94 new Context.Key<Enter>();
aoqi@0 95
aoqi@0 96 Log log;
aoqi@0 97 Symtab syms;
aoqi@0 98 Check chk;
aoqi@0 99 TreeMaker make;
aoqi@0 100 ClassReader reader;
aoqi@0 101 Annotate annotate;
aoqi@0 102 MemberEnter memberEnter;
aoqi@0 103 Types types;
aoqi@0 104 Lint lint;
aoqi@0 105 Names names;
aoqi@0 106 JavaFileManager fileManager;
aoqi@0 107 PkgInfo pkginfoOpt;
aoqi@0 108 TypeEnvs typeEnvs;
aoqi@0 109
aoqi@0 110 private final Todo todo;
aoqi@0 111
aoqi@0 112 public static Enter instance(Context context) {
aoqi@0 113 Enter instance = context.get(enterKey);
aoqi@0 114 if (instance == null)
aoqi@0 115 instance = new Enter(context);
aoqi@0 116 return instance;
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 protected Enter(Context context) {
aoqi@0 120 context.put(enterKey, this);
aoqi@0 121
aoqi@0 122 log = Log.instance(context);
aoqi@0 123 reader = ClassReader.instance(context);
aoqi@0 124 make = TreeMaker.instance(context);
aoqi@0 125 syms = Symtab.instance(context);
aoqi@0 126 chk = Check.instance(context);
aoqi@0 127 memberEnter = MemberEnter.instance(context);
aoqi@0 128 types = Types.instance(context);
aoqi@0 129 annotate = Annotate.instance(context);
aoqi@0 130 lint = Lint.instance(context);
aoqi@0 131 names = Names.instance(context);
aoqi@0 132
aoqi@0 133 predefClassDef = make.ClassDef(
aoqi@0 134 make.Modifiers(PUBLIC),
aoqi@0 135 syms.predefClass.name,
aoqi@0 136 List.<JCTypeParameter>nil(),
aoqi@0 137 null,
aoqi@0 138 List.<JCExpression>nil(),
aoqi@0 139 List.<JCTree>nil());
aoqi@0 140 predefClassDef.sym = syms.predefClass;
aoqi@0 141 todo = Todo.instance(context);
aoqi@0 142 fileManager = context.get(JavaFileManager.class);
aoqi@0 143
aoqi@0 144 Options options = Options.instance(context);
aoqi@0 145 pkginfoOpt = PkgInfo.get(options);
aoqi@0 146 typeEnvs = TypeEnvs.instance(context);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 /** Accessor for typeEnvs
aoqi@0 150 */
aoqi@0 151 public Env<AttrContext> getEnv(TypeSymbol sym) {
aoqi@0 152 return typeEnvs.get(sym);
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 public Env<AttrContext> getClassEnv(TypeSymbol sym) {
aoqi@0 156 Env<AttrContext> localEnv = getEnv(sym);
aoqi@0 157 Env<AttrContext> lintEnv = localEnv;
aoqi@0 158 while (lintEnv.info.lint == null)
aoqi@0 159 lintEnv = lintEnv.next;
aoqi@0 160 localEnv.info.lint = lintEnv.info.lint.augment(sym);
aoqi@0 161 return localEnv;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 /** The queue of all classes that might still need to be completed;
aoqi@0 165 * saved and initialized by main().
aoqi@0 166 */
aoqi@0 167 ListBuffer<ClassSymbol> uncompleted;
aoqi@0 168
aoqi@0 169 /** A dummy class to serve as enclClass for toplevel environments.
aoqi@0 170 */
aoqi@0 171 private JCClassDecl predefClassDef;
aoqi@0 172
aoqi@0 173 /* ************************************************************************
aoqi@0 174 * environment construction
aoqi@0 175 *************************************************************************/
aoqi@0 176
aoqi@0 177
aoqi@0 178 /** Create a fresh environment for class bodies.
aoqi@0 179 * This will create a fresh scope for local symbols of a class, referred
aoqi@0 180 * to by the environments info.scope field.
aoqi@0 181 * This scope will contain
aoqi@0 182 * - symbols for this and super
aoqi@0 183 * - symbols for any type parameters
aoqi@0 184 * In addition, it serves as an anchor for scopes of methods and initializers
aoqi@0 185 * which are nested in this scope via Scope.dup().
aoqi@0 186 * This scope should not be confused with the members scope of a class.
aoqi@0 187 *
aoqi@0 188 * @param tree The class definition.
aoqi@0 189 * @param env The environment current outside of the class definition.
aoqi@0 190 */
aoqi@0 191 public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) {
aoqi@0 192 Env<AttrContext> localEnv =
aoqi@0 193 env.dup(tree, env.info.dup(new Scope(tree.sym)));
aoqi@0 194 localEnv.enclClass = tree;
aoqi@0 195 localEnv.outer = env;
aoqi@0 196 localEnv.info.isSelfCall = false;
aoqi@0 197 localEnv.info.lint = null; // leave this to be filled in by Attr,
aoqi@0 198 // when annotations have been processed
aoqi@0 199 return localEnv;
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 /** Create a fresh environment for toplevels.
aoqi@0 203 * @param tree The toplevel tree.
aoqi@0 204 */
aoqi@0 205 Env<AttrContext> topLevelEnv(JCCompilationUnit tree) {
aoqi@0 206 Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext());
aoqi@0 207 localEnv.toplevel = tree;
aoqi@0 208 localEnv.enclClass = predefClassDef;
aoqi@0 209 tree.namedImportScope = new ImportScope(tree.packge);
aoqi@0 210 tree.starImportScope = new StarImportScope(tree.packge);
aoqi@0 211 localEnv.info.scope = tree.namedImportScope;
aoqi@0 212 localEnv.info.lint = lint;
aoqi@0 213 return localEnv;
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 public Env<AttrContext> getTopLevelEnv(JCCompilationUnit tree) {
aoqi@0 217 Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext());
aoqi@0 218 localEnv.toplevel = tree;
aoqi@0 219 localEnv.enclClass = predefClassDef;
aoqi@0 220 localEnv.info.scope = tree.namedImportScope;
aoqi@0 221 localEnv.info.lint = lint;
aoqi@0 222 return localEnv;
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 /** The scope in which a member definition in environment env is to be entered
aoqi@0 226 * This is usually the environment's scope, except for class environments,
aoqi@0 227 * where the local scope is for type variables, and the this and super symbol
aoqi@0 228 * only, and members go into the class member scope.
aoqi@0 229 */
aoqi@0 230 Scope enterScope(Env<AttrContext> env) {
aoqi@0 231 return (env.tree.hasTag(JCTree.Tag.CLASSDEF))
aoqi@0 232 ? ((JCClassDecl) env.tree).sym.members_field
aoqi@0 233 : env.info.scope;
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 /* ************************************************************************
aoqi@0 237 * Visitor methods for phase 1: class enter
aoqi@0 238 *************************************************************************/
aoqi@0 239
aoqi@0 240 /** Visitor argument: the current environment.
aoqi@0 241 */
aoqi@0 242 protected Env<AttrContext> env;
aoqi@0 243
aoqi@0 244 /** Visitor result: the computed type.
aoqi@0 245 */
aoqi@0 246 Type result;
aoqi@0 247
aoqi@0 248 /** Visitor method: enter all classes in given tree, catching any
aoqi@0 249 * completion failure exceptions. Return the tree's type.
aoqi@0 250 *
aoqi@0 251 * @param tree The tree to be visited.
aoqi@0 252 * @param env The environment visitor argument.
aoqi@0 253 */
aoqi@0 254 Type classEnter(JCTree tree, Env<AttrContext> env) {
aoqi@0 255 Env<AttrContext> prevEnv = this.env;
aoqi@0 256 try {
aoqi@0 257 this.env = env;
aoqi@0 258 tree.accept(this);
aoqi@0 259 return result;
aoqi@0 260 } catch (CompletionFailure ex) {
aoqi@0 261 return chk.completionError(tree.pos(), ex);
aoqi@0 262 } finally {
aoqi@0 263 this.env = prevEnv;
aoqi@0 264 }
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 /** Visitor method: enter classes of a list of trees, returning a list of types.
aoqi@0 268 */
aoqi@0 269 <T extends JCTree> List<Type> classEnter(List<T> trees, Env<AttrContext> env) {
aoqi@0 270 ListBuffer<Type> ts = new ListBuffer<Type>();
aoqi@0 271 for (List<T> l = trees; l.nonEmpty(); l = l.tail) {
aoqi@0 272 Type t = classEnter(l.head, env);
aoqi@0 273 if (t != null)
aoqi@0 274 ts.append(t);
aoqi@0 275 }
aoqi@0 276 return ts.toList();
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 @Override
aoqi@0 280 public void visitTopLevel(JCCompilationUnit tree) {
aoqi@0 281 JavaFileObject prev = log.useSource(tree.sourcefile);
aoqi@0 282 boolean addEnv = false;
aoqi@0 283 boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info",
aoqi@0 284 JavaFileObject.Kind.SOURCE);
aoqi@0 285 if (tree.pid != null) {
aoqi@0 286 tree.packge = reader.enterPackage(TreeInfo.fullName(tree.pid));
aoqi@0 287 if (tree.packageAnnotations.nonEmpty()
aoqi@0 288 || pkginfoOpt == PkgInfo.ALWAYS
aoqi@0 289 || tree.docComments != null) {
aoqi@0 290 if (isPkgInfo) {
aoqi@0 291 addEnv = true;
aoqi@0 292 } else if (tree.packageAnnotations.nonEmpty()){
aoqi@0 293 log.error(tree.packageAnnotations.head.pos(),
aoqi@0 294 "pkg.annotations.sb.in.package-info.java");
aoqi@0 295 }
aoqi@0 296 }
aoqi@0 297 } else {
aoqi@0 298 tree.packge = syms.unnamedPackage;
aoqi@0 299 }
aoqi@0 300 tree.packge.complete(); // Find all classes in package.
aoqi@0 301 Env<AttrContext> topEnv = topLevelEnv(tree);
aoqi@0 302
aoqi@0 303 // Save environment of package-info.java file.
aoqi@0 304 if (isPkgInfo) {
aoqi@0 305 Env<AttrContext> env0 = typeEnvs.get(tree.packge);
aoqi@0 306 if (env0 == null) {
aoqi@0 307 typeEnvs.put(tree.packge, topEnv);
aoqi@0 308 } else {
aoqi@0 309 JCCompilationUnit tree0 = env0.toplevel;
aoqi@0 310 if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) {
aoqi@0 311 log.warning(tree.pid != null ? tree.pid.pos()
aoqi@0 312 : null,
aoqi@0 313 "pkg-info.already.seen",
aoqi@0 314 tree.packge);
aoqi@0 315 if (addEnv || (tree0.packageAnnotations.isEmpty() &&
aoqi@0 316 tree.docComments != null &&
aoqi@0 317 tree.docComments.hasComment(tree))) {
aoqi@0 318 typeEnvs.put(tree.packge, topEnv);
aoqi@0 319 }
aoqi@0 320 }
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner)
aoqi@0 324 q.flags_field |= EXISTS;
aoqi@0 325
aoqi@0 326 Name name = names.package_info;
aoqi@0 327 ClassSymbol c = reader.enterClass(name, tree.packge);
aoqi@0 328 c.flatname = names.fromString(tree.packge + "." + name);
aoqi@0 329 c.sourcefile = tree.sourcefile;
aoqi@0 330 c.completer = null;
aoqi@0 331 c.members_field = new Scope(c);
aoqi@0 332 tree.packge.package_info = c;
aoqi@0 333 }
aoqi@0 334 classEnter(tree.defs, topEnv);
aoqi@0 335 if (addEnv) {
aoqi@0 336 todo.append(topEnv);
aoqi@0 337 }
aoqi@0 338 log.useSource(prev);
aoqi@0 339 result = null;
aoqi@0 340 }
aoqi@0 341
aoqi@0 342 @Override
aoqi@0 343 public void visitClassDef(JCClassDecl tree) {
aoqi@0 344 Symbol owner = env.info.scope.owner;
aoqi@0 345 Scope enclScope = enterScope(env);
aoqi@0 346 ClassSymbol c;
aoqi@0 347 if (owner.kind == PCK) {
aoqi@0 348 // We are seeing a toplevel class.
aoqi@0 349 PackageSymbol packge = (PackageSymbol)owner;
aoqi@0 350 for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner)
aoqi@0 351 q.flags_field |= EXISTS;
aoqi@0 352 c = reader.enterClass(tree.name, packge);
aoqi@0 353 packge.members().enterIfAbsent(c);
aoqi@0 354 if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env)) {
aoqi@0 355 log.error(tree.pos(),
aoqi@0 356 "class.public.should.be.in.file", tree.name);
aoqi@0 357 }
aoqi@0 358 } else {
aoqi@0 359 if (!tree.name.isEmpty() &&
aoqi@0 360 !chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) {
aoqi@0 361 result = null;
aoqi@0 362 return;
aoqi@0 363 }
aoqi@0 364 if (owner.kind == TYP) {
aoqi@0 365 // We are seeing a member class.
aoqi@0 366 c = reader.enterClass(tree.name, (TypeSymbol)owner);
aoqi@0 367 if ((owner.flags_field & INTERFACE) != 0) {
aoqi@0 368 tree.mods.flags |= PUBLIC | STATIC;
aoqi@0 369 }
aoqi@0 370 } else {
aoqi@0 371 // We are seeing a local class.
aoqi@0 372 c = reader.defineClass(tree.name, owner);
aoqi@0 373 c.flatname = chk.localClassName(c);
aoqi@0 374 if (!c.name.isEmpty())
aoqi@0 375 chk.checkTransparentClass(tree.pos(), c, env.info.scope);
aoqi@0 376 }
aoqi@0 377 }
aoqi@0 378 tree.sym = c;
aoqi@0 379
aoqi@0 380 // Enter class into `compiled' table and enclosing scope.
aoqi@0 381 if (chk.compiled.get(c.flatname) != null) {
aoqi@0 382 duplicateClass(tree.pos(), c);
aoqi@0 383 result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType);
aoqi@0 384 tree.sym = (ClassSymbol)result.tsym;
aoqi@0 385 return;
aoqi@0 386 }
aoqi@0 387 chk.compiled.put(c.flatname, c);
aoqi@0 388 enclScope.enter(c);
aoqi@0 389
aoqi@0 390 // Set up an environment for class block and store in `typeEnvs'
aoqi@0 391 // table, to be retrieved later in memberEnter and attribution.
aoqi@0 392 Env<AttrContext> localEnv = classEnv(tree, env);
aoqi@0 393 typeEnvs.put(c, localEnv);
aoqi@0 394
aoqi@0 395 // Fill out class fields.
aoqi@0 396 c.completer = memberEnter;
aoqi@0 397 c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree);
aoqi@0 398 c.sourcefile = env.toplevel.sourcefile;
aoqi@0 399 c.members_field = new Scope(c);
aoqi@0 400
aoqi@0 401 ClassType ct = (ClassType)c.type;
aoqi@0 402 if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
aoqi@0 403 // We are seeing a local or inner class.
aoqi@0 404 // Set outer_field of this class to closest enclosing class
aoqi@0 405 // which contains this class in a non-static context
aoqi@0 406 // (its "enclosing instance class"), provided such a class exists.
aoqi@0 407 Symbol owner1 = owner;
aoqi@0 408 while ((owner1.kind & (VAR | MTH)) != 0 &&
aoqi@0 409 (owner1.flags_field & STATIC) == 0) {
aoqi@0 410 owner1 = owner1.owner;
aoqi@0 411 }
aoqi@0 412 if (owner1.kind == TYP) {
aoqi@0 413 ct.setEnclosingType(owner1.type);
aoqi@0 414 }
aoqi@0 415 }
aoqi@0 416
aoqi@0 417 // Enter type parameters.
aoqi@0 418 ct.typarams_field = classEnter(tree.typarams, localEnv);
aoqi@0 419
aoqi@0 420 // Add non-local class to uncompleted, to make sure it will be
aoqi@0 421 // completed later.
aoqi@0 422 if (!c.isLocal() && uncompleted != null) uncompleted.append(c);
aoqi@0 423 // System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
aoqi@0 424
aoqi@0 425 // Recursively enter all member classes.
aoqi@0 426 classEnter(tree.defs, localEnv);
aoqi@0 427
aoqi@0 428 result = c.type;
aoqi@0 429 }
aoqi@0 430 //where
aoqi@0 431 /** Does class have the same name as the file it appears in?
aoqi@0 432 */
aoqi@0 433 private static boolean classNameMatchesFileName(ClassSymbol c,
aoqi@0 434 Env<AttrContext> env) {
aoqi@0 435 return env.toplevel.sourcefile.isNameCompatible(c.name.toString(),
aoqi@0 436 JavaFileObject.Kind.SOURCE);
aoqi@0 437 }
aoqi@0 438
aoqi@0 439 /** Complain about a duplicate class. */
aoqi@0 440 protected void duplicateClass(DiagnosticPosition pos, ClassSymbol c) {
aoqi@0 441 log.error(pos, "duplicate.class", c.fullname);
aoqi@0 442 }
aoqi@0 443
aoqi@0 444 /** Class enter visitor method for type parameters.
aoqi@0 445 * Enter a symbol for type parameter in local scope, after checking that it
aoqi@0 446 * is unique.
aoqi@0 447 */
aoqi@0 448 @Override
aoqi@0 449 public void visitTypeParameter(JCTypeParameter tree) {
aoqi@0 450 TypeVar a = (tree.type != null)
aoqi@0 451 ? (TypeVar)tree.type
aoqi@0 452 : new TypeVar(tree.name, env.info.scope.owner, syms.botType);
aoqi@0 453 tree.type = a;
aoqi@0 454 if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) {
aoqi@0 455 env.info.scope.enter(a.tsym);
aoqi@0 456 }
aoqi@0 457 result = a;
aoqi@0 458 }
aoqi@0 459
aoqi@0 460 /** Default class enter visitor method: do nothing.
aoqi@0 461 */
aoqi@0 462 @Override
aoqi@0 463 public void visitTree(JCTree tree) {
aoqi@0 464 result = null;
aoqi@0 465 }
aoqi@0 466
aoqi@0 467 /** Main method: enter all classes in a list of toplevel trees.
aoqi@0 468 * @param trees The list of trees to be processed.
aoqi@0 469 */
aoqi@0 470 public void main(List<JCCompilationUnit> trees) {
aoqi@0 471 complete(trees, null);
aoqi@0 472 }
aoqi@0 473
aoqi@0 474 /** Main method: enter one class from a list of toplevel trees and
aoqi@0 475 * place the rest on uncompleted for later processing.
aoqi@0 476 * @param trees The list of trees to be processed.
aoqi@0 477 * @param c The class symbol to be processed.
aoqi@0 478 */
aoqi@0 479 public void complete(List<JCCompilationUnit> trees, ClassSymbol c) {
aoqi@0 480 annotate.enterStart();
aoqi@0 481 ListBuffer<ClassSymbol> prevUncompleted = uncompleted;
aoqi@0 482 if (memberEnter.completionEnabled) uncompleted = new ListBuffer<ClassSymbol>();
aoqi@0 483
aoqi@0 484 try {
aoqi@0 485 // enter all classes, and construct uncompleted list
aoqi@0 486 classEnter(trees, null);
aoqi@0 487
aoqi@0 488 // complete all uncompleted classes in memberEnter
aoqi@0 489 if (memberEnter.completionEnabled) {
aoqi@0 490 while (uncompleted.nonEmpty()) {
aoqi@0 491 ClassSymbol clazz = uncompleted.next();
aoqi@0 492 if (c == null || c == clazz || prevUncompleted == null)
aoqi@0 493 clazz.complete();
aoqi@0 494 else
aoqi@0 495 // defer
aoqi@0 496 prevUncompleted.append(clazz);
aoqi@0 497 }
aoqi@0 498
aoqi@0 499 // if there remain any unimported toplevels (these must have
aoqi@0 500 // no classes at all), process their import statements as well.
aoqi@0 501 for (JCCompilationUnit tree : trees) {
aoqi@0 502 if (tree.starImportScope.elems == null) {
aoqi@0 503 JavaFileObject prev = log.useSource(tree.sourcefile);
aoqi@0 504 Env<AttrContext> topEnv = topLevelEnv(tree);
aoqi@0 505 memberEnter.memberEnter(tree, topEnv);
aoqi@0 506 log.useSource(prev);
aoqi@0 507 }
aoqi@0 508 }
aoqi@0 509 }
aoqi@0 510 } finally {
aoqi@0 511 uncompleted = prevUncompleted;
aoqi@0 512 annotate.enterDone();
aoqi@0 513 }
aoqi@0 514 }
aoqi@0 515 }

mercurial