< prev index next >

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

Print this page




  32 import javax.lang.model.element.ElementKind;
  33 import javax.tools.JavaFileObject;
  34 
  35 import com.sun.source.tree.CaseTree.CaseKind;
  36 import com.sun.source.tree.IdentifierTree;
  37 import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
  38 import com.sun.source.tree.MemberSelectTree;
  39 import com.sun.source.tree.TreeVisitor;
  40 import com.sun.source.util.SimpleTreeVisitor;
  41 import com.sun.tools.javac.code.*;
  42 import com.sun.tools.javac.code.Lint.LintCategory;
  43 import com.sun.tools.javac.code.Scope.WriteableScope;
  44 import com.sun.tools.javac.code.Source.Feature;
  45 import com.sun.tools.javac.code.Symbol.*;
  46 import com.sun.tools.javac.code.Type.*;
  47 import com.sun.tools.javac.code.TypeMetadata.Annotations;
  48 import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
  49 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
  50 import com.sun.tools.javac.comp.Check.CheckContext;
  51 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;

  52 import com.sun.tools.javac.jvm.*;
  53 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.Diamond;
  54 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArg;
  55 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArgs;
  56 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  57 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  58 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  59 import com.sun.tools.javac.tree.*;
  60 import com.sun.tools.javac.tree.JCTree.*;
  61 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
  62 import com.sun.tools.javac.util.*;
  63 import com.sun.tools.javac.util.DefinedBy.Api;
  64 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  65 import com.sun.tools.javac.util.JCDiagnostic.Error;
  66 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
  67 import com.sun.tools.javac.util.JCDiagnostic.Warning;
  68 import com.sun.tools.javac.util.List;
  69 
  70 import static com.sun.tools.javac.code.Flags.*;
  71 import static com.sun.tools.javac.code.Flags.ANNOTATION;
  72 import static com.sun.tools.javac.code.Flags.BLOCK;
  73 import static com.sun.tools.javac.code.Kinds.*;
  74 import static com.sun.tools.javac.code.Kinds.Kind.*;
  75 import static com.sun.tools.javac.code.TypeTag.*;
  76 import static com.sun.tools.javac.code.TypeTag.WILDCARD;
  77 import com.sun.tools.javac.comp.Analyzer.AnalyzerMode;


  78 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  79 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  80 import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
  81 import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
  82 
  83 /** This is the main context-dependent analysis phase in GJC. It
  84  *  encompasses name resolution, type checking and constant folding as
  85  *  subtasks. Some subtasks involve auxiliary classes.
  86  *  @see Check
  87  *  @see Resolve
  88  *  @see ConstFold
  89  *  @see Infer
  90  *
  91  *  <p><b>This is NOT part of any supported API.
  92  *  If you write code that depends on this, you do so at your own risk.
  93  *  This code and its internal interfaces are subject to change or
  94  *  deletion without notice.</b>
  95  */
  96 public class Attr extends JCTree.Visitor {
  97     protected static final Context.Key<Attr> attrKey = new Context.Key<>();


 103     final Operators operators;
 104     final Infer infer;
 105     final Analyzer analyzer;
 106     final DeferredAttr deferredAttr;
 107     final Check chk;
 108     final Flow flow;
 109     final MemberEnter memberEnter;
 110     final TypeEnter typeEnter;
 111     final TreeMaker make;
 112     final ConstFold cfolder;
 113     final Enter enter;
 114     final Target target;
 115     final Types types;
 116     final JCDiagnostic.Factory diags;
 117     final TypeAnnotations typeAnnotations;
 118     final DeferredLintHandler deferredLintHandler;
 119     final TypeEnvs typeEnvs;
 120     final Dependencies dependencies;
 121     final Annotate annotate;
 122     final ArgumentAttr argumentAttr;

 123 
 124     public static Attr instance(Context context) {
 125         Attr instance = context.get(attrKey);
 126         if (instance == null)
 127             instance = new Attr(context);
 128         return instance;
 129     }
 130 
 131     protected Attr(Context context) {
 132         context.put(attrKey, this);
 133 
 134         names = Names.instance(context);
 135         log = Log.instance(context);
 136         syms = Symtab.instance(context);
 137         rs = Resolve.instance(context);
 138         operators = Operators.instance(context);
 139         chk = Check.instance(context);
 140         flow = Flow.instance(context);
 141         memberEnter = MemberEnter.instance(context);
 142         typeEnter = TypeEnter.instance(context);
 143         make = TreeMaker.instance(context);
 144         enter = Enter.instance(context);
 145         infer = Infer.instance(context);
 146         analyzer = Analyzer.instance(context);
 147         deferredAttr = DeferredAttr.instance(context);
 148         cfolder = ConstFold.instance(context);
 149         target = Target.instance(context);
 150         types = Types.instance(context);
 151         diags = JCDiagnostic.Factory.instance(context);
 152         annotate = Annotate.instance(context);
 153         typeAnnotations = TypeAnnotations.instance(context);
 154         deferredLintHandler = DeferredLintHandler.instance(context);
 155         typeEnvs = TypeEnvs.instance(context);
 156         dependencies = Dependencies.instance(context);
 157         argumentAttr = ArgumentAttr.instance(context);

 158 
 159         Options options = Options.instance(context);
 160 
 161         Source source = Source.instance(context);
 162         allowPoly = Feature.POLY.allowedInSource(source);
 163         allowTypeAnnos = Feature.TYPE_ANNOTATIONS.allowedInSource(source);
 164         allowLambda = Feature.LAMBDA.allowedInSource(source);
 165         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
 166         allowStaticInterfaceMethods = Feature.STATIC_INTERFACE_METHODS.allowedInSource(source);
 167         sourceName = source.name;
 168         useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
 169 
 170         statInfo = new ResultInfo(KindSelector.NIL, Type.noType);
 171         varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType);
 172         unknownExprInfo = new ResultInfo(KindSelector.VAL, Type.noType);
 173         methodAttrInfo = new MethodAttrInfo();
 174         unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
 175         unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
 176         recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
 177     }


 278     }
 279 
 280     /** Check that variable can be assigned to.
 281      *  @param pos    The current source code position.
 282      *  @param v      The assigned variable
 283      *  @param base   If the variable is referred to in a Select, the part
 284      *                to the left of the `.', null otherwise.
 285      *  @param env    The current environment.
 286      */
 287     void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
 288         if (v.name == names._this) {
 289             log.error(pos, Errors.CantAssignValToThis);
 290         } else if ((v.flags() & FINAL) != 0 &&
 291             ((v.flags() & HASINIT) != 0
 292              ||
 293              !((base == null ||
 294                TreeInfo.isThisQualifier(base)) &&
 295                isAssignableAsBlankFinal(v, env)))) {
 296             if (v.isResourceVariable()) { //TWR resource
 297                 log.error(pos, Errors.TryResourceMayNotBeAssigned(v));


 298             } else {
 299                 log.error(pos, Errors.CantAssignValToFinalVar(v));
 300             }
 301         }
 302     }
 303 
 304     /** Does tree represent a static reference to an identifier?
 305      *  It is assumed that tree is either a SELECT or an IDENT.
 306      *  We have to weed out selects from non-type names here.
 307      *  @param tree    The candidate tree.
 308      */
 309     boolean isStaticReference(JCTree tree) {
 310         if (tree.hasTag(SELECT)) {
 311             Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
 312             if (lsym == null || lsym.kind != TYP) {
 313                 return false;
 314             }
 315         }
 316         return true;
 317     }


