Symtab.java revision 3868:5ec5a14e1627
133965Sjdp/* 2130561Sobrien * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. 3218822Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 433965Sjdp * 533965Sjdp * This code is free software; you can redistribute it and/or modify it 633965Sjdp * under the terms of the GNU General Public License version 2 only, as 733965Sjdp * published by the Free Software Foundation. Oracle designates this 833965Sjdp * particular file as subject to the "Classpath" exception as provided 933965Sjdp * by Oracle in the LICENSE file that accompanied this code. 1033965Sjdp * 1133965Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT 1233965Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1333965Sjdp * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1433965Sjdp * version 2 for more details (a copy is included in the LICENSE file that 1533965Sjdp * accompanied this code). 1633965Sjdp * 1733965Sjdp * You should have received a copy of the GNU General Public License version 1833965Sjdp * 2 along with this work; if not, write to the Free Software Foundation, 1933965Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20218822Sdim * 2133965Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2277298Sobrien * or visit www.oracle.com if you need additional information or have any 2333965Sjdp * questions. 2433965Sjdp */ 2533965Sjdp 2633965Sjdppackage com.sun.tools.javac.code; 2733965Sjdp 2833965Sjdpimport java.util.Collection; 2933965Sjdpimport java.util.Collections; 3033965Sjdpimport java.util.EnumSet; 3133965Sjdpimport java.util.HashMap; 32130561Sobrienimport java.util.LinkedHashMap; 3333965Sjdpimport java.util.Map; 3433965Sjdp 35218822Sdimimport javax.lang.model.element.ElementVisitor; 3633965Sjdp 3733965Sjdpimport com.sun.tools.javac.code.Scope.WriteableScope; 3833965Sjdpimport com.sun.tools.javac.code.Symbol.ClassSymbol; 3933965Sjdpimport com.sun.tools.javac.code.Symbol.Completer; 4033965Sjdpimport com.sun.tools.javac.code.Symbol.CompletionFailure; 4133965Sjdpimport com.sun.tools.javac.code.Symbol.MethodSymbol; 4233965Sjdpimport com.sun.tools.javac.code.Symbol.ModuleSymbol; 4333965Sjdpimport com.sun.tools.javac.code.Symbol.PackageSymbol; 4433965Sjdpimport com.sun.tools.javac.code.Symbol.TypeSymbol; 4533965Sjdpimport com.sun.tools.javac.code.Symbol.VarSymbol; 4633965Sjdpimport com.sun.tools.javac.code.Type.BottomType; 4733965Sjdpimport com.sun.tools.javac.code.Type.ClassType; 4833965Sjdpimport com.sun.tools.javac.code.Type.ErrorType; 4933965Sjdpimport com.sun.tools.javac.code.Type.JCPrimitiveType; 5033965Sjdpimport com.sun.tools.javac.code.Type.JCVoidType; 5133965Sjdpimport com.sun.tools.javac.code.Type.MethodType; 5233965Sjdpimport com.sun.tools.javac.code.Type.UnknownType; 5377298Sobrienimport com.sun.tools.javac.comp.Modules; 5433965Sjdpimport com.sun.tools.javac.util.Assert; 5533965Sjdpimport com.sun.tools.javac.util.Context; 5633965Sjdpimport com.sun.tools.javac.util.Convert; 5733965Sjdpimport com.sun.tools.javac.util.DefinedBy; 5833965Sjdpimport com.sun.tools.javac.util.DefinedBy.Api; 5933965Sjdpimport com.sun.tools.javac.util.Iterators; 6033965Sjdpimport com.sun.tools.javac.util.JavacMessages; 6133965Sjdpimport com.sun.tools.javac.util.List; 6233965Sjdpimport com.sun.tools.javac.util.Name; 6333965Sjdpimport com.sun.tools.javac.util.Names; 6433965Sjdp 6533965Sjdpimport static com.sun.tools.javac.code.Flags.*; 6633965Sjdpimport static com.sun.tools.javac.code.Kinds.Kind.*; 67130561Sobrienimport static com.sun.tools.javac.code.TypeTag.*; 6833965Sjdp 6933965Sjdp/** A class that defines all predefined constants and operators 7033965Sjdp * as well as special classes such as java.lang.Object, which need 7133965Sjdp * to be known to the compiler. All symbols are held in instance 7233965Sjdp * fields. This makes it possible to work in multiple concurrent 7333965Sjdp * projects, which might use different class files for library classes. 7433965Sjdp * 7533965Sjdp * <p><b>This is NOT part of any supported API. 7633965Sjdp * If you write code that depends on this, you do so at your own risk. 7733965Sjdp * This code and its internal interfaces are subject to change or 7833965Sjdp * deletion without notice.</b> 7977298Sobrien */ 8077298Sobrienpublic class Symtab { 8177298Sobrien /** The context key for the symbol table. */ 82130561Sobrien protected static final Context.Key<Symtab> symtabKey = new Context.Key<>(); 8333965Sjdp 8433965Sjdp /** Get the symbol table instance. */ 8533965Sjdp public static Symtab instance(Context context) { 8633965Sjdp Symtab instance = context.get(symtabKey); 8777298Sobrien if (instance == null) 8833965Sjdp instance = new Symtab(context); 8977298Sobrien return instance; 9077298Sobrien } 9177298Sobrien 9233965Sjdp /** Builtin types. 9377298Sobrien */ 9477298Sobrien public final JCPrimitiveType byteType = new JCPrimitiveType(BYTE, null); 9533965Sjdp public final JCPrimitiveType charType = new JCPrimitiveType(CHAR, null); 9677298Sobrien public final JCPrimitiveType shortType = new JCPrimitiveType(SHORT, null); 9733965Sjdp public final JCPrimitiveType intType = new JCPrimitiveType(INT, null); 9833965Sjdp public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null); 9933965Sjdp public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null); 10033965Sjdp public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null); 10177298Sobrien public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null); 10233965Sjdp public final Type botType = new BottomType(); 10333965Sjdp public final JCVoidType voidType = new JCVoidType(); 10433965Sjdp 10533965Sjdp private final Names names; 10633965Sjdp private final JavacMessages messages; 10733965Sjdp private final Completer initialCompleter; 10833965Sjdp private final Completer moduleCompleter; 10933965Sjdp 11033965Sjdp /** A symbol for the unnamed module. 11133965Sjdp */ 11233965Sjdp public final ModuleSymbol unnamedModule; 11377298Sobrien 11477298Sobrien /** The error module. 11533965Sjdp */ 11633965Sjdp public final ModuleSymbol errModule; 11777298Sobrien 11833965Sjdp /** A symbol for no module, for use with -source 8 or less 11977298Sobrien */ 12033965Sjdp public final ModuleSymbol noModule; 12133965Sjdp 12233965Sjdp /** A symbol for the root package. 12333965Sjdp */ 12433965Sjdp public final PackageSymbol rootPackage; 12533965Sjdp 12633965Sjdp /** A symbol that stands for a missing symbol. 12733965Sjdp */ 12833965Sjdp public final TypeSymbol noSymbol; 12933965Sjdp 13033965Sjdp /** The error symbol. 13133965Sjdp */ 13233965Sjdp public final ClassSymbol errSymbol; 13333965Sjdp 13433965Sjdp /** The unknown symbol. 13533965Sjdp */ 13677298Sobrien public final ClassSymbol unknownSymbol; 13733965Sjdp 13833965Sjdp /** A value for the errType, with a originalType of noType */ 139130561Sobrien public final Type errType; 14089857Sobrien 14133965Sjdp /** A value for the unknown type. */ 14289857Sobrien public final Type unknownType; 14333965Sjdp 144130561Sobrien /** The builtin type of all arrays. */ 14533965Sjdp public final ClassSymbol arrayClass; 14677298Sobrien public final MethodSymbol arrayCloneMethod; 14789857Sobrien 14889857Sobrien /** VGJ: The (singleton) type of all bound types. */ 14989857Sobrien public final ClassSymbol boundClass; 15033965Sjdp 15133965Sjdp /** The builtin type of all methods. */ 15233965Sjdp public final ClassSymbol methodClass; 15333965Sjdp 15433965Sjdp /** A symbol for the java.base module. 15533965Sjdp */ 156130561Sobrien public final ModuleSymbol java_base; 15733965Sjdp 15833965Sjdp /** Predefined types. 159130561Sobrien */ 16033965Sjdp public final Type objectType; 16189857Sobrien public final Type objectsType; 16233965Sjdp public final Type classType; 163130561Sobrien public final Type classLoaderType; 16433965Sjdp public final Type stringType; 16577298Sobrien public final Type stringBufferType; 16633965Sjdp public final Type stringBuilderType; 16733965Sjdp public final Type cloneableType; 16833965Sjdp public final Type serializableType; 16933965Sjdp public final Type serializedLambdaType; 17033965Sjdp public final Type varHandleType; 17133965Sjdp public final Type methodHandleType; 172130561Sobrien public final Type methodHandleLookupType; 17333965Sjdp public final Type methodTypeType; 17433965Sjdp public final Type nativeHeaderType; 17533965Sjdp public final Type throwableType; 17633965Sjdp public final Type errorType; 17733965Sjdp public final Type interruptedExceptionType; 17833965Sjdp public final Type illegalArgumentExceptionType; 17933965Sjdp public final Type exceptionType; 18033965Sjdp public final Type runtimeExceptionType; 18133965Sjdp public final Type classNotFoundExceptionType; 18233965Sjdp public final Type noClassDefFoundErrorType; 18333965Sjdp public final Type noSuchFieldErrorType; 18433965Sjdp public final Type assertionErrorType; 18533965Sjdp public final Type cloneNotSupportedExceptionType; 18633965Sjdp public final Type annotationType; 18733965Sjdp public final TypeSymbol enumSym; 18833965Sjdp public final Type listType; 18977298Sobrien public final Type collectionsType; 19033965Sjdp public final Type comparableType; 19133965Sjdp public final Type comparatorType; 19233965Sjdp public final Type arraysType; 19389857Sobrien public final Type iterableType; 19433965Sjdp public final Type iteratorType; 19560484Sobrien public final Type annotationTargetType; 196130561Sobrien public final Type overrideType; 19789857Sobrien public final Type retentionType; 19889857Sobrien public final Type deprecatedType; 199104834Sobrien public final Type suppressWarningsType; 20033965Sjdp public final Type supplierType; 20133965Sjdp public final Type inheritedType; 202130561Sobrien public final Type profileType; 203130561Sobrien public final Type proprietaryType; 204130561Sobrien public final Type systemType; 205130561Sobrien public final Type autoCloseableType; 206130561Sobrien public final Type trustMeType; 20733965Sjdp public final Type lambdaMetafactory; 208130561Sobrien public final Type stringConcatFactory; 20933965Sjdp public final Type repeatableType; 210130561Sobrien public final Type documentedType; 211130561Sobrien public final Type elementTypeType; 212130561Sobrien public final Type functionalInterfaceType; 213130561Sobrien 214130561Sobrien /** The symbol representing the length field of an array. 215130561Sobrien */ 21633965Sjdp public final VarSymbol lengthVar; 217130561Sobrien 218130561Sobrien /** The symbol representing the final finalize method on enums */ 219130561Sobrien public final MethodSymbol enumFinalFinalize; 220130561Sobrien 221130561Sobrien /** The symbol representing the close method on TWR AutoCloseable type */ 222130561Sobrien public final MethodSymbol autoCloseableClose; 223130561Sobrien 224130561Sobrien /** The predefined type that belongs to a tag. 225130561Sobrien */ 226130561Sobrien public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()]; 22777298Sobrien 22833965Sjdp /** The name of the class that belongs to a basic type tag. 22960484Sobrien */ 23077298Sobrien public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()]; 23133965Sjdp 23233965Sjdp /** A hashtable containing the encountered top-level and member classes, 233218822Sdim * indexed by flat names. The table does not contain local classes. 234218822Sdim * It should be updated from the outside to reflect classes defined 23577298Sobrien * by compiled source files. 236130561Sobrien */ 23733965Sjdp private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>(); 23833965Sjdp 23933965Sjdp /** A hashtable containing the encountered packages. 240130561Sobrien * the table should be updated from outside to reflect packages defined 241130561Sobrien * by compiled source files. 242130561Sobrien */ 243130561Sobrien private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>(); 244130561Sobrien 245130561Sobrien /** A hashtable giving the encountered modules. 24633965Sjdp */ 24733965Sjdp private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>(); 24833965Sjdp 24933965Sjdp public void initType(Type type, ClassSymbol c) { 250130561Sobrien type.tsym = c; 25133965Sjdp typeOfTag[type.getTag().ordinal()] = type; 25233965Sjdp } 25377298Sobrien 25433965Sjdp public void initType(Type type, String name) { 25533965Sjdp initType( 25633965Sjdp type, 25733965Sjdp new ClassSymbol( 25833965Sjdp PUBLIC, names.fromString(name), type, rootPackage)); 259218822Sdim } 26033965Sjdp 26133965Sjdp public void initType(Type type, String name, String bname) { 26233965Sjdp initType(type, name); 26333965Sjdp boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname); 264130561Sobrien } 265130561Sobrien 26633965Sjdp /** The class symbol that owns all predefined symbols. 26733965Sjdp */ 268130561Sobrien public final ClassSymbol predefClass; 26933965Sjdp 27033965Sjdp /** Enter a class into symbol table. 27177298Sobrien * @param s The name of the class. 272218822Sdim */ 273218822Sdim private Type enterClass(String s) { 27433965Sjdp return enterClass(java_base, names.fromString(s)).type; 27577298Sobrien } 27689857Sobrien 27733965Sjdp public void synthesizeEmptyInterfaceIfMissing(final Type type) { 27833965Sjdp final Completer completer = type.tsym.completer; 27977298Sobrien type.tsym.completer = new Completer() { 28077298Sobrien @Override 28133965Sjdp public void complete(Symbol sym) throws CompletionFailure { 28233965Sjdp try { 28333965Sjdp completer.complete(sym); 28433965Sjdp } catch (CompletionFailure e) { 28533965Sjdp sym.flags_field |= (PUBLIC | INTERFACE); 28633965Sjdp ((ClassType) sym.type).supertype_field = objectType; 28733965Sjdp } 28833965Sjdp } 28977298Sobrien 29033965Sjdp @Override 29133965Sjdp public boolean isTerminal() { 29233965Sjdp return completer.isTerminal(); 29377298Sobrien } 29433965Sjdp }; 29533965Sjdp } 29633965Sjdp 29733965Sjdp public void synthesizeBoxTypeIfMissing(final Type type) { 29877298Sobrien ClassSymbol sym = enterClass(java_base, boxedName[type.getTag().ordinal()]); 29977298Sobrien final Completer completer = sym.completer; 30077298Sobrien sym.completer = new Completer() { 30133965Sjdp @Override 30233965Sjdp public void complete(Symbol sym) throws CompletionFailure { 30377298Sobrien try { 30433965Sjdp completer.complete(sym); 30533965Sjdp } catch (CompletionFailure e) { 30633965Sjdp sym.flags_field |= PUBLIC; 30777298Sobrien ((ClassType) sym.type).supertype_field = objectType; 30833965Sjdp MethodSymbol boxMethod = 30933965Sjdp new MethodSymbol(PUBLIC | STATIC, names.valueOf, 31033965Sjdp new MethodType(List.of(type), sym.type, 31177298Sobrien List.nil(), methodClass), 31233965Sjdp sym); 31333965Sjdp sym.members().enter(boxMethod); 31433965Sjdp MethodSymbol unboxMethod = 31577298Sobrien new MethodSymbol(PUBLIC, 31677298Sobrien type.tsym.name.append(names.Value), // x.intValue() 31733965Sjdp new MethodType(List.nil(), type, 31877298Sobrien List.nil(), methodClass), 31977298Sobrien sym); 32077298Sobrien sym.members().enter(unboxMethod); 32177298Sobrien } 32233965Sjdp } 32333965Sjdp 32433965Sjdp @Override 32533965Sjdp public boolean isTerminal() { 32677298Sobrien return completer.isTerminal(); 32733965Sjdp } 32833965Sjdp }; 32977298Sobrien } 33033965Sjdp 33133965Sjdp // Enter a synthetic class that is used to mark classes in ct.sym. 332 // This class does not have a class file. 333 private Type enterSyntheticAnnotation(String name) { 334 // for now, leave the module null, to prevent problems from synthesizing the 335 // existence of a class in any specific module, including noModule 336 ClassType type = (ClassType)enterClass(java_base, names.fromString(name)).type; 337 ClassSymbol sym = (ClassSymbol)type.tsym; 338 sym.completer = Completer.NULL_COMPLETER; 339 sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE; 340 sym.erasure_field = type; 341 sym.members_field = WriteableScope.create(sym); 342 type.typarams_field = List.nil(); 343 type.allparams_field = List.nil(); 344 type.supertype_field = annotationType; 345 type.interfaces_field = List.nil(); 346 return type; 347 } 348 349 /** Constructor; enters all predefined identifiers and operators 350 * into symbol table. 351 */ 352 protected Symtab(Context context) throws CompletionFailure { 353 context.put(symtabKey, this); 354 355 names = Names.instance(context); 356 357 // Create the unknown type 358 unknownType = new UnknownType(); 359 360 messages = JavacMessages.instance(context); 361 362 rootPackage = new PackageSymbol(names.empty, null); 363 364 // create the basic builtin symbols 365 unnamedModule = new ModuleSymbol(names.empty, null) { 366 { 367 directives = List.nil(); 368 exports = List.nil(); 369 provides = List.nil(); 370 uses = List.nil(); 371 ModuleSymbol java_base = enterModule(names.java_base); 372 com.sun.tools.javac.code.Directive.RequiresDirective d = 373 new com.sun.tools.javac.code.Directive.RequiresDirective(java_base, 374 EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED)); 375 requires = List.of(d); 376 } 377 @Override 378 public String toString() { 379 return messages.getLocalizedString("compiler.misc.unnamed.module"); 380 } 381 }; 382 addRootPackageFor(unnamedModule); 383 unnamedModule.enclosedPackages = unnamedModule.enclosedPackages.prepend(unnamedModule.unnamedPackage); 384 385 errModule = new ModuleSymbol(names.empty, null) { 386 { 387 directives = List.nil(); 388 exports = List.nil(); 389 provides = List.nil(); 390 uses = List.nil(); 391 ModuleSymbol java_base = enterModule(names.java_base); 392 com.sun.tools.javac.code.Directive.RequiresDirective d = 393 new com.sun.tools.javac.code.Directive.RequiresDirective(java_base, 394 EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED)); 395 requires = List.of(d); 396 } 397 }; 398 addRootPackageFor(errModule); 399 400 noModule = new ModuleSymbol(names.empty, null) { 401 @Override public boolean isNoModule() { 402 return true; 403 } 404 }; 405 addRootPackageFor(noModule); 406 407 noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) { 408 @Override @DefinedBy(Api.LANGUAGE_MODEL) 409 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 410 return v.visitUnknown(this, p); 411 } 412 }; 413 414 // create the error symbols 415 errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage); 416 errType = new ErrorType(errSymbol, Type.noType); 417 418 unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage); 419 unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol); 420 unknownSymbol.type = unknownType; 421 422 // initialize builtin types 423 initType(byteType, "byte", "Byte"); 424 initType(shortType, "short", "Short"); 425 initType(charType, "char", "Character"); 426 initType(intType, "int", "Integer"); 427 initType(longType, "long", "Long"); 428 initType(floatType, "float", "Float"); 429 initType(doubleType, "double", "Double"); 430 initType(booleanType, "boolean", "Boolean"); 431 initType(voidType, "void", "Void"); 432 initType(botType, "<nulltype>"); 433 initType(errType, errSymbol); 434 initType(unknownType, unknownSymbol); 435 436 // the builtin class of all arrays 437 arrayClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Array, noSymbol); 438 439 // VGJ 440 boundClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Bound, noSymbol); 441 boundClass.members_field = new Scope.ErrorScope(boundClass); 442 443 // the builtin class of all methods 444 methodClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Method, noSymbol); 445 methodClass.members_field = new Scope.ErrorScope(boundClass); 446 447 // Create class to hold all predefined constants and operations. 448 predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage); 449 WriteableScope scope = WriteableScope.create(predefClass); 450 predefClass.members_field = scope; 451 452 // Get the initial completer for Symbols from the ClassFinder 453 initialCompleter = ClassFinder.instance(context).getCompleter(); 454 rootPackage.members_field = WriteableScope.create(rootPackage); 455 456 // Enter symbols for basic types. 457 scope.enter(byteType.tsym); 458 scope.enter(shortType.tsym); 459 scope.enter(charType.tsym); 460 scope.enter(intType.tsym); 461 scope.enter(longType.tsym); 462 scope.enter(floatType.tsym); 463 scope.enter(doubleType.tsym); 464 scope.enter(booleanType.tsym); 465 scope.enter(errType.tsym); 466 467 // Enter symbol for the errSymbol 468 scope.enter(errSymbol); 469 470 Source source = Source.instance(context); 471 if (source.allowModules()) { 472 java_base = enterModule(names.java_base); 473 //avoid completing java.base during the Symtab initialization 474 java_base.completer = Completer.NULL_COMPLETER; 475 java_base.visiblePackages = Collections.emptyMap(); 476 } else { 477 java_base = noModule; 478 } 479 480 // Get the initial completer for ModuleSymbols from Modules 481 moduleCompleter = Modules.instance(context).getCompleter(); 482 483 // Enter predefined classes. All are assumed to be in the java.base module. 484 objectType = enterClass("java.lang.Object"); 485 objectsType = enterClass("java.util.Objects"); 486 classType = enterClass("java.lang.Class"); 487 stringType = enterClass("java.lang.String"); 488 stringBufferType = enterClass("java.lang.StringBuffer"); 489 stringBuilderType = enterClass("java.lang.StringBuilder"); 490 cloneableType = enterClass("java.lang.Cloneable"); 491 throwableType = enterClass("java.lang.Throwable"); 492 serializableType = enterClass("java.io.Serializable"); 493 serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda"); 494 varHandleType = enterClass("java.lang.invoke.VarHandle"); 495 methodHandleType = enterClass("java.lang.invoke.MethodHandle"); 496 methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup"); 497 methodTypeType = enterClass("java.lang.invoke.MethodType"); 498 errorType = enterClass("java.lang.Error"); 499 illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException"); 500 interruptedExceptionType = enterClass("java.lang.InterruptedException"); 501 exceptionType = enterClass("java.lang.Exception"); 502 runtimeExceptionType = enterClass("java.lang.RuntimeException"); 503 classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException"); 504 noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError"); 505 noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError"); 506 assertionErrorType = enterClass("java.lang.AssertionError"); 507 cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException"); 508 annotationType = enterClass("java.lang.annotation.Annotation"); 509 classLoaderType = enterClass("java.lang.ClassLoader"); 510 enumSym = enterClass(java_base, names.java_lang_Enum); 511 enumFinalFinalize = 512 new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL, 513 names.finalize, 514 new MethodType(List.nil(), voidType, 515 List.nil(), methodClass), 516 enumSym); 517 listType = enterClass("java.util.List"); 518 collectionsType = enterClass("java.util.Collections"); 519 comparableType = enterClass("java.lang.Comparable"); 520 comparatorType = enterClass("java.util.Comparator"); 521 arraysType = enterClass("java.util.Arrays"); 522 iterableType = enterClass("java.lang.Iterable"); 523 iteratorType = enterClass("java.util.Iterator"); 524 annotationTargetType = enterClass("java.lang.annotation.Target"); 525 overrideType = enterClass("java.lang.Override"); 526 retentionType = enterClass("java.lang.annotation.Retention"); 527 deprecatedType = enterClass("java.lang.Deprecated"); 528 suppressWarningsType = enterClass("java.lang.SuppressWarnings"); 529 supplierType = enterClass("java.util.function.Supplier"); 530 inheritedType = enterClass("java.lang.annotation.Inherited"); 531 repeatableType = enterClass("java.lang.annotation.Repeatable"); 532 documentedType = enterClass("java.lang.annotation.Documented"); 533 elementTypeType = enterClass("java.lang.annotation.ElementType"); 534 systemType = enterClass("java.lang.System"); 535 autoCloseableType = enterClass("java.lang.AutoCloseable"); 536 autoCloseableClose = new MethodSymbol(PUBLIC, 537 names.close, 538 new MethodType(List.nil(), voidType, 539 List.of(exceptionType), methodClass), 540 autoCloseableType.tsym); 541 trustMeType = enterClass("java.lang.SafeVarargs"); 542 nativeHeaderType = enterClass("java.lang.annotation.Native"); 543 lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory"); 544 stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory"); 545 functionalInterfaceType = enterClass("java.lang.FunctionalInterface"); 546 547 synthesizeEmptyInterfaceIfMissing(autoCloseableType); 548 synthesizeEmptyInterfaceIfMissing(cloneableType); 549 synthesizeEmptyInterfaceIfMissing(serializableType); 550 synthesizeEmptyInterfaceIfMissing(lambdaMetafactory); 551 synthesizeEmptyInterfaceIfMissing(serializedLambdaType); 552 synthesizeEmptyInterfaceIfMissing(stringConcatFactory); 553 synthesizeBoxTypeIfMissing(doubleType); 554 synthesizeBoxTypeIfMissing(floatType); 555 synthesizeBoxTypeIfMissing(voidType); 556 557 // Enter a synthetic class that is used to mark internal 558 // proprietary classes in ct.sym. This class does not have a 559 // class file. 560 proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation"); 561 562 // Enter a synthetic class that is used to provide profile info for 563 // classes in ct.sym. This class does not have a class file. 564 profileType = enterSyntheticAnnotation("jdk.Profile+Annotation"); 565 MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym); 566 profileType.tsym.members().enter(m); 567 568 // Enter a class for arrays. 569 // The class implements java.lang.Cloneable and java.io.Serializable. 570 // It has a final length field and a clone method. 571 ClassType arrayClassType = (ClassType)arrayClass.type; 572 arrayClassType.supertype_field = objectType; 573 arrayClassType.interfaces_field = List.of(cloneableType, serializableType); 574 arrayClass.members_field = WriteableScope.create(arrayClass); 575 lengthVar = new VarSymbol( 576 PUBLIC | FINAL, 577 names.length, 578 intType, 579 arrayClass); 580 arrayClass.members().enter(lengthVar); 581 arrayCloneMethod = new MethodSymbol( 582 PUBLIC, 583 names.clone, 584 new MethodType(List.nil(), objectType, 585 List.nil(), methodClass), 586 arrayClass); 587 arrayClass.members().enter(arrayCloneMethod); 588 589 if (java_base != noModule) 590 java_base.completer = moduleCompleter::complete; //bootstrap issues 591 592 } 593 594 /** Define a new class given its name and owner. 595 */ 596 public ClassSymbol defineClass(Name name, Symbol owner) { 597 ClassSymbol c = new ClassSymbol(0, name, owner); 598 c.completer = initialCompleter; 599 return c; 600 } 601 602 /** Create a new toplevel or member class symbol with given name 603 * and owner and enter in `classes' unless already there. 604 */ 605 public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) { 606 Assert.checkNonNull(msym); 607 Name flatname = TypeSymbol.formFlatName(name, owner); 608 ClassSymbol c = getClass(msym, flatname); 609 if (c == null) { 610 c = defineClass(name, owner); 611 doEnterClass(msym, c); 612 } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) { 613 // reassign fields of classes that might have been loaded with 614 // their flat names. 615 c.owner.members().remove(c); 616 c.name = name; 617 c.owner = owner; 618 c.fullname = ClassSymbol.formFullName(name, owner); 619 } 620 return c; 621 } 622 623 public ClassSymbol getClass(ModuleSymbol msym, Name flatName) { 624 Assert.checkNonNull(msym, flatName::toString); 625 return classes.getOrDefault(flatName, Collections.emptyMap()).get(msym); 626 } 627 628 public PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName) { 629 Assert.checkNonNull(msym); 630 631 if (flatName.isEmpty()) { 632 //unnamed packages only from the current module - visiblePackages contains *root* package, not unnamed package! 633 return msym.unnamedPackage; 634 } 635 636 if (msym == noModule) { 637 return enterPackage(msym, flatName); 638 } 639 640 msym.complete(); 641 642 PackageSymbol pack; 643 644 pack = msym.visiblePackages.get(flatName); 645 646 if (pack != null) 647 return pack; 648 649 pack = getPackage(msym, flatName); 650 651 if (pack != null && pack.exists()) 652 return pack; 653 654 boolean dependsOnUnnamed = msym.requires != null && 655 msym.requires.stream() 656 .map(rd -> rd.module) 657 .anyMatch(mod -> mod == unnamedModule); 658 659 if (dependsOnUnnamed) { 660 //msyms depends on the unnamed module, for which we generally don't know 661 //the list of packages it "exports" ahead of time. So try to lookup the package in the 662 //current module, and in the unnamed module and see if it exists in one of them 663 PackageSymbol unnamedPack = getPackage(unnamedModule, flatName); 664 665 if (unnamedPack != null && unnamedPack.exists()) { 666 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack); 667 return unnamedPack; 668 } 669 670 pack = enterPackage(msym, flatName); 671 pack.complete(); 672 if (pack.exists()) 673 return pack; 674 675 unnamedPack = enterPackage(unnamedModule, flatName); 676 unnamedPack.complete(); 677 if (unnamedPack.exists()) { 678 msym.visiblePackages.put(unnamedPack.fullname, unnamedPack); 679 return unnamedPack; 680 } 681 682 return pack; 683 } 684 685 return enterPackage(msym, flatName); 686 } 687 688 private static final Map<ModuleSymbol, ClassSymbol> EMPTY = new HashMap<>(); 689 690 public void removeClass(ModuleSymbol msym, Name flatName) { 691 classes.getOrDefault(flatName, EMPTY).remove(msym); 692 } 693 694 public Iterable<ClassSymbol> getAllClasses() { 695 return () -> Iterators.createCompoundIterator(classes.values(), v -> v.values().iterator()); 696 } 697 698 private void doEnterClass(ModuleSymbol msym, ClassSymbol cs) { 699 classes.computeIfAbsent(cs.flatname, n -> new HashMap<>()).put(msym, cs); 700 } 701 702 /** Create a new member or toplevel class symbol with given flat name 703 * and enter in `classes' unless already there. 704 */ 705 public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) { 706 Assert.checkNonNull(msym); 707 PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname)); 708 Assert.checkNonNull(ps); 709 Assert.checkNonNull(ps.modle); 710 ClassSymbol c = getClass(ps.modle, flatname); 711 if (c == null) { 712 c = defineClass(Convert.shortName(flatname), ps); 713 doEnterClass(ps.modle, c); 714 return c; 715 } else 716 return c; 717 } 718 719 /** Check to see if a package exists, given its fully qualified name. 720 */ 721 public boolean packageExists(ModuleSymbol msym, Name fullname) { 722 Assert.checkNonNull(msym); 723 return lookupPackage(msym, fullname).exists(); 724 } 725 726 /** Make a package, given its fully qualified name. 727 */ 728 public PackageSymbol enterPackage(ModuleSymbol currModule, Name fullname) { 729 Assert.checkNonNull(currModule); 730 PackageSymbol p = getPackage(currModule, fullname); 731 if (p == null) { 732 Assert.check(!fullname.isEmpty(), () -> "rootPackage missing!; currModule: " + currModule); 733 p = new PackageSymbol( 734 Convert.shortName(fullname), 735 enterPackage(currModule, Convert.packagePart(fullname))); 736 p.completer = initialCompleter; 737 p.modle = currModule; 738 doEnterPackage(currModule, p); 739 } 740 return p; 741 } 742 743 private void doEnterPackage(ModuleSymbol msym, PackageSymbol pack) { 744 packages.computeIfAbsent(pack.fullname, n -> new HashMap<>()).put(msym, pack); 745 msym.enclosedPackages = msym.enclosedPackages.prepend(pack); 746 } 747 748 private void addRootPackageFor(ModuleSymbol module) { 749 doEnterPackage(module, rootPackage); 750 PackageSymbol unnamedPackage = new PackageSymbol(names.empty, rootPackage) { 751 @Override 752 public String toString() { 753 return messages.getLocalizedString("compiler.misc.unnamed.package"); 754 } 755 }; 756 unnamedPackage.modle = module; 757 //we cannot use a method reference below, as initialCompleter might be null now 758 unnamedPackage.completer = s -> initialCompleter.complete(s); 759 module.unnamedPackage = unnamedPackage; 760 } 761 762 public PackageSymbol getPackage(ModuleSymbol module, Name fullname) { 763 return packages.getOrDefault(fullname, Collections.emptyMap()).get(module); 764 } 765 766 public ModuleSymbol enterModule(Name name) { 767 ModuleSymbol msym = modules.get(name); 768 if (msym == null) { 769 msym = ModuleSymbol.create(name, names.module_info); 770 addRootPackageFor(msym); 771 msym.completer = s -> moduleCompleter.complete(s); //bootstrap issues 772 modules.put(name, msym); 773 } 774 return msym; 775 } 776 777 public ModuleSymbol getModule(Name name) { 778 return modules.get(name); 779 } 780 781 //temporary: 782 public ModuleSymbol inferModule(Name packageName) { 783 if (packageName.isEmpty()) 784 return java_base == noModule ? noModule : unnamedModule;//! 785 786 ModuleSymbol msym = null; 787 Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName); 788 if (map == null) 789 return null; 790 for (Map.Entry<ModuleSymbol,PackageSymbol> e: map.entrySet()) { 791 if (!e.getValue().members().isEmpty()) { 792 if (msym == null) { 793 msym = e.getKey(); 794 } else { 795 return null; 796 } 797 } 798 } 799 return msym; 800 } 801 802 public List<ModuleSymbol> listPackageModules(Name packageName) { 803 if (packageName.isEmpty()) 804 return List.nil(); 805 806 List<ModuleSymbol> result = List.nil(); 807 Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName); 808 if (map != null) { 809 for (Map.Entry<ModuleSymbol, PackageSymbol> e: map.entrySet()) { 810 if (!e.getValue().members().isEmpty()) { 811 result = result.prepend(e.getKey()); 812 } 813 } 814 } 815 return result; 816 } 817 818 public Collection<ModuleSymbol> getAllModules() { 819 return modules.values(); 820 } 821} 822