82 private final Types types; |
82 private final Types types; |
83 private final JCDiagnostic.Factory diags; |
83 private final JCDiagnostic.Factory diags; |
84 private final Source source; |
84 private final Source source; |
85 private final Target target; |
85 private final Target target; |
86 private final DeferredLintHandler deferredLintHandler; |
86 private final DeferredLintHandler deferredLintHandler; |
|
87 private final Lint lint; |
87 |
88 |
88 public static MemberEnter instance(Context context) { |
89 public static MemberEnter instance(Context context) { |
89 MemberEnter instance = context.get(memberEnterKey); |
90 MemberEnter instance = context.get(memberEnterKey); |
90 if (instance == null) |
91 if (instance == null) |
91 instance = new MemberEnter(context); |
92 instance = new MemberEnter(context); |
107 types = Types.instance(context); |
108 types = Types.instance(context); |
108 diags = JCDiagnostic.Factory.instance(context); |
109 diags = JCDiagnostic.Factory.instance(context); |
109 source = Source.instance(context); |
110 source = Source.instance(context); |
110 target = Target.instance(context); |
111 target = Target.instance(context); |
111 deferredLintHandler = DeferredLintHandler.instance(context); |
112 deferredLintHandler = DeferredLintHandler.instance(context); |
|
113 lint = Lint.instance(context); |
112 allowTypeAnnos = source.allowTypeAnnotations(); |
114 allowTypeAnnos = source.allowTypeAnnotations(); |
113 } |
115 } |
114 |
116 |
115 /** Switch: support type annotations. |
117 /** Switch: support type annotations. |
116 */ |
118 */ |
504 p = p.owner; |
506 p = p.owner; |
505 } |
507 } |
506 } |
508 } |
507 |
509 |
508 // process package annotations |
510 // process package annotations |
509 annotateLater(tree.packageAnnotations, env, tree.packge); |
511 annotateLater(tree.packageAnnotations, env, tree.packge, null); |
510 |
512 |
511 DeferredLintHandler prevLintHandler = chk.setDeferredLintHandler(DeferredLintHandler.immediateHandler); |
513 DiagnosticPosition prevLintPos = deferredLintHandler.immediate(); |
|
514 Lint prevLint = chk.setLint(lint); |
512 |
515 |
513 try { |
516 try { |
514 // Import-on-demand java.lang. |
517 // Import-on-demand java.lang. |
515 importAll(tree.pos, reader.enterPackage(names.java_lang), env); |
518 importAll(tree.pos, reader.enterPackage(names.java_lang), env); |
516 |
519 |
517 // Process all import clauses. |
520 // Process all import clauses. |
518 memberEnter(tree.defs, env); |
521 memberEnter(tree.defs, env); |
519 } finally { |
522 } finally { |
520 chk.setDeferredLintHandler(prevLintHandler); |
523 chk.setLint(prevLint); |
|
524 deferredLintHandler.setPos(prevLintPos); |
521 } |
525 } |
522 } |
526 } |
523 |
527 |
524 // process the non-static imports and the static imports of types. |
528 // process the non-static imports and the static imports of types. |
525 public void visitImport(JCImport tree) { |
529 public void visitImport(JCImport tree) { |
562 m.enclClass().flags_field |= DEFAULT; |
566 m.enclClass().flags_field |= DEFAULT; |
563 } |
567 } |
564 |
568 |
565 Env<AttrContext> localEnv = methodEnv(tree, env); |
569 Env<AttrContext> localEnv = methodEnv(tree, env); |
566 |
570 |
567 DeferredLintHandler prevLintHandler = |
571 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
568 chk.setDeferredLintHandler(deferredLintHandler.setPos(tree.pos())); |
|
569 try { |
572 try { |
570 // Compute the method type |
573 // Compute the method type |
571 m.type = signature(m, tree.typarams, tree.params, |
574 m.type = signature(m, tree.typarams, tree.params, |
572 tree.restype, tree.recvparam, |
575 tree.restype, tree.recvparam, |
573 tree.thrown, |
576 tree.thrown, |
574 localEnv); |
577 localEnv); |
575 } finally { |
578 } finally { |
576 chk.setDeferredLintHandler(prevLintHandler); |
579 deferredLintHandler.setPos(prevLintPos); |
577 } |
580 } |
578 |
581 |
579 if (types.isSignaturePolymorphic(m)) { |
582 if (types.isSignaturePolymorphic(m)) { |
580 m.flags_field |= SIGNATURE_POLYMORPHIC; |
583 m.flags_field |= SIGNATURE_POLYMORPHIC; |
581 } |
584 } |
595 |
598 |
596 localEnv.info.scope.leave(); |
599 localEnv.info.scope.leave(); |
597 if (chk.checkUnique(tree.pos(), m, enclScope)) { |
600 if (chk.checkUnique(tree.pos(), m, enclScope)) { |
598 enclScope.enter(m); |
601 enclScope.enter(m); |
599 } |
602 } |
600 annotateLater(tree.mods.annotations, localEnv, m); |
603 annotateLater(tree.mods.annotations, localEnv, m, tree.pos()); |
601 // Visit the signature of the method. Note that |
604 // Visit the signature of the method. Note that |
602 // TypeAnnotate doesn't descend into the body. |
605 // TypeAnnotate doesn't descend into the body. |
603 typeAnnotate(tree, localEnv, m); |
606 typeAnnotate(tree, localEnv, m, tree.pos()); |
604 |
607 |
605 if (tree.defaultValue != null) |
608 if (tree.defaultValue != null) |
606 annotateDefaultValueLater(tree.defaultValue, localEnv, m); |
609 annotateDefaultValueLater(tree.defaultValue, localEnv, m); |
607 } |
610 } |
608 |
611 |
628 if ((tree.mods.flags & STATIC) != 0 || |
631 if ((tree.mods.flags & STATIC) != 0 || |
629 (env.info.scope.owner.flags() & INTERFACE) != 0) { |
632 (env.info.scope.owner.flags() & INTERFACE) != 0) { |
630 localEnv = env.dup(tree, env.info.dup()); |
633 localEnv = env.dup(tree, env.info.dup()); |
631 localEnv.info.staticLevel++; |
634 localEnv.info.staticLevel++; |
632 } |
635 } |
633 DeferredLintHandler prevLintHandler = |
636 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
634 chk.setDeferredLintHandler(deferredLintHandler.setPos(tree.pos())); |
|
635 try { |
637 try { |
636 if (TreeInfo.isEnumInit(tree)) { |
638 if (TreeInfo.isEnumInit(tree)) { |
637 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype); |
639 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype); |
638 } else { |
640 } else { |
639 // Make sure type annotations are processed. |
641 // Make sure type annotations are processed. |
640 // But we don't have a symbol to attach them to yet - use null. |
642 // But we don't have a symbol to attach them to yet - use null. |
641 typeAnnotate(tree.vartype, env, null); |
643 typeAnnotate(tree.vartype, env, null, tree.pos()); |
642 attr.attribType(tree.vartype, localEnv); |
644 attr.attribType(tree.vartype, localEnv); |
643 if (tree.nameexpr != null) { |
645 if (tree.nameexpr != null) { |
644 attr.attribExpr(tree.nameexpr, localEnv); |
646 attr.attribExpr(tree.nameexpr, localEnv); |
645 MethodSymbol m = localEnv.enclMethod.sym; |
647 MethodSymbol m = localEnv.enclMethod.sym; |
646 if (m.isConstructor()) { |
648 if (m.isConstructor()) { |
656 checkType(tree.nameexpr, m.owner.type, "incorrect.receiver.name"); |
658 checkType(tree.nameexpr, m.owner.type, "incorrect.receiver.name"); |
657 } |
659 } |
658 } |
660 } |
659 } |
661 } |
660 } finally { |
662 } finally { |
661 chk.setDeferredLintHandler(prevLintHandler); |
663 deferredLintHandler.setPos(prevLintPos); |
662 } |
664 } |
663 |
665 |
664 if ((tree.mods.flags & VARARGS) != 0) { |
666 if ((tree.mods.flags & VARARGS) != 0) { |
665 //if we are entering a varargs parameter, we need to replace its type |
667 //if we are entering a varargs parameter, we need to replace its type |
666 //(a plain array type) with the more precise VarargsType --- we need |
668 //(a plain array type) with the more precise VarargsType --- we need |
678 v.flags_field |= HASINIT; |
680 v.flags_field |= HASINIT; |
679 if ((v.flags_field & FINAL) != 0 && |
681 if ((v.flags_field & FINAL) != 0 && |
680 needsLazyConstValue(tree.init)) { |
682 needsLazyConstValue(tree.init)) { |
681 Env<AttrContext> initEnv = getInitEnv(tree, env); |
683 Env<AttrContext> initEnv = getInitEnv(tree, env); |
682 initEnv.info.enclVar = v; |
684 initEnv.info.enclVar = v; |
683 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree.init); |
685 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree); |
684 } |
686 } |
685 } |
687 } |
686 if (chk.checkUnique(tree.pos(), v, enclScope)) { |
688 if (chk.checkUnique(tree.pos(), v, enclScope)) { |
687 chk.checkTransparentVar(tree.pos(), v, enclScope); |
689 chk.checkTransparentVar(tree.pos(), v, enclScope); |
688 enclScope.enter(v); |
690 enclScope.enter(v); |
689 } |
691 } |
690 annotateLater(tree.mods.annotations, localEnv, v); |
692 annotateLater(tree.mods.annotations, localEnv, v, tree.pos()); |
691 typeAnnotate(tree.vartype, env, v); |
693 typeAnnotate(tree.vartype, env, v, tree.pos()); |
692 annotate.flush(); |
694 annotate.flush(); |
693 v.pos = tree.pos; |
695 v.pos = tree.pos; |
694 } |
696 } |
695 // where |
697 // where |
696 void checkType(JCTree tree, Type type, String diag) { |
698 void checkType(JCTree tree, Type type, String diag) { |
818 *********************************************************************/ |
830 *********************************************************************/ |
819 |
831 |
820 /** Queue annotations for later processing. */ |
832 /** Queue annotations for later processing. */ |
821 void annotateLater(final List<JCAnnotation> annotations, |
833 void annotateLater(final List<JCAnnotation> annotations, |
822 final Env<AttrContext> localEnv, |
834 final Env<AttrContext> localEnv, |
823 final Symbol s) { |
835 final Symbol s, |
|
836 final DiagnosticPosition deferPos) { |
824 if (annotations.isEmpty()) { |
837 if (annotations.isEmpty()) { |
825 return; |
838 return; |
826 } |
839 } |
827 if (s.kind != PCK) { |
840 if (s.kind != PCK) { |
828 s.resetAnnotations(); // mark Annotations as incomplete for now |
841 s.resetAnnotations(); // mark Annotations as incomplete for now |
835 |
848 |
836 @Override |
849 @Override |
837 public void enterAnnotation() { |
850 public void enterAnnotation() { |
838 Assert.check(s.kind == PCK || s.annotationsPendingCompletion()); |
851 Assert.check(s.kind == PCK || s.annotationsPendingCompletion()); |
839 JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
852 JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); |
|
853 DiagnosticPosition prevLintPos = |
|
854 deferPos != null |
|
855 ? deferredLintHandler.setPos(deferPos) |
|
856 : deferredLintHandler.immediate(); |
|
857 Lint prevLint = deferPos != null ? null : chk.setLint(lint); |
840 try { |
858 try { |
841 if (s.hasAnnotations() && |
859 if (s.hasAnnotations() && |
842 annotations.nonEmpty()) |
860 annotations.nonEmpty()) |
843 log.error(annotations.head.pos, |
861 log.error(annotations.head.pos, |
844 "already.annotated", |
862 "already.annotated", |
845 kindName(s), s); |
863 kindName(s), s); |
846 actualEnterAnnotations(annotations, localEnv, s); |
864 actualEnterAnnotations(annotations, localEnv, s); |
847 } finally { |
865 } finally { |
|
866 if (prevLint != null) |
|
867 chk.setLint(prevLint); |
|
868 deferredLintHandler.setPos(prevLintPos); |
848 log.useSource(prev); |
869 log.useSource(prev); |
849 } |
870 } |
850 } |
871 } |
851 }); |
872 }); |
852 } |
873 } |
962 JCClassDecl tree = (JCClassDecl)env.tree; |
983 JCClassDecl tree = (JCClassDecl)env.tree; |
963 boolean wasFirst = isFirst; |
984 boolean wasFirst = isFirst; |
964 isFirst = false; |
985 isFirst = false; |
965 |
986 |
966 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
987 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
988 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos()); |
967 try { |
989 try { |
968 // Save class environment for later member enter (2) processing. |
990 // Save class environment for later member enter (2) processing. |
969 halfcompleted.append(env); |
991 halfcompleted.append(env); |
970 |
992 |
971 // Mark class as not yet attributed. |
993 // Mark class as not yet attributed. |
983 |
1005 |
984 // create an environment for evaluating the base clauses |
1006 // create an environment for evaluating the base clauses |
985 Env<AttrContext> baseEnv = baseEnv(tree, env); |
1007 Env<AttrContext> baseEnv = baseEnv(tree, env); |
986 |
1008 |
987 if (tree.extending != null) |
1009 if (tree.extending != null) |
988 typeAnnotate(tree.extending, baseEnv, sym); |
1010 typeAnnotate(tree.extending, baseEnv, sym, tree.pos()); |
989 for (JCExpression impl : tree.implementing) |
1011 for (JCExpression impl : tree.implementing) |
990 typeAnnotate(impl, baseEnv, sym); |
1012 typeAnnotate(impl, baseEnv, sym, tree.pos()); |
991 annotate.flush(); |
1013 annotate.flush(); |
992 |
1014 |
993 // Determine supertype. |
1015 // Determine supertype. |
994 Type supertype = |
1016 Type supertype = |
995 (tree.extending != null) |
1017 (tree.extending != null) |
1046 // can attribute the annotation types and then check to see if the |
1068 // can attribute the annotation types and then check to see if the |
1047 // @Deprecated annotation is present. |
1069 // @Deprecated annotation is present. |
1048 attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); |
1070 attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); |
1049 if (hasDeprecatedAnnotation(tree.mods.annotations)) |
1071 if (hasDeprecatedAnnotation(tree.mods.annotations)) |
1050 c.flags_field |= DEPRECATED; |
1072 c.flags_field |= DEPRECATED; |
1051 annotateLater(tree.mods.annotations, baseEnv, c); |
1073 annotateLater(tree.mods.annotations, baseEnv, c, tree.pos()); |
1052 // class type parameters use baseEnv but everything uses env |
1074 // class type parameters use baseEnv but everything uses env |
1053 |
1075 |
1054 chk.checkNonCyclicDecl(tree); |
1076 chk.checkNonCyclicDecl(tree); |
1055 |
1077 |
1056 attr.attribTypeVariables(tree.typarams, baseEnv); |
1078 attr.attribTypeVariables(tree.typarams, baseEnv); |
1057 // Do this here, where we have the symbol. |
1079 // Do this here, where we have the symbol. |
1058 for (JCTypeParameter tp : tree.typarams) |
1080 for (JCTypeParameter tp : tree.typarams) |
1059 typeAnnotate(tp, baseEnv, sym); |
1081 typeAnnotate(tp, baseEnv, sym, tree.pos()); |
1060 annotate.flush(); |
1082 annotate.flush(); |
1061 |
1083 |
1062 // Add default constructor if needed. |
1084 // Add default constructor if needed. |
1063 if ((c.flags() & INTERFACE) == 0 && |
1085 if ((c.flags() & INTERFACE) == 0 && |
1064 !TreeInfo.hasConstructors(tree.defs)) { |
1086 !TreeInfo.hasConstructors(tree.defs)) { |
1124 c.flags_field |= AUXILIARY; |
1146 c.flags_field |= AUXILIARY; |
1125 } |
1147 } |
1126 } catch (CompletionFailure ex) { |
1148 } catch (CompletionFailure ex) { |
1127 chk.completionError(tree.pos(), ex); |
1149 chk.completionError(tree.pos(), ex); |
1128 } finally { |
1150 } finally { |
|
1151 deferredLintHandler.setPos(prevLintPos); |
1129 log.useSource(prev); |
1152 log.useSource(prev); |
1130 } |
1153 } |
1131 |
1154 |
1132 // Enter all member fields and methods of a set of half completed |
1155 // Enter all member fields and methods of a set of half completed |
1133 // classes in a second phase. |
1156 // classes in a second phase. |
1184 s.appendTypeAttributesWithCompletion( |
1207 s.appendTypeAttributesWithCompletion( |
1185 annotate.new AnnotateRepeatedContext<Attribute.TypeCompound>(env, annotated, pos, log, true)); |
1208 annotate.new AnnotateRepeatedContext<Attribute.TypeCompound>(env, annotated, pos, log, true)); |
1186 } |
1209 } |
1187 } |
1210 } |
1188 |
1211 |
1189 public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym) { |
1212 public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) { |
1190 if (allowTypeAnnos) { |
1213 if (allowTypeAnnos) { |
1191 tree.accept(new TypeAnnotate(env, sym)); |
1214 tree.accept(new TypeAnnotate(env, sym, deferPos)); |
1192 } |
1215 } |
1193 } |
1216 } |
1194 |
1217 |
1195 /** |
1218 /** |
1196 * We need to use a TreeScanner, because it is not enough to visit the top-level |
1219 * We need to use a TreeScanner, because it is not enough to visit the top-level |
1197 * annotations. We also need to visit type arguments, etc. |
1220 * annotations. We also need to visit type arguments, etc. |
1198 */ |
1221 */ |
1199 private class TypeAnnotate extends TreeScanner { |
1222 private class TypeAnnotate extends TreeScanner { |
1200 private Env<AttrContext> env; |
1223 private Env<AttrContext> env; |
1201 private Symbol sym; |
1224 private Symbol sym; |
1202 |
1225 private DiagnosticPosition deferPos; |
1203 public TypeAnnotate(final Env<AttrContext> env, final Symbol sym) { |
1226 |
|
1227 public TypeAnnotate(final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) { |
1204 this.env = env; |
1228 this.env = env; |
1205 this.sym = sym; |
1229 this.sym = sym; |
|
1230 this.deferPos = deferPos; |
1206 } |
1231 } |
1207 |
1232 |
1208 void annotateTypeLater(final List<JCAnnotation> annotations) { |
1233 void annotateTypeLater(final List<JCAnnotation> annotations) { |
1209 if (annotations.isEmpty()) { |
1234 if (annotations.isEmpty()) { |
1210 return; |
1235 return; |
1211 } |
1236 } |
|
1237 |
|
1238 final DiagnosticPosition deferPos = this.deferPos; |
1212 |
1239 |
1213 annotate.normal(new Annotate.Annotator() { |
1240 annotate.normal(new Annotate.Annotator() { |
1214 @Override |
1241 @Override |
1215 public String toString() { |
1242 public String toString() { |
1216 return "type annotate " + annotations + " onto " + sym + " in " + sym.owner; |
1243 return "type annotate " + annotations + " onto " + sym + " in " + sym.owner; |
1217 } |
1244 } |
1218 @Override |
1245 @Override |
1219 public void enterAnnotation() { |
1246 public void enterAnnotation() { |
1220 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
1247 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
1248 DiagnosticPosition prevLintPos = null; |
|
1249 |
|
1250 if (deferPos != null) { |
|
1251 prevLintPos = deferredLintHandler.setPos(deferPos); |
|
1252 } |
1221 try { |
1253 try { |
1222 actualEnterTypeAnnotations(annotations, env, sym); |
1254 actualEnterTypeAnnotations(annotations, env, sym); |
1223 } finally { |
1255 } finally { |
|
1256 if (prevLintPos != null) |
|
1257 deferredLintHandler.setPos(prevLintPos); |
1224 log.useSource(prev); |
1258 log.useSource(prev); |
1225 } |
1259 } |
1226 } |
1260 } |
1227 }); |
1261 }); |
1228 } |
1262 } |
1260 // scan(tree.body); |
1294 // scan(tree.body); |
1261 } |
1295 } |
1262 |
1296 |
1263 @Override |
1297 @Override |
1264 public void visitVarDef(final JCVariableDecl tree) { |
1298 public void visitVarDef(final JCVariableDecl tree) { |
1265 if (sym != null && sym.kind == Kinds.VAR) { |
1299 DiagnosticPosition prevPos = deferPos; |
1266 // Don't visit a parameter once when the sym is the method |
1300 deferPos = tree.pos(); |
1267 // and once when the sym is the parameter. |
1301 try { |
1268 scan(tree.mods); |
1302 if (sym != null && sym.kind == Kinds.VAR) { |
1269 scan(tree.vartype); |
1303 // Don't visit a parameter once when the sym is the method |
1270 } |
1304 // and once when the sym is the parameter. |
1271 scan(tree.init); |
1305 scan(tree.mods); |
|
1306 scan(tree.vartype); |
|
1307 } |
|
1308 scan(tree.init); |
|
1309 } finally { |
|
1310 deferPos = prevPos; |
|
1311 } |
1272 } |
1312 } |
1273 |
1313 |
1274 @Override |
1314 @Override |
1275 public void visitClassDef(JCClassDecl tree) { |
1315 public void visitClassDef(JCClassDecl tree) { |
1276 // We can only hit a classdef if it is declared within |
1316 // We can only hit a classdef if it is declared within |