1302                 } else {
1303                     cs.appendInitTypeAttributes(tas);
1304                 }
1305             }
1306         } else {
1307             // Create a new local environment with a local scope.
1308             Env<AttrContext> localEnv =
1309                 env.dup(tree, env.info.dup(env.info.scope.dup()));
1310             try {
1311                 attribStats(tree.stats, localEnv);
1312             } finally {
1313                 localEnv.info.scope.leave();
1314             }
1315         }
1316         result = null;
1317     }
1318 
1319     public void visitDoLoop(JCDoWhileLoop tree) {
1320         attribStat(tree.body, env.dup(tree));
1321         attribExpr(tree.cond, env, syms.booleanType);






1322         result = null;
1323     }
1324 
1325     public void visitWhileLoop(JCWhileLoop tree) {
1326         attribExpr(tree.cond, env, syms.booleanType);
1327         attribStat(tree.body, env.dup(tree));












1328         result = null;
1329     }
1330 





1331     public void visitForLoop(JCForLoop tree) {
1332         Env<AttrContext> loopEnv =
1333             env.dup(env.tree, env.info.dup(env.info.scope.dup()));
1334         try {
1335             attribStats(tree.init, loopEnv);
1336             if (tree.cond != null) attribExpr(tree.cond, loopEnv, syms.booleanType);
1337             loopEnv.tree = tree; // before, we were not in loop!
1338             attribStats(tree.step, loopEnv);
1339             attribStat(tree.body, loopEnv);










1340             result = null;
1341         }
1342         finally {
1343             loopEnv.info.scope.leave();






1344         }
1345     }
1346 
1347     public void visitForeachLoop(JCEnhancedForLoop tree) {
1348         Env<AttrContext> loopEnv =
1349             env.dup(env.tree, env.info.dup(env.info.scope.dup()));
1350         try {
1351             //the Formal Parameter of a for-each loop is not in the scope when
1352             //attributing the for-each expression; we mimick this by attributing
1353             //the for-each expression first (against original scope).
1354             Type exprType = types.cvarUpperBound(attribExpr(tree.expr, loopEnv));
1355             chk.checkNonVoid(tree.pos(), exprType);
1356             Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
1357             if (elemtype == null) {
1358                 // or perhaps expr implements Iterable<T>?
1359                 Type base = types.asSuper(exprType, syms.iterableType.tsym);
1360                 if (base == null) {
1361                     log.error(tree.expr.pos(),
1362                               Errors.ForeachNotApplicableToType(exprType,
1363                                                                 Fragments.TypeReqArrayOrIterable));


1675 
1676     public void visitConditional(JCConditional tree) {
1677         Type condtype = attribExpr(tree.cond, env, syms.booleanType);
1678 
1679         tree.polyKind = (!allowPoly ||
1680                 pt().hasTag(NONE) && pt() != Type.recoveryType && pt() != Infer.anyPoly ||
1681                 isBooleanOrNumeric(env, tree)) ?
1682                 PolyKind.STANDALONE : PolyKind.POLY;
1683 
1684         if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) {
1685             //this means we are returning a poly conditional from void-compatible lambda expression
1686             resultInfo.checkContext.report(tree, diags.fragment(Fragments.ConditionalTargetCantBeVoid));
1687             result = tree.type = types.createErrorType(resultInfo.pt);
1688             return;
1689         }
1690 
1691         ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ?
1692                 unknownExprInfo :
1693                 resultInfo.dup(conditionalContext(resultInfo.checkContext));
1694 
1695         Type truetype = attribTree(tree.truepart, env, condInfo);
1696         Type falsetype = attribTree(tree.falsepart, env, condInfo);


















1697 
1698         Type owntype = (tree.polyKind == PolyKind.STANDALONE) ?
1699                 condType(List.of(tree.truepart.pos(), tree.falsepart.pos()),
1700                          List.of(truetype, falsetype)) : pt();
1701         if (condtype.constValue() != null &&
1702                 truetype.constValue() != null &&
1703                 falsetype.constValue() != null &&
1704                 !owntype.hasTag(NONE)) {
1705             //constant folding
1706             owntype = cfolder.coerce(condtype.isTrue() ? truetype : falsetype, owntype);
1707         }
1708         result = check(tree, owntype, KindSelector.VAL, resultInfo);
1709     }
1710     //where
1711         private boolean isBooleanOrNumeric(Env<AttrContext> env, JCExpression tree) {
1712             switch (tree.getTag()) {
1713                 case LITERAL: return ((JCLiteral)tree).typetag.isSubRangeOf(DOUBLE) ||
1714                               ((JCLiteral)tree).typetag == BOOLEAN ||
1715                               ((JCLiteral)tree).typetag == BOT;
1716                 case LAMBDA: case REFERENCE: return false;


1831                                  .map(t -> chk.checkNonVoid(posIt.next(), t))
1832                                  .collect(List.collector());
1833 
1834             // both are known to be reference types.  The result is
1835             // lub(thentype,elsetype). This cannot fail, as it will
1836             // always be possible to infer "Object" if nothing better.
1837             return types.lub(condTypes.stream().map(t -> t.baseType()).collect(List.collector()));
1838         }
1839 
1840     final static TypeTag[] primitiveTags = new TypeTag[]{
1841         BYTE,
1842         CHAR,
1843         SHORT,
1844         INT,
1845         LONG,
1846         FLOAT,
1847         DOUBLE,
1848         BOOLEAN,
1849     };
1850 






1851     public void visitIf(JCIf tree) {
1852         attribExpr(tree.cond, env, syms.booleanType);
1853         attribStat(tree.thenpart, env);
1854         if (tree.elsepart != null)
1855             attribStat(tree.elsepart, env);



























1856         chk.checkEmptyIf(tree);












1857         result = null;
1858     }
1859 















1860     public void visitExec(JCExpressionStatement tree) {
1861         //a fresh environment is required for 292 inference to work properly ---
1862         //see Infer.instantiatePolymorphicSignatureInstance()
1863         Env<AttrContext> localEnv = env.dup(tree);
1864         attribExpr(tree.expr, localEnv);
1865         result = null;
1866     }
1867 
1868     public void visitBreak(JCBreak tree) {
1869         tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
1870         result = null;
1871     }
1872 
1873     public void visitYield(JCYield tree) {
1874         if (env.info.yieldResult != null) {
1875             attribTree(tree.value, env, env.info.yieldResult);
1876             tree.target = findJumpTarget(tree.pos(), tree.getTag(), names.empty, env);
1877         } else {
1878             log.error(tree.pos(), tree.value.hasTag(PARENS)
1879                     ? Errors.NoSwitchExpressionQualify


3521                 !argtype.isErroneous()) {
3522             owntype = (tree.getTag().isIncOrDecUnaryOp())
3523                 ? tree.arg.type
3524                 : operator.type.getReturnType();
3525             int opc = ((OperatorSymbol)operator).opcode;
3526 
3527             // If the argument is constant, fold it.
3528             if (argtype.constValue() != null) {
3529                 Type ctype = cfolder.fold1(opc, argtype);
3530                 if (ctype != null) {
3531                     owntype = cfolder.coerce(ctype, owntype);
3532                 }
3533             }
3534         }
3535         result = check(tree, owntype, KindSelector.VAL, resultInfo);
3536     }
3537 
3538     public void visitBinary(JCBinary tree) {
3539         // Attribute arguments.
3540         Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env));
3541         Type right = chk.checkNonVoid(tree.rhs.pos(), attribExpr(tree.rhs, env));




















3542         // Find operator.
3543         Symbol operator = tree.operator = operators.resolveBinary(tree, tree.getTag(), left, right);
3544         Type owntype = types.createErrorType(tree.type);
3545         if (operator != operators.noOpSymbol &&
3546                 !left.isErroneous() &&
3547                 !right.isErroneous()) {
3548             owntype = operator.type.getReturnType();
3549             int opc = ((OperatorSymbol)operator).opcode;
3550             // If both arguments are constants, fold them.
3551             if (left.constValue() != null && right.constValue() != null) {
3552                 Type ctype = cfolder.fold2(opc, left, right);
3553                 if (ctype != null) {
3554                     owntype = cfolder.coerce(ctype, owntype);
3555                 }
3556             }
3557 
3558             // Check that argument types of a reference ==, != are
3559             // castable to each other, (JLS 15.21).  Note: unboxing
3560             // comparisons will not have an acmp* opc at this point.
3561             if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) {


3587                 public boolean compatible(Type found, Type req, Warner warn) {
3588                     return types.isCastable(found, req, warn);
3589                 }
3590             });
3591         } else {
3592             //standalone cast - target-type info is not propagated
3593             castInfo = unknownExprInfo;
3594         }
3595         Type exprtype = attribTree(tree.expr, localEnv, castInfo);
3596         Type owntype = isPoly ? clazztype : chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
3597         if (exprtype.constValue() != null)
3598             owntype = cfolder.coerce(exprtype, owntype);
3599         result = check(tree, capture(owntype), KindSelector.VAL, resultInfo);
3600         if (!isPoly)
3601             chk.checkRedundantCast(localEnv, tree);
3602     }
3603 
3604     public void visitTypeTest(JCInstanceOf tree) {
3605         Type exprtype = chk.checkNullOrRefType(
3606                 tree.expr.pos(), attribExpr(tree.expr, env));
3607         Type clazztype = attribType(tree.clazz, env);
3608         if (!clazztype.hasTag(TYPEVAR)) {
3609             clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype);
3610         }
3611         if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) {
3612             log.error(tree.clazz.pos(), Errors.IllegalGenericTypeForInstof);
3613             clazztype = types.createErrorType(clazztype);











3614         }
3615         chk.validate(tree.clazz, env, false);
3616         chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
3617         result = check(tree, syms.booleanType, KindSelector.VAL, resultInfo);
3618     }
3619 















