< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java

Print this page

  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.ArrayList;
  29 import java.util.HashSet;
  30 import java.util.Set;
  31 import java.util.function.BiConsumer;
  32 import java.util.stream.Collectors;
  33 
  34 import javax.tools.JavaFileObject;
  35 
  36 import com.sun.tools.javac.code.*;
  37 import com.sun.tools.javac.code.Lint.LintCategory;
  38 import com.sun.tools.javac.code.Scope.ImportFilter;
  39 import com.sun.tools.javac.code.Scope.NamedImportScope;
  40 import com.sun.tools.javac.code.Scope.StarImportScope;
  41 import com.sun.tools.javac.code.Scope.WriteableScope;
  42 import com.sun.tools.javac.code.Source.Feature;
  43 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;

  44 import com.sun.tools.javac.tree.*;
  45 import com.sun.tools.javac.util.*;
  46 import com.sun.tools.javac.util.DefinedBy.Api;
  47 
  48 import com.sun.tools.javac.code.Symbol.*;
  49 import com.sun.tools.javac.code.Type.*;
  50 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  51 import com.sun.tools.javac.tree.JCTree.*;
  52 
  53 import static com.sun.tools.javac.code.Flags.*;
  54 import static com.sun.tools.javac.code.Flags.ANNOTATION;
  55 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  56 import static com.sun.tools.javac.code.Kinds.Kind.*;
  57 import static com.sun.tools.javac.code.TypeTag.CLASS;
  58 import static com.sun.tools.javac.code.TypeTag.ERROR;
  59 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  60 
  61 import static com.sun.tools.javac.code.TypeTag.*;
  62 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  63 

 673                     if (n == 1) {
 674                         TypeVar v = new TypeVar(names.fromString("T"), sym, syms.botType);
 675                         ct.typarams_field = ct.typarams_field.prepend(v);
 676                     } else {
 677                         for (int i = n; i > 0; i--) {
 678                             TypeVar v = new TypeVar(names.fromString("T" + i), sym,
 679                                                     syms.botType);
 680                             ct.typarams_field = ct.typarams_field.prepend(v);
 681                         }
 682                     }
 683                 }
 684             }
 685 
 686         protected void attribSuperTypes(Env<AttrContext> env, Env<AttrContext> baseEnv) {
 687             JCClassDecl tree = env.enclClass;
 688             ClassSymbol sym = tree.sym;
 689             ClassType ct = (ClassType)sym.type;
 690             // Determine supertype.
 691             Type supertype;
 692             JCExpression extending;

 693 
 694             if (tree.extending != null) {
 695                 extending = clearTypeParams(tree.extending);
 696                 supertype = attr.attribBase(extending, baseEnv, true, false, true);
 697                 if (supertype == syms.recordType) {
 698                     log.error(tree, Errors.InvalidSupertypeRecord(supertype.tsym));
 699                 }
 700             } else {
 701                 extending = null;
 702                 supertype = ((tree.mods.flags & Flags.ENUM) != 0)
 703                 ? attr.attribBase(extending = enumBase(tree.pos, sym), baseEnv,
 704                                   true, false, false)
 705                 : (sym.fullname == names.java_lang_Object)
 706                 ? Type.noType
 707                 : sym.isRecord()
 708                 ? attr.attribBase(extending = recordBase(tree.pos, sym), baseEnv,
 709                                   true, false, false)
 710                 : syms.objectType;
 711             }
 712             ct.supertype_field = modelMissingTypes(baseEnv, supertype, extending, false);
 713 
 714             // Determine interfaces.
 715             ListBuffer<Type> interfaces = new ListBuffer<>();
 716             ListBuffer<Type> all_interfaces = null; // lazy init
 717             List<JCExpression> interfaceTrees = tree.implementing;
 718             for (JCExpression iface : interfaceTrees) {
 719                 iface = clearTypeParams(iface);
 720                 Type it = attr.attribBase(iface, baseEnv, false, true, true);
 721                 if (it.hasTag(CLASS)) {



 722                     interfaces.append(it);
 723                     if (all_interfaces != null) all_interfaces.append(it);
 724                 } else {
 725                     if (all_interfaces == null)
 726                         all_interfaces = new ListBuffer<Type>().appendList(interfaces);
 727                     all_interfaces.append(modelMissingTypes(baseEnv, it, iface, true));
 728                 }
 729             }
 730 
 731             // Determine permits.
 732             ListBuffer<Symbol> permittedSubtypeSymbols = new ListBuffer<>();
 733             List<JCExpression> permittedTrees = tree.permitting;
 734             for (JCExpression permitted : permittedTrees) {
 735                 Type pt = attr.attribBase(permitted, baseEnv, false, false, false);
 736                 permittedSubtypeSymbols.append(pt.tsym);
 737             }
 738 
 739             if ((sym.flags_field & ANNOTATION) != 0) {
 740                 ct.interfaces_field = List.of(syms.annotationType);
 741                 ct.all_interfaces_field = ct.interfaces_field;
 742             }  else {
 743                 ct.interfaces_field = interfaces.toList();
 744                 ct.all_interfaces_field = (all_interfaces == null)
 745                         ? ct.interfaces_field : all_interfaces.toList();
 746             }
 747 
 748             /* it could be that there are already some symbols in the permitted list, for the case
 749              * where there are subtypes in the same compilation unit but the permits list is empty
 750              * so don't overwrite the permitted list if it is not empty
 751              */
 752             if (!permittedSubtypeSymbols.isEmpty()) {
 753                 sym.permitted = permittedSubtypeSymbols.toList();
 754             }
 755             sym.isPermittedExplicit = !permittedSubtypeSymbols.isEmpty();
 756         }
 757             //where:
 758             protected JCExpression clearTypeParams(JCExpression superType) {
 759                 return superType;
 760             }
 761     }
 762 
 763     private final class HierarchyPhase extends AbstractHeaderPhase implements Completer {
 764 
 765         public HierarchyPhase() {
 766             super(CompletionCause.HIERARCHY_PHASE, new PermitsPhase());
 767         }

  24  */
  25 
  26 package com.sun.tools.javac.comp;
  27 
  28 import java.util.ArrayList;
  29 import java.util.HashSet;
  30 import java.util.Set;
  31 import java.util.function.BiConsumer;
  32 import java.util.stream.Collectors;
  33 
  34 import javax.tools.JavaFileObject;
  35 
  36 import com.sun.tools.javac.code.*;
  37 import com.sun.tools.javac.code.Lint.LintCategory;
  38 import com.sun.tools.javac.code.Scope.ImportFilter;
  39 import com.sun.tools.javac.code.Scope.NamedImportScope;
  40 import com.sun.tools.javac.code.Scope.StarImportScope;
  41 import com.sun.tools.javac.code.Scope.WriteableScope;
  42 import com.sun.tools.javac.code.Source.Feature;
  43 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  44 import com.sun.tools.javac.jvm.Target;
  45 import com.sun.tools.javac.tree.*;
  46 import com.sun.tools.javac.util.*;
  47 import com.sun.tools.javac.util.DefinedBy.Api;
  48 
  49 import com.sun.tools.javac.code.Symbol.*;
  50 import com.sun.tools.javac.code.Type.*;
  51 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  52 import com.sun.tools.javac.tree.JCTree.*;
  53 
  54 import static com.sun.tools.javac.code.Flags.*;
  55 import static com.sun.tools.javac.code.Flags.ANNOTATION;
  56 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  57 import static com.sun.tools.javac.code.Kinds.Kind.*;
  58 import static com.sun.tools.javac.code.TypeTag.CLASS;
  59 import static com.sun.tools.javac.code.TypeTag.ERROR;
  60 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  61 
  62 import static com.sun.tools.javac.code.TypeTag.*;
  63 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  64 

 674                     if (n == 1) {
 675                         TypeVar v = new TypeVar(names.fromString("T"), sym, syms.botType);
 676                         ct.typarams_field = ct.typarams_field.prepend(v);
 677                     } else {
 678                         for (int i = n; i > 0; i--) {
 679                             TypeVar v = new TypeVar(names.fromString("T" + i), sym,
 680                                                     syms.botType);
 681                             ct.typarams_field = ct.typarams_field.prepend(v);
 682                         }
 683                     }
 684                 }
 685             }
 686 
 687         protected void attribSuperTypes(Env<AttrContext> env, Env<AttrContext> baseEnv) {
 688             JCClassDecl tree = env.enclClass;
 689             ClassSymbol sym = tree.sym;
 690             ClassType ct = (ClassType)sym.type;
 691             // Determine supertype.
 692             Type supertype;
 693             JCExpression extending;
 694             final boolean isPrimitiveClass = (tree.mods.flags & Flags.PRIMITIVE_CLASS) != 0;
 695 
 696             if (tree.extending != null) {
 697                 extending = clearTypeParams(tree.extending);
 698                 supertype = attr.attribBase(extending, baseEnv, true, false, true);
 699                 if (supertype == syms.recordType) {
 700                     log.error(tree, Errors.InvalidSupertypeRecord(supertype.tsym));
 701                 }
 702             } else {
 703                 extending = null;
 704                 supertype = ((tree.mods.flags & Flags.ENUM) != 0)
 705                 ? attr.attribBase(extending = enumBase(tree.pos, sym), baseEnv,
 706                                   true, false, false)
 707                 : (sym.fullname == names.java_lang_Object)
 708                 ? Type.noType
 709                 : sym.isRecord()
 710                 ? attr.attribBase(extending = recordBase(tree.pos, sym), baseEnv,
 711                                   true, false, false)
 712                 : syms.objectType;
 713             }
 714             ct.supertype_field = modelMissingTypes(baseEnv, supertype, extending, false);
 715 
 716             // Determine interfaces.
 717             ListBuffer<Type> interfaces = new ListBuffer<>();
 718             ListBuffer<Type> all_interfaces = null; // lazy init
 719             List<JCExpression> interfaceTrees = tree.implementing;
 720             for (JCExpression iface : interfaceTrees) {
 721                 iface = clearTypeParams(iface);
 722                 Type it = attr.attribBase(iface, baseEnv, false, true, true);
 723                 if (it.hasTag(CLASS)) {
 724                     if (isPrimitiveClass && it.tsym == syms.cloneableType.tsym) {
 725                         log.error(tree, Errors.PrimitiveClassMustNotImplementCloneable(ct));
 726                     }
 727                     interfaces.append(it);
 728                     if (all_interfaces != null) all_interfaces.append(it);
 729                 } else {
 730                     if (all_interfaces == null)
 731                         all_interfaces = new ListBuffer<Type>().appendList(interfaces);
 732                     all_interfaces.append(modelMissingTypes(baseEnv, it, iface, true));
 733                 }
 734             }
 735 
 736             // Determine permits.
 737             ListBuffer<Symbol> permittedSubtypeSymbols = new ListBuffer<>();
 738             List<JCExpression> permittedTrees = tree.permitting;
 739             for (JCExpression permitted : permittedTrees) {
 740                 Type pt = attr.attribBase(permitted, baseEnv, false, false, false);
 741                 permittedSubtypeSymbols.append(pt.tsym);
 742             }
 743 
 744             if ((sym.flags_field & ANNOTATION) != 0) {
 745                 ct.interfaces_field = List.of(syms.annotationType);
 746                 ct.all_interfaces_field = ct.interfaces_field;
 747             }  else {
 748                 ct.interfaces_field = interfaces.toList();
 749                 ct.all_interfaces_field = (all_interfaces == null)
 750                         ? ct.interfaces_field : all_interfaces.toList();
 751             }

 752             /* it could be that there are already some symbols in the permitted list, for the case
 753              * where there are subtypes in the same compilation unit but the permits list is empty
 754              * so don't overwrite the permitted list if it is not empty
 755              */
 756             if (!permittedSubtypeSymbols.isEmpty()) {
 757                 sym.permitted = permittedSubtypeSymbols.toList();
 758             }
 759             sym.isPermittedExplicit = !permittedSubtypeSymbols.isEmpty();
 760         }
 761             //where:
 762             protected JCExpression clearTypeParams(JCExpression superType) {
 763                 return superType;
 764             }
 765     }
 766 
 767     private final class HierarchyPhase extends AbstractHeaderPhase implements Completer {
 768 
 769         public HierarchyPhase() {
 770             super(CompletionCause.HIERARCHY_PHASE, new PermitsPhase());
 771         }
< prev index next >