langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
changeset 29842 826ac2519523
parent 29776 984a79b71cfe
child 29959 61bfc66866da
equal deleted inserted replaced
29782:85d4f5471a74 29842:826ac2519523
    38 import com.sun.tools.javac.code.*;
    38 import com.sun.tools.javac.code.*;
    39 import com.sun.tools.javac.code.Lint.LintCategory;
    39 import com.sun.tools.javac.code.Lint.LintCategory;
    40 import com.sun.tools.javac.code.Scope.WriteableScope;
    40 import com.sun.tools.javac.code.Scope.WriteableScope;
    41 import com.sun.tools.javac.code.Symbol.*;
    41 import com.sun.tools.javac.code.Symbol.*;
    42 import com.sun.tools.javac.code.Type.*;
    42 import com.sun.tools.javac.code.Type.*;
       
    43 import com.sun.tools.javac.code.TypeMetadata.Annotations;
    43 import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
    44 import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
    44 import com.sun.tools.javac.comp.Check.CheckContext;
    45 import com.sun.tools.javac.comp.Check.CheckContext;
    45 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
    46 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
    46 import com.sun.tools.javac.comp.Infer.InferenceContext;
    47 import com.sun.tools.javac.comp.Infer.InferenceContext;
    47 import com.sun.tools.javac.comp.Infer.FreeTypeListener;
    48 import com.sun.tools.javac.comp.Infer.FreeTypeListener;
   101     final ConstFold cfolder;
   102     final ConstFold cfolder;
   102     final Enter enter;
   103     final Enter enter;
   103     final Target target;
   104     final Target target;
   104     final Types types;
   105     final Types types;
   105     final JCDiagnostic.Factory diags;
   106     final JCDiagnostic.Factory diags;
   106     final Annotate annotate;
       
   107     final TypeAnnotations typeAnnotations;
   107     final TypeAnnotations typeAnnotations;
   108     final DeferredLintHandler deferredLintHandler;
   108     final DeferredLintHandler deferredLintHandler;
   109     final TypeEnvs typeEnvs;
   109     final TypeEnvs typeEnvs;
   110     final Dependencies dependencies;
   110     final Dependencies dependencies;
       
   111     final Annotate annotate;
   111 
   112 
   112     public static Attr instance(Context context) {
   113     public static Attr instance(Context context) {
   113         Attr instance = context.get(attrKey);
   114         Attr instance = context.get(attrKey);
   114         if (instance == null)
   115         if (instance == null)
   115             instance = new Attr(context);
   116             instance = new Attr(context);
   995                                 env.enclClass.sym);
   996                                 env.enclClass.sym);
   996                     }
   997                     }
   997                 }
   998                 }
   998 
   999 
   999                 // Attribute all type annotations in the body
  1000                 // Attribute all type annotations in the body
  1000                 annotate.annotateTypeLater(tree.body, localEnv, m, null);
  1001                 annotate.queueScanTreeAndTypeAnnotate(tree.body, localEnv, m, null);
  1001                 annotate.flush();
  1002                 annotate.flush();
  1002 
  1003 
  1003                 // Attribute method body.
  1004                 // Attribute method body.
  1004                 attribStat(tree.body, localEnv);
  1005                 attribStat(tree.body, localEnv);
  1005             }
  1006             }
  1018             if (tree.sym != null) {
  1019             if (tree.sym != null) {
  1019                 // parameters have already been entered
  1020                 // parameters have already been entered
  1020                 env.info.scope.enter(tree.sym);
  1021                 env.info.scope.enter(tree.sym);
  1021             } else {
  1022             } else {
  1022                 try {
  1023                 try {
  1023                     annotate.enterStart();
  1024                     annotate.blockAnnotations();
  1024                     memberEnter.memberEnter(tree, env);
  1025                     memberEnter.memberEnter(tree, env);
  1025                 } finally {
  1026                 } finally {
  1026                     annotate.enterDone();
  1027                     annotate.unblockAnnotations();
  1027                 }
  1028                 }
  1028             }
  1029             }
  1029         } else {
  1030         } else {
  1030             if (tree.init != null) {
  1031             if (tree.init != null) {
  1031                 // Field initializer expression need to be entered.
  1032                 // Field initializer expression need to be entered.
  1032                 annotate.annotateTypeLater(tree.init, env, tree.sym, tree.pos());
  1033                 annotate.queueScanTreeAndTypeAnnotate(tree.init, env, tree.sym, tree.pos());
  1033                 annotate.flush();
  1034                 annotate.flush();
  1034             }
  1035             }
  1035         }
  1036         }
  1036 
  1037 
  1037         VarSymbol v = tree.sym;
  1038         VarSymbol v = tree.sym;
  1088             final Env<AttrContext> localEnv =
  1089             final Env<AttrContext> localEnv =
  1089                 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
  1090                 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
  1090 
  1091 
  1091             if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
  1092             if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
  1092             // Attribute all type annotations in the block
  1093             // Attribute all type annotations in the block
  1093             annotate.annotateTypeLater(tree, localEnv, localEnv.info.scope.owner, null);
  1094             annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner, null);
  1094             annotate.flush();
  1095             annotate.flush();
  1095             attribStats(tree.stats, localEnv);
  1096             attribStats(tree.stats, localEnv);
  1096 
  1097 
  1097             {
  1098             {
  1098                 // Store init and clinit type annotations with the ClassSymbol
  1099                 // Store init and clinit type annotations with the ClassSymbol
  1951             clazz = clazzid1;
  1952             clazz = clazzid1;
  1952         }
  1953         }
  1953 
  1954 
  1954         // Attribute clazz expression and store
  1955         // Attribute clazz expression and store
  1955         // symbol + type back into the attributed tree.
  1956         // symbol + type back into the attributed tree.
  1956         Type clazztype = TreeInfo.isEnumInit(env.tree) ?
  1957         Type clazztype;
  1957             attribIdentAsEnumType(env, (JCIdent)clazz) :
  1958 
  1958             attribType(clazz, env);
  1959         try {
       
  1960             env.info.isNewClass = true;
       
  1961             clazztype = TreeInfo.isEnumInit(env.tree) ?
       
  1962                 attribIdentAsEnumType(env, (JCIdent)clazz) :
       
  1963                 attribType(clazz, env);
       
  1964         } finally {
       
  1965             env.info.isNewClass = false;
       
  1966         }
  1959 
  1967 
  1960         clazztype = chk.checkDiamond(tree, clazztype);
  1968         clazztype = chk.checkDiamond(tree, clazztype);
  1961         chk.validate(clazz, localEnv);
  1969         chk.validate(clazz, localEnv);
  1962         if (tree.encl != null) {
  1970         if (tree.encl != null) {
  1963             // We have to work in this case to store
  1971             // We have to work in this case to store
  4000 
  4008 
  4001     public void visitTypeParameter(JCTypeParameter tree) {
  4009     public void visitTypeParameter(JCTypeParameter tree) {
  4002         TypeVar typeVar = (TypeVar) tree.type;
  4010         TypeVar typeVar = (TypeVar) tree.type;
  4003 
  4011 
  4004         if (tree.annotations != null && tree.annotations.nonEmpty()) {
  4012         if (tree.annotations != null && tree.annotations.nonEmpty()) {
  4005             annotateType(tree, tree.annotations);
  4013             annotate.annotateTypeParameterSecondStage(tree, tree.annotations);
  4006         }
  4014         }
  4007 
  4015 
  4008         if (!typeVar.bound.isErroneous()) {
  4016         if (!typeVar.bound.isErroneous()) {
  4009             //fixup type-parameter bound computed in 'attribTypeVariables'
  4017             //fixup type-parameter bound computed in 'attribTypeVariables'
  4010             typeVar.bound = checkIntersection(tree, tree.bounds);
  4018             typeVar.bound = checkIntersection(tree, tree.bounds);
  4090                                               syms.boundClass),
  4098                                               syms.boundClass),
  4091                 KindSelector.TYP, resultInfo);
  4099                 KindSelector.TYP, resultInfo);
  4092     }
  4100     }
  4093 
  4101 
  4094     public void visitAnnotation(JCAnnotation tree) {
  4102     public void visitAnnotation(JCAnnotation tree) {
  4095         Assert.error("should be handled in Annotate");
  4103         Assert.error("should be handled in annotate");
  4096     }
  4104     }
  4097 
  4105 
  4098     public void visitAnnotatedType(JCAnnotatedType tree) {
  4106     public void visitAnnotatedType(JCAnnotatedType tree) {
  4099         Type underlyingType = attribType(tree.getUnderlyingType(), env);
  4107         attribAnnotationTypes(tree.annotations, env);
  4100         this.attribAnnotationTypes(tree.annotations, env);
  4108         Type underlyingType = attribType(tree.underlyingType, env);
  4101         annotateType(tree, tree.annotations);
  4109         Type annotatedType = underlyingType.annotatedType(Annotations.TO_BE_SET);
  4102         result = tree.type = underlyingType;
  4110 
  4103     }
  4111         if (!env.info.isNewClass)
  4104 
  4112             annotate.annotateTypeSecondStage(tree, tree.annotations, annotatedType);
  4105     /**
  4113         result = tree.type = annotatedType;
  4106      * Apply the annotations to the particular type.
       
  4107      */
       
  4108     public void annotateType(final JCTree tree, final List<JCAnnotation> annotations) {
       
  4109         annotate.typeAnnotation(new Annotate.Worker() {
       
  4110             @Override
       
  4111             public String toString() {
       
  4112                 return "annotate " + annotations + " onto " + tree;
       
  4113             }
       
  4114             @Override
       
  4115             public void run() {
       
  4116                 List<Attribute.TypeCompound> compounds = fromAnnotations(annotations);
       
  4117                 Assert.check(annotations.size() == compounds.size());
       
  4118                 tree.type = tree.type.annotatedType(compounds);
       
  4119                 }
       
  4120         });
       
  4121     }
       
  4122 
       
  4123     private static List<Attribute.TypeCompound> fromAnnotations(List<JCAnnotation> annotations) {
       
  4124         if (annotations.isEmpty()) {
       
  4125             return List.nil();
       
  4126         }
       
  4127 
       
  4128         ListBuffer<Attribute.TypeCompound> buf = new ListBuffer<>();
       
  4129         for (JCAnnotation anno : annotations) {
       
  4130             Assert.checkNonNull(anno.attribute);
       
  4131             buf.append((Attribute.TypeCompound) anno.attribute);
       
  4132         }
       
  4133         return buf.toList();
       
  4134     }
  4114     }
  4135 
  4115 
  4136     public void visitErroneous(JCErroneous tree) {
  4116     public void visitErroneous(JCErroneous tree) {
  4137         if (tree.errs != null)
  4117         if (tree.errs != null)
  4138             for (JCTree err : tree.errs)
  4118             for (JCTree err : tree.errs)
  4296                           "cant.extend.intf.annotation");
  4276                           "cant.extend.intf.annotation");
  4297             if (tree.typarams.nonEmpty())
  4277             if (tree.typarams.nonEmpty())
  4298                 log.error(tree.typarams.head.pos(),
  4278                 log.error(tree.typarams.head.pos(),
  4299                           "intf.annotation.cant.have.type.params");
  4279                           "intf.annotation.cant.have.type.params");
  4300 
  4280 
  4301             // If this annotation has a @Repeatable, validate
  4281             // If this annotation type has a @Repeatable, validate
  4302             Attribute.Compound repeatable = c.attribute(syms.repeatableType.tsym);
  4282             Attribute.Compound repeatable = c.getAnnotationTypeMetadata().getRepeatable();
       
  4283             // If this annotation type has a @Repeatable, validate
  4303             if (repeatable != null) {
  4284             if (repeatable != null) {
  4304                 // get diagnostic position for error reporting
  4285                 // get diagnostic position for error reporting
  4305                 DiagnosticPosition cbPos = getDiagnosticPosition(tree, repeatable.type);
  4286                 DiagnosticPosition cbPos = getDiagnosticPosition(tree, repeatable.type);
  4306                 Assert.checkNonNull(cbPos);
  4287                 Assert.checkNonNull(cbPos);
  4307 
  4288 
  4673             // Note that a tree type might be an AnnotatedType with
  4654             // Note that a tree type might be an AnnotatedType with
  4674             // empty annotations, if only declaration annotations were given.
  4655             // empty annotations, if only declaration annotations were given.
  4675             // This method will raise an error for such a type.
  4656             // This method will raise an error for such a type.
  4676             for (JCAnnotation ai : annotations) {
  4657             for (JCAnnotation ai : annotations) {
  4677                 if (!ai.type.isErroneous() &&
  4658                 if (!ai.type.isErroneous() &&
  4678                         typeAnnotations.annotationType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) {
  4659                         typeAnnotations.annotationTargetType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) {
  4679                     log.error(ai.pos(), "annotation.type.not.applicable");
  4660                     log.error(ai.pos(), "annotation.type.not.applicable");
  4680                 }
  4661                 }
  4681             }
  4662             }
  4682         }
  4663         }
  4683     }
  4664     }