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