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