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