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