< 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 

203     public final Type retentionType;
204     public final Type deprecatedType;
205     public final Type suppressWarningsType;
206     public final Type supplierType;
207     public final Type inheritedType;
208     public final Type profileType;
209     public final Type proprietaryType;
210     public final Type systemType;
211     public final Type autoCloseableType;
212     public final Type trustMeType;
213     public final Type lambdaMetafactory;
214     public final Type stringConcatFactory;
215     public final Type repeatableType;
216     public final Type documentedType;
217     public final Type elementTypeType;
218     public final Type functionalInterfaceType;
219     public final Type previewFeatureType;
220     public final Type previewFeatureInternalType;
221     public final Type typeDescriptorType;
222     public final Type recordType;


223     public final Type switchBootstrapsType;
224     public final Type valueBasedType;
225     public final Type valueBasedInternalType;
226 
227     /** The symbol representing the length field of an array.
228      */
229     public final VarSymbol lengthVar;
230 
231     /** The symbol representing the final finalize method on enums */
232     public final MethodSymbol enumFinalFinalize;
233 
234     /** The symbol representing the close method on TWR AutoCloseable type */
235     public final MethodSymbol autoCloseableClose;
236 
237     /** The predefined type that belongs to a tag.
238      */
239     public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()];
240 
241     /** The name of the class that belongs to a basic type tag.
242      */

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









270                 else if (type.isPrimitiveOrVoid())
271                     arg = types.boxedClass(type).type;
272                 else
273                     throw new AssertionError(type);
274 
275                 Type t = new ClassType(
276                     classType.getEnclosingType(), List.of(arg), classType.tsym);
277                 return new VarSymbol(
278                     STATIC | PUBLIC | FINAL, names._class, t, type.tsym);
279             });
280     }
281 
282     public void initType(Type type, ClassSymbol c) {
283         type.tsym = c;
284         typeOfTag[type.getTag().ordinal()] = type;
285     }
286 
287     public void initType(Type type, String name) {
288         initType(
289             type,

570         inheritedType = enterClass("java.lang.annotation.Inherited");
571         repeatableType = enterClass("java.lang.annotation.Repeatable");
572         documentedType = enterClass("java.lang.annotation.Documented");
573         elementTypeType = enterClass("java.lang.annotation.ElementType");
574         systemType = enterClass("java.lang.System");
575         autoCloseableType = enterClass("java.lang.AutoCloseable");
576         autoCloseableClose = new MethodSymbol(PUBLIC,
577                              names.close,
578                              new MethodType(List.nil(), voidType,
579                                             List.of(exceptionType), methodClass),
580                              autoCloseableType.tsym);
581         trustMeType = enterClass("java.lang.SafeVarargs");
582         nativeHeaderType = enterClass("java.lang.annotation.Native");
583         lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
584         stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory");
585         functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
586         previewFeatureType = enterClass("jdk.internal.javac.PreviewFeature");
587         previewFeatureInternalType = enterSyntheticAnnotation("jdk.internal.PreviewFeature+Annotation");
588         typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
589         recordType = enterClass("java.lang.Record");


590         switchBootstrapsType = enterClass("java.lang.runtime.SwitchBootstraps");
591         valueBasedType = enterClass("jdk.internal.ValueBased");
592         valueBasedInternalType = enterSyntheticAnnotation("jdk.internal.ValueBased+Annotation");
593 
594         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
595         synthesizeEmptyInterfaceIfMissing(cloneableType);
596         synthesizeEmptyInterfaceIfMissing(serializableType);
597         synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
598         synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
599         synthesizeEmptyInterfaceIfMissing(stringConcatFactory);


600         synthesizeBoxTypeIfMissing(doubleType);
601         synthesizeBoxTypeIfMissing(floatType);
602         synthesizeBoxTypeIfMissing(voidType);
603 
604         // Enter a synthetic class that is used to mark internal
605         // proprietary classes in ct.sym.  This class does not have a
606         // class file.
607         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
608 
609         // Enter a synthetic class that is used to provide profile info for
610         // classes in ct.sym.  This class does not have a class file.
611         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
612         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
613         profileType.tsym.members().enter(m);
614 
615         // Enter a class for arrays.
616         // The class implements java.lang.Cloneable and java.io.Serializable.
617         // It has a final length field and a clone method.
618         ClassType arrayClassType = (ClassType)arrayClass.type;
619         arrayClassType.supertype_field = objectType;
620         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);


621         arrayClass.members_field = WriteableScope.create(arrayClass);
622         lengthVar = new VarSymbol(
623             PUBLIC | FINAL,
624             names.length,
625             intType,
626             arrayClass);
627         arrayClass.members().enter(lengthVar);
628         arrayCloneMethod = new MethodSymbol(
629             PUBLIC,
630             names.clone,
631             new MethodType(List.nil(), objectType,
632                            List.nil(), methodClass),
633             arrayClass);
634         arrayClass.members().enter(arrayCloneMethod);
635 
636         if (java_base != noModule)
637             java_base.completer = moduleCompleter::complete; //bootstrap issues
638 
639     }
640 

 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 

