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.code;
 27 
 28 import java.util.Collection;
 29 import java.util.Collections;
 30 import java.util.EnumSet;
 31 import java.util.HashMap;
 32 import java.util.LinkedHashMap;
 33 import java.util.Map;
 34 
 35 import javax.lang.model.element.ElementVisitor;
 36 
 37 import com.sun.tools.javac.code.Scope.WriteableScope;
 38 import com.sun.tools.javac.code.Source.Feature;
 39 import com.sun.tools.javac.code.Symbol.ClassSymbol;
 40 import com.sun.tools.javac.code.Symbol.Completer;
 41 import com.sun.tools.javac.code.Symbol.CompletionFailure;
 42 import com.sun.tools.javac.code.Symbol.MethodSymbol;
 43 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
 44 import com.sun.tools.javac.code.Symbol.PackageSymbol;
 45 import com.sun.tools.javac.code.Symbol.RootPackageSymbol;
 46 import com.sun.tools.javac.code.Symbol.TypeSymbol;
 47 import com.sun.tools.javac.code.Symbol.VarSymbol;
 48 import com.sun.tools.javac.code.Type.BottomType;
 49 import com.sun.tools.javac.code.Type.ClassType;
 50 import com.sun.tools.javac.code.Type.ErrorType;
 51 import com.sun.tools.javac.code.Type.JCPrimitiveType;
 52 import com.sun.tools.javac.code.Type.JCVoidType;
 53 import com.sun.tools.javac.code.Type.MethodType;
 54 import com.sun.tools.javac.code.Type.UnknownType;
 55 import com.sun.tools.javac.code.Types.UniqueType;
 56 import com.sun.tools.javac.comp.Modules;
 57 import com.sun.tools.javac.jvm.Target;
 58 import com.sun.tools.javac.util.Assert;
 59 import com.sun.tools.javac.util.Context;
 60 import com.sun.tools.javac.util.Convert;
 61 import com.sun.tools.javac.util.DefinedBy;
 62 import com.sun.tools.javac.util.DefinedBy.Api;
 63 import com.sun.tools.javac.util.Iterators;
 64 import com.sun.tools.javac.util.JavacMessages;
 65 import com.sun.tools.javac.util.List;
 66 import com.sun.tools.javac.util.Name;
 67 import com.sun.tools.javac.util.Names;
 68 
 69 import static com.sun.tools.javac.code.Flags.*;
 70 import static com.sun.tools.javac.code.Kinds.Kind.*;
 71 import static com.sun.tools.javac.code.TypeTag.*;
 72 
 73 /** A class that defines all predefined constants and operators
 74  *  as well as special classes such as java.lang.Object, which need
 75  *  to be known to the compiler. All symbols are held in instance
 76  *  fields. This makes it possible to work in multiple concurrent
 77  *  projects, which might use different class files for library classes.
 78  *
 79  *  <p><b>This is NOT part of any supported API.
 80  *  If you write code that depends on this, you do so at your own risk.
 81  *  This code and its internal interfaces are subject to change or
 82  *  deletion without notice.</b>
 83  */
 84 public class Symtab {
 85     /** The context key for the symbol table. */
 86     protected static final Context.Key<Symtab> symtabKey = new Context.Key<>();
 87 
 88     /** Get the symbol table instance. */
 89     public static Symtab instance(Context context) {
 90         Symtab instance = context.get(symtabKey);
 91         if (instance == null)
 92             instance = new Symtab(context);
 93         return instance;
 94     }
 95 
 96     /** Builtin types.
 97      */
 98     public final JCPrimitiveType byteType = new JCPrimitiveType(BYTE, null);
 99     public final JCPrimitiveType charType = new JCPrimitiveType(CHAR, null);
100     public final JCPrimitiveType shortType = new JCPrimitiveType(SHORT, null);
101     public final JCPrimitiveType intType = new JCPrimitiveType(INT, null);
102     public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null);
103     public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null);
104     public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null);
105     public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null);
106     public final Type botType = new BottomType();
107     public final JCVoidType voidType = new JCVoidType();
108 
109     private final Names names;
110     private final JavacMessages messages;
111     private final Completer initialCompleter;
112     private final Completer moduleCompleter;
113 
114     /** A symbol for the unnamed module.
115      */
116     public final ModuleSymbol unnamedModule;
117 
118     /** The error module.
119      */
120     public final ModuleSymbol errModule;
121 
122     /** A symbol for no module, for use with -source 8 or less
123      */
124     public final ModuleSymbol noModule;
125 
126     /** A symbol for the root package.
127      */
128     public final PackageSymbol rootPackage;
129 
130     /** A symbol that stands for a missing symbol.
131      */
132     public final TypeSymbol noSymbol;
133 
134     /** The error symbol.
135      */
136     public final ClassSymbol errSymbol;
137 
138     /** The unknown symbol.
139      */
140     public final ClassSymbol unknownSymbol;
141 
142     /** A value for the errType, with a originalType of noType */
143     public final Type errType;
144 
145     /** A value for the unknown type. */
146     public final Type unknownType;
147 
148     /** The builtin type of all arrays. */
149     public final ClassSymbol arrayClass;
150     public final MethodSymbol arrayCloneMethod;
151 
152     /** VGJ: The (singleton) type of all bound types. */
153     public final ClassSymbol boundClass;
154 
155     /** The builtin type of all methods. */
156     public final ClassSymbol methodClass;
157 
158     /** A symbol for the java.base module.
159      */
160     public final ModuleSymbol java_base;
161 
162     /** Predefined types.
163      */
164     public final Type objectType;
165     public final Type objectMethodsType;
166     public final Type exactConversionsSupportType;
167     public final Type objectsType;
168     public final Type classType;
169     public final Type classLoaderType;
170     public final Type stringType;
171     public final Type stringBufferType;
172     public final Type stringBuilderType;
173     public final Type cloneableType;
174     public final Type serializableType;
175     public final Type serializedLambdaType;
176     public final Type varHandleType;
177     public final Type methodHandleType;
178     public final Type methodHandlesType;
179     public final Type methodHandleLookupType;
180     public final Type methodTypeType;
181     public final Type nativeHeaderType;
182     public final Type throwableType;
183     public final Type errorType;
184     public final Type interruptedExceptionType;
185     public final Type illegalArgumentExceptionType;
186     public final Type exceptionType;
187     public final Type runtimeExceptionType;
188     public final Type classNotFoundExceptionType;
189     public final Type noClassDefFoundErrorType;
190     public final Type noSuchFieldErrorType;
191     public final Type assertionErrorType;
192     public final Type incompatibleClassChangeErrorType;
193     public final Type cloneNotSupportedExceptionType;
194     public final Type matchExceptionType;
195     public final Type annotationType;
196     public final TypeSymbol enumSym;
197     public final Type listType;
198     public final Type collectionsType;
199     public final Type comparableType;
200     public final Type comparatorType;
201     public final Type arraysType;
202     public final Type iterableType;
203     public final Type iteratorType;
204     public final Type annotationTargetType;
205     public final Type overrideType;
206     public final Type retentionType;
207     public final Type deprecatedType;
208     public final Type suppressWarningsType;
209     public final Type supplierType;
210     public final Type inheritedType;
211     public final Type profileType;
212     public final Type proprietaryType;
213     public final Type systemType;
214     public final Type autoCloseableType;
215     public final Type trustMeType;
216     public final Type lambdaMetafactory;
217     public final Type stringConcatFactory;
218     public final Type repeatableType;
219     public final Type documentedType;
220     public final Type elementTypeType;
221     public final Type functionalInterfaceType;
222     public final Type previewFeatureType;
223     public final Type previewFeatureInternalType;
224     public final Type restrictedType;
225     public final Type typeDescriptorType;
226     public final Type recordType;
227     public final Type switchBootstrapsType;
228     public final Type constantBootstrapsType;
229     public final Type classDescType;
230     public final Type enumDescType;
231 
232     // For serialization lint checking
233     public final Type objectStreamFieldType;
234     public final Type objectInputStreamType;
235     public final Type objectOutputStreamType;
236     public final Type ioExceptionType;
237     public final Type objectStreamExceptionType;
238     // For externalization lint checking
239     public final Type externalizableType;
240     public final Type objectInputType;
241     public final Type objectOutputType;
242 
243     // For string templates
244     public final Type stringTemplateType;
245     public final Type templateRuntimeType;
246     public final Type processorType;
247     public final Type linkageType;
248 
249     // valhalla
250     public final Type valueBasedType;
251     public final Type valueBasedInternalType;
252     public final Type migratedValueClassType;
253     public final Type migratedValueClassInternalType;
254     public final Type strictType;
255 
256     /** The symbol representing the length field of an array.
257      */
258     public final VarSymbol lengthVar;
259 
260     /** The symbol representing the final finalize method on enums */
261     public final MethodSymbol enumFinalFinalize;
262 
263     /** The symbol representing the close method on TWR AutoCloseable type */
264     public final MethodSymbol autoCloseableClose;
265 
266     /** The predefined type that belongs to a tag.
267      */
268     public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()];
269 
270     /** The name of the class that belongs to a basic type tag.
271      */
272     public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()];
273 
274     /** A hashtable containing the encountered top-level and member classes,
275      *  indexed by flat names. The table does not contain local classes.
276      *  It should be updated from the outside to reflect classes defined
277      *  by compiled source files.
278      */
279     private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>();
280 
281     /** A hashtable containing the encountered packages.
282      *  the table should be updated from outside to reflect packages defined
283      *  by compiled source files.
284      */
285     private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>();
286 
287     /** A hashtable giving the encountered modules.
288      */
289     private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>();
290 
291     private final Map<Types.UniqueType, VarSymbol> classFields = new HashMap<>();
292 
293     public VarSymbol getClassField(Type type, Types types) {
294         return classFields.computeIfAbsent(
295             new UniqueType(type, types), k -> {
296                 Type arg = null;
297                 if (type.getTag() == ARRAY || type.getTag() == CLASS)
298                     arg = types.erasure(type);
299                 else if (type.isPrimitiveOrVoid())
300                     arg = types.boxedClass(type).type;
301                 else
302                     throw new AssertionError(type);
303 
304                 Type t = new ClassType(
305                     classType.getEnclosingType(), List.of(arg), classType.tsym);
306                 return new VarSymbol(
307                     STATIC | PUBLIC | FINAL, names._class, t, type.tsym);
308             });
309     }
310 
311     public void initType(Type type, ClassSymbol c) {
312         type.tsym = c;
313         typeOfTag[type.getTag().ordinal()] = type;
314     }
315 
316     public void initType(Type type, String name) {
317         initType(
318             type,
319             new ClassSymbol(
320                 PUBLIC, names.fromString(name), type, rootPackage));
321     }
322 
323     public void initType(Type type, String name, String bname) {
324         initType(type, name);
325         boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname);
326     }
327 
328     /** The class symbol that owns all predefined symbols.
329      */
330     public final ClassSymbol predefClass;
331 
332     /** Enter a class into symbol table.
333      *  @param s The name of the class.
334      */
335     private Type enterClass(String s) {
336         return enterClass(java_base, names.fromString(s)).type;
337     }
338 
339     public void synthesizeEmptyInterfaceIfMissing(final Type type) {
340         final Completer completer = type.tsym.completer;
341         type.tsym.completer = new Completer() {
342             @Override
343             public void complete(Symbol sym) throws CompletionFailure {
344                 try {
345                     completer.complete(sym);
346                 } catch (CompletionFailure e) {
347                     sym.flags_field |= (PUBLIC | INTERFACE);
348                     ((ClassType) sym.type).supertype_field = objectType;
349                 }
350             }
351 
352             @Override
353             public boolean isTerminal() {
354                 return completer.isTerminal();
355             }
356         };
357     }
358 
359     public void synthesizeBoxTypeIfMissing(final Type type) {
360         ClassSymbol sym = enterClass(java_base, boxedName[type.getTag().ordinal()]);
361         final Completer completer = sym.completer;
362         sym.completer = new Completer() {
363             @Override
364             public void complete(Symbol sym) throws CompletionFailure {
365                 try {
366                     completer.complete(sym);
367                 } catch (CompletionFailure e) {
368                     sym.flags_field |= PUBLIC;
369                     ((ClassType) sym.type).supertype_field = objectType;
370                     MethodSymbol boxMethod =
371                         new MethodSymbol(PUBLIC | STATIC, names.valueOf,
372                                          new MethodType(List.of(type), sym.type,
373                                 List.nil(), methodClass),
374                             sym);
375                     sym.members().enter(boxMethod);
376                     MethodSymbol unboxMethod =
377                         new MethodSymbol(PUBLIC,
378                             type.tsym.name.append(names.Value), // x.intValue()
379                             new MethodType(List.nil(), type,
380                                 List.nil(), methodClass),
381                             sym);
382                     sym.members().enter(unboxMethod);
383                 }
384             }
385 
386             @Override
387             public boolean isTerminal() {
388                 return completer.isTerminal();
389             }
390         };
391     }
392 
393     // Enter a synthetic class that is used to mark classes in ct.sym.
394     // This class does not have a class file.
395     private Type enterSyntheticAnnotation(String name) {
396         // for now, leave the module null, to prevent problems from synthesizing the
397         // existence of a class in any specific module, including noModule
398         ClassType type = (ClassType)enterClass(java_base, names.fromString(name)).type;
399         ClassSymbol sym = (ClassSymbol)type.tsym;
400         sym.completer = Completer.NULL_COMPLETER;
401         sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
402         sym.erasure_field = type;
403         sym.members_field = WriteableScope.create(sym);
404         type.typarams_field = List.nil();
405         type.allparams_field = List.nil();
406         type.supertype_field = annotationType;
407         type.interfaces_field = List.nil();
408         return type;
409     }
410 
411     /** Constructor; enters all predefined identifiers and operators
412      *  into symbol table.
413      */
414     @SuppressWarnings("this-escape")
415     protected Symtab(Context context) throws CompletionFailure {
416         context.put(symtabKey, this);
417 
418         names = Names.instance(context);
419 
420         // Create the unknown type
421         unknownType = new UnknownType();
422 
423         messages = JavacMessages.instance(context);
424 
425         MissingInfoHandler missingInfoHandler = MissingInfoHandler.instance(context);
426 
427         Target target = Target.instance(context);
428         rootPackage = new RootPackageSymbol(names.empty, null,
429                                             missingInfoHandler,
430                                             target.runtimeUseNestAccess());
431 
432         // create the basic builtin symbols
433         unnamedModule = new ModuleSymbol(names.empty, null) {
434                 {
435                     directives = List.nil();
436                     exports = List.nil();
437                     provides = List.nil();
438                     uses = List.nil();
439                     ModuleSymbol java_base = enterModule(names.java_base);
440                     com.sun.tools.javac.code.Directive.RequiresDirective d =
441                             new com.sun.tools.javac.code.Directive.RequiresDirective(java_base,
442                                     EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED));
443                     requires = List.of(d);
444                 }
445                 @Override
446                 public String toString() {
447                     return messages.getLocalizedString("compiler.misc.unnamed.module");
448                 }
449             };
450         addRootPackageFor(unnamedModule);
451         unnamedModule.enclosedPackages = unnamedModule.enclosedPackages.prepend(unnamedModule.unnamedPackage);
452 
453         errModule = new ModuleSymbol(names.empty, null) {
454                 {
455                     directives = List.nil();
456                     exports = List.nil();
457                     provides = List.nil();
458                     uses = List.nil();
459                     ModuleSymbol java_base = enterModule(names.java_base);
460                     com.sun.tools.javac.code.Directive.RequiresDirective d =
461                             new com.sun.tools.javac.code.Directive.RequiresDirective(java_base,
462                                     EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED));
463                     requires = List.of(d);
464                 }
465             };
466         addRootPackageFor(errModule);
467 
468         noModule = new ModuleSymbol(names.empty, null) {
469             @Override public boolean isNoModule() {
470                 return true;
471             }
472         };
473         addRootPackageFor(noModule);
474 
475         noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) {
476             @Override @DefinedBy(Api.LANGUAGE_MODEL)
477             public <R, P> R accept(ElementVisitor<R, P> v, P p) {
478                 return v.visitUnknown(this, p);
479             }
480         };
481 
482         // create the error symbols
483         errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
484         errType = new ErrorType(errSymbol, Type.noType);
485 
486         unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage);
487         unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol);
488         unknownSymbol.type = unknownType;
489 
490         // initialize builtin types
491         initType(byteType, "byte", "Byte");
492         initType(shortType, "short", "Short");
493         initType(charType, "char", "Character");
494         initType(intType, "int", "Integer");
495         initType(longType, "long", "Long");
496         initType(floatType, "float", "Float");
497         initType(doubleType, "double", "Double");
498         initType(booleanType, "boolean", "Boolean");
499         initType(voidType, "void", "Void");
500         initType(botType, "<nulltype>");
501         initType(errType, errSymbol);
502         initType(unknownType, unknownSymbol);
503 
504         // the builtin class of all arrays
505         arrayClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Array, noSymbol);
506 
507         // VGJ
508         boundClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Bound, noSymbol);
509         boundClass.members_field = new Scope.ErrorScope(boundClass);
510 
511         // the builtin class of all methods
512         methodClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Method, noSymbol);
513         methodClass.members_field = new Scope.ErrorScope(boundClass);
514 
515         // Create class to hold all predefined constants and operations.
516         predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage);
517         WriteableScope scope = WriteableScope.create(predefClass);
518         predefClass.members_field = scope;
519 
520         // Get the initial completer for Symbols from the ClassFinder
521         initialCompleter = ClassFinder.instance(context).getCompleter();
522         rootPackage.members_field = WriteableScope.create(rootPackage);
523 
524         // Enter symbols for basic types.
525         scope.enter(byteType.tsym);
526         scope.enter(shortType.tsym);
527         scope.enter(charType.tsym);
528         scope.enter(intType.tsym);
529         scope.enter(longType.tsym);
530         scope.enter(floatType.tsym);
531         scope.enter(doubleType.tsym);
532         scope.enter(booleanType.tsym);
533         scope.enter(errType.tsym);
534 
535         // Enter symbol for the errSymbol
536         scope.enter(errSymbol);
537 
538         Source source = Source.instance(context);
539         if (Feature.MODULES.allowedInSource(source)) {
540             java_base = enterModule(names.java_base);
541             //avoid completing java.base during the Symtab initialization
542             java_base.completer = Completer.NULL_COMPLETER;
543             java_base.visiblePackages = Collections.emptyMap();
544         } else {
545             java_base = noModule;
546         }
547 
548         // Get the initial completer for ModuleSymbols from Modules
549         moduleCompleter = Modules.instance(context).getCompleter();
550 
551         // Enter predefined classes. All are assumed to be in the java.base module.
552         objectType = enterClass("java.lang.Object");
553         objectMethodsType = enterClass("java.lang.runtime.ObjectMethods");
554         exactConversionsSupportType = enterClass("java.lang.runtime.ExactConversionsSupport");
555         objectsType = enterClass("java.util.Objects");
556         classType = enterClass("java.lang.Class");
557         stringType = enterClass("java.lang.String");
558         stringBufferType = enterClass("java.lang.StringBuffer");
559         stringBuilderType = enterClass("java.lang.StringBuilder");
560         cloneableType = enterClass("java.lang.Cloneable");
561         throwableType = enterClass("java.lang.Throwable");
562         serializableType = enterClass("java.io.Serializable");
563         serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda");
564         varHandleType = enterClass("java.lang.invoke.VarHandle");
565         methodHandleType = enterClass("java.lang.invoke.MethodHandle");
566         methodHandlesType = enterClass("java.lang.invoke.MethodHandles");
567         methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup");
568         methodTypeType = enterClass("java.lang.invoke.MethodType");
569         errorType = enterClass("java.lang.Error");
570         illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException");
571         interruptedExceptionType = enterClass("java.lang.InterruptedException");
572         exceptionType = enterClass("java.lang.Exception");
573         runtimeExceptionType = enterClass("java.lang.RuntimeException");
574         classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
575         noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
576         noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError");
577         assertionErrorType = enterClass("java.lang.AssertionError");
578         incompatibleClassChangeErrorType = enterClass("java.lang.IncompatibleClassChangeError");
579         cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
580         matchExceptionType = enterClass("java.lang.MatchException");
581         annotationType = enterClass("java.lang.annotation.Annotation");
582         classLoaderType = enterClass("java.lang.ClassLoader");
583         enumSym = enterClass(java_base, names.java_lang_Enum);
584         enumFinalFinalize =
585             new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL,
586                              names.finalize,
587                              new MethodType(List.nil(), voidType,
588                                             List.nil(), methodClass),
589                              enumSym);
590         listType = enterClass("java.util.List");
591         collectionsType = enterClass("java.util.Collections");
592         comparableType = enterClass("java.lang.Comparable");
593         comparatorType = enterClass("java.util.Comparator");
594         arraysType = enterClass("java.util.Arrays");
595         iterableType = enterClass("java.lang.Iterable");
596         iteratorType = enterClass("java.util.Iterator");
597         annotationTargetType = enterClass("java.lang.annotation.Target");
598         overrideType = enterClass("java.lang.Override");
599         retentionType = enterClass("java.lang.annotation.Retention");
600         deprecatedType = enterClass("java.lang.Deprecated");
601         suppressWarningsType = enterClass("java.lang.SuppressWarnings");
602         supplierType = enterClass("java.util.function.Supplier");
603         inheritedType = enterClass("java.lang.annotation.Inherited");
604         repeatableType = enterClass("java.lang.annotation.Repeatable");
605         documentedType = enterClass("java.lang.annotation.Documented");
606         elementTypeType = enterClass("java.lang.annotation.ElementType");
607         systemType = enterClass("java.lang.System");
608         autoCloseableType = enterClass("java.lang.AutoCloseable");
609         autoCloseableClose = new MethodSymbol(PUBLIC,
610                              names.close,
611                              new MethodType(List.nil(), voidType,
612                                             List.of(exceptionType), methodClass),
613                              autoCloseableType.tsym);
614         trustMeType = enterClass("java.lang.SafeVarargs");
615         nativeHeaderType = enterClass("java.lang.annotation.Native");
616         lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
617         stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory");
618         functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
619         previewFeatureType = enterClass("jdk.internal.javac.PreviewFeature");
620         previewFeatureInternalType = enterSyntheticAnnotation("jdk.internal.PreviewFeature+Annotation");
621         restrictedType = enterClass("jdk.internal.javac.Restricted");
622         typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
623         recordType = enterClass("java.lang.Record");
624         switchBootstrapsType = enterClass("java.lang.runtime.SwitchBootstraps");
625         constantBootstrapsType = enterClass("java.lang.invoke.ConstantBootstraps");
626         valueBasedType = enterClass("jdk.internal.ValueBased");
627         valueBasedInternalType = enterSyntheticAnnotation("jdk.internal.ValueBased+Annotation");
628         strictType = enterSyntheticAnnotation("jdk.internal.vm.annotation.Strict");
629         migratedValueClassType = enterClass("jdk.internal.MigratedValueClass");
630         migratedValueClassInternalType = enterSyntheticAnnotation("jdk.internal.MigratedValueClass+Annotation");
631         classDescType = enterClass("java.lang.constant.ClassDesc");
632         enumDescType = enterClass("java.lang.Enum$EnumDesc");
633         // For serialization lint checking
634         objectStreamFieldType = enterClass("java.io.ObjectStreamField");
635         objectInputStreamType = enterClass("java.io.ObjectInputStream");
636         objectOutputStreamType = enterClass("java.io.ObjectOutputStream");
637         ioExceptionType = enterClass("java.io.IOException");
638         objectStreamExceptionType = enterClass("java.io.ObjectStreamException");
639         externalizableType = enterClass("java.io.Externalizable");
640         objectInputType  = enterClass("java.io.ObjectInput");
641         objectOutputType = enterClass("java.io.ObjectOutput");
642         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
643         synthesizeEmptyInterfaceIfMissing(cloneableType);
644         synthesizeEmptyInterfaceIfMissing(serializableType);
645         synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
646         synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
647         synthesizeEmptyInterfaceIfMissing(stringConcatFactory);
648         synthesizeBoxTypeIfMissing(doubleType);
649         synthesizeBoxTypeIfMissing(floatType);
650         synthesizeBoxTypeIfMissing(voidType);
651 
652         // For string templates
653         stringTemplateType = enterClass("java.lang.StringTemplate");
654         templateRuntimeType = enterClass("java.lang.runtime.TemplateRuntime");
655         processorType = enterClass("java.lang.StringTemplate$Processor");
656         linkageType = enterClass("java.lang.StringTemplate$Processor$Linkage");
657 
658         // Enter a synthetic class that is used to mark internal
659         // proprietary classes in ct.sym.  This class does not have a
660         // class file.
661         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
662 
663         // Enter a synthetic class that is used to provide profile info for
664         // classes in ct.sym.  This class does not have a class file.
665         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
666         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
667         profileType.tsym.members().enter(m);
668 
669         // Enter a class for arrays.
670         // The class implements java.lang.Cloneable and java.io.Serializable.
671         // It has a final length field and a clone method.
672         ClassType arrayClassType = (ClassType)arrayClass.type;
673         arrayClassType.supertype_field = objectType;
674         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
675         arrayClass.members_field = WriteableScope.create(arrayClass);
676         lengthVar = new VarSymbol(
677             PUBLIC | FINAL,
678             names.length,
679             intType,
680             arrayClass);
681         arrayClass.members().enter(lengthVar);
682         arrayCloneMethod = new MethodSymbol(
683             PUBLIC,
684             names.clone,
685             new MethodType(List.nil(), objectType,
686                            List.nil(), methodClass),
687             arrayClass);
688         arrayClass.members().enter(arrayCloneMethod);
689 
690         if (java_base != noModule)
691             java_base.completer = moduleCompleter::complete; //bootstrap issues
692 
693     }
694 
695     /** Define a new class given its name and owner.
696      */
697     public ClassSymbol defineClass(Name name, Symbol owner) {
698         ClassSymbol c = new ClassSymbol(0, name, owner);
699         c.completer = initialCompleter;
700         return c;
701     }
702 
703     /** Create a new toplevel or member class symbol with given name
704      *  and owner and enter in `classes' unless already there.
705      */
706     public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) {
707         Assert.checkNonNull(msym);
708         Name flatname = TypeSymbol.formFlatName(name, owner);
709         ClassSymbol c = getClass(msym, flatname);
710         if (c == null) {
711             c = defineClass(name, owner);
712             doEnterClass(msym, c);
713         } else if ((c.name != name || c.owner != owner) && owner.kind == TYP &&
714                    c.owner.kind == PCK && ((c.flags_field & FROM_SOURCE) == 0)) {
715             // reassign fields of classes that might have been loaded with
716             // their flat names.
717             c.owner.members().remove(c);
718             c.name = name;
719             c.owner = owner;
720             c.fullname = ClassSymbol.formFullName(name, owner);
721         }
722         return c;
723     }
724 
725     public ClassSymbol getClass(ModuleSymbol msym, Name flatName) {
726         Assert.checkNonNull(msym, flatName::toString);
727         return classes.getOrDefault(flatName, Collections.emptyMap()).get(msym);
728     }
729 
730     public PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName) {
731         return lookupPackage(msym, flatName, false);
732     }
733 
734     private PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName, boolean onlyExisting) {
735         Assert.checkNonNull(msym);
736 
737         if (flatName.isEmpty()) {
738             //unnamed packages only from the current module - visiblePackages contains *root* package, not unnamed package!
739             return msym.unnamedPackage;
740         }
741 
742         if (msym == noModule) {
743             return enterPackage(msym, flatName);
744         }
745 
746         msym.complete();
747 
748         PackageSymbol pack;
749 
750         pack = msym.visiblePackages.get(flatName);
751 
752         if (pack != null)
753             return pack;
754 
755         pack = getPackage(msym, flatName);
756 
757         if ((pack != null && pack.exists()) || onlyExisting)
758             return pack;
759 
760         boolean dependsOnUnnamed = msym.requires != null &&
761                                    msym.requires.stream()
762                                                 .map(rd -> rd.module)
763                                                 .anyMatch(mod -> mod == unnamedModule);
764 
765         if (dependsOnUnnamed) {
766             //msyms depends on the unnamed module, for which we generally don't know
767             //the list of packages it "exports" ahead of time. So try to lookup the package in the
768             //current module, and in the unnamed module and see if it exists in one of them
769             PackageSymbol unnamedPack = getPackage(unnamedModule, flatName);
770 
771             if (unnamedPack != null && unnamedPack.exists()) {
772                 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack);
773                 return unnamedPack;
774             }
775 
776             pack = enterPackage(msym, flatName);
777             pack.complete();
778             if (pack.exists())
779                 return pack;
780 
781             unnamedPack = enterPackage(unnamedModule, flatName);
782             unnamedPack.complete();
783             if (unnamedPack.exists()) {
784                 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack);
785                 return unnamedPack;
786             }
787 
788             return pack;
789         }
790 
791         return enterPackage(msym, flatName);
792     }
793 
794     private static final Map<ModuleSymbol, ClassSymbol> EMPTY = new HashMap<>();
795 
796     public void removeClass(ModuleSymbol msym, Name flatName) {
797         classes.getOrDefault(flatName, EMPTY).remove(msym);
798     }
799 
800     public Iterable<ClassSymbol> getAllClasses() {
801         return () -> Iterators.createCompoundIterator(classes.values(), v -> v.values().iterator());
802     }
803 
804     private void doEnterClass(ModuleSymbol msym, ClassSymbol cs) {
805         classes.computeIfAbsent(cs.flatname, n -> new HashMap<>()).put(msym, cs);
806     }
807 
808     /** Create a new member or toplevel class symbol with given flat name
809      *  and enter in `classes' unless already there.
810      */
811     public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) {
812         Assert.checkNonNull(msym);
813         PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname));
814         Assert.checkNonNull(ps);
815         Assert.checkNonNull(ps.modle);
816         ClassSymbol c = getClass(ps.modle, flatname);
817         if (c == null) {
818             c = defineClass(Convert.shortName(flatname), ps);
819             doEnterClass(ps.modle, c);
820             return c;
821         } else
822             return c;
823     }
824 
825     /** Check to see if a package exists, given its fully qualified name.
826      */
827     public boolean packageExists(ModuleSymbol msym, Name fullname) {
828         Assert.checkNonNull(msym);
829         PackageSymbol pack = lookupPackage(msym, fullname, true);
830         return pack != null && pack.exists();
831     }
832 
833     /** Make a package, given its fully qualified name.
834      */
835     public PackageSymbol enterPackage(ModuleSymbol currModule, Name fullname) {
836         Assert.checkNonNull(currModule);
837         PackageSymbol p = getPackage(currModule, fullname);
838         if (p == null) {
839             Assert.check(!fullname.isEmpty(), () -> "rootPackage missing!; currModule: " + currModule);
840             p = new PackageSymbol(
841                     Convert.shortName(fullname),
842                     enterPackage(currModule, Convert.packagePart(fullname)));
843             p.completer = initialCompleter;
844             p.modle = currModule;
845             doEnterPackage(currModule, p);
846         }
847         return p;
848     }
849 
850     private void doEnterPackage(ModuleSymbol msym, PackageSymbol pack) {
851         packages.computeIfAbsent(pack.fullname, n -> new HashMap<>()).put(msym, pack);
852         msym.enclosedPackages = msym.enclosedPackages.prepend(pack);
853     }
854 
855     private void addRootPackageFor(ModuleSymbol module) {
856         doEnterPackage(module, rootPackage);
857         PackageSymbol unnamedPackage = new PackageSymbol(names.empty, rootPackage) {
858                 @Override
859                 public String toString() {
860                     return messages.getLocalizedString("compiler.misc.unnamed.package");
861                 }
862             };
863         unnamedPackage.modle = module;
864         //we cannot use a method reference below, as initialCompleter might be null now
865         unnamedPackage.completer = s -> initialCompleter.complete(s);
866         unnamedPackage.flags_field |= EXISTS;
867         module.unnamedPackage = unnamedPackage;
868     }
869 
870     public PackageSymbol getPackage(ModuleSymbol module, Name fullname) {
871         return packages.getOrDefault(fullname, Collections.emptyMap()).get(module);
872     }
873 
874     public ModuleSymbol enterModule(Name name) {
875         ModuleSymbol msym = modules.get(name);
876         if (msym == null) {
877             msym = ModuleSymbol.create(name, names.module_info);
878             addRootPackageFor(msym);
879             msym.completer = s -> moduleCompleter.complete(s); //bootstrap issues
880             modules.put(name, msym);
881         }
882         return msym;
883     }
884 
885     public ModuleSymbol getModule(Name name) {
886         return modules.get(name);
887     }
888 
889     //temporary:
890     public ModuleSymbol inferModule(Name packageName) {
891         if (packageName.isEmpty())
892             return java_base == noModule ? noModule : unnamedModule;//!
893 
894         ModuleSymbol msym = null;
895         Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
896         if (map == null)
897             return null;
898         for (Map.Entry<ModuleSymbol,PackageSymbol> e: map.entrySet()) {
899             if (!e.getValue().members().isEmpty()) {
900                 if (msym == null) {
901                     msym = e.getKey();
902                 } else {
903                     return null;
904                 }
905             }
906         }
907         return msym;
908     }
909 
910     public List<ModuleSymbol> listPackageModules(Name packageName) {
911         if (packageName.isEmpty())
912             return List.nil();
913 
914         List<ModuleSymbol> result = List.nil();
915         Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
916         if (map != null) {
917             for (Map.Entry<ModuleSymbol, PackageSymbol> e: map.entrySet()) {
918                 if (!e.getValue().members().isEmpty()) {
919                     result = result.prepend(e.getKey());
920                 }
921             }
922         }
923         return result;
924     }
925 
926     public Collection<ModuleSymbol> getAllModules() {
927         return modules.values();
928     }
929 
930     public Iterable<ClassSymbol> getClassesForName(Name candidate) {
931         return classes.getOrDefault(candidate, Collections.emptyMap()).values();
932     }
933 
934     public Iterable<PackageSymbol> getPackagesForName(Name candidate) {
935         return packages.getOrDefault(candidate, Collections.emptyMap()).values();
936     }
937 }