1473 result = null;
1474 }
1475 finally {
1476 loopEnv.info.scope.leave();
1477 }
1478 if (!breaksOutOf(tree, tree.body)) {
1479 //include condition's body when false after the while, if cannot get out of the loop
1480 List<BindingSymbol> bindings =
1481 matchBindingsComputer.getMatchBindings(tree.cond, false);
1482
1483 bindings.forEach(env.info.scope::enter);
1484 bindings.forEach(BindingSymbol::preserveBinding);
1485 }
1486 }
1487
1488 public void visitForeachLoop(JCEnhancedForLoop tree) {
1489 Env<AttrContext> loopEnv =
1490 env.dup(env.tree, env.info.dup(env.info.scope.dup()));
1491 try {
1492 //the Formal Parameter of a for-each loop is not in the scope when
1493 //attributing the for-each expression; we mimick this by attributing
1494 //the for-each expression first (against original scope).
1495 Type exprType = types.cvarUpperBound(attribExpr(tree.expr, loopEnv));
1496 chk.checkNonVoid(tree.pos(), exprType);
1497 Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
1498 if (elemtype == null) {
1499 // or perhaps expr implements Iterable<T>?
1500 Type base = types.asSuper(exprType, syms.iterableType.tsym);
1501 if (base == null) {
1502 log.error(tree.expr.pos(),
1503 Errors.ForeachNotApplicableToType(exprType,
1504 Fragments.TypeReqArrayOrIterable));
1505 elemtype = types.createErrorType(exprType);
1506 } else {
1507 List<Type> iterableParams = base.allparams();
1508 elemtype = iterableParams.isEmpty()
1509 ? syms.objectType
1510 : types.wildUpperBound(iterableParams.head);
1511 }
1512 }
1513 if (tree.var.isImplicitlyTyped()) {
3449 log.report(diag);
3450 }
3451 result = that.type = types.createErrorType(currentTarget);
3452 return;
3453 }
3454 }
3455
3456 that.sym = refSym.isConstructor() ? refSym.baseSymbol() : refSym;
3457 that.kind = lookupHelper.referenceKind(that.sym);
3458 that.ownerAccessible = rs.isAccessible(localEnv, that.sym.enclClass());
3459
3460 if (desc.getReturnType() == Type.recoveryType) {
3461 // stop here
3462 result = that.type = currentTarget;
3463 return;
3464 }
3465
3466 if (!env.info.attributionMode.isSpeculative && that.getMode() == JCMemberReference.ReferenceMode.NEW) {
3467 Type enclosingType = exprType.getEnclosingType();
3468 if (enclosingType != null && enclosingType.hasTag(CLASS)) {
3469 // Check for the existence of an apropriate outer instance
3470 rs.resolveImplicitThis(that.pos(), env, exprType);
3471 }
3472 }
3473
3474 if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
3475
3476 if (that.getMode() == ReferenceMode.INVOKE &&
3477 TreeInfo.isStaticSelector(that.expr, names) &&
3478 that.kind.isUnbound() &&
3479 lookupHelper.site.isRaw()) {
3480 chk.checkRaw(that.expr, localEnv);
3481 }
3482
3483 if (that.sym.isStatic() && TreeInfo.isStaticSelector(that.expr, names) &&
3484 exprType.getTypeArguments().nonEmpty()) {
3485 //static ref with class type-args
3486 log.error(that.expr.pos(),
3487 Errors.InvalidMref(Kinds.kindName(that.getMode()),
3488 Fragments.StaticMrefWithTargs));
3489 result = that.type = types.createErrorType(currentTarget);
4180 if (resultInfo.pt.hasTag(METHOD) || resultInfo.pt.hasTag(FORALL)) {
4181 return rs.resolveQualifiedMethod(
4182 pos, env, location, site, name, resultInfo.pt.getParameterTypes(), resultInfo.pt.getTypeArguments());
4183 } else if (name == names._this || name == names._super) {
4184 return rs.resolveSelf(pos, env, site.tsym, name);
4185 } else if (name == names._class) {
4186 // In this case, we have already made sure in
4187 // visitSelect that qualifier expression is a type.
4188 return syms.getClassField(site, types);
4189 } else {
4190 // We are seeing a plain identifier as selector.
4191 Symbol sym = rs.findIdentInType(pos, env, site, name, resultInfo.pkind);
4192 sym = rs.accessBase(sym, pos, location, site, name, true);
4193 return sym;
4194 }
4195 case WILDCARD:
4196 throw new AssertionError(tree);
4197 case TYPEVAR:
4198 // Normally, site.getUpperBound() shouldn't be null.
4199 // It should only happen during memberEnter/attribBase
4200 // when determining the super type which *must* beac
4201 // done before attributing the type variables. In
4202 // other words, we are seeing this illegal program:
4203 // class B<T> extends A<T.foo> {}
4204 Symbol sym = (site.getUpperBound() != null)
4205 ? selectSym(tree, location, capture(site.getUpperBound()), env, resultInfo)
4206 : null;
4207 if (sym == null) {
4208 log.error(pos, Errors.TypeVarCantBeDeref);
4209 return syms.errSymbol;
4210 } else {
4211 Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ?
4212 rs.new AccessError(env, site, sym) :
4213 sym;
4214 rs.accessBase(sym2, pos, location, site, name, true);
4215 return sym;
4216 }
4217 case ERROR:
4218 // preserve identifier names through errors
4219 return types.createErrorType(name, site.tsym, site).tsym;
4220 default:
5153 sym.kind != VAR ||
5154 ((VarSymbol) sym).getConstValue() == null)
5155 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5156 }
5157 }
5158
5159 // Check for cycles among non-initial constructors.
5160 chk.checkCyclicConstructors(tree);
5161
5162 // Check for cycles among annotation elements.
5163 chk.checkNonCyclicElements(tree);
5164
5165 // Check for proper use of serialVersionUID
5166 if (env.info.lint.isEnabled(LintCategory.SERIAL)
5167 && isSerializable(c.type)
5168 && (c.flags() & (Flags.ENUM | Flags.INTERFACE)) == 0
5169 && !c.isAnonymous()) {
5170 checkSerialVersionUID(tree, c);
5171 }
5172 if (allowTypeAnnos) {
5173 // Correctly organize the postions of the type annotations
5174 typeAnnotations.organizeTypeAnnotationsBodies(tree);
5175
5176 // Check type annotations applicability rules
5177 validateTypeAnnotations(tree, false);
5178 }
5179 }
5180 // where
5181 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5182 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5183 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5184 if (types.isSameType(al.head.annotationType.type, t))
5185 return al.head.pos();
5186 }
5187
5188 return null;
5189 }
5190
5191 /** check if a type is a subtype of Serializable, if that is available. */
5192 boolean isSerializable(Type t) {
5193 try {
5463 // Ensure that no declaration annotations are present.
5464 // Note that a tree type might be an AnnotatedType with
5465 // empty annotations, if only declaration annotations were given.
5466 // This method will raise an error for such a type.
5467 for (JCAnnotation ai : annotations) {
5468 if (!ai.type.isErroneous() &&
5469 typeAnnotations.annotationTargetType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) {
5470 log.error(ai.pos(), Errors.AnnotationTypeNotApplicableToType(ai.type));
5471 }
5472 }
5473 }
5474 }
5475
5476 // <editor-fold desc="post-attribution visitor">
5477
5478 /**
5479 * Handle missing types/symbols in an AST. This routine is useful when
5480 * the compiler has encountered some errors (which might have ended up
5481 * terminating attribution abruptly); if the compiler is used in fail-over
5482 * mode (e.g. by an IDE) and the AST contains semantic errors, this routine
5483 * prevents NPE to be progagated during subsequent compilation steps.
5484 */
5485 public void postAttr(JCTree tree) {
5486 new PostAttrAnalyzer().scan(tree);
5487 }
5488
5489 class PostAttrAnalyzer extends TreeScanner {
5490
5491 private void initTypeIfNeeded(JCTree that) {
5492 if (that.type == null) {
5493 if (that.hasTag(METHODDEF)) {
5494 that.type = dummyMethodType((JCMethodDecl)that);
5495 } else {
5496 that.type = syms.unknownType;
5497 }
5498 }
5499 }
5500
5501 /* Construct a dummy method type. If we have a method declaration,
5502 * and the declared return type is void, then use that return type
5503 * instead of UNKNOWN to avoid spurious error messages in lambda
|
1473 result = null;
1474 }
1475 finally {
1476 loopEnv.info.scope.leave();
1477 }
1478 if (!breaksOutOf(tree, tree.body)) {
1479 //include condition's body when false after the while, if cannot get out of the loop
1480 List<BindingSymbol> bindings =
1481 matchBindingsComputer.getMatchBindings(tree.cond, false);
1482
1483 bindings.forEach(env.info.scope::enter);
1484 bindings.forEach(BindingSymbol::preserveBinding);
1485 }
1486 }
1487
1488 public void visitForeachLoop(JCEnhancedForLoop tree) {
1489 Env<AttrContext> loopEnv =
1490 env.dup(env.tree, env.info.dup(env.info.scope.dup()));
1491 try {
1492 //the Formal Parameter of a for-each loop is not in the scope when
1493 //attributing the for-each expression; we mimic this by attributing
1494 //the for-each expression first (against original scope).
1495 Type exprType = types.cvarUpperBound(attribExpr(tree.expr, loopEnv));
1496 chk.checkNonVoid(tree.pos(), exprType);
1497 Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
1498 if (elemtype == null) {
1499 // or perhaps expr implements Iterable<T>?
1500 Type base = types.asSuper(exprType, syms.iterableType.tsym);
1501 if (base == null) {
1502 log.error(tree.expr.pos(),
1503 Errors.ForeachNotApplicableToType(exprType,
1504 Fragments.TypeReqArrayOrIterable));
1505 elemtype = types.createErrorType(exprType);
1506 } else {
1507 List<Type> iterableParams = base.allparams();
1508 elemtype = iterableParams.isEmpty()
1509 ? syms.objectType
1510 : types.wildUpperBound(iterableParams.head);
1511 }
1512 }
1513 if (tree.var.isImplicitlyTyped()) {
3449 log.report(diag);
3450 }
3451 result = that.type = types.createErrorType(currentTarget);
3452 return;
3453 }
3454 }
3455
3456 that.sym = refSym.isConstructor() ? refSym.baseSymbol() : refSym;
3457 that.kind = lookupHelper.referenceKind(that.sym);
3458 that.ownerAccessible = rs.isAccessible(localEnv, that.sym.enclClass());
3459
3460 if (desc.getReturnType() == Type.recoveryType) {
3461 // stop here
3462 result = that.type = currentTarget;
3463 return;
3464 }
3465
3466 if (!env.info.attributionMode.isSpeculative && that.getMode() == JCMemberReference.ReferenceMode.NEW) {
3467 Type enclosingType = exprType.getEnclosingType();
3468 if (enclosingType != null && enclosingType.hasTag(CLASS)) {
3469 // Check for the existence of an appropriate outer instance
3470 rs.resolveImplicitThis(that.pos(), env, exprType);
3471 }
3472 }
3473
3474 if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
3475
3476 if (that.getMode() == ReferenceMode.INVOKE &&
3477 TreeInfo.isStaticSelector(that.expr, names) &&
3478 that.kind.isUnbound() &&
3479 lookupHelper.site.isRaw()) {
3480 chk.checkRaw(that.expr, localEnv);
3481 }
3482
3483 if (that.sym.isStatic() && TreeInfo.isStaticSelector(that.expr, names) &&
3484 exprType.getTypeArguments().nonEmpty()) {
3485 //static ref with class type-args
3486 log.error(that.expr.pos(),
3487 Errors.InvalidMref(Kinds.kindName(that.getMode()),
3488 Fragments.StaticMrefWithTargs));
3489 result = that.type = types.createErrorType(currentTarget);
4180 if (resultInfo.pt.hasTag(METHOD) || resultInfo.pt.hasTag(FORALL)) {
4181 return rs.resolveQualifiedMethod(
4182 pos, env, location, site, name, resultInfo.pt.getParameterTypes(), resultInfo.pt.getTypeArguments());
4183 } else if (name == names._this || name == names._super) {
4184 return rs.resolveSelf(pos, env, site.tsym, name);
4185 } else if (name == names._class) {
4186 // In this case, we have already made sure in
4187 // visitSelect that qualifier expression is a type.
4188 return syms.getClassField(site, types);
4189 } else {
4190 // We are seeing a plain identifier as selector.
4191 Symbol sym = rs.findIdentInType(pos, env, site, name, resultInfo.pkind);
4192 sym = rs.accessBase(sym, pos, location, site, name, true);
4193 return sym;
4194 }
4195 case WILDCARD:
4196 throw new AssertionError(tree);
4197 case TYPEVAR:
4198 // Normally, site.getUpperBound() shouldn't be null.
4199 // It should only happen during memberEnter/attribBase
4200 // when determining the super type which *must* be
4201 // done before attributing the type variables. In
4202 // other words, we are seeing this illegal program:
4203 // class B<T> extends A<T.foo> {}
4204 Symbol sym = (site.getUpperBound() != null)
4205 ? selectSym(tree, location, capture(site.getUpperBound()), env, resultInfo)
4206 : null;
4207 if (sym == null) {
4208 log.error(pos, Errors.TypeVarCantBeDeref);
4209 return syms.errSymbol;
4210 } else {
4211 Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ?
4212 rs.new AccessError(env, site, sym) :
4213 sym;
4214 rs.accessBase(sym2, pos, location, site, name, true);
4215 return sym;
4216 }
4217 case ERROR:
4218 // preserve identifier names through errors
4219 return types.createErrorType(name, site.tsym, site).tsym;
4220 default:
5153 sym.kind != VAR ||
5154 ((VarSymbol) sym).getConstValue() == null)
5155 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5156 }
5157 }
5158
5159 // Check for cycles among non-initial constructors.
5160 chk.checkCyclicConstructors(tree);
5161
5162 // Check for cycles among annotation elements.
5163 chk.checkNonCyclicElements(tree);
5164
5165 // Check for proper use of serialVersionUID
5166 if (env.info.lint.isEnabled(LintCategory.SERIAL)
5167 && isSerializable(c.type)
5168 && (c.flags() & (Flags.ENUM | Flags.INTERFACE)) == 0
5169 && !c.isAnonymous()) {
5170 checkSerialVersionUID(tree, c);
5171 }
5172 if (allowTypeAnnos) {
5173 // Correctly organize the positions of the type annotations
5174 typeAnnotations.organizeTypeAnnotationsBodies(tree);
5175
5176 // Check type annotations applicability rules
5177 validateTypeAnnotations(tree, false);
5178 }
5179 }
5180 // where
5181 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5182 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5183 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5184 if (types.isSameType(al.head.annotationType.type, t))
5185 return al.head.pos();
5186 }
5187
5188 return null;
5189 }
5190
5191 /** check if a type is a subtype of Serializable, if that is available. */
5192 boolean isSerializable(Type t) {
5193 try {
5463 // Ensure that no declaration annotations are present.
5464 // Note that a tree type might be an AnnotatedType with
5465 // empty annotations, if only declaration annotations were given.
5466 // This method will raise an error for such a type.
5467 for (JCAnnotation ai : annotations) {
5468 if (!ai.type.isErroneous() &&
5469 typeAnnotations.annotationTargetType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) {
5470 log.error(ai.pos(), Errors.AnnotationTypeNotApplicableToType(ai.type));
5471 }
5472 }
5473 }
5474 }
5475
5476 // <editor-fold desc="post-attribution visitor">
5477
5478 /**
5479 * Handle missing types/symbols in an AST. This routine is useful when
5480 * the compiler has encountered some errors (which might have ended up
5481 * terminating attribution abruptly); if the compiler is used in fail-over
5482 * mode (e.g. by an IDE) and the AST contains semantic errors, this routine
5483 * prevents NPE to be propagated during subsequent compilation steps.
5484 */
5485 public void postAttr(JCTree tree) {
5486 new PostAttrAnalyzer().scan(tree);
5487 }
5488
5489 class PostAttrAnalyzer extends TreeScanner {
5490
5491 private void initTypeIfNeeded(JCTree that) {
5492 if (that.type == null) {
5493 if (that.hasTag(METHODDEF)) {
5494 that.type = dummyMethodType((JCMethodDecl)that);
5495 } else {
5496 that.type = syms.unknownType;
5497 }
5498 }
5499 }
5500
5501 /* Construct a dummy method type. If we have a method declaration,
5502 * and the declared return type is void, then use that return type
5503 * instead of UNKNOWN to avoid spurious error messages in lambda
|