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