author | jlahoda |
Wed, 03 Dec 2014 13:46:12 +0100 | |
changeset 27857 | 7e913a535736 |
parent 27224 | 228abfa87080 |
child 28589 | cbf8df194957 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22163
diff
changeset
|
2 |
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
import javax.tools.JavaFileObject; |
|
30 |
import javax.tools.JavaFileManager; |
|
31 |
||
32 |
import com.sun.tools.javac.code.*; |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
33 |
import com.sun.tools.javac.code.Kinds.KindSelector; |
7615 | 34 |
import com.sun.tools.javac.code.Scope.*; |
35 |
import com.sun.tools.javac.code.Symbol.*; |
|
36 |
import com.sun.tools.javac.code.Type.*; |
|
10 | 37 |
import com.sun.tools.javac.jvm.*; |
11314 | 38 |
import com.sun.tools.javac.main.Option.PkgInfo; |
10 | 39 |
import com.sun.tools.javac.tree.*; |
7615 | 40 |
import com.sun.tools.javac.tree.JCTree.*; |
10 | 41 |
import com.sun.tools.javac.util.*; |
42 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
43 |
import com.sun.tools.javac.util.List; |
|
44 |
||
45 |
||
46 |
import static com.sun.tools.javac.code.Flags.*; |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
47 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
10 | 48 |
|
49 |
/** This class enters symbols for all encountered definitions into |
|
27857 | 50 |
* the symbol table. The pass consists of high-level two phases, |
51 |
* organized as follows: |
|
10 | 52 |
* |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
13844
diff
changeset
|
53 |
* <p>In the first phase, all class symbols are entered into their |
10 | 54 |
* enclosing scope, descending recursively down the tree for classes |
55 |
* which are members of other classes. The class symbols are given a |
|
27857 | 56 |
* TypeEnter object as completer. |
10 | 57 |
* |
58 |
* <p>In the second phase classes are completed using |
|
27857 | 59 |
* TypeEnter.complete(). Completion might occur on demand, but |
10 | 60 |
* any classes that are not completed that way will be eventually |
27857 | 61 |
* completed by processing the `uncompleted' queue. Completion |
62 |
* entails determination of a class's parameters, supertype and |
|
63 |
* interfaces, as well as entering all symbols defined in the |
|
10 | 64 |
* class into its scope, with the exception of class symbols which |
27857 | 65 |
* have been entered in phase 1. |
10 | 66 |
* |
67 |
* <p>Whereas the first phase is organized as a sweep through all |
|
27857 | 68 |
* compiled syntax trees, the second phase is on-demand. Members of a |
10 | 69 |
* class are entered when the contents of a class are first |
70 |
* accessed. This is accomplished by installing completer objects in |
|
27857 | 71 |
* class symbols for compiled classes which invoke the type-enter |
10 | 72 |
* phase for the corresponding class tree. |
73 |
* |
|
74 |
* <p>Classes migrate from one phase to the next via queues: |
|
75 |
* |
|
13844 | 76 |
* <pre>{@literal |
27857 | 77 |
* class enter -> (Enter.uncompleted) --> type enter |
10 | 78 |
* -> (Todo) --> attribute |
79 |
* (only for toplevel classes) |
|
13844 | 80 |
* }</pre> |
10 | 81 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
82 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
83 |
* If you write code that depends on this, you do so at your own risk. |
10 | 84 |
* This code and its internal interfaces are subject to change or |
85 |
* deletion without notice.</b> |
|
86 |
*/ |
|
87 |
public class Enter extends JCTree.Visitor { |
|
22163 | 88 |
protected static final Context.Key<Enter> enterKey = new Context.Key<>(); |
10 | 89 |
|
90 |
Log log; |
|
91 |
Symtab syms; |
|
92 |
Check chk; |
|
93 |
TreeMaker make; |
|
94 |
Annotate annotate; |
|
27857 | 95 |
TypeEnter typeEnter; |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
96 |
Types types; |
10 | 97 |
Lint lint; |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
98 |
Names names; |
10 | 99 |
JavaFileManager fileManager; |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
100 |
PkgInfo pkginfoOpt; |
25007 | 101 |
TypeEnvs typeEnvs; |
10 | 102 |
|
103 |
private final Todo todo; |
|
104 |
||
105 |
public static Enter instance(Context context) { |
|
106 |
Enter instance = context.get(enterKey); |
|
107 |
if (instance == null) |
|
108 |
instance = new Enter(context); |
|
109 |
return instance; |
|
110 |
} |
|
111 |
||
112 |
protected Enter(Context context) { |
|
113 |
context.put(enterKey, this); |
|
114 |
||
115 |
log = Log.instance(context); |
|
116 |
make = TreeMaker.instance(context); |
|
117 |
syms = Symtab.instance(context); |
|
118 |
chk = Check.instance(context); |
|
27857 | 119 |
typeEnter = TypeEnter.instance(context); |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
120 |
types = Types.instance(context); |
10 | 121 |
annotate = Annotate.instance(context); |
122 |
lint = Lint.instance(context); |
|
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
123 |
names = Names.instance(context); |
10 | 124 |
|
125 |
predefClassDef = make.ClassDef( |
|
126 |
make.Modifiers(PUBLIC), |
|
15354 | 127 |
syms.predefClass.name, |
128 |
List.<JCTypeParameter>nil(), |
|
129 |
null, |
|
130 |
List.<JCExpression>nil(), |
|
131 |
List.<JCTree>nil()); |
|
10 | 132 |
predefClassDef.sym = syms.predefClass; |
133 |
todo = Todo.instance(context); |
|
134 |
fileManager = context.get(JavaFileManager.class); |
|
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
135 |
|
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
136 |
Options options = Options.instance(context); |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
137 |
pkginfoOpt = PkgInfo.get(options); |
25007 | 138 |
typeEnvs = TypeEnvs.instance(context); |
10 | 139 |
} |
140 |
||
141 |
/** Accessor for typeEnvs |
|
142 |
*/ |
|
143 |
public Env<AttrContext> getEnv(TypeSymbol sym) { |
|
144 |
return typeEnvs.get(sym); |
|
145 |
} |
|
146 |
||
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
147 |
public Iterable<Env<AttrContext>> getEnvs() { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
148 |
return typeEnvs.values(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
149 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
150 |
|
10 | 151 |
public Env<AttrContext> getClassEnv(TypeSymbol sym) { |
152 |
Env<AttrContext> localEnv = getEnv(sym); |
|
153 |
Env<AttrContext> lintEnv = localEnv; |
|
154 |
while (lintEnv.info.lint == null) |
|
155 |
lintEnv = lintEnv.next; |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
15354
diff
changeset
|
156 |
localEnv.info.lint = lintEnv.info.lint.augment(sym); |
10 | 157 |
return localEnv; |
158 |
} |
|
159 |
||
160 |
/** The queue of all classes that might still need to be completed; |
|
161 |
* saved and initialized by main(). |
|
162 |
*/ |
|
163 |
ListBuffer<ClassSymbol> uncompleted; |
|
164 |
||
165 |
/** A dummy class to serve as enclClass for toplevel environments. |
|
166 |
*/ |
|
167 |
private JCClassDecl predefClassDef; |
|
168 |
||
169 |
/* ************************************************************************ |
|
170 |
* environment construction |
|
171 |
*************************************************************************/ |
|
172 |
||
173 |
||
174 |
/** Create a fresh environment for class bodies. |
|
175 |
* This will create a fresh scope for local symbols of a class, referred |
|
176 |
* to by the environments info.scope field. |
|
177 |
* This scope will contain |
|
178 |
* - symbols for this and super |
|
179 |
* - symbols for any type parameters |
|
180 |
* In addition, it serves as an anchor for scopes of methods and initializers |
|
181 |
* which are nested in this scope via Scope.dup(). |
|
182 |
* This scope should not be confused with the members scope of a class. |
|
183 |
* |
|
184 |
* @param tree The class definition. |
|
185 |
* @param env The environment current outside of the class definition. |
|
186 |
*/ |
|
187 |
public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) { |
|
188 |
Env<AttrContext> localEnv = |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
189 |
env.dup(tree, env.info.dup(WriteableScope.create(tree.sym))); |
10 | 190 |
localEnv.enclClass = tree; |
191 |
localEnv.outer = env; |
|
192 |
localEnv.info.isSelfCall = false; |
|
193 |
localEnv.info.lint = null; // leave this to be filled in by Attr, |
|
194 |
// when annotations have been processed |
|
195 |
return localEnv; |
|
196 |
} |
|
197 |
||
198 |
/** Create a fresh environment for toplevels. |
|
199 |
* @param tree The toplevel tree. |
|
200 |
*/ |
|
201 |
Env<AttrContext> topLevelEnv(JCCompilationUnit tree) { |
|
22163 | 202 |
Env<AttrContext> localEnv = new Env<>(tree, new AttrContext()); |
10 | 203 |
localEnv.toplevel = tree; |
204 |
localEnv.enclClass = predefClassDef; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
205 |
tree.toplevelScope = WriteableScope.create(tree.packge); |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
206 |
tree.namedImportScope = new NamedImportScope(tree.packge, tree.toplevelScope); |
7615 | 207 |
tree.starImportScope = new StarImportScope(tree.packge); |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
208 |
localEnv.info.scope = tree.toplevelScope; |
10 | 209 |
localEnv.info.lint = lint; |
210 |
return localEnv; |
|
211 |
} |
|
212 |
||
213 |
public Env<AttrContext> getTopLevelEnv(JCCompilationUnit tree) { |
|
22163 | 214 |
Env<AttrContext> localEnv = new Env<>(tree, new AttrContext()); |
10 | 215 |
localEnv.toplevel = tree; |
216 |
localEnv.enclClass = predefClassDef; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
217 |
localEnv.info.scope = tree.toplevelScope; |
10 | 218 |
localEnv.info.lint = lint; |
219 |
return localEnv; |
|
220 |
} |
|
221 |
||
222 |
/** The scope in which a member definition in environment env is to be entered |
|
223 |
* This is usually the environment's scope, except for class environments, |
|
224 |
* where the local scope is for type variables, and the this and super symbol |
|
225 |
* only, and members go into the class member scope. |
|
226 |
*/ |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
227 |
WriteableScope enterScope(Env<AttrContext> env) { |
10950 | 228 |
return (env.tree.hasTag(JCTree.Tag.CLASSDEF)) |
10 | 229 |
? ((JCClassDecl) env.tree).sym.members_field |
230 |
: env.info.scope; |
|
231 |
} |
|
232 |
||
233 |
/* ************************************************************************ |
|
234 |
* Visitor methods for phase 1: class enter |
|
235 |
*************************************************************************/ |
|
236 |
||
237 |
/** Visitor argument: the current environment. |
|
238 |
*/ |
|
239 |
protected Env<AttrContext> env; |
|
240 |
||
241 |
/** Visitor result: the computed type. |
|
242 |
*/ |
|
243 |
Type result; |
|
244 |
||
245 |
/** Visitor method: enter all classes in given tree, catching any |
|
246 |
* completion failure exceptions. Return the tree's type. |
|
247 |
* |
|
248 |
* @param tree The tree to be visited. |
|
249 |
* @param env The environment visitor argument. |
|
250 |
*/ |
|
251 |
Type classEnter(JCTree tree, Env<AttrContext> env) { |
|
252 |
Env<AttrContext> prevEnv = this.env; |
|
253 |
try { |
|
254 |
this.env = env; |
|
255 |
tree.accept(this); |
|
256 |
return result; |
|
257 |
} catch (CompletionFailure ex) { |
|
258 |
return chk.completionError(tree.pos(), ex); |
|
259 |
} finally { |
|
260 |
this.env = prevEnv; |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
/** Visitor method: enter classes of a list of trees, returning a list of types. |
|
265 |
*/ |
|
266 |
<T extends JCTree> List<Type> classEnter(List<T> trees, Env<AttrContext> env) { |
|
22163 | 267 |
ListBuffer<Type> ts = new ListBuffer<>(); |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
268 |
for (List<T> l = trees; l.nonEmpty(); l = l.tail) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
269 |
Type t = classEnter(l.head, env); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
270 |
if (t != null) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
271 |
ts.append(t); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
272 |
} |
10 | 273 |
return ts.toList(); |
274 |
} |
|
275 |
||
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
276 |
@Override |
10 | 277 |
public void visitTopLevel(JCCompilationUnit tree) { |
278 |
JavaFileObject prev = log.useSource(tree.sourcefile); |
|
279 |
boolean addEnv = false; |
|
280 |
boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info", |
|
281 |
JavaFileObject.Kind.SOURCE); |
|
24069 | 282 |
JCPackageDecl pd = tree.getPackage(); |
283 |
if (pd != null) { |
|
284 |
tree.packge = pd.packge = syms.enterPackage(TreeInfo.fullName(pd.pid)); |
|
285 |
if ( pd.annotations.nonEmpty() |
|
286 |
|| pkginfoOpt == PkgInfo.ALWAYS |
|
287 |
|| tree.docComments != null) { |
|
10 | 288 |
if (isPkgInfo) { |
289 |
addEnv = true; |
|
24069 | 290 |
} else if (pd.annotations.nonEmpty()) { |
291 |
log.error(pd.annotations.head.pos(), |
|
10 | 292 |
"pkg.annotations.sb.in.package-info.java"); |
293 |
} |
|
294 |
} |
|
295 |
} else { |
|
296 |
tree.packge = syms.unnamedPackage; |
|
297 |
} |
|
298 |
tree.packge.complete(); // Find all classes in package. |
|
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
299 |
Env<AttrContext> topEnv = topLevelEnv(tree); |
24069 | 300 |
Env<AttrContext> packageEnv = isPkgInfo ? topEnv.dup(pd) : null; |
10 | 301 |
|
302 |
// Save environment of package-info.java file. |
|
303 |
if (isPkgInfo) { |
|
304 |
Env<AttrContext> env0 = typeEnvs.get(tree.packge); |
|
24069 | 305 |
if (env0 != null) { |
10 | 306 |
JCCompilationUnit tree0 = env0.toplevel; |
307 |
if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) { |
|
24069 | 308 |
log.warning(pd != null ? pd.pid.pos() : null, |
10 | 309 |
"pkg-info.already.seen", |
310 |
tree.packge); |
|
311 |
} |
|
312 |
} |
|
24069 | 313 |
typeEnvs.put(tree.packge, packageEnv); |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
314 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
315 |
for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner) |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
316 |
q.flags_field |= EXISTS; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
317 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
318 |
Name name = names.package_info; |
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22163
diff
changeset
|
319 |
ClassSymbol c = syms.enterClass(name, tree.packge); |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
320 |
c.flatname = names.fromString(tree.packge + "." + name); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
321 |
c.sourcefile = tree.sourcefile; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
322 |
c.completer = null; |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
323 |
c.members_field = WriteableScope.create(c); |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
324 |
tree.packge.package_info = c; |
10 | 325 |
} |
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
326 |
classEnter(tree.defs, topEnv); |
10 | 327 |
if (addEnv) { |
24069 | 328 |
todo.append(packageEnv); |
10 | 329 |
} |
330 |
log.useSource(prev); |
|
331 |
result = null; |
|
332 |
} |
|
333 |
||
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
334 |
@Override |
10 | 335 |
public void visitClassDef(JCClassDecl tree) { |
336 |
Symbol owner = env.info.scope.owner; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
337 |
WriteableScope enclScope = enterScope(env); |
10 | 338 |
ClassSymbol c; |
339 |
if (owner.kind == PCK) { |
|
340 |
// We are seeing a toplevel class. |
|
341 |
PackageSymbol packge = (PackageSymbol)owner; |
|
342 |
for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner) |
|
343 |
q.flags_field |= EXISTS; |
|
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22163
diff
changeset
|
344 |
c = syms.enterClass(tree.name, packge); |
10 | 345 |
packge.members().enterIfAbsent(c); |
346 |
if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env)) { |
|
347 |
log.error(tree.pos(), |
|
348 |
"class.public.should.be.in.file", tree.name); |
|
349 |
} |
|
350 |
} else { |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
351 |
if (!tree.name.isEmpty() && |
10 | 352 |
!chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) { |
353 |
result = null; |
|
354 |
return; |
|
355 |
} |
|
356 |
if (owner.kind == TYP) { |
|
357 |
// We are seeing a member class. |
|
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22163
diff
changeset
|
358 |
c = syms.enterClass(tree.name, (TypeSymbol)owner); |
10 | 359 |
if ((owner.flags_field & INTERFACE) != 0) { |
360 |
tree.mods.flags |= PUBLIC | STATIC; |
|
361 |
} |
|
362 |
} else { |
|
363 |
// We are seeing a local class. |
|
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22163
diff
changeset
|
364 |
c = syms.defineClass(tree.name, owner); |
10 | 365 |
c.flatname = chk.localClassName(c); |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
366 |
if (!c.name.isEmpty()) |
10 | 367 |
chk.checkTransparentClass(tree.pos(), c, env.info.scope); |
368 |
} |
|
369 |
} |
|
370 |
tree.sym = c; |
|
371 |
||
372 |
// Enter class into `compiled' table and enclosing scope. |
|
373 |
if (chk.compiled.get(c.flatname) != null) { |
|
374 |
duplicateClass(tree.pos(), c); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
375 |
result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType); |
10 | 376 |
tree.sym = (ClassSymbol)result.tsym; |
377 |
return; |
|
378 |
} |
|
379 |
chk.compiled.put(c.flatname, c); |
|
380 |
enclScope.enter(c); |
|
381 |
||
382 |
// Set up an environment for class block and store in `typeEnvs' |
|
383 |
// table, to be retrieved later in memberEnter and attribution. |
|
384 |
Env<AttrContext> localEnv = classEnv(tree, env); |
|
385 |
typeEnvs.put(c, localEnv); |
|
386 |
||
387 |
// Fill out class fields. |
|
27857 | 388 |
c.completer = typeEnter; |
10 | 389 |
c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree); |
390 |
c.sourcefile = env.toplevel.sourcefile; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
391 |
c.members_field = WriteableScope.create(c); |
10 | 392 |
|
393 |
ClassType ct = (ClassType)c.type; |
|
394 |
if (owner.kind != PCK && (c.flags_field & STATIC) == 0) { |
|
395 |
// We are seeing a local or inner class. |
|
396 |
// Set outer_field of this class to closest enclosing class |
|
397 |
// which contains this class in a non-static context |
|
398 |
// (its "enclosing instance class"), provided such a class exists. |
|
399 |
Symbol owner1 = owner; |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
400 |
while (owner1.kind.matches(KindSelector.VAL_MTH) && |
10 | 401 |
(owner1.flags_field & STATIC) == 0) { |
402 |
owner1 = owner1.owner; |
|
403 |
} |
|
404 |
if (owner1.kind == TYP) { |
|
405 |
ct.setEnclosingType(owner1.type); |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
// Enter type parameters. |
|
410 |
ct.typarams_field = classEnter(tree.typarams, localEnv); |
|
411 |
||
412 |
// Add non-local class to uncompleted, to make sure it will be |
|
413 |
// completed later. |
|
414 |
if (!c.isLocal() && uncompleted != null) uncompleted.append(c); |
|
415 |
// System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG |
|
416 |
||
417 |
// Recursively enter all member classes. |
|
418 |
classEnter(tree.defs, localEnv); |
|
419 |
||
420 |
result = c.type; |
|
421 |
} |
|
422 |
//where |
|
423 |
/** Does class have the same name as the file it appears in? |
|
424 |
*/ |
|
425 |
private static boolean classNameMatchesFileName(ClassSymbol c, |
|
426 |
Env<AttrContext> env) { |
|
427 |
return env.toplevel.sourcefile.isNameCompatible(c.name.toString(), |
|
428 |
JavaFileObject.Kind.SOURCE); |
|
429 |
} |
|
430 |
||
431 |
/** Complain about a duplicate class. */ |
|
432 |
protected void duplicateClass(DiagnosticPosition pos, ClassSymbol c) { |
|
433 |
log.error(pos, "duplicate.class", c.fullname); |
|
434 |
} |
|
435 |
||
436 |
/** Class enter visitor method for type parameters. |
|
437 |
* Enter a symbol for type parameter in local scope, after checking that it |
|
438 |
* is unique. |
|
439 |
*/ |
|
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
440 |
@Override |
10 | 441 |
public void visitTypeParameter(JCTypeParameter tree) { |
442 |
TypeVar a = (tree.type != null) |
|
443 |
? (TypeVar)tree.type |
|
444 |
: new TypeVar(tree.name, env.info.scope.owner, syms.botType); |
|
445 |
tree.type = a; |
|
446 |
if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) { |
|
447 |
env.info.scope.enter(a.tsym); |
|
448 |
} |
|
449 |
result = a; |
|
450 |
} |
|
451 |
||
452 |
/** Default class enter visitor method: do nothing. |
|
453 |
*/ |
|
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
454 |
@Override |
10 | 455 |
public void visitTree(JCTree tree) { |
456 |
result = null; |
|
457 |
} |
|
458 |
||
459 |
/** Main method: enter all classes in a list of toplevel trees. |
|
460 |
* @param trees The list of trees to be processed. |
|
461 |
*/ |
|
462 |
public void main(List<JCCompilationUnit> trees) { |
|
463 |
complete(trees, null); |
|
464 |
} |
|
465 |
||
27857 | 466 |
/** Main method: enter classes from the list of toplevel trees, possibly |
467 |
* skipping TypeEnter for all but 'c' by placing them on the uncompleted |
|
468 |
* list. |
|
10 | 469 |
* @param trees The list of trees to be processed. |
27857 | 470 |
* @param c The class symbol to be processed or null to process all. |
10 | 471 |
*/ |
472 |
public void complete(List<JCCompilationUnit> trees, ClassSymbol c) { |
|
473 |
annotate.enterStart(); |
|
474 |
ListBuffer<ClassSymbol> prevUncompleted = uncompleted; |
|
27857 | 475 |
if (typeEnter.completionEnabled) uncompleted = new ListBuffer<>(); |
10 | 476 |
|
477 |
try { |
|
478 |
// enter all classes, and construct uncompleted list |
|
479 |
classEnter(trees, null); |
|
480 |
||
481 |
// complete all uncompleted classes in memberEnter |
|
27857 | 482 |
if (typeEnter.completionEnabled) { |
10 | 483 |
while (uncompleted.nonEmpty()) { |
484 |
ClassSymbol clazz = uncompleted.next(); |
|
485 |
if (c == null || c == clazz || prevUncompleted == null) |
|
486 |
clazz.complete(); |
|
487 |
else |
|
488 |
// defer |
|
489 |
prevUncompleted.append(clazz); |
|
490 |
} |
|
491 |
||
27857 | 492 |
typeEnter.ensureImportsChecked(trees); |
10 | 493 |
} |
494 |
} finally { |
|
495 |
uncompleted = prevUncompleted; |
|
496 |
annotate.enterDone(); |
|
497 |
} |
|
498 |
} |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
499 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
500 |
public void newRound() { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
501 |
typeEnvs.clear(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
502 |
} |
10 | 503 |
} |