/** Check that a given type can be cast to a given target type. * Return the result of the cast. * @param pos Position to be used for error reporting. * @param found The type that is being cast. * @param req The target type of the cast. */ Type checkCastable(DiagnosticPosition pos, Type found, Type req) { return checkCastable(pos, found, req, basicHandler); } Type checkCastable(DiagnosticPosition pos, Type found, Type req, CheckContext checkContext) {
/** Check that a given type can be cast to a given target type. * Return the result of the cast. * @param pos Position to be used for error reporting. * @param found The type that is being cast. * @param req The target type of the cast. */ Type checkCastable(DiagnosticPosition pos, Type found, Type req) { return checkCastable(pos, found, req, basicHandler); } Type checkCastable(DiagnosticPosition pos, Type found, Type req, CheckContext checkContext) {
public void visitTypeCast(JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null) owntype = cfolder.coerce(exprtype, owntype); result = check(tree, capture(owntype), VAL, pkind, pt); }
public void visitTypeCast(JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null) owntype = cfolder.coerce(exprtype, owntype); result = check(tree, capture(owntype), VAL, pkind, pt); }
public void visitTypeTest(JCInstanceOf tree) { Type exprtype = chk.checkNullOrRefType( tree.expr.pos(), attribExpr(tree.expr, env)); Type clazztype = chk.checkReifiableReferenceType( tree.clazz.pos(), attribType(tree.clazz, env)); chk.checkCastable(tree.expr.pos(), exprtype, clazztype); result = check(tree, syms.booleanType, VAL, pkind, pt); }
public void visitTypeTest(JCInstanceOf tree) { Type exprtype = chk.checkNullOrRefType( tree.expr.pos(), attribExpr(tree.expr, env)); Type clazztype = chk.checkReifiableReferenceType( tree.clazz.pos(), attribType(tree.clazz, env)); chk.checkCastable(tree.expr.pos(), exprtype, clazztype); result = check(tree, syms.booleanType, VAL, pkind, pt); }
public void visitTypeTest(JCInstanceOf tree) { Type exprtype = chk.checkNullOrRefType( tree.expr.pos(), attribExpr(tree.expr, env)); Type clazztype = attribType(tree.clazz, env); if (!clazztype.hasTag(TYPEVAR)) { clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype); } if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) { log.error(tree.clazz.pos(), "illegal.generic.type.for.instof"); clazztype = types.createErrorType(clazztype); } chk.validate(tree.clazz, env, false); chk.checkCastable(tree.expr.pos(), exprtype, clazztype); result = check(tree, syms.booleanType, VAL, resultInfo); }
public void visitTypeTest(JCInstanceOf tree) { Type exprtype = chk.checkNullOrRefType( tree.expr.pos(), attribExpr(tree.expr, env)); Type clazztype = attribType(tree.clazz, env); if (!clazztype.hasTag(TYPEVAR)) { clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype); } if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) { log.error(tree.clazz.pos(), "illegal.generic.type.for.instof"); clazztype = types.createErrorType(clazztype); } chk.validate(tree.clazz, env, false); chk.checkCastable(tree.expr.pos(), exprtype, clazztype); result = check(tree, syms.booleanType, VAL, resultInfo); }
public void visitTypeCast(final JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); chk.validate(tree.clazz, env, false); //a fresh environment is required for 292 inference to work properly --- //see Infer.instantiatePolymorphicSignatureInstance() Env<AttrContext> localEnv = env.dup(tree); //should we propagate the target type? final ResultInfo castInfo; JCExpression expr = TreeInfo.skipParens(tree.expr); boolean isPoly = allowPoly && (expr.hasTag(LAMBDA) || expr.hasTag(REFERENCE)); if (isPoly) { //expression is a poly - we need to propagate target type info castInfo = new ResultInfo(VAL, clazztype, new Check.NestedCheckContext(resultInfo.checkContext) { @Override public boolean compatible(Type found, Type req, Warner warn) { return types.isCastable(found, req, warn); } }); } else { //standalone cast - target-type info is not propagated castInfo = unknownExprInfo; } Type exprtype = attribTree(tree.expr, localEnv, castInfo); Type owntype = isPoly ? clazztype : chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null) owntype = cfolder.coerce(exprtype, owntype); result = check(tree, capture(owntype), VAL, resultInfo); if (!isPoly) chk.checkRedundantCast(localEnv, tree); }
public void visitTypeCast(final JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); chk.validate(tree.clazz, env, false); //a fresh environment is required for 292 inference to work properly --- //see Infer.instantiatePolymorphicSignatureInstance() Env<AttrContext> localEnv = env.dup(tree); //should we propagate the target type? final ResultInfo castInfo; JCExpression expr = TreeInfo.skipParens(tree.expr); boolean isPoly = allowPoly && (expr.hasTag(LAMBDA) || expr.hasTag(REFERENCE)); if (isPoly) { //expression is a poly - we need to propagate target type info castInfo = new ResultInfo(VAL, clazztype, new Check.NestedCheckContext(resultInfo.checkContext) { @Override public boolean compatible(Type found, Type req, Warner warn) { return types.isCastable(found, req, warn); } }); } else { //standalone cast - target-type info is not propagated castInfo = unknownExprInfo; } Type exprtype = attribTree(tree.expr, localEnv, castInfo); Type owntype = isPoly ? clazztype : chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null) owntype = cfolder.coerce(exprtype, owntype); result = check(tree, capture(owntype), VAL, resultInfo); if (!isPoly) chk.checkRedundantCast(localEnv, tree); }
public void visitAssignop(JCAssignOp tree) { // Attribute arguments. Type owntype = attribTree(tree.lhs, env, VAR, Type.noType); Type operand = attribExpr(tree.rhs, env); // Find operator. Symbol operator = tree.operator = rs.resolveBinaryOperator( tree.pos(), tree.tag - JCTree.ASGOffset, env, owntype, operand); if (operator.kind == MTH) { chk.checkOperator(tree.pos(), (OperatorSymbol)operator, tree.tag - JCTree.ASGOffset, owntype, operand); if (types.isSameType(operator.type.getReturnType(), syms.stringType)) { // String assignment; make sure the lhs is a string chk.checkType(tree.lhs.pos(), owntype, syms.stringType); } else { chk.checkDivZero(tree.rhs.pos(), operator, operand); chk.checkCastable(tree.rhs.pos(), operator.type.getReturnType(), owntype); } } result = check(tree, owntype, VAL, pkind, pt); }
public void visitAssignop(JCAssignOp tree) { // Attribute arguments. Type owntype = attribTree(tree.lhs, env, varInfo); Type operand = attribExpr(tree.rhs, env); // Find operator. Symbol operator = tree.operator = rs.resolveBinaryOperator( tree.pos(), tree.getTag().noAssignOp(), env, owntype, operand); if (operator.kind == MTH && !owntype.isErroneous() && !operand.isErroneous()) { chk.checkOperator(tree.pos(), (OperatorSymbol)operator, tree.getTag().noAssignOp(), owntype, operand); chk.checkDivZero(tree.rhs.pos(), operator, operand); chk.checkCastable(tree.rhs.pos(), operator.type.getReturnType(), owntype); } result = check(tree, owntype, VAL, resultInfo); }
public void visitAssignop(JCAssignOp tree) { // Attribute arguments. Type owntype = attribTree(tree.lhs, env, varInfo); Type operand = attribExpr(tree.rhs, env); // Find operator. Symbol operator = tree.operator = rs.resolveBinaryOperator( tree.pos(), tree.getTag().noAssignOp(), env, owntype, operand); if (operator.kind == MTH && !owntype.isErroneous() && !operand.isErroneous()) { chk.checkOperator(tree.pos(), (OperatorSymbol)operator, tree.getTag().noAssignOp(), owntype, operand); chk.checkDivZero(tree.rhs.pos(), operator, operand); chk.checkCastable(tree.rhs.pos(), operator.type.getReturnType(), owntype); } result = check(tree, owntype, VAL, resultInfo); }
public void visitAssignop(JCAssignOp tree) { // Attribute arguments. Type owntype = attribTree(tree.lhs, env, VAR, Type.noType); Type operand = attribExpr(tree.rhs, env); // Find operator. Symbol operator = tree.operator = rs.resolveBinaryOperator( tree.pos(), tree.getTag() - JCTree.ASGOffset, env, owntype, operand); if (operator.kind == MTH) { chk.checkOperator(tree.pos(), (OperatorSymbol)operator, tree.getTag() - JCTree.ASGOffset, owntype, operand); if (types.isSameType(operator.type.getReturnType(), syms.stringType)) { // String assignment; make sure the lhs is a string chk.checkType(tree.lhs.pos(), owntype, syms.stringType); } else { chk.checkDivZero(tree.rhs.pos(), operator, operand); chk.checkCastable(tree.rhs.pos(), operator.type.getReturnType(), owntype); } } result = check(tree, owntype, VAL, pkind, pt); }