1 /*
  2  * Copyright (c) 1999, 2023, 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  */
 25 
 26 package com.sun.tools.javac.comp;
 27 
 28 import java.util.Map;
 29 import java.util.Optional;
 30 
 31 import javax.tools.JavaFileObject;
 32 import javax.tools.JavaFileManager;
 33 
 34 import com.sun.tools.javac.code.*;
 35 import com.sun.tools.javac.code.Kinds.KindName;
 36 import com.sun.tools.javac.code.Kinds.KindSelector;
 37 import com.sun.tools.javac.code.Scope.*;
 38 import com.sun.tools.javac.code.Symbol.*;
 39 import com.sun.tools.javac.code.Type.*;
 40 import com.sun.tools.javac.main.Option.PkgInfo;
 41 import com.sun.tools.javac.resources.CompilerProperties.Errors;
 42 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
 43 import com.sun.tools.javac.tree.*;
 44 import com.sun.tools.javac.tree.JCTree.*;
 45 import com.sun.tools.javac.util.*;
 46 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
 47 import com.sun.tools.javac.util.List;
 48 
 49 import static com.sun.tools.javac.code.Flags.*;
 50 import static com.sun.tools.javac.code.Kinds.Kind.*;
 51 
 52 /** This class enters symbols for all encountered definitions into
 53  *  the symbol table. The pass consists of high-level two phases,
 54  *  organized as follows:
 55  *
 56  *  <p>In the first phase, all class symbols are entered into their
 57  *  enclosing scope, descending recursively down the tree for classes
 58  *  which are members of other classes. The class symbols are given a
 59  *  TypeEnter object as completer.
 60  *
 61  *  <p>In the second phase classes are completed using
 62  *  TypeEnter.complete(). Completion might occur on demand, but
 63  *  any classes that are not completed that way will be eventually
 64  *  completed by processing the `uncompleted' queue. Completion
 65  *  entails determination of a class's parameters, supertype and
 66  *  interfaces, as well as entering all symbols defined in the
 67  *  class into its scope, with the exception of class symbols which
 68  *  have been entered in phase 1.
 69  *
 70  *  <p>Whereas the first phase is organized as a sweep through all
 71  *  compiled syntax trees, the second phase is on-demand. Members of a
 72  *  class are entered when the contents of a class are first
 73  *  accessed. This is accomplished by installing completer objects in
 74  *  class symbols for compiled classes which invoke the type-enter
 75  *  phase for the corresponding class tree.
 76  *
 77  *  <p>Classes migrate from one phase to the next via queues:
 78  *
 79  *  <pre>{@literal
 80  *  class enter -> (Enter.uncompleted)         --> type enter
 81  *              -> (Todo)                      --> attribute
 82  *                                              (only for toplevel classes)
 83  *  }</pre>
 84  *
 85  *  <p><b>This is NOT part of any supported API.
 86  *  If you write code that depends on this, you do so at your own risk.
 87  *  This code and its internal interfaces are subject to change or
 88  *  deletion without notice.</b>
 89  */
 90 public class Enter extends JCTree.Visitor {
 91     protected static final Context.Key<Enter> enterKey = new Context.Key<>();
 92 
 93     Annotate annotate;
 94     Log log;
 95     Symtab syms;
 96     Check chk;
 97     TreeMaker make;
 98     TypeEnter typeEnter;
 99     Types types;
100     Lint lint;
101     Names names;
102     JavaFileManager fileManager;
103     PkgInfo pkginfoOpt;
104     TypeEnvs typeEnvs;
105     Modules modules;
106     JCDiagnostic.Factory diags;
107 
108     private final Todo todo;
109 
110     public static Enter instance(Context context) {
111         Enter instance = context.get(enterKey);
112         if (instance == null)
113             instance = new Enter(context);
114         return instance;
115     }
116 
117     @SuppressWarnings("this-escape")
118     protected Enter(Context context) {
119         context.put(enterKey, this);
120 
121         log = Log.instance(context);
122         make = TreeMaker.instance(context);
123         syms = Symtab.instance(context);
124         chk = Check.instance(context);
125         typeEnter = TypeEnter.instance(context);
126         types = Types.instance(context);
127         annotate = Annotate.instance(context);
128         lint = Lint.instance(context);
129         names = Names.instance(context);
130         modules = Modules.instance(context);
131         diags = JCDiagnostic.Factory.instance(context);
132 
133         predefClassDef = make.ClassDef(
134             make.Modifiers(PUBLIC),
135             syms.predefClass.name,
136             List.nil(),
137             null,
138             List.nil(),
139             List.nil());
140         predefClassDef.sym = syms.predefClass;
141         todo = Todo.instance(context);
142         fileManager = context.get(JavaFileManager.class);
143 
144         Options options = Options.instance(context);
145         pkginfoOpt = PkgInfo.get(options);
146         typeEnvs = TypeEnvs.instance(context);
147     }
148 
149     /** Accessor for typeEnvs
150      */
151     public Env<AttrContext> getEnv(TypeSymbol sym) {
152         return typeEnvs.get(sym);
153     }
154 
155     public Iterable<Env<AttrContext>> getEnvs() {
156         return typeEnvs.values();
157     }
158 
159     public Env<AttrContext> getClassEnv(TypeSymbol sym) {
160         Env<AttrContext> localEnv = getEnv(sym);
161         if (localEnv == null) return null;
162         Env<AttrContext> lintEnv = localEnv;
163         while (lintEnv.info.lint == null)
164             lintEnv = lintEnv.next;
165         localEnv.info.lint = lintEnv.info.lint.augment(sym);
166         return localEnv;
167     }
168 
169     /** The queue of all classes that might still need to be completed;
170      *  saved and initialized by main().
171      */
172     ListBuffer<ClassSymbol> uncompleted;
173 
174     /** The queue of classes that should have typeEnter completer installed after
175      *  all the classes are discovered.
176      */
177     List<ClassSymbol> pendingCompleter = null;
178 
179     /** The queue of modules whose imports still need to be checked. */
180     ListBuffer<JCCompilationUnit> unfinishedModules = new ListBuffer<>();
181 
182     /** A dummy class to serve as enclClass for toplevel environments.
183      */
184     private JCClassDecl predefClassDef;
185 
186 /* ************************************************************************
187  * environment construction
188  *************************************************************************/
189 
190 
191     /** Create a fresh environment for class bodies.
192      *  This will create a fresh scope for local symbols of a class, referred
193      *  to by the environments info.scope field.
194      *  This scope will contain
195      *    - symbols for this and super
196      *    - symbols for any type parameters
197      *  In addition, it serves as an anchor for scopes of methods and initializers
198      *  which are nested in this scope via Scope.dup().
199      *  This scope should not be confused with the members scope of a class.
200      *
201      *  @param tree     The class definition.
202      *  @param env      The environment current outside of the class definition.
203      */
204     public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) {
205         Env<AttrContext> localEnv =
206             env.dup(tree, env.info.dup(WriteableScope.create(tree.sym)));
207         localEnv.enclClass = tree;
208         localEnv.outer = env;
209         localEnv.info.lint = null; // leave this to be filled in by Attr,
210                                    // when annotations have been processed
211         localEnv.info.isAnonymousDiamond = TreeInfo.isDiamond(env.tree);
212         return localEnv;
213     }
214 
215     /** Create a fresh environment for toplevels.
216      *  @param tree     The toplevel tree.
217      */
218     Env<AttrContext> topLevelEnv(JCCompilationUnit tree) {
219         Env<AttrContext> localEnv = new Env<>(tree, new AttrContext());
220         localEnv.toplevel = tree;
221         localEnv.enclClass = predefClassDef;
222         tree.toplevelScope = WriteableScope.create(tree.packge);
223         tree.namedImportScope = new NamedImportScope(tree.packge);
224         tree.starImportScope = new StarImportScope(tree.packge);
225         localEnv.info.scope = tree.toplevelScope;
226         localEnv.info.lint = lint;
227         return localEnv;
228     }
229 
230     public Env<AttrContext> getTopLevelEnv(JCCompilationUnit tree) {
231         Env<AttrContext> localEnv = new Env<>(tree, new AttrContext());
232         localEnv.toplevel = tree;
233         localEnv.enclClass = predefClassDef;
234         localEnv.info.scope = tree.toplevelScope;
235         localEnv.info.lint = lint;
236         return localEnv;
237     }
238 
239     /** The scope in which a member definition in environment env is to be entered
240      *  This is usually the environment's scope, except for class environments,
241      *  where the local scope is for type variables, and the this and super symbol
242      *  only, and members go into the class member scope.
243      */
244     WriteableScope enterScope(Env<AttrContext> env) {
245         return (env.tree.hasTag(JCTree.Tag.CLASSDEF))
246             ? ((JCClassDecl) env.tree).sym.members_field
247             : env.info.scope;
248     }
249 
250     /** Create a fresh environment for modules.
251      *
252      *  @param tree     The module definition.
253      *  @param env      The environment current outside of the module definition.
254      */
255     public Env<AttrContext> moduleEnv(JCModuleDecl tree, Env<AttrContext> env) {
256         Assert.checkNonNull(tree.sym);
257         Env<AttrContext> localEnv =
258             env.dup(tree, env.info.dup(WriteableScope.create(tree.sym)));
259         localEnv.enclClass = predefClassDef;
260         localEnv.outer = env;
261         localEnv.info.lint = null; // leave this to be filled in by Attr,
262                                    // when annotations have been processed
263         return localEnv;
264     }
265 
266 
267 /* ************************************************************************
268  * Visitor methods for phase 1: class enter
269  *************************************************************************/
270 
271     /** Visitor argument: the current environment.
272      */
273     protected Env<AttrContext> env;
274 
275     /** Visitor result: the computed type.
276      */
277     Type result;
278 
279     /** Visitor method: enter all classes in given tree, catching any
280      *  completion failure exceptions. Return the tree's type.
281      *
282      *  @param tree    The tree to be visited.
283      *  @param env     The environment visitor argument.
284      */
285     Type classEnter(JCTree tree, Env<AttrContext> env) {
286         Env<AttrContext> prevEnv = this.env;
287         try {
288             this.env = env;
289             annotate.blockAnnotations();
290             tree.accept(this);
291             return result;
292         }  catch (CompletionFailure ex) {
293             return chk.completionError(tree.pos(), ex);
294         } finally {
295             annotate.unblockAnnotations();
296             this.env = prevEnv;
297         }
298     }
299 
300     /** Visitor method: enter classes of a list of trees, returning a list of types.
301      */
302     <T extends JCTree> List<Type> classEnter(List<T> trees, Env<AttrContext> env) {
303         ListBuffer<Type> ts = new ListBuffer<>();
304         for (List<T> l = trees; l.nonEmpty(); l = l.tail) {
305             Type t = classEnter(l.head, env);
306             if (t != null)
307                 ts.append(t);
308         }
309         return ts.toList();
310     }
311 
312     @Override
313     public void visitTopLevel(JCCompilationUnit tree) {
314         JavaFileObject prev = log.useSource(tree.sourcefile);
315         boolean addEnv = false;
316         boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info",
317                                                              JavaFileObject.Kind.SOURCE);
318         if (TreeInfo.isModuleInfo(tree)) {
319             JCPackageDecl pd = tree.getPackage();
320             if (pd != null) {
321                 log.error(pd.pos(), Errors.NoPkgInModuleInfoJava);
322             }
323             tree.packge = syms.rootPackage;
324             Env<AttrContext> topEnv = topLevelEnv(tree);
325             classEnter(tree.defs, topEnv);
326             tree.modle.usesProvidesCompleter = modules.getUsesProvidesCompleter();
327         } else {
328             JCPackageDecl pd = tree.getPackage();
329             if (pd != null) {
330                 tree.packge = pd.packge = syms.enterPackage(tree.modle, TreeInfo.fullName(pd.pid));
331                 setPackageSymbols.scan(pd);
332                 if (   pd.annotations.nonEmpty()
333                     || pkginfoOpt == PkgInfo.ALWAYS
334                     || tree.docComments != null) {
335                     if (isPkgInfo) {
336                         addEnv = true;
337                     } else if (pd.annotations.nonEmpty()) {
338                         log.error(pd.annotations.head.pos(),
339                                   Errors.PkgAnnotationsSbInPackageInfoJava);
340                     }
341                 }
342             } else {
343                 tree.packge = tree.modle.unnamedPackage;
344             }
345 
346             Map<Name, PackageSymbol> visiblePackages = tree.modle.visiblePackages;
347             Optional<ModuleSymbol> dependencyWithPackage =
348                 syms.listPackageModules(tree.packge.fullname)
349                     .stream()
350                     .filter(m -> m != tree.modle)
351                     .filter(cand -> visiblePackages.get(tree.packge.fullname) == syms.getPackage(cand, tree.packge.fullname))
352                     .findAny();
353 
354             if (dependencyWithPackage.isPresent()) {
355                 log.error(pd, Errors.PackageInOtherModule(dependencyWithPackage.get()));
356             }
357 
358             tree.packge.complete(); // Find all classes in package.
359 
360             Env<AttrContext> topEnv = topLevelEnv(tree);
361             Env<AttrContext> packageEnv = null;
362 
363             // Save environment of package-info.java file.
364             if (isPkgInfo) {
365                 packageEnv = topEnv.dup(pd != null ? pd : tree);
366 
367                 Env<AttrContext> env0 = typeEnvs.get(tree.packge);
368                 if (env0 != null) {
369                     JCCompilationUnit tree0 = env0.toplevel;
370                     if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) {
371                         log.warning(pd != null ? pd.pid.pos() : null,
372                                     Warnings.PkgInfoAlreadySeen(tree.packge));
373                     }
374                 }
375                 typeEnvs.put(tree.packge, packageEnv);
376 
377                 for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner)
378                     q.flags_field |= EXISTS;
379 
380                 Name name = names.package_info;
381                 ClassSymbol c = syms.enterClass(tree.modle, name, tree.packge);
382                 c.flatname = names.fromString(tree.packge + "." + name);
383                 c.classfile = c.sourcefile = tree.sourcefile;
384                 c.completer = Completer.NULL_COMPLETER;
385                 c.members_field = WriteableScope.create(c);
386                 tree.packge.package_info = c;
387                 tree.packge.sourcefile = tree.sourcefile;
388             }
389             classEnter(tree.defs, topEnv);
390             if (addEnv) {
391                 todo.append(packageEnv);
392             }
393         }
394         log.useSource(prev);
395         result = null;
396     }
397         //where:
398         //set package Symbols to the package expression:
399         private final TreeScanner setPackageSymbols = new TreeScanner() {
400             Symbol currentPackage;
401 
402             @Override
403             public void visitIdent(JCIdent tree) {
404                 tree.sym = currentPackage;
405                 tree.type = currentPackage.type;
406             }
407 
408             @Override
409             public void visitSelect(JCFieldAccess tree) {
410                 tree.sym = currentPackage;
411                 tree.type = currentPackage.type;
412                 currentPackage = currentPackage.owner;
413                 super.visitSelect(tree);
414             }
415 
416             @Override
417             public void visitPackageDef(JCPackageDecl tree) {
418                 currentPackage = tree.packge;
419                 scan(tree.pid);
420             }
421         };
422 
423     @Override
424     public void visitClassDef(JCClassDecl tree) {
425         Symbol owner = env.info.scope.owner;
426         WriteableScope enclScope = enterScope(env);
427         ClassSymbol c;
428         if (owner.kind == PCK) {
429             // We are seeing a toplevel class.
430             PackageSymbol packge = (PackageSymbol)owner;
431             for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner)
432                 q.flags_field |= EXISTS;
433             c = syms.enterClass(env.toplevel.modle, tree.name, packge);
434             packge.members().enterIfAbsent(c);
435             if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env)) {
436                 KindName topElement = KindName.CLASS;
437                 if ((tree.mods.flags & ENUM) != 0) {
438                     topElement = KindName.ENUM;
439                 } else if ((tree.mods.flags & INTERFACE) != 0) {
440                     topElement = KindName.INTERFACE;
441                 }
442                 log.error(tree.pos(),
443                           Errors.ClassPublicShouldBeInFile(topElement, tree.name));
444             }
445             if ((tree.mods.flags & UNNAMED_CLASS) != 0) {
446                 syms.removeClass(env.toplevel.modle, tree.name);
447             }
448         } else {
449             if (!tree.name.isEmpty() &&
450                 !chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) {
451                 result = null;
452                 return;
453             }
454             if (owner.kind == TYP) {
455                 // We are seeing a member class.
456                 c = syms.enterClass(env.toplevel.modle, tree.name, (TypeSymbol)owner);
457                 if (c.owner != owner) {
458                     if (c.name != tree.name) {
459                         log.error(tree.pos(), Errors.SameBinaryName(c.name, tree.name));
460                         result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType);
461                         tree.sym = (ClassSymbol)result.tsym;
462                         return;
463                     }
464                     //anonymous class loaded from a classfile may be recreated from source (see below)
465                     //if this class is a member of such an anonymous class, fix the owner:
466                     Assert.check(owner.owner.kind != TYP, owner::toString);
467                     Assert.check(c.owner.kind == TYP, () -> c.owner.toString());
468                     ClassSymbol cowner = (ClassSymbol) c.owner;
469                     if (cowner.members_field != null) {
470                         cowner.members_field.remove(c);
471                     }
472                     c.owner = owner;
473                 }
474                 if ((owner.flags_field & INTERFACE) != 0) {
475                     tree.mods.flags |= PUBLIC | STATIC;
476                 }
477             } else {
478                 // We are seeing a local class.
479                 c = syms.defineClass(tree.name, owner);
480                 c.flatname = chk.localClassName(c);
481                 if (!c.name.isEmpty())
482                     chk.checkTransparentClass(tree.pos(), c, env.info.scope);
483             }
484         }
485         tree.sym = c;
486 
487         // Enter class into `compiled' table and enclosing scope.
488         if (chk.getCompiled(c) != null) {
489             duplicateClass(tree.pos(), c);
490             result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType);
491             tree.sym = (ClassSymbol)result.tsym;
492             return;
493         }
494         chk.putCompiled(c);
495         enclScope.enter(c);
496 
497         // Set up an environment for class block and store in `typeEnvs'
498         // table, to be retrieved later in memberEnter and attribution.
499         Env<AttrContext> localEnv = classEnv(tree, env);
500         typeEnvs.put(c, localEnv);
501 
502         // Fill out class fields.
503         c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on.
504         c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree) | FROM_SOURCE;
505         c.classfile = c.sourcefile = env.toplevel.sourcefile;
506         c.members_field = WriteableScope.create(c);
507         c.isPermittedExplicit = tree.permitting.nonEmpty();
508         c.clearAnnotationMetadata();
509 
510         ClassType ct = (ClassType)c.type;
511         if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
512             // We are seeing a local or inner class.
513             // Set outer_field of this class to closest enclosing class
514             // which contains this class in a non-static context
515             // (its "enclosing instance class"), provided such a class exists.
516             Symbol owner1 = owner;
517             while (owner1.kind.matches(KindSelector.VAL_MTH) &&
518                    (owner1.flags_field & STATIC) == 0) {
519                 owner1 = owner1.owner;
520             }
521             if (owner1.kind == TYP) {
522                 ct.setEnclosingType(owner1.type);
523             }
524         }
525 
526         // Enter type parameters.
527         ct.typarams_field = classEnter(tree.typarams, localEnv);
528         ct.allparams_field = null;
529 
530         // schedule installation of further completer for this type.
531         if (pendingCompleter != null) {
532             pendingCompleter = pendingCompleter.prepend(c);
533         } else {
534             c.completer = typeEnter;
535         }
536 
537         // Add non-local class to uncompleted, to make sure it will be
538         // completed later.
539         if (!c.isDirectlyOrIndirectlyLocal() && uncompleted != null) uncompleted.append(c);
540 //      System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
541 
542         // Recursively enter all member classes.
543         classEnter(tree.defs, localEnv);
544 
545 //        Assert.checkNonNull(c.modle, c.sourcefile.toString());
546 
547         result = c.type;
548     }
549     //where
550         /** Does class have the same name as the file it appears in?
551          */
552         private static boolean classNameMatchesFileName(ClassSymbol c,
553                                                         Env<AttrContext> env) {
554             return env.toplevel.sourcefile.isNameCompatible(c.name.toString(),
555                                                             JavaFileObject.Kind.SOURCE);
556         }
557 
558     /** Complain about a duplicate class. */
559     protected void duplicateClass(DiagnosticPosition pos, ClassSymbol c) {
560         log.error(pos, Errors.DuplicateClass(c.fullname));
561     }
562 
563     /** Class enter visitor method for type parameters.
564      *  Enter a symbol for type parameter in local scope, after checking that it
565      *  is unique.
566      */
567     @Override
568     public void visitTypeParameter(JCTypeParameter tree) {
569         TypeVar a = (tree.type != null)
570             ? (TypeVar)tree.type
571             : new TypeVar(tree.name, env.info.scope.owner, syms.botType);
572         tree.type = a;
573         if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) {
574             env.info.scope.enter(a.tsym);
575         }
576         result = a;
577     }
578 
579     @Override
580     public void visitModuleDef(JCModuleDecl tree) {
581         Env<AttrContext> moduleEnv = moduleEnv(tree, env);
582         typeEnvs.put(tree.sym, moduleEnv);
583         if (modules.isInModuleGraph(tree.sym)) {
584             todo.append(moduleEnv);
585         }
586     }
587 
588     /** Default class enter visitor method: do nothing.
589      */
590     @Override
591     public void visitTree(JCTree tree) {
592         result = null;
593     }
594 
595     /** Main method: enter all classes in a list of toplevel trees.
596      *  @param trees      The list of trees to be processed.
597      */
598     public void main(List<JCCompilationUnit> trees) {
599         complete(trees, null);
600     }
601 
602     /** Main method: enter classes from the list of toplevel trees, possibly
603      *  skipping TypeEnter for all but 'c' by placing them on the uncompleted
604      *  list.
605      *  @param trees      The list of trees to be processed.
606      *  @param c          The class symbol to be processed or null to process all.
607      */
608     public void complete(List<JCCompilationUnit> trees, ClassSymbol c) {
609         annotate.blockAnnotations();
610         ListBuffer<ClassSymbol> prevUncompleted = uncompleted;
611         if (typeEnter.completionEnabled) uncompleted = new ListBuffer<>();
612 
613         try {
614             List<ClassSymbol> prevPendingCompleter = pendingCompleter;
615             try {
616                 pendingCompleter = List.nil();
617                 // enter all classes, and construct uncompleted list
618                 classEnter(trees, null);
619                 // install further completer for classes recognized by the above task:
620                 for (ClassSymbol sym : pendingCompleter) {
621                     sym.completer = typeEnter;
622                 }
623             } finally {
624                 pendingCompleter = prevPendingCompleter;
625             }
626 
627             // complete all uncompleted classes in memberEnter
628             if (typeEnter.completionEnabled) {
629                 while (uncompleted.nonEmpty()) {
630                     ClassSymbol clazz = uncompleted.next();
631                     if (c == null || c == clazz || prevUncompleted == null)
632                         clazz.complete();
633                     else
634                         // defer
635                         prevUncompleted.append(clazz);
636                 }
637 
638                 if (!modules.modulesInitialized()) {
639                     for (JCCompilationUnit cut : trees) {
640                         if (cut.getModuleDecl() != null) {
641                             unfinishedModules.append(cut);
642                         } else {
643                             typeEnter.ensureImportsChecked(List.of(cut));
644                         }
645                     }
646                 } else {
647                     typeEnter.ensureImportsChecked(unfinishedModules.toList());
648                     unfinishedModules.clear();
649                     typeEnter.ensureImportsChecked(trees);
650                 }
651             }
652         } finally {
653             uncompleted = prevUncompleted;
654             annotate.unblockAnnotations();
655         }
656     }
657 
658     public void newRound() {
659         typeEnvs.clear();
660     }
661 
662     public void unenter(JCCompilationUnit topLevel, JCTree tree) {
663         new UnenterScanner(topLevel.modle).scan(tree);
664     }
665         class UnenterScanner extends TreeScanner {
666             private final ModuleSymbol msym;
667 
668             public UnenterScanner(ModuleSymbol msym) {
669                 this.msym = msym;
670             }
671 
672             @Override
673             public void visitClassDef(JCClassDecl tree) {
674                 ClassSymbol csym = tree.sym;
675                 //if something went wrong during method applicability check
676                 //it is possible that nested expressions inside argument expression
677                 //are left unchecked - in such cases there's nothing to clean up.
678                 if (csym == null) return;
679                 typeEnvs.remove(csym);
680                 chk.removeCompiled(csym);
681                 chk.clearLocalClassNameIndexes(csym);
682                 syms.removeClass(msym, csym.flatname);
683                 super.visitClassDef(tree);
684             }
685         }
686 }