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 
 158     /** Predefined types.
 159      */
 160     public final Type objectType;
 161     public final Type valueClassType;
 162     public final Type objectsType;
 163     public final Type classType;
 164     public final Type classLoaderType;
 165     public final Type stringType;
 166     public final Type stringBufferType;
 167     public final Type stringBuilderType;
 168     public final Type cloneableType;
 169     public final Type serializableType;
 170     public final Type serializedLambdaType;
 171     public final Type varHandleType;
 172     public final Type methodHandleType;
 173     public final Type methodHandleLookupType;
 174     public final Type methodTypeType;
 175     public final Type nativeHeaderType;
 176     public final Type throwableType;
 177     public final Type errorType;
 178     public final Type interruptedExceptionType;
 179     public final Type illegalArgumentExceptionType;
 180     public final Type exceptionType;
 181     public final Type runtimeExceptionType;
 182     public final Type classNotFoundExceptionType;
 183     public final Type noClassDefFoundErrorType;
 184     public final Type noSuchFieldErrorType;
 185     public final Type assertionErrorType;
 186     public final Type cloneNotSupportedExceptionType;
 187     public final Type annotationType;
 188     public final TypeSymbol enumSym;
 189     public final Type listType;
 190     public final Type collectionsType;
 191     public final Type comparableType;
 192     public final Type comparatorType;
 193     public final Type arraysType;
 194     public final Type iterableType;
 195     public final Type iteratorType;
 196     public final Type annotationTargetType;
 197     public final Type overrideType;
 198     public final Type retentionType;
 199     public final Type deprecatedType;
 200     public final Type suppressWarningsType;
 201     public final Type supplierType;
 202     public final Type inheritedType;
 203     public final Type profileType;
 204     public final Type proprietaryType;
 205     public final Type systemType;
 206     public final Type autoCloseableType;
 207     public final Type trustMeType;
 208     public final Type lambdaMetafactory;
 209     public final Type stringConcatFactory;
 210     public final Type repeatableType;
 211     public final Type documentedType;
 212     public final Type elementTypeType;
 213     public final Type functionalInterfaceType;
 214     public final Type valueCapableClass;
 215 
 216     /** The symbol representing the length field of an array.
 217      */
 218     public final VarSymbol lengthVar;
 219 
 220     /** The symbol representing the final finalize method on enums */
 221     public final MethodSymbol enumFinalFinalize;
 222 
 223     /** The symbol representing the close method on TWR AutoCloseable type */
 224     public final MethodSymbol autoCloseableClose;
 225 
 226     /** The predefined type that belongs to a tag.
 227      */
 228     public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()];
 229 
 230     /** The name of the class that belongs to a basic type tag.
 231      */
 232     public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()];
 233 
 234     /** A hashtable containing the encountered top-level and member classes,
 235      *  indexed by flat names. The table does not contain local classes.
 236      *  It should be updated from the outside to reflect classes defined
 237      *  by compiled source files.
 238      */
 239     private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>();
 240 
 241     /** A hashtable containing the encountered packages.
 242      *  the table should be updated from outside to reflect packages defined
 243      *  by compiled source files.
 244      */
 245     private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>();
 246 
 247     /** A hashtable giving the encountered modules.
 248      */
 249     private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>();
 250 
 251     public void initType(Type type, ClassSymbol c) {
 252         type.tsym = c;
 253         typeOfTag[type.getTag().ordinal()] = type;
 254     }
 255 
 256     public void initType(Type type, String name) {
 257         initType(
 258             type,
 259             new ClassSymbol(
 260                 PUBLIC, names.fromString(name), type, rootPackage));
 261     }
 262 
 263     public void initType(Type type, String name, String bname) {
 264         initType(type, name);
 265         boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname);
 266     }
 267 
 268     /** The class symbol that owns all predefined symbols.
 269      */
 270     public final ClassSymbol predefClass;
 271 
 272     /** Enter a class into symbol table.
 273      *  @param s The name of the class.
 274      */
 275     private Type enterClass(String s) {
 276         return enterClass(java_base, names.fromString(s)).type;
 277     }
 278 
 279     public void synthesizeEmptyInterfaceIfMissing(final Type type) {
 280         final Completer completer = type.tsym.completer;
 281         type.tsym.completer = new Completer() {
 282             @Override
 283             public void complete(Symbol sym) throws CompletionFailure {
 284                 try {
 285                     completer.complete(sym);
 286                 } catch (CompletionFailure e) {
 287                     sym.flags_field |= (PUBLIC | INTERFACE);
 288                     ((ClassType) sym.type).supertype_field = objectType;
 289                 }
 290             }
 291 
 292             @Override
 293             public boolean isTerminal() {
 294                 return completer.isTerminal();
 295             }
 296         };
 297     }
 298 
 299     public void synthesizeDeriveValueTypeIfMissing(final Type type) {
 300         final Completer completer = type.tsym.completer;
 301         type.tsym.completer = new Completer() {
 302             public void complete(Symbol sym) throws CompletionFailure {
 303                 try {
 304                     completer.complete(sym);
 305                 } catch (CompletionFailure e) {
 306                     ClassType ctype = (ClassType) type;
 307                     sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
 308                     sym.erasure_field = ctype;
 309                     ((ClassSymbol) sym).members_field = WriteableScope.create(sym);
 310                     ctype.typarams_field = List.nil();
 311                     ctype.allparams_field = List.nil();
 312                     ctype.supertype_field = annotationType;
 313                     ctype.interfaces_field = List.nil();
 314                 }
 315             }
 316 
 317             @Override
 318             public boolean isTerminal() {
 319                 return completer.isTerminal();
 320             }
 321         };
 322     }
 323 
 324     public void synthesizeJavaLangValueClassIfMissing(final Type type) {
 325         final Completer completer = type.tsym.completer;
 326         type.tsym.completer = new Completer() {
 327             public void complete(Symbol sym) throws CompletionFailure {
 328                 try {
 329                     completer.complete(sym);
 330                 } catch (CompletionFailure e) {
 331                     sym.flags_field |= (PUBLIC | VALUE);
 332                     ((ClassType) sym.type).supertype_field = Type.noType;
 333                     ((ClassSymbol) sym).members_field = WriteableScope.create(sym);
 334                     sym.members().enter(new MethodSymbol(PUBLIC, names.init,
 335                             new MethodType(List.<Type>nil(), voidType, List.<Type>nil(), methodClass), sym));
 336                     sym.members().enter(new MethodSymbol(PUBLIC, names.hashCode,
 337                             new MethodType(List.<Type>nil(), intType, List.<Type>nil(), methodClass), sym));
 338                     sym.members().enter(new MethodSymbol(PUBLIC, names.toString,
 339                             new MethodType(List.<Type>nil(), stringType, List.<Type>nil(), methodClass), sym));
 340                     sym.members().enter(new MethodSymbol(PUBLIC, names.equals,
 341                             new MethodType(List.<Type>of(objectType), booleanType, List.<Type>nil(), methodClass), sym));
 342                 }
 343             }
 344 
 345             @Override
 346             public boolean isTerminal() {
 347                 return completer.isTerminal();
 348             }
 349         };
 350     }
 351 
 352 
 353     public void synthesizeBoxTypeIfMissing(final Type type) {
 354         ClassSymbol sym = enterClass(java_base, boxedName[type.getTag().ordinal()]);
 355         final Completer completer = sym.completer;
 356         sym.completer = new Completer() {
 357             @Override
 358             public void complete(Symbol sym) throws CompletionFailure {
 359                 try {
 360                     completer.complete(sym);
 361                 } catch (CompletionFailure e) {
 362                     sym.flags_field |= PUBLIC;
 363                     ((ClassType) sym.type).supertype_field = objectType;
 364                     MethodSymbol boxMethod =
 365                         new MethodSymbol(PUBLIC | STATIC, names.valueOf,
 366                                          new MethodType(List.of(type), sym.type,
 367                                 List.nil(), methodClass),
 368                             sym);
 369                     sym.members().enter(boxMethod);
 370                     MethodSymbol unboxMethod =
 371                         new MethodSymbol(PUBLIC,
 372                             type.tsym.name.append(names.Value), // x.intValue()
 373                             new MethodType(List.nil(), type,
 374                                 List.nil(), methodClass),
 375                             sym);
 376                     sym.members().enter(unboxMethod);
 377                 }
 378             }
 379 
 380             @Override
 381             public boolean isTerminal() {
 382                 return completer.isTerminal();
 383             }
 384         };
 385     }
 386 
 387     // Enter a synthetic class that is used to mark classes in ct.sym.
 388     // This class does not have a class file.
 389     private Type enterSyntheticAnnotation(String name) {
 390         // for now, leave the module null, to prevent problems from synthesizing the
 391         // existence of a class in any specific module, including noModule
 392         ClassType type = (ClassType)enterClass(java_base, names.fromString(name)).type;
 393         ClassSymbol sym = (ClassSymbol)type.tsym;
 394         sym.completer = Completer.NULL_COMPLETER;
 395         sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
 396         sym.erasure_field = type;
 397         sym.members_field = WriteableScope.create(sym);
 398         type.typarams_field = List.nil();
 399         type.allparams_field = List.nil();
 400         type.supertype_field = annotationType;
 401         type.interfaces_field = List.nil();
 402         return type;
 403     }
 404 
 405     /** Constructor; enters all predefined identifiers and operators
 406      *  into symbol table.
 407      */
 408     protected Symtab(Context context) throws CompletionFailure {
 409         context.put(symtabKey, this);
 410 
 411         names = Names.instance(context);
 412 
 413         // Create the unknown type
 414         unknownType = new UnknownType();
 415 
 416         messages = JavacMessages.instance(context);
 417 
 418         rootPackage = new PackageSymbol(names.empty, null);
 419 
 420         // create the basic builtin symbols
 421         unnamedModule = new ModuleSymbol(names.empty, null) {
 422                 {
 423                     directives = List.nil();
 424                     exports = List.nil();
 425                     provides = List.nil();
 426                     uses = List.nil();
 427                     ModuleSymbol java_base = enterModule(names.java_base);
 428                     com.sun.tools.javac.code.Directive.RequiresDirective d =
 429                             new com.sun.tools.javac.code.Directive.RequiresDirective(java_base,
 430                                     EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED));
 431                     requires = List.of(d);
 432                 }
 433                 @Override
 434                 public String toString() {
 435                     return messages.getLocalizedString("compiler.misc.unnamed.module");
 436                 }
 437             };
 438         addRootPackageFor(unnamedModule);
 439         unnamedModule.enclosedPackages = unnamedModule.enclosedPackages.prepend(unnamedModule.unnamedPackage);
 440 
 441         errModule = new ModuleSymbol(names.empty, null) {
 442                 {
 443                     directives = List.nil();
 444                     exports = List.nil();
 445                     provides = List.nil();
 446                     uses = List.nil();
 447                     ModuleSymbol java_base = enterModule(names.java_base);
 448                     com.sun.tools.javac.code.Directive.RequiresDirective d =
 449                             new com.sun.tools.javac.code.Directive.RequiresDirective(java_base,
 450                                     EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED));
 451                     requires = List.of(d);
 452                 }
 453             };
 454         addRootPackageFor(errModule);
 455 
 456         noModule = new ModuleSymbol(names.empty, null) {
 457             @Override public boolean isNoModule() {
 458                 return true;
 459             }
 460         };
 461         addRootPackageFor(noModule);
 462 
 463         noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) {
 464             @Override @DefinedBy(Api.LANGUAGE_MODEL)
 465             public <R, P> R accept(ElementVisitor<R, P> v, P p) {
 466                 return v.visitUnknown(this, p);
 467             }
 468         };
 469 
 470         // create the error symbols
 471         errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
 472         errType = new ErrorType(errSymbol, Type.noType);
 473 
 474         unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage);
 475         unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol);
 476         unknownSymbol.type = unknownType;
 477 
 478         // initialize builtin types
 479         initType(byteType, "byte", "Byte");
 480         initType(shortType, "short", "Short");
 481         initType(charType, "char", "Character");
 482         initType(intType, "int", "Integer");
 483         initType(longType, "long", "Long");
 484         initType(floatType, "float", "Float");
 485         initType(doubleType, "double", "Double");
 486         initType(booleanType, "boolean", "Boolean");
 487         initType(voidType, "void", "Void");
 488         initType(botType, "<nulltype>");
 489         initType(errType, errSymbol);
 490         initType(unknownType, unknownSymbol);
 491 
 492         // the builtin class of all arrays
 493         arrayClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Array, noSymbol);
 494 
 495         // VGJ
 496         boundClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Bound, noSymbol);
 497         boundClass.members_field = new Scope.ErrorScope(boundClass);
 498 
 499         // the builtin class of all methods
 500         methodClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Method, noSymbol);
 501         methodClass.members_field = new Scope.ErrorScope(boundClass);
 502 
 503         // Create class to hold all predefined constants and operations.
 504         predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage);
 505         WriteableScope scope = WriteableScope.create(predefClass);
 506         predefClass.members_field = scope;
 507 
 508         // Get the initial completer for Symbols from the ClassFinder
 509         initialCompleter = ClassFinder.instance(context).getCompleter();
 510         rootPackage.members_field = WriteableScope.create(rootPackage);
 511 
 512         // Enter symbols for basic types.
 513         scope.enter(byteType.tsym);
 514         scope.enter(shortType.tsym);
 515         scope.enter(charType.tsym);
 516         scope.enter(intType.tsym);
 517         scope.enter(longType.tsym);
 518         scope.enter(floatType.tsym);
 519         scope.enter(doubleType.tsym);
 520         scope.enter(booleanType.tsym);
 521         scope.enter(errType.tsym);
 522 
 523         // Enter symbol for the errSymbol
 524         scope.enter(errSymbol);
 525 
 526         Source source = Source.instance(context);
 527         if (source.allowModules()) {
 528             java_base = enterModule(names.java_base);
 529             //avoid completing java.base during the Symtab initialization
 530             java_base.completer = Completer.NULL_COMPLETER;
 531             java_base.visiblePackages = Collections.emptyMap();




 532         } else {
 533             java_base = noModule;

 534         }
 535 
 536         // Get the initial completer for ModuleSymbols from Modules
 537         moduleCompleter = Modules.instance(context).getCompleter();
 538 
 539         // Enter predefined classes. All are assumed to be in the java.base module.
 540         objectType = enterClass("java.lang.Object");
 541         objectsType = enterClass("java.util.Objects");
 542         valueClassType = enterClass("java.lang.__Value");
 543         classType = enterClass("java.lang.Class");
 544         stringType = enterClass("java.lang.String");
 545         stringBufferType = enterClass("java.lang.StringBuffer");
 546         stringBuilderType = enterClass("java.lang.StringBuilder");
 547         cloneableType = enterClass("java.lang.Cloneable");
 548         throwableType = enterClass("java.lang.Throwable");
 549         serializableType = enterClass("java.io.Serializable");
 550         serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda");
 551         varHandleType = enterClass("java.lang.invoke.VarHandle");
 552         methodHandleType = enterClass("java.lang.invoke.MethodHandle");
 553         methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup");
 554         methodTypeType = enterClass("java.lang.invoke.MethodType");
 555         errorType = enterClass("java.lang.Error");
 556         illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException");
 557         interruptedExceptionType = enterClass("java.lang.InterruptedException");
 558         exceptionType = enterClass("java.lang.Exception");
 559         runtimeExceptionType = enterClass("java.lang.RuntimeException");
 560         classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
 561         noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
 562         noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError");
 563         assertionErrorType = enterClass("java.lang.AssertionError");
 564         cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
 565         annotationType = enterClass("java.lang.annotation.Annotation");
 566         classLoaderType = enterClass("java.lang.ClassLoader");
 567         enumSym = enterClass(java_base, names.java_lang_Enum);
 568         enumFinalFinalize =
 569             new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL,
 570                              names.finalize,
 571                              new MethodType(List.nil(), voidType,
 572                                             List.nil(), methodClass),
 573                              enumSym);
 574         listType = enterClass("java.util.List");
 575         collectionsType = enterClass("java.util.Collections");
 576         comparableType = enterClass("java.lang.Comparable");
 577         comparatorType = enterClass("java.util.Comparator");
 578         arraysType = enterClass("java.util.Arrays");
 579         iterableType = enterClass("java.lang.Iterable");
 580         iteratorType = enterClass("java.util.Iterator");
 581         annotationTargetType = enterClass("java.lang.annotation.Target");
 582         overrideType = enterClass("java.lang.Override");
 583         retentionType = enterClass("java.lang.annotation.Retention");
 584         deprecatedType = enterClass("java.lang.Deprecated");
 585         suppressWarningsType = enterClass("java.lang.SuppressWarnings");
 586         supplierType = enterClass("java.util.function.Supplier");
 587         inheritedType = enterClass("java.lang.annotation.Inherited");
 588         repeatableType = enterClass("java.lang.annotation.Repeatable");
 589         documentedType = enterClass("java.lang.annotation.Documented");
 590         elementTypeType = enterClass("java.lang.annotation.ElementType");
 591         systemType = enterClass("java.lang.System");
 592         autoCloseableType = enterClass("java.lang.AutoCloseable");
 593         autoCloseableClose = new MethodSymbol(PUBLIC,
 594                              names.close,
 595                              new MethodType(List.nil(), voidType,
 596                                             List.of(exceptionType), methodClass),
 597                              autoCloseableType.tsym);
 598         trustMeType = enterClass("java.lang.SafeVarargs");
 599         nativeHeaderType = enterClass("java.lang.annotation.Native");
 600         lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
 601         stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory");
 602         functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
 603         valueCapableClass = enterClass("jvm.internal.value.ValueCapableClass");


 604 
 605         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
 606         synthesizeEmptyInterfaceIfMissing(cloneableType);
 607         synthesizeEmptyInterfaceIfMissing(serializableType);
 608         synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
 609         synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
 610         synthesizeEmptyInterfaceIfMissing(stringConcatFactory);
 611         synthesizeBoxTypeIfMissing(doubleType);
 612         synthesizeBoxTypeIfMissing(floatType);
 613         synthesizeBoxTypeIfMissing(voidType);
 614         synthesizeJavaLangValueClassIfMissing(valueClassType);
 615         synthesizeDeriveValueTypeIfMissing(valueCapableClass);
 616 
 617         // Enter a synthetic class that is used to mark internal
 618         // proprietary classes in ct.sym.  This class does not have a
 619         // class file.
 620         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
 621 
 622         // Enter a synthetic class that is used to provide profile info for
 623         // classes in ct.sym.  This class does not have a class file.
 624         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
 625         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
 626         profileType.tsym.members().enter(m);
 627 
 628         // Enter a class for arrays.
 629         // The class implements java.lang.Cloneable and java.io.Serializable.
 630         // It has a final length field and a clone method.
 631         ClassType arrayClassType = (ClassType)arrayClass.type;
 632         arrayClassType.supertype_field = objectType;
 633         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
 634         arrayClass.members_field = WriteableScope.create(arrayClass);
 635         lengthVar = new VarSymbol(
 636             PUBLIC | FINAL,
 637             names.length,
 638             intType,
 639             arrayClass);
 640         arrayClass.members().enter(lengthVar);
 641         arrayCloneMethod = new MethodSymbol(
 642             PUBLIC,
 643             names.clone,
 644             new MethodType(List.nil(), objectType,
 645                            List.nil(), methodClass),
 646             arrayClass);
 647         arrayClass.members().enter(arrayCloneMethod);
 648 
 649         if (java_base != noModule)
 650             java_base.completer = moduleCompleter::complete; //bootstrap issues


 651 
 652     }
 653 
 654     /** Define a new class given its name and owner.
 655      */
 656     public ClassSymbol defineClass(Name name, Symbol owner) {
 657         ClassSymbol c = new ClassSymbol(0, name, owner);
 658         c.completer = initialCompleter;
 659         return c;
 660     }
 661 
 662     /** Create a new toplevel or member class symbol with given name
 663      *  and owner and enter in `classes' unless already there.
 664      */
 665     public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) {
 666         Assert.checkNonNull(msym);
 667         Name flatname = TypeSymbol.formFlatName(name, owner);
 668         ClassSymbol c = getClass(msym, flatname);
 669         if (c == null) {
 670             c = defineClass(name, owner);
 671             doEnterClass(msym, c);
 672         } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
 673             // reassign fields of classes that might have been loaded with
 674             // their flat names.
 675             c.owner.members().remove(c);
 676             c.name = name;
 677             c.owner = owner;
 678             c.fullname = ClassSymbol.formFullName(name, owner);
 679         }
 680         return c;
 681     }
 682 
 683     public ClassSymbol getClass(ModuleSymbol msym, Name flatName) {
 684         Assert.checkNonNull(msym, flatName::toString);
 685         return classes.getOrDefault(flatName, Collections.emptyMap()).get(msym);
 686     }
 687 
 688     public PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName) {
 689         Assert.checkNonNull(msym);
 690 
 691         if (flatName.isEmpty()) {
 692             //unnamed packages only from the current module - visiblePackages contains *root* package, not unnamed package!
 693             return msym.unnamedPackage;
 694         }
 695 
 696         if (msym == noModule) {
 697             return enterPackage(msym, flatName);
 698         }
 699 
 700         msym.complete();
 701 
 702         PackageSymbol pack;
 703 
 704         pack = msym.visiblePackages.get(flatName);
 705 
 706         if (pack != null)
 707             return pack;
 708 
 709         pack = getPackage(msym, flatName);
 710 
 711         if (pack != null && pack.exists())
 712             return pack;
 713 
 714         boolean dependsOnUnnamed = msym.requires != null &&
 715                                    msym.requires.stream()
 716                                                 .map(rd -> rd.module)
 717                                                 .anyMatch(mod -> mod == unnamedModule);
 718 
 719         if (dependsOnUnnamed) {
 720             //msyms depends on the unnamed module, for which we generally don't know
 721             //the list of packages it "exports" ahead of time. So try to lookup the package in the
 722             //current module, and in the unnamed module and see if it exists in one of them
 723             PackageSymbol unnamedPack = getPackage(unnamedModule, flatName);
 724 
 725             if (unnamedPack != null && unnamedPack.exists()) {
 726                 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack);
 727                 return unnamedPack;
 728             }
 729 
 730             pack = enterPackage(msym, flatName);
 731             pack.complete();
 732             if (pack.exists())
 733                 return pack;
 734 
 735             unnamedPack = enterPackage(unnamedModule, flatName);
 736             unnamedPack.complete();
 737             if (unnamedPack.exists()) {
 738                 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack);
 739                 return unnamedPack;
 740             }
 741 
 742             return pack;
 743         }
 744 
 745         return enterPackage(msym, flatName);
 746     }
 747 
 748     private static final Map<ModuleSymbol, ClassSymbol> EMPTY = new HashMap<>();
 749 
 750     public void removeClass(ModuleSymbol msym, Name flatName) {
 751         classes.getOrDefault(flatName, EMPTY).remove(msym);
 752     }
 753 
 754     public Iterable<ClassSymbol> getAllClasses() {
 755         return () -> Iterators.createCompoundIterator(classes.values(), v -> v.values().iterator());
 756     }
 757 
 758     private void doEnterClass(ModuleSymbol msym, ClassSymbol cs) {
 759         classes.computeIfAbsent(cs.flatname, n -> new HashMap<>()).put(msym, cs);
 760     }
 761 
 762     /** Create a new member or toplevel class symbol with given flat name
 763      *  and enter in `classes' unless already there.
 764      */
 765     public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) {
 766         Assert.checkNonNull(msym);
 767         PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname));
 768         Assert.checkNonNull(ps);
 769         Assert.checkNonNull(ps.modle);
 770         ClassSymbol c = getClass(ps.modle, flatname);
 771         if (c == null) {
 772             c = defineClass(Convert.shortName(flatname), ps);
 773             doEnterClass(ps.modle, c);
 774             return c;
 775         } else
 776             return c;
 777     }
 778 
 779     /** Check to see if a package exists, given its fully qualified name.
 780      */
 781     public boolean packageExists(ModuleSymbol msym, Name fullname) {
 782         Assert.checkNonNull(msym);
 783         return lookupPackage(msym, fullname).exists();
 784     }
 785 
 786     /** Make a package, given its fully qualified name.
 787      */
 788     public PackageSymbol enterPackage(ModuleSymbol currModule, Name fullname) {
 789         Assert.checkNonNull(currModule);
 790         PackageSymbol p = getPackage(currModule, fullname);
 791         if (p == null) {
 792             Assert.check(!fullname.isEmpty(), () -> "rootPackage missing!; currModule: " + currModule);
 793             p = new PackageSymbol(
 794                     Convert.shortName(fullname),
 795                     enterPackage(currModule, Convert.packagePart(fullname)));
 796             p.completer = initialCompleter;
 797             p.modle = currModule;
 798             doEnterPackage(currModule, p);
 799         }
 800         return p;
 801     }
 802 
 803     private void doEnterPackage(ModuleSymbol msym, PackageSymbol pack) {
 804         packages.computeIfAbsent(pack.fullname, n -> new HashMap<>()).put(msym, pack);
 805         msym.enclosedPackages = msym.enclosedPackages.prepend(pack);
 806     }
 807 
 808     private void addRootPackageFor(ModuleSymbol module) {
 809         doEnterPackage(module, rootPackage);
 810         PackageSymbol unnamedPackage = new PackageSymbol(names.empty, rootPackage) {
 811                 @Override
 812                 public String toString() {
 813                     return messages.getLocalizedString("compiler.misc.unnamed.package");
 814                 }
 815             };
 816         unnamedPackage.modle = module;
 817         //we cannot use a method reference below, as initialCompleter might be null now
 818         unnamedPackage.completer = s -> initialCompleter.complete(s);
 819         module.unnamedPackage = unnamedPackage;
 820     }
 821 
 822     public PackageSymbol getPackage(ModuleSymbol module, Name fullname) {
 823         return packages.getOrDefault(fullname, Collections.emptyMap()).get(module);
 824     }
 825 
 826     public ModuleSymbol enterModule(Name name) {
 827         ModuleSymbol msym = modules.get(name);
 828         if (msym == null) {
 829             msym = ModuleSymbol.create(name, names.module_info);
 830             addRootPackageFor(msym);
 831             msym.completer = s -> moduleCompleter.complete(s); //bootstrap issues
 832             modules.put(name, msym);
 833         }
 834         return msym;
 835     }
 836 
 837     public ModuleSymbol getModule(Name name) {
 838         return modules.get(name);
 839     }
 840 
 841     //temporary:
 842     public ModuleSymbol inferModule(Name packageName) {
 843         if (packageName.isEmpty())
 844             return java_base == noModule ? noModule : unnamedModule;//!
 845 
 846         ModuleSymbol msym = null;
 847         Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
 848         if (map == null)
 849             return null;
 850         for (Map.Entry<ModuleSymbol,PackageSymbol> e: map.entrySet()) {
 851             if (!e.getValue().members().isEmpty()) {
 852                 if (msym == null) {
 853                     msym = e.getKey();
 854                 } else {
 855                     return null;
 856                 }
 857             }
 858         }
 859         return msym;
 860     }
 861 
 862     public List<ModuleSymbol> listPackageModules(Name packageName) {
 863         if (packageName.isEmpty())
 864             return List.nil();
 865 
 866         List<ModuleSymbol> result = List.nil();
 867         Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
 868         if (map != null) {
 869             for (Map.Entry<ModuleSymbol, PackageSymbol> e: map.entrySet()) {
 870                 if (!e.getValue().members().isEmpty()) {
 871                     result = result.prepend(e.getKey());
 872                 }
 873             }
 874         }
 875         return result;
 876     }
 877 
 878     public Collection<ModuleSymbol> getAllModules() {
 879         return modules.values();
 880     }
 881 
 882     public Iterable<ClassSymbol> getClassesForName(Name candidate) {
 883         return classes.getOrDefault(candidate, Collections.emptyMap()).values();
 884     }
 885 
 886     public Iterable<PackageSymbol> getPackagesForName(Name candidate) {
 887         return packages.getOrDefault(candidate, Collections.emptyMap()).values();
 888     }
 889 }
--- EOF ---