< 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 

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,

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 

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

626         synthesizeBoxTypeIfMissing(doubleType);
627         synthesizeBoxTypeIfMissing(floatType);
628         synthesizeBoxTypeIfMissing(voidType);
629 
630         // Enter a synthetic class that is used to mark internal
631         // proprietary classes in ct.sym.  This class does not have a
632         // class file.
633         proprietaryType = enterSyntheticAnnotation("sun.Proprietary+Annotation");
634 
635         // Enter a synthetic class that is used to provide profile info for
636         // classes in ct.sym.  This class does not have a class file.
637         profileType = enterSyntheticAnnotation("jdk.Profile+Annotation");
638         MethodSymbol m = new MethodSymbol(PUBLIC | ABSTRACT, names.value, intType, profileType.tsym);
639         profileType.tsym.members().enter(m);
640 
641         // Enter a class for arrays.
642         // The class implements java.lang.Cloneable and java.io.Serializable.
643         // It has a final length field and a clone method.
644         ClassType arrayClassType = (ClassType)arrayClass.type;
645         arrayClassType.supertype_field = objectType;
646         arrayClassType.interfaces_field =
647                 List.of(cloneableType, serializableType);
648 
649         arrayClass.members_field = WriteableScope.create(arrayClass);
650         lengthVar = new VarSymbol(
651             PUBLIC | FINAL,
652             names.length,
653             intType,
654             arrayClass);
655         arrayClass.members().enter(lengthVar);
656         arrayCloneMethod = new MethodSymbol(
657             PUBLIC,
658             names.clone,
659             new MethodType(List.nil(), objectType,
660                            List.nil(), methodClass),
661             arrayClass);
662         arrayClass.members().enter(arrayCloneMethod);
663 
664         if (java_base != noModule)
665             java_base.completer = moduleCompleter::complete; //bootstrap issues
666 
667     }
668 
< prev index next >