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