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