< 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     // For serialization lint checking
228     public final Type objectStreamFieldType;
229     public final Type objectInputStreamType;
230     public final Type objectOutputStreamType;
231     public final Type ioExceptionType;
232     public final Type objectStreamExceptionType;
233     public final Type externalizableType;
234 
235     /** The symbol representing the length field of an array.
236      */
237     public final VarSymbol lengthVar;
238 
239     /** The symbol representing the final finalize method on enums */
240     public final MethodSymbol enumFinalFinalize;
241 
242     /** The symbol representing the close method on TWR AutoCloseable type */

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









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

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


598         switchBootstrapsType = enterClass("java.lang.runtime.SwitchBootstraps");
599         valueBasedType = enterClass("jdk.internal.ValueBased");
600         valueBasedInternalType = enterSyntheticAnnotation("jdk.internal.ValueBased+Annotation");
601         // For serialization lint checking
602         objectStreamFieldType = enterClass("java.io.ObjectStreamField");
603         objectInputStreamType = enterClass("java.io.ObjectInputStream");
604         objectOutputStreamType = enterClass("java.io.ObjectOutputStream");
605         ioExceptionType = enterClass("java.io.IOException");
606         objectStreamExceptionType = enterClass("java.io.ObjectStreamException");
607         externalizableType = enterClass("java.io.Externalizable");
608 
609         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
610         synthesizeEmptyInterfaceIfMissing(cloneableType);
611         synthesizeEmptyInterfaceIfMissing(serializableType);
612         synthesizeEmptyInterfaceIfMissing(lambdaMetafactory);
613         synthesizeEmptyInterfaceIfMissing(serializedLambdaType);
614         synthesizeEmptyInterfaceIfMissing(stringConcatFactory);


615         synthesizeBoxTypeIfMissing(doubleType);
616         synthesizeBoxTypeIfMissing(floatType);
617         synthesizeBoxTypeIfMissing(voidType);
618 
619         // Enter a synthetic class that is used to mark internal
620         // proprietary classes in ct.sym.  This class does not have a
621         // class file.
622         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
623 
624         // Enter a synthetic class that is used to provide profile info for
625         // classes in ct.sym.  This class does not have a class file.
626         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
627         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
628         profileType.tsym.members().enter(m);
629 
630         // Enter a class for arrays.
631         // The class implements java.lang.Cloneable and java.io.Serializable.
632         // It has a final length field and a clone method.
633         ClassType arrayClassType = (ClassType)arrayClass.type;
634         arrayClassType.supertype_field = objectType;
635         arrayClassType.interfaces_field = List.of(cloneableType, serializableType);


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

 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 valueObjectType;
227     public final Type switchBootstrapsType;
228     public final Type valueBasedType;
229     public final Type valueBasedInternalType;
230 
231     // For serialization lint checking
232     public final Type objectStreamFieldType;
233     public final Type objectInputStreamType;
234     public final Type objectOutputStreamType;
235     public final Type ioExceptionType;
236     public final Type objectStreamExceptionType;
237     public final Type externalizableType;
238 
239     /** The symbol representing the length field of an array.
240      */
241     public final VarSymbol lengthVar;
242 
243     /** The symbol representing the final finalize method on enums */
244     public final MethodSymbol enumFinalFinalize;
245 
246     /** The symbol representing the close method on TWR AutoCloseable type */

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

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