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