< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java

Print this page

 35 import javax.lang.model.element.ElementVisitor;
 36 
 37 import com.sun.tools.javac.code.Scope.WriteableScope;
 38 import com.sun.tools.javac.code.Source.Feature;
 39 import com.sun.tools.javac.code.Symbol.ClassSymbol;
 40 import com.sun.tools.javac.code.Symbol.Completer;
 41 import com.sun.tools.javac.code.Symbol.CompletionFailure;
 42 import com.sun.tools.javac.code.Symbol.MethodSymbol;
 43 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
 44 import com.sun.tools.javac.code.Symbol.PackageSymbol;
 45 import com.sun.tools.javac.code.Symbol.RootPackageSymbol;
 46 import com.sun.tools.javac.code.Symbol.TypeSymbol;
 47 import com.sun.tools.javac.code.Symbol.VarSymbol;
 48 import com.sun.tools.javac.code.Type.BottomType;
 49 import com.sun.tools.javac.code.Type.ClassType;
 50 import com.sun.tools.javac.code.Type.ErrorType;
 51 import com.sun.tools.javac.code.Type.JCPrimitiveType;
 52 import com.sun.tools.javac.code.Type.JCVoidType;
 53 import com.sun.tools.javac.code.Type.MethodType;
 54 import com.sun.tools.javac.code.Type.UnknownType;

 55 import com.sun.tools.javac.code.Types.UniqueType;
 56 import com.sun.tools.javac.comp.Modules;
 57 import com.sun.tools.javac.jvm.Target;
 58 import com.sun.tools.javac.util.Assert;
 59 import com.sun.tools.javac.util.Context;
 60 import com.sun.tools.javac.util.Convert;
 61 import com.sun.tools.javac.util.DefinedBy;
 62 import com.sun.tools.javac.util.DefinedBy.Api;
 63 import com.sun.tools.javac.util.Iterators;
 64 import com.sun.tools.javac.util.JavacMessages;
 65 import com.sun.tools.javac.util.List;
 66 import com.sun.tools.javac.util.Name;
 67 import com.sun.tools.javac.util.Names;

 68 
 69 import static com.sun.tools.javac.code.Flags.*;
 70 import static com.sun.tools.javac.code.Kinds.Kind.*;
 71 import static com.sun.tools.javac.code.TypeTag.*;
 72 
 73 /** A class that defines all predefined constants and operators
 74  *  as well as special classes such as java.lang.Object, which need
 75  *  to be known to the compiler. All symbols are held in instance
 76  *  fields. This makes it possible to work in multiple concurrent
 77  *  projects, which might use different class files for library classes.
 78  *
 79  *  <p><b>This is NOT part of any supported API.
 80  *  If you write code that depends on this, you do so at your own risk.
 81  *  This code and its internal interfaces are subject to change or
 82  *  deletion without notice.</b>
 83  */
 84 public class Symtab {
 85     /** The context key for the symbol table. */
 86     protected static final Context.Key<Symtab> symtabKey = new Context.Key<>();
 87 
 88     /** Get the symbol table instance. */
 89     public static Symtab instance(Context context) {
 90         Symtab instance = context.get(symtabKey);
 91         if (instance == null)
 92             instance = new Symtab(context);
 93         return instance;
 94     }
 95 


 96     /** Builtin types.
 97      */
 98     public final JCPrimitiveType byteType = new JCPrimitiveType(BYTE, null);
 99     public final JCPrimitiveType charType = new JCPrimitiveType(CHAR, null);
100     public final JCPrimitiveType shortType = new JCPrimitiveType(SHORT, null);
101     public final JCPrimitiveType intType = new JCPrimitiveType(INT, null);
102     public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null);
103     public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null);
104     public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null);
105     public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null);
106     public final Type botType = new BottomType();
107     public final JCVoidType voidType = new JCVoidType();
108 
109     private final Names names;
110     private final JavacMessages messages;
111     private final Completer initialCompleter;
112     private final Completer moduleCompleter;
113 
114     /** A symbol for the unnamed module.
115      */

