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