Symtab.java revision 3907:84bfe4b79603
1/* 2 * Copyright (c) 1999, 2017, 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 26package com.sun.tools.javac.code; 27 28import java.util.Collection; 29import java.util.Collections; 30import java.util.EnumSet; 31import java.util.HashMap; 32import java.util.LinkedHashMap; 33import java.util.Map; 34 35import javax.lang.model.element.ElementVisitor; 36 37import com.sun.tools.javac.code.Scope.WriteableScope; 38import com.sun.tools.javac.code.Symbol.ClassSymbol; 39import com.sun.tools.javac.code.Symbol.Completer; 40import com.sun.tools.javac.code.Symbol.CompletionFailure; 41import com.sun.tools.javac.code.Symbol.MethodSymbol; 42import com.sun.tools.javac.code.Symbol.ModuleSymbol; 43import com.sun.tools.javac.code.Symbol.PackageSymbol; 44import com.sun.tools.javac.code.Symbol.TypeSymbol; 45import com.sun.tools.javac.code.Symbol.VarSymbol; 46import com.sun.tools.javac.code.Type.BottomType; 47import com.sun.tools.javac.code.Type.ClassType; 48import com.sun.tools.javac.code.Type.ErrorType; 49import com.sun.tools.javac.code.Type.JCPrimitiveType; 50import com.sun.tools.javac.code.Type.JCVoidType; 51import com.sun.tools.javac.code.Type.MethodType; 52import com.sun.tools.javac.code.Type.UnknownType; 53import com.sun.tools.javac.comp.Modules; 54import com.sun.tools.javac.util.Assert; 55import com.sun.tools.javac.util.Context; 56import com.sun.tools.javac.util.Convert; 57import com.sun.tools.javac.util.DefinedBy; 58import com.sun.tools.javac.util.DefinedBy.Api; 59import com.sun.tools.javac.util.Iterators; 60import com.sun.tools.javac.util.JavacMessages; 61import com.sun.tools.javac.util.List; 62import com.sun.tools.javac.util.Name; 63import com.sun.tools.javac.util.Names; 64 65import static com.sun.tools.javac.code.Flags.*; 66import static com.sun.tools.javac.code.Kinds.Kind.*; 67import static com.sun.tools.javac.code.TypeTag.*; 68 69/** A class that defines all predefined constants and operators 70 * as well as special classes such as java.lang.Object, which need 71 * to be known to the compiler. All symbols are held in instance 72 * fields. This makes it possible to work in multiple concurrent 73 * projects, which might use different class files for library classes. 74 * 75 * <p><b>This is NOT part of any supported API. 76 * If you write code that depends on this, you do so at your own risk. 77 * This code and its internal interfaces are subject to change or 78 * deletion without notice.</b> 79 */ 80public class Symtab { 81 /** The context key for the symbol table. */ 82 protected static final Context.Key<Symtab> symtabKey = new Context.Key<>(); 83 84 /** Get the symbol table instance. */ 85 public static Symtab instance(Context context) { 86 Symtab instance = context.get(symtabKey); 87 if (instance == null) 88 instance = new Symtab(context); 89 return instance; 90 } 91 92 /** Builtin types. 93 */ 94 public final JCPrimitiveType byteType = new JCPrimitiveType(BYTE, null); 95 public final JCPrimitiveType charType = new JCPrimitiveType(CHAR, null); 96 public final JCPrimitiveType shortType = new JCPrimitiveType(SHORT, null); 97 public final JCPrimitiveType intType = new JCPrimitiveType(INT, null); 98 public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null); 99 public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null); 100 public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null); 101 public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null); 102 public final Type botType = new BottomType(); 103 public final JCVoidType voidType = new JCVoidType(); 104 105 private final Names names; 106 private final JavacMessages messages; 107 private final Completer initialCompleter; 108 private final Completer moduleCompleter; 109 110 /** A symbol for the unnamed module. 111 */ 112 public final ModuleSymbol unnamedModule; 113 114 /** The error module. 115 */ 116 public final ModuleSymbol errModule; 117 118 /** A symbol for no module, for use with -source 8 or less 119 */ 120 public final ModuleSymbol noModule; 121 122 /** A symbol for the root package. 123 */ 124 public final PackageSymbol rootPackage; 125 126 /** A symbol that stands for a missing symbol. 127 */ 128 public final TypeSymbol noSymbol; 129 130 /** The error symbol. 131 */ 132 public final ClassSymbol errSymbol; 133 134 /** The unknown symbol. 135 */ 136 public final ClassSymbol unknownSymbol; 137 138 /** A value for the errType, with a originalType of noType */ 139 public final Type errType; 140 141 /** A value for the unknown type. */ 142 public final Type unknownType; 143 144 /** The builtin type of all arrays. */ 145 public final ClassSymbol arrayClass; 146 public final MethodSymbol arrayCloneMethod; 147 148 /** VGJ: The (singleton) type of all bound types. */ 149 public final ClassSymbol boundClass; 150 151 /** The builtin type of all methods. */ 152 public final ClassSymbol methodClass; 153 154 /** A symbol for the java.base module. 155 */ 156 public final ModuleSymbol java_base; 157 158 /** Predefined types. 159 */ 160 public final Type objectType; 161 public final Type objectsType; 162 public final Type classType; 163 public final Type classLoaderType; 164 public final Type stringType; 165 public final Type stringBufferType; 166 public final Type stringBuilderType; 167 public final Type cloneableType; 168 public final Type serializableType; 169 public final Type serializedLambdaType; 170 public final Type varHandleType; 171 public final Type methodHandleType; 172 public final Type methodHandleLookupType; 173 public final Type methodTypeType; 174 public final Type nativeHeaderType; 175 public final Type throwableType; 176 public final Type errorType; 177 public final Type interruptedExceptionType; 178 public final Type illegalArgumentExceptionType; 179 public final Type exceptionType; 180 public final Type runtimeExceptionType; 181 public final Type classNotFoundExceptionType; 182 public final Type noClassDefFoundErrorType; 183 public final Type noSuchFieldErrorType; 184 public final Type assertionErrorType; 185 public final Type cloneNotSupportedExceptionType; 186 public final Type annotationType; 187 public final TypeSymbol enumSym; 188 public final Type listType; 189 public final Type collectionsType; 190 public final Type comparableType; 191 public final Type comparatorType; 192 public final Type arraysType; 193 public final Type iterableType; 194 public final Type iteratorType; 195 public final Type annotationTargetType; 196 public final Type overrideType; 197 public final Type retentionType; 198 public final Type deprecatedType; 199 public final Type suppressWarningsType; 200 public final Type supplierType; 201 public final Type inheritedType; 202 public final Type profileType; 203 public final Type proprietaryType; 204 public final Type systemType; 205 public final Type autoCloseableType; 206 public final Type trustMeType; 207 public final Type lambdaMetafactory; 208 public final Type stringConcatFactory; 209 public final Type repeatableType; 210 public final Type documentedType; 211 public final Type elementTypeType; 212 public final Type functionalInterfaceType; 213 214 /** The symbol representing the length field of an array. 215 */ 216 public final VarSymbol lengthVar; 217 218 /** The symbol representing the final finalize method on enums */ 219 public final MethodSymbol enumFinalFinalize; 220 221 /** The symbol representing the close method on TWR AutoCloseable type */ 222 public final MethodSymbol autoCloseableClose; 223 224 /** The predefined type that belongs to a tag. 225 */ 226 public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()]; 227 228 /** The name of the class that belongs to a basic type tag. 229 */ 230 public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()]; 231 232 /** A hashtable containing the encountered top-level and member classes, 233 * indexed by flat names. The table does not contain local classes. 234 * It should be updated from the outside to reflect classes defined 235 * by compiled source files. 236 */ 237 private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>(); 238 239 /** A hashtable containing the encountered packages. 240 * the table should be updated from outside to reflect packages defined 241 * by compiled source files. 242 */ 243 private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>(); 244 245 /** A hashtable giving the encountered modules. 246 */ 247 private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>(); 248 249 public void initType(Type type, ClassSymbol c) { 250 type.tsym = c; 251 typeOfTag[type.getTag().ordinal()] = type; 252 } 253 254 public void initType(Type type, String name) { 255 initType( 256 type, 257 new ClassSymbol( 258 PUBLIC, names.fromString(name), type, rootPackage)); 259 } 260 261 public void initType(Type type, String name, String bname) { 262 initType(type, name); 263 boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname); 264 } 265 266 /** The class symbol that owns all predefined symbols. 267 */ 268 public final ClassSymbol predefClass; 269 270 /** Enter a class into symbol table. 271 * @param s The name of the class. 272 */ 273 private Type enterClass(String s) { 274 return enterClass(java_base, names.fromString(s)).type; 275 } 276 277 public void synthesizeEmptyInterfaceIfMissing(final Type type) { 278 final Completer completer = type.tsym.completer; 279 type.tsym.completer = new Completer() { 280 @Override 281 public void complete(Symbol sym) throws CompletionFailure { 282 try { 283 completer.complete(sym); 284 } catch (CompletionFailure e) { 285 sym.flags_field |= (PUBLIC | INTERFACE); 286 ((ClassType) sym.type).supertype_field = objectType; 287 } 288 } 289 290 @Override 291 public boolean isTerminal() { 292 return completer.isTerminal(); 293 } 294 }; 295 } 296 297 public void synthesizeBoxTypeIfMissing(final Type type) { 298 ClassSymbol sym = enterClass(java_base, boxedName[type.getTag().ordinal()]); 299 final Completer completer = sym.completer; 300 sym.completer = new Completer() { 301 @Override 302 public void complete(Symbol sym) throws CompletionFailure { 303 try { 304 completer.complete(sym); 305 } catch (CompletionFailure e) { 306 sym.flags_field |= PUBLIC; 307 ((ClassType) sym.type).supertype_field = objectType; 308 MethodSymbol boxMethod = 309 new MethodSymbol(PUBLIC | STATIC, names.valueOf, 310 new MethodType(List.of(type), sym.type, 311 List.nil(), methodClass), 312 sym); 313 sym.members().enter(boxMethod); 314 MethodSymbol unboxMethod = 315 new MethodSymbol(PUBLIC, 316 type.tsym.name.append(names.Value), // x.intValue() 317 new MethodType(List.nil(), type, 318 List.nil(), methodClass), 319 sym); 320 sym.members().enter(unboxMethod); 321 } 322 } 323 324 @Override 325 public boolean isTerminal() { 326 return completer.isTerminal(); 327 } 328 }; 329 } 330 331 // 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 public Iterable<ClassSymbol> getClassesForName(Name candidate) { 823 return classes.getOrDefault(candidate, Collections.emptyMap()).values(); 824 } 825 826 public Iterable<PackageSymbol> getPackagesForName(Name candidate) { 827 return packages.getOrDefault(candidate, Collections.emptyMap()).values(); 828 } 829} 830