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