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

Print this page




 227              &&
 228              v.owner == owner.owner
 229              &&
 230              ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
 231     }
 232 
 233     /** Check that variable can be assigned to.
 234      *  @param pos    The current source code position.
 235      *  @param v      The assigned varaible
 236      *  @param base   If the variable is referred to in a Select, the part
 237      *                to the left of the `.', null otherwise.
 238      *  @param env    The current environment.
 239      */
 240     void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
 241         if ((v.flags() & FINAL) != 0 &&
 242             ((v.flags() & HASINIT) != 0
 243              ||
 244              !((base == null ||
 245                (base.getTag() == JCTree.IDENT && TreeInfo.name(base) == names._this)) &&
 246                isAssignableAsBlankFinal(v, env)))) {



 247             log.error(pos, "cant.assign.val.to.final.var", v);
 248         }
 249     }

 250 
 251     /** Does tree represent a static reference to an identifier?
 252      *  It is assumed that tree is either a SELECT or an IDENT.
 253      *  We have to weed out selects from non-type names here.
 254      *  @param tree    The candidate tree.
 255      */
 256     boolean isStaticReference(JCTree tree) {
 257         if (tree.getTag() == JCTree.SELECT) {
 258             Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
 259             if (lsym == null || lsym.kind != TYP) {
 260                 return false;
 261             }
 262         }
 263         return true;
 264     }
 265 
 266     /** Is this symbol a type?
 267      */
 268     static boolean isType(Symbol sym) {
 269         return sym != null && sym.kind == TYP;


 964         for (Scope.Entry e = enumType.tsym.members().lookup(name);
 965              e.scope != null; e = e.next()) {
 966             if (e.sym.kind == VAR) {
 967                 Symbol s = ident.sym = e.sym;
 968                 ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
 969                 ident.type = s.type;
 970                 return ((s.flags_field & Flags.ENUM) == 0)
 971                     ? null : s;
 972             }
 973         }
 974         return null;
 975     }
 976 
 977     public void visitSynchronized(JCSynchronized tree) {
 978         chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
 979         attribStat(tree.body, env);
 980         result = null;
 981     }
 982 
 983     public void visitTry(JCTry tree) {













 984         // Attribute body
 985         attribStat(tree.body, env.dup(tree, env.info.dup()));






 986 
 987         // Attribute catch clauses
 988         for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
 989             JCCatch c = l.head;
 990             Env<AttrContext> catchEnv =
 991                 env.dup(c, env.info.dup(env.info.scope.dup()));
 992             Type ctype = attribStat(c.param, catchEnv);
 993             if (TreeInfo.isMultiCatch(c)) {
 994                 //check that multi-catch parameter is marked as final
 995                 if ((c.param.sym.flags() & FINAL) == 0) {
 996                     log.error(c.param.pos(), "multicatch.param.must.be.final", c.param.sym);
 997                 }
 998                 c.param.sym.flags_field = c.param.sym.flags() | DISJOINT;
 999             }
1000             if (c.param.type.tsym.kind == Kinds.VAR) {
1001                 c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER);
1002             }
1003             chk.checkType(c.param.vartype.pos(),
1004                           chk.checkClassType(c.param.vartype.pos(), ctype),
1005                           syms.throwableType);
1006             attribStat(c.body, catchEnv);
1007             catchEnv.info.scope.leave();
1008         }
1009 
1010         // Attribute finalizer
1011         if (tree.finalizer != null) attribStat(tree.finalizer, env);