3620     public void visitIndexed(JCArrayAccess tree) {
3621         Type owntype = types.createErrorType(tree.type);
3622         Type atype = attribExpr(tree.indexed, env);
3623         attribExpr(tree.index, env, syms.intType);
3624         if (types.isArray(atype))
3625             owntype = types.elemtype(atype);
3626         else if (!atype.hasTag(ERROR))
3627             log.error(tree.pos(), Errors.ArrayReqButFound(atype));
3628         if (!pkind().contains(KindSelector.VAL))
3629             owntype = capture(owntype);
3630         result = check(tree, owntype, KindSelector.VAR, resultInfo);
3631     }
3632 
3633     public void visitIdent(JCIdent tree) {
3634         Symbol sym;
3635 
3636         // Find symbol
3637         if (pt().hasTag(METHOD) || pt().hasTag(FORALL)) {
3638             // If we are looking for a method, the prototype `pt' will be a
3639             // method type with the type of the call's arguments as parameters.


4990                 scan(tree.defaultValue);
4991                 scan(tree.body);
4992             }
4993         }
4994         public void visitVarDef(final JCVariableDecl tree) {
4995             //System.err.println("validateTypeAnnotations.visitVarDef " + tree);
4996             if (tree.sym != null && tree.sym.type != null && !tree.isImplicitlyTyped())
4997                 validateAnnotatedType(tree.vartype, tree.sym.type);
4998             scan(tree.mods);
4999             scan(tree.vartype);
5000             if (!sigOnly) {
5001                 scan(tree.init);
5002             }
5003         }
5004         public void visitTypeCast(JCTypeCast tree) {
5005             if (tree.clazz != null && tree.clazz.type != null)
5006                 validateAnnotatedType(tree.clazz, tree.clazz.type);
5007             super.visitTypeCast(tree);
5008         }
5009         public void visitTypeTest(JCInstanceOf tree) {
5010             if (tree.clazz != null && tree.clazz.type != null)
5011                 validateAnnotatedType(tree.clazz, tree.clazz.type);
5012             super.visitTypeTest(tree);
5013         }
5014         public void visitNewClass(JCNewClass tree) {
5015             if (tree.clazz != null && tree.clazz.type != null) {
5016                 if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
5017                     checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
5018                             tree.clazz.type.tsym);
5019                 }
5020                 if (tree.def != null) {
5021                     checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym);
5022                 }
5023 
5024                 validateAnnotatedType(tree.clazz, tree.clazz.type);
5025             }
5026             super.visitNewClass(tree);
5027         }
5028         public void visitNewArray(JCNewArray tree) {
5029             if (tree.elemtype != null && tree.elemtype.type != null) {
5030                 if (tree.elemtype.hasTag(ANNOTATED_TYPE)) {
5031                     checkForDeclarationAnnotations(((JCAnnotatedType) tree.elemtype).annotations,


5249         @Override
5250         public void visitMethodDef(JCMethodDecl that) {
5251             initTypeIfNeeded(that);
5252             if (that.sym == null) {
5253                 that.sym = new MethodSymbol(0, that.name, that.type, syms.noSymbol);
5254             }
5255             super.visitMethodDef(that);
5256         }
5257 
5258         @Override
5259         public void visitVarDef(JCVariableDecl that) {
5260             initTypeIfNeeded(that);
5261             if (that.sym == null) {
5262                 that.sym = new VarSymbol(0, that.name, that.type, syms.noSymbol);
5263                 that.sym.adr = 0;
5264             }
5265             if (that.vartype == null) {
5266                 that.vartype = make.at(Position.NOPOS).Erroneous();
5267             }
5268             super.visitVarDef(that);










5269         }
5270 
5271         @Override
5272         public void visitNewClass(JCNewClass that) {
5273             if (that.constructor == null) {
5274                 that.constructor = new MethodSymbol(0, names.init,
5275                         dummyMethodType(), syms.noSymbol);
5276             }
5277             if (that.constructorType == null) {
5278                 that.constructorType = syms.unknownType;
5279             }
5280             super.visitNewClass(that);
5281         }
5282 
5283         @Override
5284         public void visitAssignop(JCAssignOp that) {
5285             if (that.operator == null) {
5286                 that.operator = new OperatorSymbol(names.empty, dummyMethodType(),
5287                         -1, syms.noSymbol);
5288             }




  32 import javax.lang.model.element.ElementKind;
  33 import javax.tools.JavaFileObject;
  34 
  35 import com.sun.source.tree.CaseTree.CaseKind;
  36 import com.sun.source.tree.IdentifierTree;
  37 import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
  38 import com.sun.source.tree.MemberSelectTree;
  39 import com.sun.source.tree.TreeVisitor;
  40 import com.sun.source.util.SimpleTreeVisitor;
  41 import com.sun.tools.javac.code.*;
  42 import com.sun.tools.javac.code.Lint.LintCategory;
  43 import com.sun.tools.javac.code.Scope.WriteableScope;
  44 import com.sun.tools.javac.code.Source.Feature;
  45 import com.sun.tools.javac.code.Symbol.*;
  46 import com.sun.tools.javac.code.Type.*;
  47 import com.sun.tools.javac.code.TypeMetadata.Annotations;
  48 import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
  49 import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
  50 import com.sun.tools.javac.comp.Check.CheckContext;
  51 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
  52 import com.sun.tools.javac.comp.MatchBindingsComputer.BindingSymbol;
  53 import com.sun.tools.javac.jvm.*;
  54 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.Diamond;
  55 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArg;
  56 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArgs;
  57 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  58 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  59 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  60 import com.sun.tools.javac.tree.*;
  61 import com.sun.tools.javac.tree.JCTree.*;
  62 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
  63 import com.sun.tools.javac.util.*;
  64 import com.sun.tools.javac.util.DefinedBy.Api;
  65 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  66 import com.sun.tools.javac.util.JCDiagnostic.Error;
  67 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
  68 import com.sun.tools.javac.util.JCDiagnostic.Warning;
  69 import com.sun.tools.javac.util.List;
  70 
  71 import static com.sun.tools.javac.code.Flags.*;
  72 import static com.sun.tools.javac.code.Flags.ANNOTATION;
  73 import static com.sun.tools.javac.code.Flags.BLOCK;
  74 import static com.sun.tools.javac.code.Kinds.*;
  75 import static com.sun.tools.javac.code.Kinds.Kind.*;
  76 import static com.sun.tools.javac.code.TypeTag.*;
  77 import static com.sun.tools.javac.code.TypeTag.WILDCARD;
  78 import com.sun.tools.javac.comp.Analyzer.AnalyzerMode;
  79 import com.sun.tools.javac.comp.MatchBindingsComputer.BindingSymbol;
  80 import com.sun.tools.javac.tree.JCTree.JCBreak;
  81 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  82 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  83 import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
  84 import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
  85 
  86 /** This is the main context-dependent analysis phase in GJC. It
  87  *  encompasses name resolution, type checking and constant folding as
  88  *  subtasks. Some subtasks involve auxiliary classes.
  89  *  @see Check
  90  *  @see Resolve
  91  *  @see ConstFold
  92  *  @see Infer
  93  *
  94  *  <p><b>This is NOT part of any supported API.
  95  *  If you write code that depends on this, you do so at your own risk.
  96  *  This code and its internal interfaces are subject to change or
  97  *  deletion without notice.</b>
  98  */
  99 public class Attr extends JCTree.Visitor {
 100     protected static final Context.Key<Attr> attrKey = new Context.Key<>();


 106     final Operators operators;
 107     final Infer infer;
 108     final Analyzer analyzer;
 109     final DeferredAttr deferredAttr;
 110     final Check chk;
 111     final Flow flow;
 112     final MemberEnter memberEnter;
 113     final TypeEnter typeEnter;
 114     final TreeMaker make;
 115     final ConstFold cfolder;
 116     final Enter enter;
 117     final Target target;
 118     final Types types;
 119     final JCDiagnostic.Factory diags;
 120     final TypeAnnotations typeAnnotations;
 121     final DeferredLintHandler deferredLintHandler;
 122     final TypeEnvs typeEnvs;
 123     final Dependencies dependencies;
 124     final Annotate annotate;
 125     final ArgumentAttr argumentAttr;
 126     final MatchBindingsComputer matchBindingsComputer;
 127 
 128     public static Attr instance(Context context) {
 129         Attr instance = context.get(attrKey);
 130         if (instance == null)
 131             instance = new Attr(context);
 132         return instance;
 133     }
 134 
 135     protected Attr(Context context) {
 136         context.put(attrKey, this);
 137 
 138         names = Names.instance(context);
 139         log = Log.instance(context);
 140         syms = Symtab.instance(context);
 141         rs = Resolve.instance(context);
 142         operators = Operators.instance(context);
 143         chk = Check.instance(context);
 144         flow = Flow.instance(context);
 145         memberEnter = MemberEnter.instance(context);
 146         typeEnter = TypeEnter.instance(context);
 147         make = TreeMaker.instance(context);
 148         enter = Enter.instance(context);
 149         infer = Infer.instance(context);
 150         analyzer = Analyzer.instance(context);
 151         deferredAttr = DeferredAttr.instance(context);
 152         cfolder = ConstFold.instance(context);
 153         target = Target.instance(context);
 154         types = Types.instance(context);
 155         diags = JCDiagnostic.Factory.instance(context);
 156         annotate = Annotate.instance(context);
 157         typeAnnotations = TypeAnnotations.instance(context);
 158         deferredLintHandler = DeferredLintHandler.instance(context);
 159         typeEnvs = TypeEnvs.instance(context);
 160         dependencies = Dependencies.instance(context);
 161         argumentAttr = ArgumentAttr.instance(context);
 162         matchBindingsComputer = MatchBindingsComputer.instance(context);
 163 
 164         Options options = Options.instance(context);
 165 
 166         Source source = Source.instance(context);
 167         allowPoly = Feature.POLY.allowedInSource(source);
 168         allowTypeAnnos = Feature.TYPE_ANNOTATIONS.allowedInSource(source);
 169         allowLambda = Feature.LAMBDA.allowedInSource(source);
 170         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
 171         allowStaticInterfaceMethods = Feature.STATIC_INTERFACE_METHODS.allowedInSource(source);
 172         sourceName = source.name;
 173         useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
 174 
 175         statInfo = new ResultInfo(KindSelector.NIL, Type.noType);
 176         varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType);
 177         unknownExprInfo = new ResultInfo(KindSelector.VAL, Type.noType);
 178         methodAttrInfo = new MethodAttrInfo();
 179         unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
 180         unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
 181         recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
 182     }


 283     }
 284 
 285     /** Check that variable can be assigned to.
 286      *  @param pos    The current source code position.
 287      *  @param v      The assigned variable
 288      *  @param base   If the variable is referred to in a Select, the part
 289      *                to the left of the `.', null otherwise.
 290      *  @param env    The current environment.
 291      */
 292     void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
 293         if (v.name == names._this) {
 294             log.error(pos, Errors.CantAssignValToThis);
 295         } else if ((v.flags() & FINAL) != 0 &&
 296             ((v.flags() & HASINIT) != 0
 297              ||
 298              !((base == null ||
 299                TreeInfo.isThisQualifier(base)) &&
 300                isAssignableAsBlankFinal(v, env)))) {
 301             if (v.isResourceVariable()) { //TWR resource
 302                 log.error(pos, Errors.TryResourceMayNotBeAssigned(v));
 303             } else if ((v.flags() & MATCH_BINDING) != 0) {
 304                 log.error(pos, Errors.PatternBindingMayNotBeAssigned(v));
 305             } else {
 306                 log.error(pos, Errors.CantAssignValToFinalVar(v));
 307             }
 308         }
 309     }
 310 
 311     /** Does tree represent a static reference to an identifier?
 312      *  It is assumed that tree is either a SELECT or an IDENT.
 313      *  We have to weed out selects from non-type names here.
 314      *  @param tree    The candidate tree.
 315      */
 316     boolean isStaticReference(JCTree tree) {
 317         if (tree.hasTag(SELECT)) {
 318             Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
 319             if (lsym == null || lsym.kind != TYP) {
 320                 return false;
 321             }
 322         }
 323         return true;
 324     }


1309                 } else {
1310                     cs.appendInitTypeAttributes(tas);
1311                 }
1312             }
1313         } else {
1314             // Create a new local environment with a local scope.
1315             Env<AttrContext> localEnv =
1316                 env.dup(tree, env.info.dup(env.info.scope.dup()));
1317             try {
1318                 attribStats(tree.stats, localEnv);
1319             } finally {
1320                 localEnv.info.scope.leave();
1321             }
1322         }
1323         result = null;
1324     }
1325 
1326     public void visitDoLoop(JCDoWhileLoop tree) {
1327         attribStat(tree.body, env.dup(tree));
1328         attribExpr(tree.cond, env, syms.booleanType);
1329         if (!breaksOutOf(tree, tree.body)) {
1330             List<BindingSymbol> bindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
1331 
1332             bindings.forEach(env.info.scope::enter);
1333             bindings.forEach(BindingSymbol::preserveBinding);
1334         }
1335         result = null;
1336     }
1337 
1338     public void visitWhileLoop(JCWhileLoop tree) {
1339         attribExpr(tree.cond, env, syms.booleanType);
1340         // include x.T in while's body
1341         Env<AttrContext> whileEnv = bindingEnv(env, matchBindingsComputer.getMatchBindings(tree.cond, true));
1342         try {
1343             attribStat(tree.body, whileEnv.dup(tree));
1344         } finally {
1345             whileEnv.info.scope.leave();
1346         }
1347         if (!breaksOutOf(tree, tree.body)) {
1348             List<BindingSymbol> bindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
1349 
1350             bindings.forEach(env.info.scope::enter);
1351             bindings.forEach(BindingSymbol::preserveBinding);
1352         }
1353         result = null;
1354     }
1355 
1356     private boolean breaksOutOf(JCTree loop, JCTree body) {
1357         preFlow(body);
1358         return flow.breaksOutOf(env, loop, body, make);
1359     }
1360 
1361     public void visitForLoop(JCForLoop tree) {
1362         Env<AttrContext> loopEnv =
1363             env.dup(env.tree, env.info.dup(env.info.scope.dup()));
1364         try {
1365             attribStats(tree.init, loopEnv);
1366             List<BindingSymbol> matchBindings = List.nil();
1367             if (tree.cond != null) {
1368                 attribExpr(tree.cond, loopEnv, syms.booleanType);
1369                 // include x.T in the evaluation scopes of body & step.
1370                 matchBindings = matchBindingsComputer.getMatchBindings(tree.cond, true);
1371             }
1372             Env<AttrContext> bodyEnv = bindingEnv(loopEnv, matchBindings);
1373             try {
1374                 bodyEnv.tree = tree; // before, we were not in loop!
1375                 attribStats(tree.step, bodyEnv);
1376                 attribStat(tree.body, bodyEnv);
1377             } finally {
1378                 bodyEnv.info.scope.leave();
1379             }
1380             result = null;
1381         }
1382         finally {
1383             loopEnv.info.scope.leave(); // all injected match bindings vanish here.
1384         }
1385         if (!breaksOutOf(tree, tree.body)) {
1386             List<BindingSymbol> bindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
1387 
1388             bindings.forEach(env.info.scope::enter);
1389             bindings.forEach(BindingSymbol::preserveBinding);
1390         }
1391     }
1392 
1393     public void visitForeachLoop(JCEnhancedForLoop tree) {
1394         Env<AttrContext> loopEnv =
1395             env.dup(env.tree, env.info.dup(env.info.scope.dup()));
1396         try {
1397             //the Formal Parameter of a for-each loop is not in the scope when
1398             //attributing the for-each expression; we mimick this by attributing
1399             //the for-each expression first (against original scope).
1400             Type exprType = types.cvarUpperBound(attribExpr(tree.expr, loopEnv));
1401             chk.checkNonVoid(tree.pos(), exprType);
1402             Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
1403             if (elemtype == null) {
1404                 // or perhaps expr implements Iterable<T>?
1405                 Type base = types.asSuper(exprType, syms.iterableType.tsym);
1406                 if (base == null) {
1407                     log.error(tree.expr.pos(),
1408                               Errors.ForeachNotApplicableToType(exprType,
1409                                                                 Fragments.TypeReqArrayOrIterable));


1721 
1722     public void visitConditional(JCConditional tree) {
1723         Type condtype = attribExpr(tree.cond, env, syms.booleanType);
1724 
1725         tree.polyKind = (!allowPoly ||
1726                 pt().hasTag(NONE) && pt() != Type.recoveryType && pt() != Infer.anyPoly ||
1727                 isBooleanOrNumeric(env, tree)) ?
1728                 PolyKind.STANDALONE : PolyKind.POLY;
1729 
1730         if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) {
1731             //this means we are returning a poly conditional from void-compatible lambda expression
1732             resultInfo.checkContext.report(tree, diags.fragment(Fragments.ConditionalTargetCantBeVoid));
1733             result = tree.type = types.createErrorType(resultInfo.pt);
1734             return;
1735         }
1736 
1737         ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ?
1738                 unknownExprInfo :
1739                 resultInfo.dup(conditionalContext(resultInfo.checkContext));
1740 
1741         /*  if e = "x ? y : z", then:
1742                 include x.T in y
1743                 include x.F in z
1744         */
1745 
1746         Type truetype;
1747         Env<AttrContext> trueEnv = bindingEnv(env, matchBindingsComputer.getMatchBindings(tree.cond, true));
1748         try {
1749             truetype = attribTree(tree.truepart, trueEnv, condInfo);
1750         } finally {
1751             trueEnv.info.scope.leave();
1752         }
1753 
1754         Type falsetype;
1755         Env<AttrContext> falseEnv = bindingEnv(env, matchBindingsComputer.getMatchBindings(tree.cond, false));
1756         try {
1757             falsetype = attribTree(tree.falsepart, falseEnv, condInfo);
1758         } finally {
1759             falseEnv.info.scope.leave();
1760         }
1761 
1762         Type owntype = (tree.polyKind == PolyKind.STANDALONE) ?
1763                 condType(List.of(tree.truepart.pos(), tree.falsepart.pos()),
1764                          List.of(truetype, falsetype)) : pt();
1765         if (condtype.constValue() != null &&
1766                 truetype.constValue() != null &&
1767                 falsetype.constValue() != null &&
1768                 !owntype.hasTag(NONE)) {
1769             //constant folding
1770             owntype = cfolder.coerce(condtype.isTrue() ? truetype : falsetype, owntype);
1771         }
1772         result = check(tree, owntype, KindSelector.VAL, resultInfo);
1773     }
1774     //where
1775         private boolean isBooleanOrNumeric(Env<AttrContext> env, JCExpression tree) {
1776             switch (tree.getTag()) {
1777                 case LITERAL: return ((JCLiteral)tree).typetag.isSubRangeOf(DOUBLE) ||
1778                               ((JCLiteral)tree).typetag == BOOLEAN ||
1779                               ((JCLiteral)tree).typetag == BOT;
1780                 case LAMBDA: case REFERENCE: return false;


1895                                  .map(t -> chk.checkNonVoid(posIt.next(), t))
1896                                  .collect(List.collector());
1897 
1898             // both are known to be reference types.  The result is
1899             // lub(thentype,elsetype). This cannot fail, as it will
1900             // always be possible to infer "Object" if nothing better.
1901             return types.lub(condTypes.stream().map(t -> t.baseType()).collect(List.collector()));
1902         }
1903 
1904     final static TypeTag[] primitiveTags = new TypeTag[]{
1905         BYTE,
1906         CHAR,
1907         SHORT,
1908         INT,
1909         LONG,
1910         FLOAT,
1911         DOUBLE,
1912         BOOLEAN,
1913     };
1914 
1915     Env<AttrContext> bindingEnv(Env<AttrContext> env, List<BindingSymbol> bindings) {
1916         Env<AttrContext> env1 = env.dup(env.tree, env.info.dup(env.info.scope.dup()));
1917         bindings.forEach(env1.info.scope::enter);
1918         return env1;
1919     }
1920 
1921     public void visitIf(JCIf tree) {
1922         attribExpr(tree.cond, env, syms.booleanType);
1923 
1924         // if (x) { y } [ else z ] include x.T in y; include x.F in z
1925 
1926         List<BindingSymbol> thenBindings = matchBindingsComputer.getMatchBindings(tree.cond, true);
1927         Env<AttrContext> thenEnv = bindingEnv(env, thenBindings);
1928 
1929         try {
1930             attribStat(tree.thenpart, thenEnv);
1931         } finally {
1932             thenEnv.info.scope.leave();
1933         }
1934 
1935         preFlow(tree.thenpart);
1936         boolean aliveAfterThen = flow.aliveAfter(env, tree.thenpart, make);
1937         boolean aliveAfterElse;
1938         List<BindingSymbol> elseBindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
1939 
1940         if (tree.elsepart != null) {
1941             Env<AttrContext> elseEnv = bindingEnv(env, elseBindings);
1942             try {
1943                 attribStat(tree.elsepart, elseEnv);
1944             } finally {
1945                 elseEnv.info.scope.leave();
1946             }
1947             preFlow(tree.elsepart);
1948             aliveAfterElse = flow.aliveAfter(env, tree.elsepart, make);
1949         } else {
1950             aliveAfterElse = true;
1951         }
1952 
1953         chk.checkEmptyIf(tree);
1954 
1955         List<BindingSymbol> afterIfBindings = List.nil();
1956 
1957         if (aliveAfterThen && !aliveAfterElse) {
1958             afterIfBindings = thenBindings;
1959         } else if (aliveAfterElse && !aliveAfterThen) {
1960             afterIfBindings = elseBindings;
1961         }
1962 
1963         afterIfBindings.forEach(env.info.scope::enter);
1964         afterIfBindings.forEach(BindingSymbol::preserveBinding);
1965 
1966         result = null;
1967     }
1968 
1969         void preFlow(JCTree tree) {
1970             new PostAttrAnalyzer() {
1971                 @Override
1972                 public void scan(JCTree tree) {
1973                     if (tree == null ||
1974                             (tree.type != null &&
1975                             tree.type == Type.stuckType)) {
1976                         //don't touch stuck expressions!
1977                         return;
1978                     }
1979                     super.scan(tree);
1980                 }
1981             }.scan(tree);
1982         }
1983 
1984     public void visitExec(JCExpressionStatement tree) {
1985         //a fresh environment is required for 292 inference to work properly ---
1986         //see Infer.instantiatePolymorphicSignatureInstance()
1987         Env<AttrContext> localEnv = env.dup(tree);
1988         attribExpr(tree.expr, localEnv);
1989         result = null;
1990     }
1991 
1992     public void visitBreak(JCBreak tree) {
1993         tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
1994         result = null;
1995     }
1996 
1997     public void visitYield(JCYield tree) {
1998         if (env.info.yieldResult != null) {
1999             attribTree(tree.value, env, env.info.yieldResult);
2000             tree.target = findJumpTarget(tree.pos(), tree.getTag(), names.empty, env);
2001         } else {
2002             log.error(tree.pos(), tree.value.hasTag(PARENS)
2003                     ? Errors.NoSwitchExpressionQualify


3645                 !argtype.isErroneous()) {
3646             owntype = (tree.getTag().isIncOrDecUnaryOp())
3647                 ? tree.arg.type
3648                 : operator.type.getReturnType();
3649             int opc = ((OperatorSymbol)operator).opcode;
3650 
3651             // If the argument is constant, fold it.
3652             if (argtype.constValue() != null) {
3653                 Type ctype = cfolder.fold1(opc, argtype);
3654                 if (ctype != null) {
3655                     owntype = cfolder.coerce(ctype, owntype);
3656                 }
3657             }
3658         }
3659         result = check(tree, owntype, KindSelector.VAL, resultInfo);
3660     }
3661 
3662     public void visitBinary(JCBinary tree) {
3663         // Attribute arguments.
3664         Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env));
3665         // If e = "x && y", then, include x.T in y;  If e = "x || y", then, include x.F in y
3666         List<BindingSymbol> matchBindings;
3667         switch (tree.getTag()) {
3668             case AND:
3669                 matchBindings = matchBindingsComputer.getMatchBindings(tree.lhs, true);
3670                 break;
3671             case OR:
3672                 matchBindings = matchBindingsComputer.getMatchBindings(tree.lhs, false);
3673                 break;
3674             default:
3675                 matchBindings = List.nil();
3676                 break;
3677         }
3678         Env<AttrContext> rhsEnv = bindingEnv(env, matchBindings);
3679         Type right;
3680         try {
3681             right = chk.checkNonVoid(tree.rhs.pos(), attribExpr(tree.rhs, rhsEnv));
3682         } finally {
3683             rhsEnv.info.scope.leave();
3684         }
3685 
3686         // Find operator.
3687         Symbol operator = tree.operator = operators.resolveBinary(tree, tree.getTag(), left, right);
3688         Type owntype = types.createErrorType(tree.type);
3689         if (operator != operators.noOpSymbol &&
3690                 !left.isErroneous() &&
3691                 !right.isErroneous()) {
3692             owntype = operator.type.getReturnType();
3693             int opc = ((OperatorSymbol)operator).opcode;
3694             // If both arguments are constants, fold them.
3695             if (left.constValue() != null && right.constValue() != null) {
3696                 Type ctype = cfolder.fold2(opc, left, right);
3697                 if (ctype != null) {
3698                     owntype = cfolder.coerce(ctype, owntype);
3699                 }
3700             }
3701 
3702             // Check that argument types of a reference ==, != are
3703             // castable to each other, (JLS 15.21).  Note: unboxing
3704             // comparisons will not have an acmp* opc at this point.
3705             if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) {


3731                 public boolean compatible(Type found, Type req, Warner warn) {
3732                     return types.isCastable(found, req, warn);
3733                 }
3734             });
3735         } else {
3736             //standalone cast - target-type info is not propagated
3737             castInfo = unknownExprInfo;
3738         }
3739         Type exprtype = attribTree(tree.expr, localEnv, castInfo);
3740         Type owntype = isPoly ? clazztype : chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
3741         if (exprtype.constValue() != null)
3742             owntype = cfolder.coerce(exprtype, owntype);
3743         result = check(tree, capture(owntype), KindSelector.VAL, resultInfo);
3744         if (!isPoly)
3745             chk.checkRedundantCast(localEnv, tree);
3746     }
3747 
3748     public void visitTypeTest(JCInstanceOf tree) {
3749         Type exprtype = chk.checkNullOrRefType(
3750                 tree.expr.pos(), attribExpr(tree.expr, env));
3751         Type clazztype;
3752         if (tree.pattern.getTag() == BINDINGPATTERN) {
3753             attribTree(tree.pattern, env, unknownExprInfo);
3754             clazztype = tree.pattern.type;
3755             if (!clazztype.hasTag(TYPEVAR)) {
3756                 JCBindingPattern pattern = (JCBindingPattern) tree.pattern;
3757                 clazztype = chk.checkClassOrArrayType(pattern.vartype.pos(), clazztype);
3758             }
3759         } else {
3760             clazztype = attribType(tree.pattern, env);
3761             if (!clazztype.hasTag(TYPEVAR)) {
3762                 clazztype = chk.checkClassOrArrayType(tree.pattern.pos(), clazztype);
3763             }
3764             if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) {
3765                 log.error(tree.pattern.pos(), Errors.IllegalGenericTypeForInstof);
3766                 clazztype = types.createErrorType(clazztype);
3767             }
3768             chk.validate(tree.pattern, env, false);
3769         }

3770         chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
3771         result = check(tree, syms.booleanType, KindSelector.VAL, resultInfo);
3772     }
3773 
3774     public void visitBindingPattern(JCBindingPattern tree) {
3775         if (tree.vartype != null) {
3776             ResultInfo varInfo = new ResultInfo(KindSelector.TYP, resultInfo.pt, resultInfo.checkContext);
3777             tree.type = attribTree(tree.vartype, env, varInfo);
3778         } else {
3779             tree.type = resultInfo.pt;
3780         }
3781         VarSymbol v = tree.symbol = new BindingSymbol(tree.name, tree.vartype != null ? tree.vartype.type : (tree.type.hasTag(BOT) ? syms.objectType : tree.type), env.info.scope.owner);
3782         if (chk.checkUnique(tree.pos(), v, env.info.scope)) {
3783             chk.checkTransparentVar(tree.pos(), v, env.info.scope);
3784             // env.info.scope.enter(v); // we inject into scopes expressly at various points.
3785         }
3786         result = tree.type;
3787     }
3788 
3789     public void visitIndexed(JCArrayAccess tree) {
3790         Type owntype = types.createErrorType(tree.type);
3791         Type atype = attribExpr(tree.indexed, env);
3792         attribExpr(tree.index, env, syms.intType);
3793         if (types.isArray(atype))
3794             owntype = types.elemtype(atype);
3795         else if (!atype.hasTag(ERROR))
3796             log.error(tree.pos(), Errors.ArrayReqButFound(atype));
3797         if (!pkind().contains(KindSelector.VAL))
3798             owntype = capture(owntype);
3799         result = check(tree, owntype, KindSelector.VAR, resultInfo);
3800     }
3801 
3802     public void visitIdent(JCIdent tree) {
3803         Symbol sym;
3804 
3805         // Find symbol
3806         if (pt().hasTag(METHOD) || pt().hasTag(FORALL)) {
3807             // If we are looking for a method, the prototype `pt' will be a
3808             // method type with the type of the call's arguments as parameters.


5159                 scan(tree.defaultValue);
5160                 scan(tree.body);
5161             }
5162         }
5163         public void visitVarDef(final JCVariableDecl tree) {
5164             //System.err.println("validateTypeAnnotations.visitVarDef " + tree);
5165             if (tree.sym != null && tree.sym.type != null && !tree.isImplicitlyTyped())
5166                 validateAnnotatedType(tree.vartype, tree.sym.type);
5167             scan(tree.mods);
5168             scan(tree.vartype);
5169             if (!sigOnly) {
5170                 scan(tree.init);
5171             }
5172         }
5173         public void visitTypeCast(JCTypeCast tree) {
5174             if (tree.clazz != null && tree.clazz.type != null)
5175                 validateAnnotatedType(tree.clazz, tree.clazz.type);
5176             super.visitTypeCast(tree);
5177         }
5178         public void visitTypeTest(JCInstanceOf tree) {
5179             if (tree.pattern != null && !(tree.pattern instanceof JCPattern) && tree.pattern.type != null)
5180                 validateAnnotatedType(tree.pattern, tree.pattern.type);
5181             super.visitTypeTest(tree);
5182         }
5183         public void visitNewClass(JCNewClass tree) {
5184             if (tree.clazz != null && tree.clazz.type != null) {
5185                 if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
5186                     checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
5187                             tree.clazz.type.tsym);
5188                 }
5189                 if (tree.def != null) {
5190                     checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym);
5191                 }
5192 
5193                 validateAnnotatedType(tree.clazz, tree.clazz.type);
5194             }
5195             super.visitNewClass(tree);
5196         }
5197         public void visitNewArray(JCNewArray tree) {
5198             if (tree.elemtype != null && tree.elemtype.type != null) {
5199                 if (tree.elemtype.hasTag(ANNOTATED_TYPE)) {
5200                     checkForDeclarationAnnotations(((JCAnnotatedType) tree.elemtype).annotations,


5418         @Override
5419         public void visitMethodDef(JCMethodDecl that) {
5420             initTypeIfNeeded(that);
5421             if (that.sym == null) {
5422                 that.sym = new MethodSymbol(0, that.name, that.type, syms.noSymbol);
5423             }
5424             super.visitMethodDef(that);
5425         }
5426 
5427         @Override
5428         public void visitVarDef(JCVariableDecl that) {
5429             initTypeIfNeeded(that);
5430             if (that.sym == null) {
5431                 that.sym = new VarSymbol(0, that.name, that.type, syms.noSymbol);
5432                 that.sym.adr = 0;
5433             }
5434             if (that.vartype == null) {
5435                 that.vartype = make.at(Position.NOPOS).Erroneous();
5436             }
5437             super.visitVarDef(that);
5438         }
5439 
5440         @Override
5441         public void visitBindingPattern(JCBindingPattern that) {
5442             //initTypeIfNeeded(that);
5443             if (that.symbol == null) {
5444                 that.symbol = new BindingSymbol(that.name, that.type, syms.noSymbol);
5445                 that.symbol.adr = 0;
5446             }
5447             super.visitBindingPattern(that);
5448         }
5449 
5450         @Override
5451         public void visitNewClass(JCNewClass that) {
5452             if (that.constructor == null) {
5453                 that.constructor = new MethodSymbol(0, names.init,
5454                         dummyMethodType(), syms.noSymbol);
5455             }
5456             if (that.constructorType == null) {
5457                 that.constructorType = syms.unknownType;
5458             }
5459             super.visitNewClass(that);
5460         }
5461 
5462         @Override
5463         public void visitAssignop(JCAssignOp that) {
5464             if (that.operator == null) {
5465                 that.operator = new OperatorSymbol(names.empty, dummyMethodType(),
5466                         -1, syms.noSymbol);
5467             }


< prev index next >