205     public final Type retentionType;
206     public final Type deprecatedType;
207     public final Type suppressWarningsType;
208     public final Type supplierType;
209     public final Type inheritedType;
210     public final Type profileType;
211     public final Type proprietaryType;
212     public final Type systemType;
213     public final Type autoCloseableType;
214     public final Type trustMeType;
215     public final Type lambdaMetafactory;
216     public final Type stringConcatFactory;
217     public final Type repeatableType;
218     public final Type documentedType;
219     public final Type elementTypeType;
220     public final Type functionalInterfaceType;
221     public final Type previewFeatureType;
222     public final Type previewFeatureInternalType;
223     public final Type typeDescriptorType;
224     public final Type recordType;
225     public final Type identityObjectType;
226     public final Type primitiveObjectType;
227     public final Type switchBootstrapsType;
228     public final Type valueBasedType;
229     public final Type valueBasedInternalType;
230 
231     /** The symbol representing the length field of an array.
232      */
233     public final VarSymbol lengthVar;
234 
235     /** The symbol representing the final finalize method on enums */
236     public final MethodSymbol enumFinalFinalize;
237 
238     /** The symbol representing the close method on TWR AutoCloseable type */
239     public final MethodSymbol autoCloseableClose;
240 
241     /** The predefined type that belongs to a tag.
242      */
243     public final Type[] typeOfTag = new Type[TypeTag.getTypeTagCount()];
244 
245     /** The name of the class that belongs to a basic type tag.
246      */

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

583         inheritedType = enterClass("java.lang.annotation.Inherited");
584         repeatableType = enterClass("java.lang.annotation.Repeatable");
585         documentedType = enterClass("java.lang.annotation.Documented");
586         elementTypeType = enterClass("java.lang.annotation.ElementType");
587         systemType = enterClass("java.lang.System");
588         autoCloseableType = enterClass("java.lang.AutoCloseable");
589         autoCloseableClose = new MethodSymbol(PUBLIC,
590                              names.close,
591                              new MethodType(List.nil(), voidType,
592                                             List.of(exceptionType), methodClass),
593                              autoCloseableType.tsym);
594         trustMeType = enterClass("java.lang.SafeVarargs");
595         nativeHeaderType = enterClass("java.lang.annotation.Native");
596         lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
597         stringConcatFactory = enterClass("java.lang.invoke.StringConcatFactory");
598         functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
599         previewFeatureType = enterClass("jdk.internal.javac.PreviewFeature");
600         previewFeatureInternalType = enterSyntheticAnnotation("jdk.internal.PreviewFeature+Annotation");
601         typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
602         recordType = enterClass("java.lang.Record");
603         identityObjectType = enterClass("java.lang.IdentityObject");
604         primitiveObjectType = enterClass("java.lang.PrimitiveObject");
605         switchBootstrapsType = enterClass("java.lang.runtime.SwitchBootstraps");
606         valueBasedType = enterClass("jdk.internal.ValueBased");
607         valueBasedInternalType = enterSyntheticAnnotation("jdk.internal.ValueBased+Annotation");
608 
609         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
610         synthesizeEmptyInterfaceIfMissing(cloneableType);
611         synthesizeEmptyInterfaceIfMissing(serializableType);
612         synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
613         synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
614         synthesizeEmptyInterfaceIfMissing(stringConcatFactory);
615         synthesizeEmptyInterfaceIfMissing(identityObjectType);
616         synthesizeEmptyInterfaceIfMissing(primitiveObjectType);
617         synthesizeBoxTypeIfMissing(doubleType);
618         synthesizeBoxTypeIfMissing(floatType);
619         synthesizeBoxTypeIfMissing(voidType);
620 
621         // Enter a synthetic class that is used to mark internal
622         // proprietary classes in ct.sym.  This class does not have a
623         // class file.
624         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
625 
626         // Enter a synthetic class that is used to provide profile info for
627         // classes in ct.sym.  This class does not have a class file.
628         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
629         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
630         profileType.tsym.members().enter(m);
631 
632         // Enter a class for arrays.
633         // The class implements java.lang.Cloneable and java.io.Serializable.
634         // It has a final length field and a clone method.
635         ClassType arrayClassType = (ClassType)arrayClass.type;
636         arrayClassType.supertype_field = objectType;
637         arrayClassType.interfaces_field =
638                 List.of(cloneableType, serializableType, identityObjectType);
639 
640         arrayClass.members_field = WriteableScope.create(arrayClass);
641         lengthVar = new VarSymbol(
642             PUBLIC | FINAL,
643             names.length,
644             intType,
645             arrayClass);
646         arrayClass.members().enter(lengthVar);
647         arrayCloneMethod = new MethodSymbol(
648             PUBLIC,
649             names.clone,
650             new MethodType(List.nil(), objectType,
651                            List.nil(), methodClass),
652             arrayClass);
653         arrayClass.members().enter(arrayCloneMethod);
654 
655         if (java_base != noModule)
656             java_base.completer = moduleCompleter::complete; //bootstrap issues
657 
658     }
659 
< prev index next >