1012         result = null;
1013     }
1014 
1015     public void visitConditional(JCConditional tree) {
1016         attribExpr(tree.cond, env, syms.booleanType);
1017         attribExpr(tree.truepart, env);
1018         attribExpr(tree.falsepart, env);
1019         result = check(tree,
1020                        capture(condType(tree.pos(), tree.cond.type,
1021                                         tree.truepart.type, tree.falsepart.type)),
1022                        VAL, pkind, pt);
1023     }
1024     //where
1025         /** Compute the type of a conditional expression, after
1026          *  checking that it exists. See Spec 15.25.
1027          *
1028          *  @param pos      The source position to be used for
1029          *                  error diagnostics.
1030          *  @param condtype The type of the expression's condition.
1031          *  @param thentype The type of the expression's then-part.




 227              &&
 228              v.owner == owner.owner
 229              &&
 230              ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
 231     }
 232 
 233     /** Check that variable can be assigned to.
 234      *  @param pos    The current source code position.
 235      *  @param v      The assigned varaible
 236      *  @param base   If the variable is referred to in a Select, the part
 237      *                to the left of the `.', null otherwise.
 238      *  @param env    The current environment.
 239      */
 240     void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
 241         if ((v.flags() & FINAL) != 0 &&
 242             ((v.flags() & HASINIT) != 0
 243              ||
 244              !((base == null ||
 245                (base.getTag() == JCTree.IDENT && TreeInfo.name(base) == names._this)) &&
 246                isAssignableAsBlankFinal(v, env)))) {
 247             if (types.asSuper(v.type, syms.autoCloseableType.tsym) != null) { //ARM resource
 248                 log.error(pos, "arm.resource.may.not.be.assigned", v);
 249             } else {
 250                 log.error(pos, "cant.assign.val.to.final.var", v);
 251             }
 252         }
 253     }
 254 
 255     /** Does tree represent a static reference to an identifier?
 256      *  It is assumed that tree is either a SELECT or an IDENT.
 257      *  We have to weed out selects from non-type names here.
 258      *  @param tree    The candidate tree.
 259      */
 260     boolean isStaticReference(JCTree tree) {
 261         if (tree.getTag() == JCTree.SELECT) {
 262             Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
 263             if (lsym == null || lsym.kind != TYP) {
 264                 return false;
 265             }
 266         }
 267         return true;
 268     }
 269 
 270     /** Is this symbol a type?
 271      */
 272     static boolean isType(Symbol sym) {
 273         return sym != null && sym.kind == TYP;


 968         for (Scope.Entry e = enumType.tsym.members().lookup(name);
 969              e.scope != null; e = e.next()) {
 970             if (e.sym.kind == VAR) {
 971                 Symbol s = ident.sym = e.sym;
 972                 ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
 973                 ident.type = s.type;
 974                 return ((s.flags_field & Flags.ENUM) == 0)
 975                     ? null : s;
 976             }
 977         }
 978         return null;
 979     }
 980 
 981     public void visitSynchronized(JCSynchronized tree) {
 982         chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
 983         attribStat(tree.body, env);
 984         result = null;
 985     }
 986 
 987     public void visitTry(JCTry tree) {
 988         // Create a new local environment with a local scope.
 989         Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
 990         // Attribute resource declarations
 991         ListBuffer<VarSymbol> resourceVars = ListBuffer.lb();
 992         for (JCTree resource : tree.resources) {
 993             attribExpr(resource, localEnv);
 994             chk.checkArmResource(resource);
 995             if (resource.getTag() == JCTree.VARDEF) {
 996                 VarSymbol var = (VarSymbol)TreeInfo.symbolFor(resource);
 997                 var.setData(ElementKind.RESOURCE_VARIABLE);
 998                 resourceVars.append(var);
 999             }
1000         }
1001         // Attribute body
1002         attribStat(tree.body, localEnv.dup(tree, localEnv.info.dup()));
1003 
1004         //remove arm resource vars from local scope - such variables cannot be
1005         //accessed from catch/finally clauses
1006         for (VarSymbol resourceVar : resourceVars) {
1007             localEnv.info.scope.remove(resourceVar);
1008         }
1009 
1010         // Attribute catch clauses
1011         for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
1012             JCCatch c = l.head;
1013             Env<AttrContext> catchEnv =
1014                 localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup()));
1015             Type ctype = attribStat(c.param, catchEnv);
1016             if (TreeInfo.isMultiCatch(c)) {
1017                 //check that multi-catch parameter is marked as final
1018                 if ((c.param.sym.flags() & FINAL) == 0) {
1019                     log.error(c.param.pos(), "multicatch.param.must.be.final", c.param.sym);
1020                 }
1021                 c.param.sym.flags_field = c.param.sym.flags() | DISJOINT;
1022             }
1023             if (c.param.type.tsym.kind == Kinds.VAR) {
1024                 c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER);
1025             }
1026             chk.checkType(c.param.vartype.pos(),
1027                           chk.checkClassType(c.param.vartype.pos(), ctype),
1028                           syms.throwableType);
1029             attribStat(c.body, catchEnv);
1030             catchEnv.info.scope.leave();
1031         }
1032 
1033         // Attribute finalizer
1034         if (tree.finalizer != null) attribStat(tree.finalizer, localEnv);
1035 
1036         localEnv.info.scope.leave();
1037         result = null;
1038     }
1039 
1040     public void visitConditional(JCConditional tree) {
1041         attribExpr(tree.cond, env, syms.booleanType);
1042         attribExpr(tree.truepart, env);
1043         attribExpr(tree.falsepart, env);
1044         result = check(tree,
1045                        capture(condType(tree.pos(), tree.cond.type,
1046                                         tree.truepart.type, tree.falsepart.type)),
1047                        VAL, pkind, pt);
1048     }
1049     //where
1050         /** Compute the type of a conditional expression, after
1051          *  checking that it exists. See Spec 15.25.
1052          *
1053          *  @param pos      The source position to be used for
1054          *                  error diagnostics.
1055          *  @param condtype The type of the expression's condition.
1056          *  @param thentype The type of the expression's then-part.