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