1 /*
   2  * Copyright (c) 1999, 2015, 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.HashMap;
  29 import java.util.Map;
  30 
  31 import javax.lang.model.element.ElementVisitor;
  32 import javax.tools.JavaFileObject;
  33 
  34 
  35 import com.sun.tools.javac.code.Scope.WriteableScope;
  36 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  37 import com.sun.tools.javac.code.Symbol.Completer;
  38 import com.sun.tools.javac.code.Symbol.CompletionFailure;
  39 import com.sun.tools.javac.code.Symbol.MethodSymbol;
  40 import com.sun.tools.javac.code.Symbol.PackageSymbol;
  41 import com.sun.tools.javac.code.Symbol.TypeSymbol;
  42 import com.sun.tools.javac.code.Symbol.VarSymbol;
  43 import com.sun.tools.javac.code.Type.BottomType;
  44 import com.sun.tools.javac.code.Type.ClassType;
  45 import com.sun.tools.javac.code.Type.ErrorType;
  46 import com.sun.tools.javac.code.Type.JCPrimitiveType;
  47 import com.sun.tools.javac.code.Type.JCVoidType;
  48 import com.sun.tools.javac.code.Type.MethodType;
  49 import com.sun.tools.javac.code.Type.UnknownType;
  50 import com.sun.tools.javac.jvm.Target;
  51 import com.sun.tools.javac.util.Assert;
  52 import com.sun.tools.javac.util.Context;
  53 import com.sun.tools.javac.util.Convert;
  54 import com.sun.tools.javac.util.DefinedBy;
  55 import com.sun.tools.javac.util.DefinedBy.Api;
  56 import com.sun.tools.javac.util.JavacMessages;
  57 import com.sun.tools.javac.util.List;
  58 import com.sun.tools.javac.util.Log;
  59 import com.sun.tools.javac.util.Name;
  60 import com.sun.tools.javac.util.Names;
  61 
  62 import static com.sun.tools.javac.code.Flags.*;
  63 import static com.sun.tools.javac.code.Kinds.Kind.*;
  64 import static com.sun.tools.javac.code.TypeTag.*;
  65 
  66 /** A class that defines all predefined constants and operators
  67  *  as well as special classes such as java.lang.Object, which need
  68  *  to be known to the compiler. All symbols are held in instance
  69  *  fields. This makes it possible to work in multiple concurrent
  70  *  projects, which might use different class files for library classes.
  71  *
  72  *  <p><b>This is NOT part of any supported API.
  73  *  If you write code that depends on this, you do so at your own risk.
  74  *  This code and its internal interfaces are subject to change or
  75  *  deletion without notice.</b>
  76  */
  77 public class Symtab {
  78     /** The context key for the symbol table. */
  79     protected static final Context.Key<Symtab> symtabKey = new Context.Key<>();
  80 
  81     /** Get the symbol table instance. */
  82     public static Symtab instance(Context context) {
  83         Symtab instance = context.get(symtabKey);
  84         if (instance == null)
  85             instance = new Symtab(context);
  86         return instance;
  87     }
  88 
  89     /** Builtin types.
  90      */
  91     public final JCPrimitiveType byteType = new JCPrimitiveType(BYTE, null);
  92     public final JCPrimitiveType charType = new JCPrimitiveType(CHAR, null);
  93     public final JCPrimitiveType shortType = new JCPrimitiveType(SHORT, null);
  94     public final JCPrimitiveType intType = new JCPrimitiveType(INT, null);
  95     public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null);
  96     public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null);
  97     public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null);
  98     public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null);
  99     public final Type botType = new BottomType();
 100     public final JCVoidType voidType = new JCVoidType();
 101 
 102     private final Names names;
 103     private final Completer initialCompleter;
 104     private final Target target;
 105 
 106     /** A symbol for the root package.
 107      */
 108     public final PackageSymbol rootPackage;
 109 
 110     /** A symbol for the unnamed package.
 111      */
 112     public final PackageSymbol unnamedPackage;
 113 
 114     /** A symbol that stands for a missing symbol.
 115      */
 116     public final TypeSymbol noSymbol;
 117 
 118     /** The error symbol.
 119      */
 120     public final ClassSymbol errSymbol;
 121 
 122     /** The unknown symbol.
 123      */
 124     public final ClassSymbol unknownSymbol;
 125 
 126     /** A value for the errType, with a originalType of noType */
 127     public final Type errType;
 128 
 129     /** A value for the unknown type. */
 130     public final Type unknownType;
 131 
 132     /** The builtin type of all arrays. */
 133     public final ClassSymbol arrayClass;
 134     public final MethodSymbol arrayCloneMethod;
 135 
 136     /** VGJ: The (singleton) type of all bound types. */
 137     public final ClassSymbol boundClass;
 138 
 139     /** The builtin type of all methods. */
 140     public final ClassSymbol methodClass;
 141 
 142     /** Predefined types.
 143      */
 144     public final Type objectType;
 145     public final Type objectsType;
 146     public final Type voidClassType;
 147     public final Type classType;
 148     public final Type classLoaderType;
 149     public final Type stringType;
 150     public final Type stringBufferType;
 151     public final Type stringBuilderType;
 152     public final Type cloneableType;
 153     public final Type serializableType;
 154     public final Type serializedLambdaType;
 155     public final Type methodHandleType;
 156     public final Type methodHandleLookupType;
 157     public final Type methodTypeType;
 158     public final Type nativeHeaderType;
 159     public final Type throwableType;
 160     public final Type errorType;
 161     public final Type interruptedExceptionType;
 162     public final Type illegalArgumentExceptionType;
 163     public final Type exceptionType;
 164     public final Type runtimeExceptionType;
 165     public final Type classNotFoundExceptionType;
 166     public final Type noClassDefFoundErrorType;
 167     public final Type noSuchFieldErrorType;
 168     public final Type assertionErrorType;
 169     public final Type cloneNotSupportedExceptionType;
 170     public final Type annotationType;
 171     public final TypeSymbol enumSym;
 172     public final Type listType;
 173     public final Type collectionsType;
 174     public final Type comparableType;
 175     public final Type comparatorType;
 176     public final Type arraysType;
 177     public final Type iterableType;
 178     public final Type iteratorType;
 179     public final Type annotationTargetType;
 180     public final Type overrideType;
 181     public final Type retentionType;
 182     public final Type deprecatedType;
 183     public final Type suppressWarningsType;
 184     public final Type supplierType;
 185     public final Type inheritedType;
 186     public final Type profileType;
 187     public final Type proprietaryType;
 188     public final Type systemType;
 189     public final Type autoCloseableType;
 190     public final Type trustMeType;
 191     public final Type lambdaMetafactory;
 192     public final Type stringConcatFactory;
 193     public final Type repeatableType;
 194     public final Type documentedType;
 195     public final Type elementTypeType;
 196     public final Type functionalInterfaceType;
 197 
 198     /** The symbol representing the length field of an array.
 199      */
 200     public final VarSymbol lengthVar;
 201 
 202     /** The symbol representing the final finalize method on enums */
 203     public final MethodSymbol enumFinalFinalize;
 204 
 205     /** The symbol representing the close method on TWR AutoCloseable type */
 206     public final MethodSymbol autoCloseableClose;
 207 
 208     /** The predefined type that belongs to a tag.
 209      */
 210     public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()];
 211 
 212     /** The name of the class that belongs to a basix type tag.
 213      */
 214     public final Name[] boxedName = new Name[TypeTag.getTypeTagCount()];
 215 
 216     /** A hashtable containing the encountered top-level and member classes,
 217      *  indexed by flat names. The table does not contain local classes.
 218      *  It should be updated from the outside to reflect classes defined
 219      *  by compiled source files.
 220      */
 221     public final Map<Name, ClassSymbol> classes = new HashMap<>();
 222 
 223     /** A hashtable containing the encountered packages.
 224      *  the table should be updated from outside to reflect packages defined
 225      *  by compiled source files.
 226      */
 227     public final Map<Name, PackageSymbol> packages = new HashMap<>();
 228 
 229     public void initType(Type type, ClassSymbol c) {
 230         type.tsym = c;
 231         typeOfTag[type.getTag().ordinal()] = type;
 232     }
 233 
 234     public void initType(Type type, String name) {
 235         initType(
 236             type,
 237             new ClassSymbol(
 238                 PUBLIC, names.fromString(name), type, rootPackage));
 239     }
 240 
 241     public void initType(Type type, String name, String bname) {
 242         initType(type, name);
 243             boxedName[type.getTag().ordinal()] = names.fromString("java.lang." + bname);
 244     }
 245 
 246     /** The class symbol that owns all predefined symbols.
 247      */
 248     public final ClassSymbol predefClass;
 249 
 250     /** Enter a class into symbol table.
 251      *  @param s The name of the class.
 252      */
 253     private Type enterClass(String s) {
 254         return enterClass(names.fromString(s)).type;
 255     }
 256 
 257     public void synthesizeEmptyInterfaceIfMissing(final Type type) {
 258         final Completer completer = type.tsym.completer;
 259         type.tsym.completer = new Completer() {
 260             public void complete(Symbol sym) throws CompletionFailure {
 261                 try {
 262                     completer.complete(sym);
 263                 } catch (CompletionFailure e) {
 264                     sym.flags_field |= (PUBLIC | INTERFACE);
 265                     ((ClassType) sym.type).supertype_field = objectType;
 266                 }
 267             }
 268 
 269             @Override
 270             public boolean isTerminal() {
 271                 return completer.isTerminal();
 272             }
 273         };
 274     }
 275 
 276     public void synthesizeBoxTypeIfMissing(final Type type) {
 277         ClassSymbol sym = enterClass(boxedName[type.getTag().ordinal()]);
 278         final Completer completer = sym.completer;
 279         sym.completer = new Completer() {
 280             public void complete(Symbol sym) throws CompletionFailure {
 281                 try {
 282                     completer.complete(sym);
 283                 } catch (CompletionFailure e) {
 284                     sym.flags_field |= PUBLIC;
 285                     ((ClassType) sym.type).supertype_field = objectType;
 286                     MethodSymbol boxMethod =
 287                         new MethodSymbol(PUBLIC | STATIC, names.valueOf,
 288                                          new MethodType(List.of(type), sym.type,
 289                                 List.<Type>nil(), methodClass),
 290                             sym);
 291                     sym.members().enter(boxMethod);
 292                     MethodSymbol unboxMethod =
 293                         new MethodSymbol(PUBLIC,
 294                             type.tsym.name.append(names.Value), // x.intValue()
 295                             new MethodType(List.<Type>nil(), type,
 296                                 List.<Type>nil(), methodClass),
 297                             sym);
 298                     sym.members().enter(unboxMethod);
 299                 }
 300             }
 301 
 302             @Override
 303             public boolean isTerminal() {
 304                 return completer.isTerminal();
 305             }
 306         };
 307     }
 308 
 309     // Enter a synthetic class that is used to mark classes in ct.sym.
 310     // This class does not have a class file.
 311     private Type enterSyntheticAnnotation(String name) {
 312         ClassType type = (ClassType)enterClass(name);
 313         ClassSymbol sym = (ClassSymbol)type.tsym;
 314         sym.completer = Completer.NULL_COMPLETER;
 315         sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
 316         sym.erasure_field = type;
 317         sym.members_field = WriteableScope.create(sym);
 318         type.typarams_field = List.nil();
 319         type.allparams_field = List.nil();
 320         type.supertype_field = annotationType;
 321         type.interfaces_field = List.nil();
 322         return type;
 323     }
 324 
 325     /** Constructor; enters all predefined identifiers and operators
 326      *  into symbol table.
 327      */
 328     protected Symtab(Context context) throws CompletionFailure {
 329         context.put(symtabKey, this);
 330 
 331         names = Names.instance(context);
 332         target = Target.instance(context);
 333 
 334         // Create the unknown type
 335         unknownType = new UnknownType();
 336 
 337         // create the basic builtin symbols
 338         rootPackage = new PackageSymbol(names.empty, null);
 339         packages.put(names.empty, rootPackage);
 340         final JavacMessages messages = JavacMessages.instance(context);
 341         unnamedPackage = new PackageSymbol(names.empty, rootPackage) {
 342                 public String toString() {
 343                     return messages.getLocalizedString("compiler.misc.unnamed.package");
 344                 }
 345             };
 346         noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) {
 347             @DefinedBy(Api.LANGUAGE_MODEL)
 348             public <R, P> R accept(ElementVisitor<R, P> v, P p) {
 349                 return v.visitUnknown(this, p);
 350             }
 351         };
 352 
 353         // create the error symbols
 354         errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
 355         errType = new ErrorType(errSymbol, Type.noType);
 356 
 357         unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage);
 358         unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol);
 359         unknownSymbol.type = unknownType;
 360 
 361         // initialize builtin types
 362         initType(byteType, "byte", "Byte");
 363         initType(shortType, "short", "Short");
 364         initType(charType, "char", "Character");
 365         initType(intType, "int", "Integer");
 366         initType(longType, "long", "Long");
 367         initType(floatType, "float", "Float");
 368         initType(doubleType, "double", "Double");
 369         initType(booleanType, "boolean", "Boolean");
 370         initType(voidType, "void", "Void");
 371         initType(botType, "<nulltype>");
 372         initType(errType, errSymbol);
 373         initType(unknownType, unknownSymbol);
 374 
 375         // the builtin class of all arrays
 376         arrayClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Array, noSymbol);
 377 
 378         // VGJ
 379         boundClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Bound, noSymbol);
 380         boundClass.members_field = new Scope.ErrorScope(boundClass);
 381 
 382         // the builtin class of all methods
 383         methodClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Method, noSymbol);
 384         methodClass.members_field = new Scope.ErrorScope(boundClass);
 385 
 386         // Create class to hold all predefined constants and operations.
 387         predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage);
 388         WriteableScope scope = WriteableScope.create(predefClass);
 389         predefClass.members_field = scope;
 390 
 391         // Get the initial completer for Symbols from the ClassFinder
 392         initialCompleter = ClassFinder.instance(context).getCompleter();
 393         rootPackage.completer = initialCompleter;
 394         unnamedPackage.completer = initialCompleter;
 395 
 396         // Enter symbols for basic types.
 397         scope.enter(byteType.tsym);
 398         scope.enter(shortType.tsym);
 399         scope.enter(charType.tsym);
 400         scope.enter(intType.tsym);
 401         scope.enter(longType.tsym);
 402         scope.enter(floatType.tsym);
 403         scope.enter(doubleType.tsym);
 404         scope.enter(booleanType.tsym);
 405         scope.enter(errType.tsym);
 406 
 407         // Enter symbol for the errSymbol
 408         scope.enter(errSymbol);
 409 
 410         classes.put(predefClass.fullname, predefClass);
 411 
 412         // Enter predefined classes.
 413         objectType = enterClass("java.lang.Object");
 414         objectsType = enterClass("java.util.Objects");
 415         classType = enterClass("java.lang.Class");
 416         voidClassType = enterClass("java.lang.Void");
 417         stringType = enterClass("java.lang.String");
 418         stringBufferType = enterClass("java.lang.StringBuffer");
 419         stringBuilderType = enterClass("java.lang.StringBuilder");
 420         cloneableType = enterClass("java.lang.Cloneable");
 421         throwableType = enterClass("java.lang.Throwable");
 422         serializableType = enterClass("java.io.Serializable");
 423         serializedLambdaType = enterClass("java.lang.invoke.SerializedLambda");
 424         methodHandleType = enterClass("java.lang.invoke.MethodHandle");
 425         methodHandleLookupType = enterClass("java.lang.invoke.MethodHandles$Lookup");
 426         methodTypeType = enterClass("java.lang.invoke.MethodType");
 427         errorType = enterClass("java.lang.Error");
 428         illegalArgumentExceptionType = enterClass("java.lang.IllegalArgumentException");
 429         interruptedExceptionType = enterClass("java.lang.InterruptedException");
 430         exceptionType = enterClass("java.lang.Exception");
 431         runtimeExceptionType = enterClass("java.lang.RuntimeException");
 432         classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
 433         noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
 434         noSuchFieldErrorType = enterClass("java.lang.NoSuchFieldError");
 435         assertionErrorType = enterClass("java.lang.AssertionError");
 436         cloneNotSupportedExceptionType = enterClass("java.lang.CloneNotSupportedException");
 437         annotationType = enterClass("java.lang.annotation.Annotation");
 438         classLoaderType = enterClass("java.lang.ClassLoader");
 439         enumSym = enterClass(names.java_lang_Enum);
 440         enumFinalFinalize =
 441             new MethodSymbol(PROTECTED|FINAL|HYPOTHETICAL,
 442                              names.finalize,
 443                              new MethodType(List.<Type>nil(), voidType,
 444                                             List.<Type>nil(), methodClass),
 445                              enumSym);
 446         listType = enterClass("java.util.List");
 447         collectionsType = enterClass("java.util.Collections");
 448         comparableType = enterClass("java.lang.Comparable");
 449         comparatorType = enterClass("java.util.Comparator");
 450         arraysType = enterClass("java.util.Arrays");
 451         iterableType = enterClass("java.lang.Iterable");
 452         iteratorType = enterClass("java.util.Iterator");
 453         annotationTargetType = enterClass("java.lang.annotation.Target");
 454         overrideType = enterClass("java.lang.Override");
 455         retentionType = enterClass("java.lang.annotation.Retention");
 456         deprecatedType = enterClass("java.lang.Deprecated");
 457         suppressWarningsType = enterClass("java.lang.SuppressWarnings");
 458         supplierType = enterClass("java.util.function.Supplier");
 459         inheritedType = enterClass("java.lang.annotation.Inherited");
 460         repeatableType = enterClass("java.lang.annotation.Repeatable");
 461         documentedType = enterClass("java.lang.annotation.Documented");
 462         elementTypeType = enterClass("java.lang.annotation.ElementType");
 463         systemType = enterClass("java.lang.System");
 464         autoCloseableType = enterClass("java.lang.AutoCloseable");
 465         autoCloseableClose = new MethodSymbol(PUBLIC,
 466                              names.close,
 467                              new MethodType(List.<Type>nil(), voidType,
 468                                             List.of(exceptionType), methodClass),
 469                              autoCloseableType.tsym);
 470         trustMeType = enterClass("java.lang.SafeVarargs");
 471         nativeHeaderType = enterClass("java.lang.annotation.Native");
 472         lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
 473         stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory");
 474         functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
 475 
 476         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
 477         synthesizeEmptyInterfaceIfMissing(cloneableType);
 478         synthesizeEmptyInterfaceIfMissing(serializableType);
 479         synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
 480         synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
 481         synthesizeEmptyInterfaceIfMissing(stringConcatFactory);
 482         synthesizeBoxTypeIfMissing(doubleType);
 483         synthesizeBoxTypeIfMissing(floatType);
 484         synthesizeBoxTypeIfMissing(voidType);
 485 
 486         // Enter a synthetic class that is used to mark internal
 487         // proprietary classes in ct.sym.  This class does not have a
 488         // class file.
 489         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
 490 
 491         // Enter a synthetic class that is used to provide profile info for
 492         // classes in ct.sym.  This class does not have a class file.
 493         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
 494         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
 495         profileType.tsym.members().enter(m);
 496 
 497         // Enter a class for arrays.
 498         // The class implements java.lang.Cloneable and java.io.Serializable.
 499         // It has a final length field and a clone method.
 500         ClassType arrayClassType = (ClassType)arrayClass.type;
 501         arrayClassType.supertype_field = objectType;
 502         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
 503         arrayClass.members_field = WriteableScope.create(arrayClass);
 504         lengthVar = new VarSymbol(
 505             PUBLIC | FINAL,
 506             names.length,
 507             intType,
 508             arrayClass);
 509         arrayClass.members().enter(lengthVar);
 510         arrayCloneMethod = new MethodSymbol(
 511             PUBLIC,
 512             names.clone,
 513             new MethodType(List.<Type>nil(), objectType,
 514                            List.<Type>nil(), methodClass),
 515             arrayClass);
 516         arrayClass.members().enter(arrayCloneMethod);
 517     }
 518 
 519     /** Define a new class given its name and owner.
 520      */
 521     public ClassSymbol defineClass(Name name, Symbol owner) {
 522         ClassSymbol c = new ClassSymbol(0, name, owner);
 523         if (owner.kind == PCK)
 524             Assert.checkNull(classes.get(c.flatname), c);
 525         c.completer = initialCompleter;
 526         return c;
 527     }
 528 
 529     /** Create a new toplevel or member class symbol with given name
 530      *  and owner and enter in `classes' unless already there.
 531      */
 532     public ClassSymbol enterClass(Name name, TypeSymbol owner) {
 533         Name flatname = TypeSymbol.formFlatName(name, owner);
 534         ClassSymbol c = classes.get(flatname);
 535         if (c == null) {
 536             c = defineClass(name, owner);
 537             classes.put(flatname, c);
 538         } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
 539             // reassign fields of classes that might have been loaded with
 540             // their flat names.
 541             c.owner.members().remove(c);
 542             c.name = name;
 543             c.owner = owner;
 544             c.fullname = ClassSymbol.formFullName(name, owner);
 545         }
 546         return c;
 547     }
 548 
 549     /**
 550      * Creates a new toplevel class symbol with given flat name and
 551      * given class (or source) file.
 552      *
 553      * @param flatName a fully qualified binary class name
 554      * @param classFile the class file or compilation unit defining
 555      * the class (may be {@code null})
 556      * @return a newly created class symbol
 557      * @throws AssertionError if the class symbol already exists
 558      */
 559     public ClassSymbol enterClass(Name flatName, JavaFileObject classFile) {
 560         ClassSymbol cs = classes.get(flatName);
 561         if (cs != null) {
 562             String msg = Log.format("%s: completer = %s; class file = %s; source file = %s",
 563                                     cs.fullname,
 564                                     cs.completer,
 565                                     cs.classfile,
 566                                     cs.sourcefile);
 567             throw new AssertionError(msg);
 568         }
 569         Name packageName = Convert.packagePart(flatName);
 570         PackageSymbol owner = packageName.isEmpty()
 571                                 ? unnamedPackage
 572                                 : enterPackage(packageName);
 573         cs = defineClass(Convert.shortName(flatName), owner);
 574         cs.classfile = classFile;
 575         classes.put(flatName, cs);
 576         return cs;
 577     }
 578 
 579     /** Create a new member or toplevel class symbol with given flat name
 580      *  and enter in `classes' unless already there.
 581      */
 582     public ClassSymbol enterClass(Name flatname) {
 583         ClassSymbol c = classes.get(flatname);
 584         if (c == null)
 585             return enterClass(flatname, (JavaFileObject)null);
 586         else
 587             return c;
 588     }
 589 
 590     /** Check to see if a package exists, given its fully qualified name.
 591      */
 592     public boolean packageExists(Name fullname) {
 593         return enterPackage(fullname).exists();
 594     }
 595 
 596     /** Make a package, given its fully qualified name.
 597      */
 598     public PackageSymbol enterPackage(Name fullname) {
 599         PackageSymbol p = packages.get(fullname);
 600         if (p == null) {
 601             Assert.check(!fullname.isEmpty(), "rootPackage missing!");
 602             p = new PackageSymbol(
 603                 Convert.shortName(fullname),
 604                 enterPackage(Convert.packagePart(fullname)));
 605             p.completer = initialCompleter;
 606             packages.put(fullname, p);
 607         }
 608         return p;
 609     }
 610 
 611     /** Make a package, given its unqualified name and enclosing package.
 612      */
 613     public PackageSymbol enterPackage(Name name, PackageSymbol owner) {
 614         return enterPackage(TypeSymbol.formFullName(name, owner));
 615     }
 616 }