257      *  by compiled source files.
258      */
259     private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>();
260 
261     /** A hashtable containing the encountered packages.
262      *  the table should be updated from outside to reflect packages defined
263      *  by compiled source files.
264      */
265     private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>();
266 
267     /** A hashtable giving the encountered modules.
268      */
269     private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>();
270 
271     private final Map<Types.UniqueType, VarSymbol> classFields = new HashMap<>();
272 
273     public VarSymbol getClassField(Type type, Types types) {
274         return classFields.computeIfAbsent(
275             new UniqueType(type, types), k -> {
276                 Type arg = null;
277                 if (type.getTag() == ARRAY || type.getTag() == CLASS)
278                     arg = types.erasure(type);









279                 else if (type.isPrimitiveOrVoid())
280                     arg = types.boxedClass(type).type;
281                 else
282                     throw new AssertionError(type);
283 
284                 Type t = new ClassType(
285                     classType.getEnclosingType(), List.of(arg), classType.tsym);
286                 return new VarSymbol(
287                     STATIC | PUBLIC | FINAL, names._class, t, type.tsym);
288             });
289     }
290 
291     public void initType(Type type, ClassSymbol c) {
292         type.tsym = c;
293         typeOfTag[type.getTag().ordinal()] = type;
294     }
295 
296     public void initType(Type type, String name) {
297         initType(
298             type,

635         ClassType arrayClassType = (ClassType)arrayClass.type;
636         arrayClassType.supertype_field = objectType;
637         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
638         arrayClass.members_field = WriteableScope.create(arrayClass);
639         lengthVar = new VarSymbol(
640             PUBLIC | FINAL,
641             names.length,
642             intType,
643             arrayClass);
644         arrayClass.members().enter(lengthVar);
645         arrayCloneMethod = new MethodSymbol(
646             PUBLIC,
647             names.clone,
648             new MethodType(List.nil(), objectType,
649                            List.nil(), methodClass),
650             arrayClass);
651         arrayClass.members().enter(arrayCloneMethod);
652 
653         if (java_base != noModule)
654             java_base.completer = moduleCompleter::complete; //bootstrap issues
655 

656     }
657 
658     /** Define a new class given its name and owner.
659      */
660     public ClassSymbol defineClass(Name name, Symbol owner) {
661         ClassSymbol c = new ClassSymbol(0, name, owner);
662         c.completer = initialCompleter;
663         return c;
664     }
665 
666     /** Create a new toplevel or member class symbol with given name
667      *  and owner and enter in `classes' unless already there.
668      */
669     public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) {
670         Assert.checkNonNull(msym);
671         Name flatname = TypeSymbol.formFlatName(name, owner);
672         ClassSymbol c = getClass(msym, flatname);
673         if (c == null) {
674             c = defineClass(name, owner);
675             doEnterClass(msym, c);

 35 import javax.lang.model.element.ElementVisitor;
 36 
 37 import com.sun.tools.javac.code.Scope.WriteableScope;
 38 import com.sun.tools.javac.code.Source.Feature;
 39 import com.sun.tools.javac.code.Symbol.ClassSymbol;
 40 import com.sun.tools.javac.code.Symbol.Completer;
 41 import com.sun.tools.javac.code.Symbol.CompletionFailure;
 42 import com.sun.tools.javac.code.Symbol.MethodSymbol;
 43 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
 44 import com.sun.tools.javac.code.Symbol.PackageSymbol;
 45 import com.sun.tools.javac.code.Symbol.RootPackageSymbol;
 46 import com.sun.tools.javac.code.Symbol.TypeSymbol;
 47 import com.sun.tools.javac.code.Symbol.VarSymbol;
 48 import com.sun.tools.javac.code.Type.BottomType;
 49 import com.sun.tools.javac.code.Type.ClassType;
 50 import com.sun.tools.javac.code.Type.ErrorType;
 51 import com.sun.tools.javac.code.Type.JCPrimitiveType;
 52 import com.sun.tools.javac.code.Type.JCVoidType;
 53 import com.sun.tools.javac.code.Type.MethodType;
 54 import com.sun.tools.javac.code.Type.UnknownType;
 55 import com.sun.tools.javac.code.Type.WildcardType;
 56 import com.sun.tools.javac.code.Types.UniqueType;
 57 import com.sun.tools.javac.comp.Modules;
 58 import com.sun.tools.javac.jvm.Target;
 59 import com.sun.tools.javac.util.Assert;
 60 import com.sun.tools.javac.util.Context;
 61 import com.sun.tools.javac.util.Convert;
 62 import com.sun.tools.javac.util.DefinedBy;
 63 import com.sun.tools.javac.util.DefinedBy.Api;
 64 import com.sun.tools.javac.util.Iterators;
 65 import com.sun.tools.javac.util.JavacMessages;
 66 import com.sun.tools.javac.util.List;
 67 import com.sun.tools.javac.util.Name;
 68 import com.sun.tools.javac.util.Names;
 69 import com.sun.tools.javac.util.Options;
 70 
 71 import static com.sun.tools.javac.code.Flags.*;
 72 import static com.sun.tools.javac.code.Kinds.Kind.*;
 73 import static com.sun.tools.javac.code.TypeTag.*;
 74 
 75 /** A class that defines all predefined constants and operators
 76  *  as well as special classes such as java.lang.Object, which need
 77  *  to be known to the compiler. All symbols are held in instance
 78  *  fields. This makes it possible to work in multiple concurrent
 79  *  projects, which might use different class files for library classes.
 80  *
 81  *  <p><b>This is NOT part of any supported API.
 82  *  If you write code that depends on this, you do so at your own risk.
 83  *  This code and its internal interfaces are subject to change or
 84  *  deletion without notice.</b>
 85  */
 86 public class Symtab {
 87     /** The context key for the symbol table. */
 88     protected static final Context.Key<Symtab> symtabKey = new Context.Key<>();
 89 
 90     /** Get the symbol table instance. */
 91     public static Symtab instance(Context context) {
 92         Symtab instance = context.get(symtabKey);
 93         if (instance == null)
 94             instance = new Symtab(context);
 95         return instance;
 96     }
 97 
 98     private final boolean allowPrimitiveClasses;
 99 
100     /** Builtin types.
101      */
102     public final JCPrimitiveType byteType = new JCPrimitiveType(BYTE, null);
103     public final JCPrimitiveType charType = new JCPrimitiveType(CHAR, null);
104     public final JCPrimitiveType shortType = new JCPrimitiveType(SHORT, null);
105     public final JCPrimitiveType intType = new JCPrimitiveType(INT, null);
106     public final JCPrimitiveType longType = new JCPrimitiveType(LONG, null);
107     public final JCPrimitiveType floatType = new JCPrimitiveType(FLOAT, null);
108     public final JCPrimitiveType doubleType = new JCPrimitiveType(DOUBLE, null);
109     public final JCPrimitiveType booleanType = new JCPrimitiveType(BOOLEAN, null);
110     public final Type botType = new BottomType();
111     public final JCVoidType voidType = new JCVoidType();
112 
113     private final Names names;
114     private final JavacMessages messages;
115     private final Completer initialCompleter;
116     private final Completer moduleCompleter;
117 
118     /** A symbol for the unnamed module.
119      */

261      *  by compiled source files.
262      */
263     private final Map<Name, Map<ModuleSymbol,ClassSymbol>> classes = new HashMap<>();
264 
265     /** A hashtable containing the encountered packages.
266      *  the table should be updated from outside to reflect packages defined
267      *  by compiled source files.
268      */
269     private final Map<Name, Map<ModuleSymbol,PackageSymbol>> packages = new HashMap<>();
270 
271     /** A hashtable giving the encountered modules.
272      */
273     private final Map<Name, ModuleSymbol> modules = new LinkedHashMap<>();
274 
275     private final Map<Types.UniqueType, VarSymbol> classFields = new HashMap<>();
276 
277     public VarSymbol getClassField(Type type, Types types) {
278         return classFields.computeIfAbsent(
279             new UniqueType(type, types), k -> {
280                 Type arg = null;
281                 if (type.getTag() == ARRAY || type.getTag() == CLASS) {
282                     /* Temporary treatment for primitive class: Given a primitive class V that implements
283                        I1, I2, ... In, V.class is typed to be Class<? extends Object & I1 & I2 .. & In>
284                     */
285                     if (allowPrimitiveClasses && type.isPrimitiveClass()) {
286                         List<Type> bounds = List.of(objectType).appendList(((ClassSymbol) type.tsym).getInterfaces());
287                         arg = new WildcardType(bounds.size() > 1 ? types.makeIntersectionType(bounds) : objectType, BoundKind.EXTENDS, boundClass);
288                     } else {
289                         arg = types.erasure(type);
290                     }
291                 }
292                 else if (type.isPrimitiveOrVoid())
293                     arg = types.boxedClass(type).type;
294                 else
295                     throw new AssertionError(type);
296 
297                 Type t = new ClassType(
298                     classType.getEnclosingType(), List.of(arg), classType.tsym);
299                 return new VarSymbol(
300                     STATIC | PUBLIC | FINAL, names._class, t, type.tsym);
301             });
302     }
303 
304     public void initType(Type type, ClassSymbol c) {
305         type.tsym = c;
306         typeOfTag[type.getTag().ordinal()] = type;
307     }
308 
309     public void initType(Type type, String name) {
310         initType(
311             type,

648         ClassType arrayClassType = (ClassType)arrayClass.type;
649         arrayClassType.supertype_field = objectType;
650         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
651         arrayClass.members_field = WriteableScope.create(arrayClass);
652         lengthVar = new VarSymbol(
653             PUBLIC | FINAL,
654             names.length,
655             intType,
656             arrayClass);
657         arrayClass.members().enter(lengthVar);
658         arrayCloneMethod = new MethodSymbol(
659             PUBLIC,
660             names.clone,
661             new MethodType(List.nil(), objectType,
662                            List.nil(), methodClass),
663             arrayClass);
664         arrayClass.members().enter(arrayCloneMethod);
665 
666         if (java_base != noModule)
667             java_base.completer = moduleCompleter::complete; //bootstrap issues
668         Options options = Options.instance(context);
669         allowPrimitiveClasses = Feature.PRIMITIVE_CLASSES.allowedInSource(source) && options.isSet("enablePrimitiveClasses");
670     }
671 
672     /** Define a new class given its name and owner.
673      */
674     public ClassSymbol defineClass(Name name, Symbol owner) {
675         ClassSymbol c = new ClassSymbol(0, name, owner);
676         c.completer = initialCompleter;
677         return c;
678     }
679 
680     /** Create a new toplevel or member class symbol with given name
681      *  and owner and enter in `classes' unless already there.
682      */
683     public ClassSymbol enterClass(ModuleSymbol msym, Name name, TypeSymbol owner) {
684         Assert.checkNonNull(msym);
685         Name flatname = TypeSymbol.formFlatName(name, owner);
686         ClassSymbol c = getClass(msym, flatname);
687         if (c == null) {
688             c = defineClass(name, owner);
689             doEnterClass(msym, c);
< prev index next >