1 /*
   2  * Copyright (c) 1999, 2019, 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 objectsType;
 167     public final Type classType;
 168     public final Type classLoaderType;
 169     public final Type stringType;
 170     public final Type stringBufferType;
 171     public final Type stringBuilderType;
 172     public final Type cloneableType;
 173     public final Type serializableType;
 174     public final Type serializedLambdaType;
 175     public final Type varHandleType;
 176     public final Type methodHandleType;
 177     public final Type methodHandleLookupType;
 178     public final Type methodTypeType;
 179     public final Type nativeHeaderType;
 180     public final Type throwableType;
 181     public final Type errorType;
 182     public final Type interruptedExceptionType;
 183     public final Type illegalArgumentExceptionType;
 184     public final Type exceptionType;
 185     public final Type runtimeExceptionType;
 186     public final Type classNotFoundExceptionType;
 187     public final Type noClassDefFoundErrorType;
 188     public final Type noSuchFieldErrorType;
 189     public final Type assertionErrorType;
 190     public final Type incompatibleClassChangeErrorType;
 191     public final Type cloneNotSupportedExceptionType;
 192     public final Type annotationType;
 193     public final TypeSymbol enumSym;
 194     public final Type listType;
 195     public final Type collectionsType;
 196     public final Type comparableType;
 197     public final Type comparatorType;
 198     public final Type arraysType;
 199     public final Type iterableType;
 200     public final Type iteratorType;
 201     public final Type annotationTargetType;
 202     public final Type overrideType;
 203     public final Type retentionType;
 204     public final Type deprecatedType;
 205     public final Type suppressWarningsType;
 206     public final Type supplierType;
 207     public final Type inheritedType;
 208     public final Type profileType;
 209     public final Type proprietaryType;
 210     public final Type systemType;
 211     public final Type autoCloseableType;
 212     public final Type trustMeType;
 213     public final Type lambdaMetafactory;
 214     public final Type stringConcatFactory;
 215     public final Type repeatableType;
 216     public final Type documentedType;
 217     public final Type elementTypeType;
 218     public final Type functionalInterfaceType;
 219     public final Type previewFeatureType;
 220     public final Type typeDescriptorType;
 221     public final Type recordType;
 222 
 223     /** The symbol representing the length field of an array.
 224      */
 225     public final VarSymbol lengthVar;
 226 
 227     /** The symbol representing the final finalize method on enums */
 228     public final MethodSymbol enumFinalFinalize;
 229 
 230     /** The symbol representing the close method on TWR AutoCloseable type */
 231     public final MethodSymbol autoCloseableClose;
 232 
 233     /** The predefined type that belongs to a tag.
 234      */
 235     public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()];
 236 
 237     /** The name of the class that belongs to a basic type tag.
 238      */
 239     public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()];
 240 
 241     /** A hashtable containing the encountered top-level and member classes,
 242      *  indexed by flat names. The table does not contain local classes.
 243      *  It should be updated from the outside to reflect classes defined
 244      *  by compiled source files.
 245      */
 246     private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>();
 247 
 248     /** A hashtable containing the encountered packages.
 249      *  the table should be updated from outside to reflect packages defined
 250      *  by compiled source files.
 251      */
 252     private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>();
 253 
 254     /** A hashtable giving the encountered modules.
 255      */
 256     private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>();
 257 
 258     private final Map<Types.UniqueType, VarSymbol> classFields = new HashMap<>();
 259 
 260     public VarSymbol getClassField(Type type, Types types) {
 261         return classFields.computeIfAbsent(
 262             new UniqueType(type, types), k -> {
 263                 Type arg = null;
 264                 if (type.getTag() == ARRAY || type.getTag() == CLASS)
 265                     arg = types.erasure(type);
 266                 else if (type.isPrimitiveOrVoid())
 267                     arg = types.boxedClass(type).type;
 268                 else
 269                     throw new AssertionError(type);
 270 
 271                 Type t = new ClassType(
 272                     classType.getEnclosingType(), List.of(arg), classType.tsym);
 273                 return new VarSymbol(
 274                     STATIC | PUBLIC | FINAL, names._class, t, type.tsym);
 275             });
 276     }
 277 
 278     public void initType(Type type, ClassSymbol c) {
 279         type.tsym = c;
 280         typeOfTag[type.getTag().ordinal()] = type;
 281     }
 282 
 283     public void initType(Type type, String name) {
 284         initType(
 285             type,
 286             new ClassSymbol(
 287                 PUBLIC, names.fromString(name), type, rootPackage));
 288     }
 289 
 290     public void initType(Type type, String name, String bname) {
 291         initType(type, name);
 292         boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname);
 293     }
 294 
 295     /** The class symbol that owns all predefined symbols.
 296      */
 297     public final ClassSymbol predefClass;
 298 
 299     /** Enter a class into symbol table.
 300      *  @param s The name of the class.
 301      */
 302     private Type enterClass(String s) {
 303         return enterClass(java_base, names.fromString(s)).type;
 304     }
 305 
 306     public void synthesizeEmptyInterfaceIfMissing(final Type type) {
 307         final Completer completer = type.tsym.completer;
 308         type.tsym.completer = new Completer() {
 309             @Override
 310             public void complete(Symbol sym) throws CompletionFailure {
 311                 try {
 312                     completer.complete(sym);
 313                 } catch (CompletionFailure e) {
 314                     sym.flags_field |= (PUBLIC | INTERFACE);
 315                     ((ClassType) sym.type).supertype_field = objectType;
 316                 }
 317             }
 318 
 319             @Override
 320             public boolean isTerminal() {
 321                 return completer.isTerminal();
 322             }
 323         };
 324     }
 325 
 326     public void synthesizeBoxTypeIfMissing(final Type type) {
 327         ClassSymbol sym = enterClass(java_base, boxedName[type.getTag().ordinal()]);
 328         final Completer completer = sym.completer;
 329         sym.completer = new Completer() {
 330             @Override
 331             public void complete(Symbol sym) throws CompletionFailure {
 332                 try {
 333                     completer.complete(sym);
 334                 } catch (CompletionFailure e) {
 335                     sym.flags_field |= PUBLIC;
 336                     ((ClassType) sym.type).supertype_field = objectType;
 337                     MethodSymbol boxMethod =
 338                         new MethodSymbol(PUBLIC | STATIC, names.valueOf,
 339                                          new MethodType(List.of(type), sym.type,
 340                                 List.nil(), methodClass),
 341                             sym);
 342                     sym.members().enter(boxMethod);
 343                     MethodSymbol unboxMethod =
 344                         new MethodSymbol(PUBLIC,
 345                             type.tsym.name.append(names.Value), // x.intValue()
 346                             new MethodType(List.nil(), type,
 347                                 List.nil(), methodClass),
 348                             sym);
 349                     sym.members().enter(unboxMethod);
 350                 }
 351             }
 352 
 353             @Override
 354             public boolean isTerminal() {
 355                 return completer.isTerminal();
 356             }
 357         };
 358     }
 359 
 360     // Enter a synthetic class that is used to mark classes in ct.sym.
 361     // This class does not have a class file.
 362     private Type enterSyntheticAnnotation(String name) {
 363         // for now, leave the module null, to prevent problems from synthesizing the
 364         // existence of a class in any specific module, including noModule
 365         ClassType type = (ClassType)enterClass(java_base, names.fromString(name)).type;
 366         ClassSymbol sym = (ClassSymbol)type.tsym;
 367         sym.completer = Completer.NULL_COMPLETER;
 368         sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
 369         sym.erasure_field = type;
 370         sym.members_field = WriteableScope.create(sym);
 371         type.typarams_field = List.nil();
 372         type.allparams_field = List.nil();
 373         type.supertype_field = annotationType;
 374         type.interfaces_field = List.nil();
 375         return type;
 376     }
 377 
 378     /** Constructor; enters all predefined identifiers and operators
 379      *  into symbol table.
 380      */
 381     protected Symtab(Context context) throws CompletionFailure {
 382         context.put(symtabKey, this);
 383 
 384         names = Names.instance(context);
 385 
 386         // Create the unknown type
 387         unknownType = new UnknownType();
 388 
 389         messages = JavacMessages.instance(context);
 390 
 391         MissingInfoHandler missingInfoHandler = MissingInfoHandler.instance(context);
 392 
 393         Target target = Target.instance(context);
 394         rootPackage = new RootPackageSymbol(names.empty, null,
 395                                             missingInfoHandler,
 396                                             target.runtimeUseNestAccess());
 397 
 398         // create the basic builtin symbols
 399         unnamedModule = new ModuleSymbol(names.empty, null) {
 400                 {
 401                     directives = List.nil();
 402                     exports = List.nil();
 403                     provides = List.nil();
 404                     uses = List.nil();
 405                     ModuleSymbol java_base = enterModule(names.java_base);
 406                     com.sun.tools.javac.code.Directive.RequiresDirective d =
 407                             new com.sun.tools.javac.code.Directive.RequiresDirective(java_base,
 408                                     EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED));
 409                     requires = List.of(d);
 410                 }
 411                 @Override
 412                 public String toString() {
 413                     return messages.getLocalizedString("compiler.misc.unnamed.module");
 414                 }
 415             };
 416         addRootPackageFor(unnamedModule);
 417         unnamedModule.enclosedPackages = unnamedModule.enclosedPackages.prepend(unnamedModule.unnamedPackage);
 418 
 419         errModule = new ModuleSymbol(names.empty, null) {
 420                 {
 421                     directives = List.nil();
 422                     exports = List.nil();
 423                     provides = List.nil();
 424                     uses = List.nil();
 425                     ModuleSymbol java_base = enterModule(names.java_base);
 426                     com.sun.tools.javac.code.Directive.RequiresDirective d =
 427                             new com.sun.tools.javac.code.Directive.RequiresDirective(java_base,
 428                                     EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED));
 429                     requires = List.of(d);
 430                 }
 431             };
 432         addRootPackageFor(errModule);
 433 
 434         noModule = new ModuleSymbol(names.empty, null) {
 435             @Override public boolean isNoModule() {
 436                 return true;
 437             }
 438         };
 439         addRootPackageFor(noModule);
 440 
 441         noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) {
 442             @Override @DefinedBy(Api.LANGUAGE_MODEL)
 443             public <R, P> R accept(ElementVisitor<R, P> v, P p) {
 444                 return v.visitUnknown(this, p);
 445             }
 446         };
 447 
 448         // create the error symbols
 449         errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
 450         errType = new ErrorType(errSymbol, Type.noType);
 451 
 452         unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage);
 453         unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol);
 454         unknownSymbol.type = unknownType;
 455 
 456         // initialize builtin types
 457         initType(byteType, "byte", "Byte");
 458         initType(shortType, "short", "Short");
 459         initType(charType, "char", "Character");
 460         initType(intType, "int", "Integer");
 461         initType(longType, "long", "Long");
 462         initType(floatType, "float", "Float");
 463         initType(doubleType, "double", "Double");
 464         initType(booleanType, "boolean", "Boolean");
 465         initType(voidType, "void", "Void");
 466         initType(botType, "<nulltype>");
 467         initType(errType, errSymbol);
 468         initType(unknownType, unknownSymbol);
 469 
 470         // the builtin class of all arrays
 471         arrayClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Array, noSymbol);
 472 
 473         // VGJ
 474         boundClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Bound, noSymbol);
 475         boundClass.members_field = new Scope.ErrorScope(boundClass);
 476 
 477         // the builtin class of all methods
 478         methodClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Method, noSymbol);
 479         methodClass.members_field = new Scope.ErrorScope(boundClass);
 480 
 481         // Create class to hold all predefined constants and operations.
 482         predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage);
 483         WriteableScope scope = WriteableScope.create(predefClass);
 484         predefClass.members_field = scope;
 485 
 486         // Get the initial completer for Symbols from the ClassFinder
 487         initialCompleter = ClassFinder.instance(context).getCompleter();
 488         rootPackage.members_field = WriteableScope.create(rootPackage);
 489 
 490         // Enter symbols for basic types.
 491         scope.enter(byteType.tsym);
 492         scope.enter(shortType.tsym);
 493         scope.enter(charType.tsym);
 494         scope.enter(intType.tsym);
 495         scope.enter(longType.tsym);
 496         scope.enter(floatType.tsym);
 497         scope.enter(doubleType.tsym);
 498         scope.enter(booleanType.tsym);
 499         scope.enter(errType.tsym);
 500 
 501         // Enter symbol for the errSymbol
 502         scope.enter(errSymbol);
 503 
 504         Source source = Source.instance(context);
 505         if (Feature.MODULES.allowedInSource(source)) {
 506             java_base = enterModule(names.java_base);
 507             //avoid completing java.base during the Symtab initialization
 508             java_base.completer = Completer.NULL_COMPLETER;
 509             java_base.visiblePackages = Collections.emptyMap();
 510         } else {
 511             java_base = noModule;
 512         }
 513 
 514         // Get the initial completer for ModuleSymbols from Modules
 515         moduleCompleter = Modules.instance(context).getCompleter();
 516 
 517         // Enter predefined classes. All are assumed to be in the java.base module.
 518         objectType = enterClass("java.lang.Object");
 519         objectMethodsType = enterClass("java.lang.runtime.ObjectMethods");
 520         objectsType = enterClass("java.util.Objects");
 521         classType = enterClass("java.lang.Class");
 522         stringType = enterClass("java.lang.String");
 523         stringBufferType = enterClass("java.lang.StringBuffer");
 524         stringBuilderType = enterClass("java.lang.StringBuilder");
 525         cloneableType = enterClass("java.lang.Cloneable");
 526         throwableType = enterClass("java.lang.Throwable");
 527         serializableType = enterClass("java.io.Serializable");
 528         serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda");
 529         varHandleType = enterClass("java.lang.invoke.VarHandle");
 530         methodHandleType = enterClass("java.lang.invoke.MethodHandle");
 531         methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup");
 532         methodTypeType = enterClass("java.lang.invoke.MethodType");
 533         errorType = enterClass("java.lang.Error");
 534         illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException");
 535         interruptedExceptionType = enterClass("java.lang.InterruptedException");
 536         exceptionType = enterClass("java.lang.Exception");
 537         runtimeExceptionType = enterClass("java.lang.RuntimeException");
 538         classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
 539         noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
 540         noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError");
 541         assertionErrorType = enterClass("java.lang.AssertionError");
 542         incompatibleClassChangeErrorType = enterClass("java.lang.IncompatibleClassChangeError");
 543         cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
 544         annotationType = enterClass("java.lang.annotation.Annotation");
 545         classLoaderType = enterClass("java.lang.ClassLoader");
 546         enumSym = enterClass(java_base, names.java_lang_Enum);
 547         enumFinalFinalize =
 548             new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL,
 549                              names.finalize,
 550                              new MethodType(List.nil(), voidType,
 551                                             List.nil(), methodClass),
 552                              enumSym);
 553         listType = enterClass("java.util.List");
 554         collectionsType = enterClass("java.util.Collections");
 555         comparableType = enterClass("java.lang.Comparable");
 556         comparatorType = enterClass("java.util.Comparator");
 557         arraysType = enterClass("java.util.Arrays");
 558         iterableType = enterClass("java.lang.Iterable");
 559         iteratorType = enterClass("java.util.Iterator");
 560         annotationTargetType = enterClass("java.lang.annotation.Target");
 561         overrideType = enterClass("java.lang.Override");
 562         retentionType = enterClass("java.lang.annotation.Retention");
 563         deprecatedType = enterClass("java.lang.Deprecated");
 564         suppressWarningsType = enterClass("java.lang.SuppressWarnings");
 565         supplierType = enterClass("java.util.function.Supplier");
 566         inheritedType = enterClass("java.lang.annotation.Inherited");
 567         repeatableType = enterClass("java.lang.annotation.Repeatable");
 568         documentedType = enterClass("java.lang.annotation.Documented");
 569         elementTypeType = enterClass("java.lang.annotation.ElementType");
 570         systemType = enterClass("java.lang.System");
 571         autoCloseableType = enterClass("java.lang.AutoCloseable");
 572         autoCloseableClose = new MethodSymbol(PUBLIC,
 573                              names.close,
 574                              new MethodType(List.nil(), voidType,
 575                                             List.of(exceptionType), methodClass),
 576                              autoCloseableType.tsym);
 577         trustMeType = enterClass("java.lang.SafeVarargs");
 578         nativeHeaderType = enterClass("java.lang.annotation.Native");
 579         lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
 580         stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory");
 581         functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
 582         previewFeatureType = enterClass("jdk.internal.PreviewFeature");
 583         typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
 584         recordType = enterClass("java.lang.Record");
 585 
 586         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
 587         synthesizeEmptyInterfaceIfMissing(cloneableType);
 588         synthesizeEmptyInterfaceIfMissing(serializableType);
 589         synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
 590         synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
 591         synthesizeEmptyInterfaceIfMissing(stringConcatFactory);
 592         synthesizeBoxTypeIfMissing(doubleType);
 593         synthesizeBoxTypeIfMissing(floatType);
 594         synthesizeBoxTypeIfMissing(voidType);
 595 
 596         // Enter a synthetic class that is used to mark internal
 597         // proprietary classes in ct.sym.  This class does not have a
 598         // class file.
 599         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
 600 
 601         // Enter a synthetic class that is used to provide profile info for
 602         // classes in ct.sym.  This class does not have a class file.
 603         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
 604         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
 605         profileType.tsym.members().enter(m);
 606 
 607         // Enter a class for arrays.
 608         // The class implements java.lang.Cloneable and java.io.Serializable.
 609         // It has a final length field and a clone method.
 610         ClassType arrayClassType = (ClassType)arrayClass.type;
 611         arrayClassType.supertype_field = objectType;
 612         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
 613         arrayClass.members_field = WriteableScope.create(arrayClass);
 614         lengthVar = new VarSymbol(
 615             PUBLIC | FINAL,
 616             names.length,
 617             intType,
 618             arrayClass);
 619         arrayClass.members().enter(lengthVar);
 620         arrayCloneMethod = new MethodSymbol(
 621             PUBLIC,
 622             names.clone,
 623             new MethodType(List.nil(), objectType,
 624                            List.nil(), methodClass),
 625             arrayClass);
 626         arrayClass.members().enter(arrayCloneMethod);
 627 
 628         if (java_base != noModule)
 629             java_base.completer = moduleCompleter::complete; //bootstrap issues
 630 
 631     }
 632 
 633     /** Define a new class given its name and owner.
 634      */
 635     public ClassSymbol defineClass(Name name, Symbol owner) {
 636         ClassSymbol c = new ClassSymbol(0, name, owner);
 637         c.completer = initialCompleter;
 638         return c;
 639     }
 640 
 641     /** Create a new toplevel or member class symbol with given name
 642      *  and owner and enter in `classes' unless already there.
 643      */
 644     public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) {
 645         Assert.checkNonNull(msym);
 646         Name flatname = TypeSymbol.formFlatName(name, owner);
 647         ClassSymbol c = getClass(msym, flatname);
 648         if (c == null) {
 649             c = defineClass(name, owner);
 650             doEnterClass(msym, c);
 651         } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
 652             // reassign fields of classes that might have been loaded with
 653             // their flat names.
 654             c.owner.members().remove(c);
 655             c.name = name;
 656             c.owner = owner;
 657             c.fullname = ClassSymbol.formFullName(name, owner);
 658         }
 659         return c;
 660     }
 661 
 662     public ClassSymbol getClass(ModuleSymbol msym, Name flatName) {
 663         Assert.checkNonNull(msym, flatName::toString);
 664         return classes.getOrDefault(flatName, Collections.emptyMap()).get(msym);
 665     }
 666 
 667     public PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName) {
 668         Assert.checkNonNull(msym);
 669 
 670         if (flatName.isEmpty()) {
 671             //unnamed packages only from the current module - visiblePackages contains *root* package, not unnamed package!
 672             return msym.unnamedPackage;
 673         }
 674 
 675         if (msym == noModule) {
 676             return enterPackage(msym, flatName);
 677         }
 678 
 679         msym.complete();
 680 
 681         PackageSymbol pack;
 682 
 683         pack = msym.visiblePackages.get(flatName);
 684 
 685         if (pack != null)
 686             return pack;
 687 
 688         pack = getPackage(msym, flatName);
 689 
 690         if (pack != null && pack.exists())
 691             return pack;
 692 
 693         boolean dependsOnUnnamed = msym.requires != null &&
 694                                    msym.requires.stream()
 695                                                 .map(rd -> rd.module)
 696                                                 .anyMatch(mod -> mod == unnamedModule);
 697 
 698         if (dependsOnUnnamed) {
 699             //msyms depends on the unnamed module, for which we generally don't know
 700             //the list of packages it "exports" ahead of time. So try to lookup the package in the
 701             //current module, and in the unnamed module and see if it exists in one of them
 702             PackageSymbol unnamedPack = getPackage(unnamedModule, flatName);
 703 
 704             if (unnamedPack != null && unnamedPack.exists()) {
 705                 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack);
 706                 return unnamedPack;
 707             }
 708 
 709             pack = enterPackage(msym, flatName);
 710             pack.complete();
 711             if (pack.exists())
 712                 return pack;
 713 
 714             unnamedPack = enterPackage(unnamedModule, flatName);
 715             unnamedPack.complete();
 716             if (unnamedPack.exists()) {
 717                 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack);
 718                 return unnamedPack;
 719             }
 720 
 721             return pack;
 722         }
 723 
 724         return enterPackage(msym, flatName);
 725     }
 726 
 727     private static final Map<ModuleSymbol, ClassSymbol> EMPTY = new HashMap<>();
 728 
 729     public void removeClass(ModuleSymbol msym, Name flatName) {
 730         classes.getOrDefault(flatName, EMPTY).remove(msym);
 731     }
 732 
 733     public Iterable<ClassSymbol> getAllClasses() {
 734         return () -> Iterators.createCompoundIterator(classes.values(), v -> v.values().iterator());
 735     }
 736 
 737     private void doEnterClass(ModuleSymbol msym, ClassSymbol cs) {
 738         classes.computeIfAbsent(cs.flatname, n -> new HashMap<>()).put(msym, cs);
 739     }
 740 
 741     /** Create a new member or toplevel class symbol with given flat name
 742      *  and enter in `classes' unless already there.
 743      */
 744     public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) {
 745         Assert.checkNonNull(msym);
 746         PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname));
 747         Assert.checkNonNull(ps);
 748         Assert.checkNonNull(ps.modle);
 749         ClassSymbol c = getClass(ps.modle, flatname);
 750         if (c == null) {
 751             c = defineClass(Convert.shortName(flatname), ps);
 752             doEnterClass(ps.modle, c);
 753             return c;
 754         } else
 755             return c;
 756     }
 757 
 758     /** Check to see if a package exists, given its fully qualified name.
 759      */
 760     public boolean packageExists(ModuleSymbol msym, Name fullname) {
 761         Assert.checkNonNull(msym);
 762         return lookupPackage(msym, fullname).exists();
 763     }
 764 
 765     /** Make a package, given its fully qualified name.
 766      */
 767     public PackageSymbol enterPackage(ModuleSymbol currModule, Name fullname) {
 768         Assert.checkNonNull(currModule);
 769         PackageSymbol p = getPackage(currModule, fullname);
 770         if (p == null) {
 771             Assert.check(!fullname.isEmpty(), () -> "rootPackage missing!; currModule: " + currModule);
 772             p = new PackageSymbol(
 773                     Convert.shortName(fullname),
 774                     enterPackage(currModule, Convert.packagePart(fullname)));
 775             p.completer = initialCompleter;
 776             p.modle = currModule;
 777             doEnterPackage(currModule, p);
 778         }
 779         return p;
 780     }
 781 
 782     private void doEnterPackage(ModuleSymbol msym, PackageSymbol pack) {
 783         packages.computeIfAbsent(pack.fullname, n -> new HashMap<>()).put(msym, pack);
 784         msym.enclosedPackages = msym.enclosedPackages.prepend(pack);
 785     }
 786 
 787     private void addRootPackageFor(ModuleSymbol module) {
 788         doEnterPackage(module, rootPackage);
 789         PackageSymbol unnamedPackage = new PackageSymbol(names.empty, rootPackage) {
 790                 @Override
 791                 public String toString() {
 792                     return messages.getLocalizedString("compiler.misc.unnamed.package");
 793                 }
 794             };
 795         unnamedPackage.modle = module;
 796         //we cannot use a method reference below, as initialCompleter might be null now
 797         unnamedPackage.completer = s -> initialCompleter.complete(s);
 798         unnamedPackage.flags_field |= EXISTS;
 799         module.unnamedPackage = unnamedPackage;
 800     }
 801 
 802     public PackageSymbol getPackage(ModuleSymbol module, Name fullname) {
 803         return packages.getOrDefault(fullname, Collections.emptyMap()).get(module);
 804     }
 805 
 806     public ModuleSymbol enterModule(Name name) {
 807         ModuleSymbol msym = modules.get(name);
 808         if (msym == null) {
 809             msym = ModuleSymbol.create(name, names.module_info);
 810             addRootPackageFor(msym);
 811             msym.completer = s -> moduleCompleter.complete(s); //bootstrap issues
 812             modules.put(name, msym);
 813         }
 814         return msym;
 815     }
 816 
 817     public ModuleSymbol getModule(Name name) {
 818         return modules.get(name);
 819     }
 820 
 821     //temporary:
 822     public ModuleSymbol inferModule(Name packageName) {
 823         if (packageName.isEmpty())
 824             return java_base == noModule ? noModule : unnamedModule;//!
 825 
 826         ModuleSymbol msym = null;
 827         Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
 828         if (map == null)
 829             return null;
 830         for (Map.Entry<ModuleSymbol,PackageSymbol> e: map.entrySet()) {
 831             if (!e.getValue().members().isEmpty()) {
 832                 if (msym == null) {
 833                     msym = e.getKey();
 834                 } else {
 835                     return null;
 836                 }
 837             }
 838         }
 839         return msym;
 840     }
 841 
 842     public List<ModuleSymbol> listPackageModules(Name packageName) {
 843         if (packageName.isEmpty())
 844             return List.nil();
 845 
 846         List<ModuleSymbol> result = List.nil();
 847         Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
 848         if (map != null) {
 849             for (Map.Entry<ModuleSymbol, PackageSymbol> e: map.entrySet()) {
 850                 if (!e.getValue().members().isEmpty()) {
 851                     result = result.prepend(e.getKey());
 852                 }
 853             }
 854         }
 855         return result;
 856     }
 857 
 858     public Collection<ModuleSymbol> getAllModules() {
 859         return modules.values();
 860     }
 861 
 862     public Iterable<ClassSymbol> getClassesForName(Name candidate) {
 863         return classes.getOrDefault(candidate, Collections.emptyMap()).values();
 864     }
 865 
 866     public Iterable<PackageSymbol> getPackagesForName(Name candidate) {
 867         return packages.getOrDefault(candidate, Collections.emptyMap()).values();
 868     }
 869 }