author | mcimadamore |
Fri, 16 Dec 2016 15:27:34 +0000 | |
changeset 42827 | 36468b5fa7f4 |
parent 42411 | 2433ceacb13e |
child 42828 | cce89649f958 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
36526 | 2 |
* Copyright (c) 1999, 2016, 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 |
||
36526 | 28 |
import java.util.Map; |
29 |
import java.util.Optional; |
|
30 |
||
10 | 31 |
import javax.tools.JavaFileObject; |
32 |
import javax.tools.JavaFileManager; |
|
33 |
||
34 |
import com.sun.tools.javac.code.*; |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
35 |
import com.sun.tools.javac.code.Kinds.KindSelector; |
7615 | 36 |
import com.sun.tools.javac.code.Scope.*; |
37 |
import com.sun.tools.javac.code.Symbol.*; |
|
38 |
import com.sun.tools.javac.code.Type.*; |
|
11314 | 39 |
import com.sun.tools.javac.main.Option.PkgInfo; |
36526 | 40 |
import com.sun.tools.javac.resources.CompilerProperties.Errors; |
10 | 41 |
import com.sun.tools.javac.tree.*; |
7615 | 42 |
import com.sun.tools.javac.tree.JCTree.*; |
10 | 43 |
import com.sun.tools.javac.util.*; |
44 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
45 |
import com.sun.tools.javac.util.List; |
|
46 |
||
47 |
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
|
48 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
10 | 49 |
|
50 |
/** This class enters symbols for all encountered definitions into |
|
27857 | 51 |
* the symbol table. The pass consists of high-level two phases, |
52 |
* organized as follows: |
|
10 | 53 |
* |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
13844
diff
changeset
|
54 |
* <p>In the first phase, all class symbols are entered into their |
10 | 55 |
* enclosing scope, descending recursively down the tree for classes |
56 |
* which are members of other classes. The class symbols are given a |
|
27857 | 57 |
* TypeEnter object as completer. |
10 | 58 |
* |
59 |
* <p>In the second phase classes are completed using |
|
27857 | 60 |
* TypeEnter.complete(). Completion might occur on demand, but |
10 | 61 |
* any classes that are not completed that way will be eventually |
27857 | 62 |
* completed by processing the `uncompleted' queue. Completion |
63 |
* entails determination of a class's parameters, supertype and |
|
64 |
* interfaces, as well as entering all symbols defined in the |
|
10 | 65 |
* class into its scope, with the exception of class symbols which |
27857 | 66 |
* have been entered in phase 1. |
10 | 67 |
* |
68 |
* <p>Whereas the first phase is organized as a sweep through all |
|
27857 | 69 |
* compiled syntax trees, the second phase is on-demand. Members of a |
10 | 70 |
* class are entered when the contents of a class are first |
71 |
* accessed. This is accomplished by installing completer objects in |
|
27857 | 72 |
* class symbols for compiled classes which invoke the type-enter |
10 | 73 |
* phase for the corresponding class tree. |
74 |
* |
|
75 |
* <p>Classes migrate from one phase to the next via queues: |
|
76 |
* |
|
13844 | 77 |
* <pre>{@literal |
27857 | 78 |
* class enter -> (Enter.uncompleted) --> type enter |
10 | 79 |
* -> (Todo) --> attribute |
80 |
* (only for toplevel classes) |
|
13844 | 81 |
* }</pre> |
10 | 82 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
83 |
* <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
|
84 |
* If you write code that depends on this, you do so at your own risk. |
10 | 85 |
* This code and its internal interfaces are subject to change or |
86 |
* deletion without notice.</b> |
|
87 |
*/ |
|
88 |
public class Enter extends JCTree.Visitor { |
|
22163 | 89 |
protected static final Context.Key<Enter> enterKey = new Context.Key<>(); |
10 | 90 |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29776
diff
changeset
|
91 |
Annotate annotate; |
10 | 92 |
Log log; |
93 |
Symtab syms; |
|
94 |
Check chk; |
|
95 |
TreeMaker make; |
|
27857 | 96 |
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
|
97 |
Types types; |
10 | 98 |
Lint lint; |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
99 |
Names names; |
10 | 100 |
JavaFileManager fileManager; |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
101 |
PkgInfo pkginfoOpt; |
25007 | 102 |
TypeEnvs typeEnvs; |
36526 | 103 |
Modules modules; |
104 |
JCDiagnostic.Factory diags; |
|
10 | 105 |
|
106 |
private final Todo todo; |
|
107 |
||
108 |
public static Enter instance(Context context) { |
|
109 |
Enter instance = context.get(enterKey); |
|
110 |
if (instance == null) |
|
111 |
instance = new Enter(context); |
|
112 |
return instance; |
|
113 |
} |
|
114 |
||
115 |
protected Enter(Context context) { |
|
116 |
context.put(enterKey, this); |
|
117 |
||
118 |
log = Log.instance(context); |
|
119 |
make = TreeMaker.instance(context); |
|
120 |
syms = Symtab.instance(context); |
|
121 |
chk = Check.instance(context); |
|
27857 | 122 |
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
|
123 |
types = Types.instance(context); |
10 | 124 |
annotate = Annotate.instance(context); |
125 |
lint = Lint.instance(context); |
|
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
126 |
names = Names.instance(context); |
36526 | 127 |
modules = Modules.instance(context); |
128 |
diags = JCDiagnostic.Factory.instance(context); |
|
10 | 129 |
|
130 |
predefClassDef = make.ClassDef( |
|
131 |
make.Modifiers(PUBLIC), |
|
15354 | 132 |
syms.predefClass.name, |
133 |
List.<JCTypeParameter>nil(), |
|
134 |
null, |
|
135 |
List.<JCExpression>nil(), |
|
136 |
List.<JCTree>nil()); |
|
10 | 137 |
predefClassDef.sym = syms.predefClass; |
138 |
todo = Todo.instance(context); |
|
139 |
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
|
140 |
|
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
5847
diff
changeset
|
141 |
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
|
142 |
pkginfoOpt = PkgInfo.get(options); |
25007 | 143 |
typeEnvs = TypeEnvs.instance(context); |
10 | 144 |
} |
145 |
||
146 |
/** Accessor for typeEnvs |
|
147 |
*/ |
|
148 |
public Env<AttrContext> getEnv(TypeSymbol sym) { |
|
149 |
return typeEnvs.get(sym); |
|
150 |
} |
|
151 |
||
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
152 |
public Iterable<Env<AttrContext>> getEnvs() { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
153 |
return typeEnvs.values(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
154 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
155 |
|
10 | 156 |
public Env<AttrContext> getClassEnv(TypeSymbol sym) { |
157 |
Env<AttrContext> localEnv = getEnv(sym); |
|
158 |
Env<AttrContext> lintEnv = localEnv; |
|
159 |
while (lintEnv.info.lint == null) |
|
160 |
lintEnv = lintEnv.next; |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
15354
diff
changeset
|
161 |
localEnv.info.lint = lintEnv.info.lint.augment(sym); |
10 | 162 |
return localEnv; |
163 |
} |
|
164 |
||
165 |
/** The queue of all classes that might still need to be completed; |
|
166 |
* saved and initialized by main(). |
|
167 |
*/ |
|
168 |
ListBuffer<ClassSymbol> uncompleted; |
|
169 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
170 |
/** The queue of modules whose imports still need to be checked. */ |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
171 |
ListBuffer<JCCompilationUnit> unfinishedModules = new ListBuffer<>(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
172 |
|
10 | 173 |
/** A dummy class to serve as enclClass for toplevel environments. |
174 |
*/ |
|
175 |
private JCClassDecl predefClassDef; |
|
176 |
||
177 |
/* ************************************************************************ |
|
178 |
* environment construction |
|
179 |
*************************************************************************/ |
|
180 |
||
181 |
||
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 = |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
197 |
env.dup(tree, env.info.dup(WriteableScope.create(tree.sym))); |
10 | 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 |
|
29776
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
28589
diff
changeset
|
203 |
localEnv.info.isAnonymousDiamond = TreeInfo.isDiamond(env.tree); |
10 | 204 |
return localEnv; |
205 |
} |
|
206 |
||
207 |
/** Create a fresh environment for toplevels. |
|
208 |
* @param tree The toplevel tree. |
|
209 |
*/ |
|
210 |
Env<AttrContext> topLevelEnv(JCCompilationUnit tree) { |
|
22163 | 211 |
Env<AttrContext> localEnv = new Env<>(tree, new AttrContext()); |
10 | 212 |
localEnv.toplevel = tree; |
213 |
localEnv.enclClass = predefClassDef; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
214 |
tree.toplevelScope = WriteableScope.create(tree.packge); |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
215 |
tree.namedImportScope = new NamedImportScope(tree.packge, tree.toplevelScope); |
7615 | 216 |
tree.starImportScope = new StarImportScope(tree.packge); |
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 |
public Env<AttrContext> getTopLevelEnv(JCCompilationUnit tree) { |
|
22163 | 223 |
Env<AttrContext> localEnv = new Env<>(tree, new AttrContext()); |
10 | 224 |
localEnv.toplevel = tree; |
225 |
localEnv.enclClass = predefClassDef; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
226 |
localEnv.info.scope = tree.toplevelScope; |
10 | 227 |
localEnv.info.lint = lint; |
228 |
return localEnv; |
|
229 |
} |
|
230 |
||
231 |
/** The scope in which a member definition in environment env is to be entered |
|
232 |
* This is usually the environment's scope, except for class environments, |
|
233 |
* where the local scope is for type variables, and the this and super symbol |
|
234 |
* only, and members go into the class member scope. |
|
235 |
*/ |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
236 |
WriteableScope enterScope(Env<AttrContext> env) { |
10950 | 237 |
return (env.tree.hasTag(JCTree.Tag.CLASSDEF)) |
10 | 238 |
? ((JCClassDecl) env.tree).sym.members_field |
239 |
: env.info.scope; |
|
240 |
} |
|
241 |
||
36526 | 242 |
/** Create a fresh environment for modules. |
243 |
* |
|
244 |
* @param tree The module definition. |
|
245 |
* @param env The environment current outside of the module definition. |
|
246 |
*/ |
|
247 |
public Env<AttrContext> moduleEnv(JCModuleDecl tree, Env<AttrContext> env) { |
|
248 |
Assert.checkNonNull(tree.sym); |
|
249 |
Env<AttrContext> localEnv = |
|
250 |
env.dup(tree, env.info.dup(WriteableScope.create(tree.sym))); |
|
251 |
localEnv.enclClass = predefClassDef; |
|
252 |
localEnv.outer = env; |
|
253 |
localEnv.info.isSelfCall = false; |
|
254 |
localEnv.info.lint = null; // leave this to be filled in by Attr, |
|
255 |
// when annotations have been processed |
|
256 |
return localEnv; |
|
257 |
} |
|
258 |
||
259 |
||
10 | 260 |
/* ************************************************************************ |
261 |
* Visitor methods for phase 1: class enter |
|
262 |
*************************************************************************/ |
|
263 |
||
264 |
/** Visitor argument: the current environment. |
|
265 |
*/ |
|
266 |
protected Env<AttrContext> env; |
|
267 |
||
268 |
/** Visitor result: the computed type. |
|
269 |
*/ |
|
270 |
Type result; |
|
271 |
||
272 |
/** Visitor method: enter all classes in given tree, catching any |
|
273 |
* completion failure exceptions. Return the tree's type. |
|
274 |
* |
|
275 |
* @param tree The tree to be visited. |
|
276 |
* @param env The environment visitor argument. |
|
277 |
*/ |
|
278 |
Type classEnter(JCTree tree, Env<AttrContext> env) { |
|
279 |
Env<AttrContext> prevEnv = this.env; |
|
280 |
try { |
|
281 |
this.env = env; |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29776
diff
changeset
|
282 |
annotate.blockAnnotations(); |
10 | 283 |
tree.accept(this); |
284 |
return result; |
|
285 |
} catch (CompletionFailure ex) { |
|
286 |
return chk.completionError(tree.pos(), ex); |
|
287 |
} finally { |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29776
diff
changeset
|
288 |
annotate.unblockAnnotations(); |
10 | 289 |
this.env = prevEnv; |
290 |
} |
|
291 |
} |
|
292 |
||
293 |
/** Visitor method: enter classes of a list of trees, returning a list of types. |
|
294 |
*/ |
|
295 |
<T extends JCTree> List<Type> classEnter(List<T> trees, Env<AttrContext> env) { |
|
22163 | 296 |
ListBuffer<Type> ts = new ListBuffer<>(); |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
297 |
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
|
298 |
Type t = classEnter(l.head, env); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
299 |
if (t != null) |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
300 |
ts.append(t); |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
10
diff
changeset
|
301 |
} |
10 | 302 |
return ts.toList(); |
303 |
} |
|
304 |
||
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
305 |
@Override |
10 | 306 |
public void visitTopLevel(JCCompilationUnit tree) { |
36526 | 307 |
// Assert.checkNonNull(tree.modle, tree.sourcefile.toString()); |
308 |
||
10 | 309 |
JavaFileObject prev = log.useSource(tree.sourcefile); |
310 |
boolean addEnv = false; |
|
311 |
boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info", |
|
312 |
JavaFileObject.Kind.SOURCE); |
|
36526 | 313 |
if (TreeInfo.isModuleInfo(tree)) { |
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
314 |
JCPackageDecl pd = tree.getPackage(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
315 |
if (pd != null) { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
316 |
log.error(pd.pos(), Errors.NoPkgInModuleInfoJava); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
317 |
} |
36526 | 318 |
tree.packge = syms.rootPackage; |
319 |
Env<AttrContext> topEnv = topLevelEnv(tree); |
|
320 |
classEnter(tree.defs, topEnv); |
|
321 |
tree.modle.usesProvidesCompleter = modules.getUsesProvidesCompleter(); |
|
322 |
} else { |
|
323 |
JCPackageDecl pd = tree.getPackage(); |
|
324 |
if (pd != null) { |
|
325 |
tree.packge = pd.packge = syms.enterPackage(tree.modle, TreeInfo.fullName(pd.pid)); |
|
326 |
if ( pd.annotations.nonEmpty() |
|
327 |
|| pkginfoOpt == PkgInfo.ALWAYS |
|
328 |
|| tree.docComments != null) { |
|
329 |
if (isPkgInfo) { |
|
330 |
addEnv = true; |
|
331 |
} else if (pd.annotations.nonEmpty()) { |
|
332 |
log.error(pd.annotations.head.pos(), |
|
333 |
"pkg.annotations.sb.in.package-info.java"); |
|
334 |
} |
|
10 | 335 |
} |
36526 | 336 |
} else { |
337 |
tree.packge = tree.modle.unnamedPackage; |
|
10 | 338 |
} |
36526 | 339 |
|
340 |
Map<Name, PackageSymbol> visiblePackages = tree.modle.visiblePackages; |
|
341 |
Optional<ModuleSymbol> dependencyWithPackage = |
|
342 |
syms.listPackageModules(tree.packge.fullname) |
|
343 |
.stream() |
|
344 |
.filter(m -> m != tree.modle) |
|
345 |
.filter(cand -> visiblePackages.get(tree.packge.fullname) == syms.getPackage(cand, tree.packge.fullname)) |
|
346 |
.findAny(); |
|
10 | 347 |
|
36526 | 348 |
if (dependencyWithPackage.isPresent()) { |
349 |
log.error(pd, Errors.PackageInOtherModule(dependencyWithPackage.get())); |
|
10 | 350 |
} |
36526 | 351 |
|
352 |
tree.packge.complete(); // Find all classes in package. |
|
353 |
||
354 |
Env<AttrContext> topEnv = topLevelEnv(tree); |
|
355 |
Env<AttrContext> packageEnv = isPkgInfo ? topEnv.dup(pd) : null; |
|
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
356 |
|
36526 | 357 |
// Save environment of package-info.java file. |
358 |
if (isPkgInfo) { |
|
359 |
Env<AttrContext> env0 = typeEnvs.get(tree.packge); |
|
360 |
if (env0 != null) { |
|
361 |
JCCompilationUnit tree0 = env0.toplevel; |
|
362 |
if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) { |
|
363 |
log.warning(pd != null ? pd.pid.pos() : null, |
|
364 |
"pkg-info.already.seen", |
|
365 |
tree.packge); |
|
366 |
} |
|
367 |
} |
|
368 |
typeEnvs.put(tree.packge, packageEnv); |
|
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
1264
diff
changeset
|
369 |
|
36526 | 370 |
for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner) |
371 |
q.flags_field |= EXISTS; |
|
372 |
||
373 |
Name name = names.package_info; |
|
374 |
ClassSymbol c = syms.enterClass(tree.modle, name, tree.packge); |
|
375 |
c.flatname = names.fromString(tree.packge + "." + name); |
|
376 |
c.sourcefile = tree.sourcefile; |
|
30066
d74c06a92bd8
8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents:
29842
diff
changeset
|
377 |
c.completer = Completer.NULL_COMPLETER; |
36526 | 378 |
c.members_field = WriteableScope.create(c); |
379 |
tree.packge.package_info = c; |
|
380 |
} |
|
381 |
classEnter(tree.defs, topEnv); |
|
382 |
if (addEnv) { |
|
383 |
todo.append(packageEnv); |
|
384 |
} |
|
10 | 385 |
} |
386 |
log.useSource(prev); |
|
387 |
result = null; |
|
388 |
} |
|
389 |
||
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
390 |
@Override |
10 | 391 |
public void visitClassDef(JCClassDecl tree) { |
392 |
Symbol owner = env.info.scope.owner; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
393 |
WriteableScope enclScope = enterScope(env); |
10 | 394 |
ClassSymbol c; |
395 |
if (owner.kind == PCK) { |
|
396 |
// We are seeing a toplevel class. |
|
397 |
PackageSymbol packge = (PackageSymbol)owner; |
|
398 |
for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner) |
|
399 |
q.flags_field |= EXISTS; |
|
36526 | 400 |
c = syms.enterClass(env.toplevel.modle, tree.name, packge); |
10 | 401 |
packge.members().enterIfAbsent(c); |
402 |
if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env)) { |
|
403 |
log.error(tree.pos(), |
|
404 |
"class.public.should.be.in.file", tree.name); |
|
405 |
} |
|
406 |
} else { |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
407 |
if (!tree.name.isEmpty() && |
10 | 408 |
!chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) { |
409 |
result = null; |
|
410 |
return; |
|
411 |
} |
|
412 |
if (owner.kind == TYP) { |
|
413 |
// We are seeing a member class. |
|
36526 | 414 |
c = syms.enterClass(env.toplevel.modle, tree.name, (TypeSymbol)owner); |
42411
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
415 |
if (c.owner != owner) { |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
416 |
//anonymous class loaded from a classfile may be recreated from source (see below) |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
417 |
//if this class is a member of such an anonymous class, fix the owner: |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42411
diff
changeset
|
418 |
Assert.check(owner.owner.kind != TYP, owner::toString); |
42411
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
419 |
Assert.check(c.owner.kind == TYP, () -> c.owner.toString()); |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
420 |
ClassSymbol cowner = (ClassSymbol) c.owner; |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
421 |
if (cowner.members_field != null) { |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
422 |
cowner.members_field.remove(c); |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
423 |
} |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
424 |
c.owner = owner; |
2433ceacb13e
8166628: Compiling with annotation processing, access error in specific situation
jlahoda
parents:
42407
diff
changeset
|
425 |
} |
10 | 426 |
if ((owner.flags_field & INTERFACE) != 0) { |
427 |
tree.mods.flags |= PUBLIC | STATIC; |
|
428 |
} |
|
429 |
} else { |
|
430 |
// We are seeing a local class. |
|
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22163
diff
changeset
|
431 |
c = syms.defineClass(tree.name, owner); |
10 | 432 |
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
|
433 |
if (!c.name.isEmpty()) |
10 | 434 |
chk.checkTransparentClass(tree.pos(), c, env.info.scope); |
435 |
} |
|
436 |
} |
|
437 |
tree.sym = c; |
|
438 |
||
439 |
// Enter class into `compiled' table and enclosing scope. |
|
36526 | 440 |
if (chk.getCompiled(c) != null) { |
10 | 441 |
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
|
442 |
result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType); |
10 | 443 |
tree.sym = (ClassSymbol)result.tsym; |
444 |
return; |
|
445 |
} |
|
36526 | 446 |
chk.putCompiled(c); |
10 | 447 |
enclScope.enter(c); |
448 |
||
449 |
// Set up an environment for class block and store in `typeEnvs' |
|
450 |
// table, to be retrieved later in memberEnter and attribution. |
|
451 |
Env<AttrContext> localEnv = classEnv(tree, env); |
|
452 |
typeEnvs.put(c, localEnv); |
|
453 |
||
454 |
// Fill out class fields. |
|
30066
d74c06a92bd8
8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents:
29842
diff
changeset
|
455 |
c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on. |
10 | 456 |
c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree); |
457 |
c.sourcefile = env.toplevel.sourcefile; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
25007
diff
changeset
|
458 |
c.members_field = WriteableScope.create(c); |
39372
a42db8c180c3
8132446: AsssertionError in ClassSymbol.setAnnotationType
sadayapalam
parents:
36526
diff
changeset
|
459 |
c.clearAnnotationMetadata(); |
10 | 460 |
|
461 |
ClassType ct = (ClassType)c.type; |
|
462 |
if (owner.kind != PCK && (c.flags_field & STATIC) == 0) { |
|
463 |
// We are seeing a local or inner class. |
|
464 |
// Set outer_field of this class to closest enclosing class |
|
465 |
// which contains this class in a non-static context |
|
466 |
// (its "enclosing instance class"), provided such a class exists. |
|
467 |
Symbol owner1 = owner; |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25874
diff
changeset
|
468 |
while (owner1.kind.matches(KindSelector.VAL_MTH) && |
10 | 469 |
(owner1.flags_field & STATIC) == 0) { |
470 |
owner1 = owner1.owner; |
|
471 |
} |
|
472 |
if (owner1.kind == TYP) { |
|
473 |
ct.setEnclosingType(owner1.type); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
// Enter type parameters. |
|
478 |
ct.typarams_field = classEnter(tree.typarams, localEnv); |
|
39372
a42db8c180c3
8132446: AsssertionError in ClassSymbol.setAnnotationType
sadayapalam
parents:
36526
diff
changeset
|
479 |
ct.allparams_field = null; |
10 | 480 |
|
28589
cbf8df194957
8052070: javac crashes when there are duplicated type parameters
jlahoda
parents:
27857
diff
changeset
|
481 |
// install further completer for this type. |
cbf8df194957
8052070: javac crashes when there are duplicated type parameters
jlahoda
parents:
27857
diff
changeset
|
482 |
c.completer = typeEnter; |
cbf8df194957
8052070: javac crashes when there are duplicated type parameters
jlahoda
parents:
27857
diff
changeset
|
483 |
|
10 | 484 |
// Add non-local class to uncompleted, to make sure it will be |
485 |
// completed later. |
|
486 |
if (!c.isLocal() && uncompleted != null) uncompleted.append(c); |
|
487 |
// System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG |
|
488 |
||
489 |
// Recursively enter all member classes. |
|
490 |
classEnter(tree.defs, localEnv); |
|
491 |
||
36526 | 492 |
// Assert.checkNonNull(c.modle, c.sourcefile.toString()); |
493 |
||
10 | 494 |
result = c.type; |
495 |
} |
|
496 |
//where |
|
497 |
/** Does class have the same name as the file it appears in? |
|
498 |
*/ |
|
499 |
private static boolean classNameMatchesFileName(ClassSymbol c, |
|
500 |
Env<AttrContext> env) { |
|
501 |
return env.toplevel.sourcefile.isNameCompatible(c.name.toString(), |
|
502 |
JavaFileObject.Kind.SOURCE); |
|
503 |
} |
|
504 |
||
505 |
/** Complain about a duplicate class. */ |
|
506 |
protected void duplicateClass(DiagnosticPosition pos, ClassSymbol c) { |
|
507 |
log.error(pos, "duplicate.class", c.fullname); |
|
508 |
} |
|
509 |
||
510 |
/** Class enter visitor method for type parameters. |
|
511 |
* Enter a symbol for type parameter in local scope, after checking that it |
|
512 |
* is unique. |
|
513 |
*/ |
|
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
514 |
@Override |
10 | 515 |
public void visitTypeParameter(JCTypeParameter tree) { |
516 |
TypeVar a = (tree.type != null) |
|
517 |
? (TypeVar)tree.type |
|
518 |
: new TypeVar(tree.name, env.info.scope.owner, syms.botType); |
|
519 |
tree.type = a; |
|
520 |
if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) { |
|
521 |
env.info.scope.enter(a.tsym); |
|
522 |
} |
|
523 |
result = a; |
|
524 |
} |
|
525 |
||
36526 | 526 |
@Override |
527 |
public void visitModuleDef(JCModuleDecl tree) { |
|
528 |
Env<AttrContext> moduleEnv = moduleEnv(tree, env); |
|
529 |
typeEnvs.put(tree.sym, moduleEnv); |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
530 |
if (modules.isInModuleGraph(tree.sym)) { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
531 |
todo.append(moduleEnv); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
532 |
} |
36526 | 533 |
} |
534 |
||
10 | 535 |
/** Default class enter visitor method: do nothing. |
536 |
*/ |
|
5008
3a90203fa91e
6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents:
4871
diff
changeset
|
537 |
@Override |
10 | 538 |
public void visitTree(JCTree tree) { |
539 |
result = null; |
|
540 |
} |
|
541 |
||
542 |
/** Main method: enter all classes in a list of toplevel trees. |
|
543 |
* @param trees The list of trees to be processed. |
|
544 |
*/ |
|
545 |
public void main(List<JCCompilationUnit> trees) { |
|
546 |
complete(trees, null); |
|
547 |
} |
|
548 |
||
27857 | 549 |
/** Main method: enter classes from the list of toplevel trees, possibly |
550 |
* skipping TypeEnter for all but 'c' by placing them on the uncompleted |
|
551 |
* list. |
|
10 | 552 |
* @param trees The list of trees to be processed. |
27857 | 553 |
* @param c The class symbol to be processed or null to process all. |
10 | 554 |
*/ |
555 |
public void complete(List<JCCompilationUnit> trees, ClassSymbol c) { |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29776
diff
changeset
|
556 |
annotate.blockAnnotations(); |
10 | 557 |
ListBuffer<ClassSymbol> prevUncompleted = uncompleted; |
27857 | 558 |
if (typeEnter.completionEnabled) uncompleted = new ListBuffer<>(); |
10 | 559 |
|
560 |
try { |
|
561 |
// enter all classes, and construct uncompleted list |
|
562 |
classEnter(trees, null); |
|
563 |
||
564 |
// complete all uncompleted classes in memberEnter |
|
27857 | 565 |
if (typeEnter.completionEnabled) { |
10 | 566 |
while (uncompleted.nonEmpty()) { |
567 |
ClassSymbol clazz = uncompleted.next(); |
|
568 |
if (c == null || c == clazz || prevUncompleted == null) |
|
569 |
clazz.complete(); |
|
570 |
else |
|
571 |
// defer |
|
572 |
prevUncompleted.append(clazz); |
|
573 |
} |
|
574 |
||
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
575 |
if (!modules.modulesInitialized()) { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
576 |
for (JCCompilationUnit cut : trees) { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
577 |
if (cut.getModuleDecl() != null) { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
578 |
unfinishedModules.append(cut); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
579 |
} else { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
580 |
typeEnter.ensureImportsChecked(List.of(cut)); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
581 |
} |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
582 |
} |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
583 |
} else { |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
584 |
typeEnter.ensureImportsChecked(unfinishedModules.toList()); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
585 |
unfinishedModules.clear(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
586 |
typeEnter.ensureImportsChecked(trees); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
39372
diff
changeset
|
587 |
} |
10 | 588 |
} |
589 |
} finally { |
|
590 |
uncompleted = prevUncompleted; |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29776
diff
changeset
|
591 |
annotate.unblockAnnotations(); |
10 | 592 |
} |
593 |
} |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
594 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
595 |
public void newRound() { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
596 |
typeEnvs.clear(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22442
diff
changeset
|
597 |
} |
10 | 598 |
} |