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