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