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