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