Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 37-47
Link Here
|
37 |
public class CastExpression extends Expression { |
37 |
public class CastExpression extends Expression { |
38 |
|
38 |
|
39 |
public Expression expression; |
39 |
public Expression expression; |
40 |
public Expression type; |
40 |
public TypeReference type; |
41 |
public TypeBinding expectedType; // when assignment conversion to a given expected type: String s = (String) t; |
41 |
public TypeBinding expectedType; // when assignment conversion to a given expected type: String s = (String) t; |
42 |
|
42 |
|
43 |
//expression.implicitConversion holds the cast for baseType casting |
43 |
//expression.implicitConversion holds the cast for baseType casting |
44 |
public CastExpression(Expression expression, Expression type) { |
44 |
public CastExpression(Expression expression, TypeReference type) { |
45 |
this.expression = expression; |
45 |
this.expression = expression; |
46 |
this.type = type; |
46 |
this.type = type; |
47 |
type.bits |= ASTNode.IgnoreRawTypeCheck; // no need to worry about raw type usage |
47 |
type.bits |= ASTNode.IgnoreRawTypeCheck; // no need to worry about raw type usage |
Lines 476-530
Link Here
|
476 |
this.constant = Constant.NotAConstant; |
476 |
this.constant = Constant.NotAConstant; |
477 |
this.implicitConversion = TypeIds.T_undefined; |
477 |
this.implicitConversion = TypeIds.T_undefined; |
478 |
|
478 |
|
479 |
if ((this.type instanceof TypeReference) || (this.type instanceof NameReference) |
479 |
boolean exprContainCast = false; |
480 |
&& ((this.type.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) == 0) { // no extra parenthesis around type: ((A))exp |
|
|
481 |
|
480 |
|
482 |
boolean exprContainCast = false; |
481 |
TypeBinding castType = this.resolvedType = this.type.resolveType(scope); |
483 |
|
482 |
//expression.setExpectedType(this.resolvedType); // needed in case of generic method invocation |
484 |
TypeBinding castType = this.resolvedType = this.type.resolveType(scope); |
483 |
if (this.expression instanceof CastExpression) { |
485 |
//expression.setExpectedType(this.resolvedType); // needed in case of generic method invocation |
484 |
this.expression.bits |= ASTNode.DisableUnnecessaryCastCheck; |
486 |
if (this.expression instanceof CastExpression) { |
485 |
exprContainCast = true; |
487 |
this.expression.bits |= ASTNode.DisableUnnecessaryCastCheck; |
486 |
} |
488 |
exprContainCast = true; |
487 |
TypeBinding expressionType = this.expression.resolveType(scope); |
489 |
} |
488 |
if (castType != null) { |
490 |
TypeBinding expressionType = this.expression.resolveType(scope); |
489 |
if (expressionType != null) { |
491 |
if (castType != null) { |
490 |
boolean isLegal = checkCastTypesCompatibility(scope, castType, expressionType, this.expression); |
492 |
if (expressionType != null) { |
491 |
if (isLegal) { |
493 |
boolean isLegal = checkCastTypesCompatibility(scope, castType, expressionType, this.expression); |
492 |
this.expression.computeConversion(scope, castType, expressionType); |
494 |
if (isLegal) { |
493 |
if ((this.bits & ASTNode.UnsafeCast) != 0) { // unsafe cast |
495 |
this.expression.computeConversion(scope, castType, expressionType); |
494 |
if (scope.compilerOptions().reportUnavoidableGenericTypeProblems || !this.expression.forcedToBeRaw(scope.referenceContext())) { |
496 |
if ((this.bits & ASTNode.UnsafeCast) != 0) { // unsafe cast |
495 |
scope.problemReporter().unsafeCast(this, scope); |
497 |
if (scope.compilerOptions().reportUnavoidableGenericTypeProblems || !this.expression.forcedToBeRaw(scope.referenceContext())) { |
|
|
498 |
scope.problemReporter().unsafeCast(this, scope); |
499 |
} |
500 |
} else { |
501 |
if (castType.isRawType() && scope.compilerOptions().getSeverity(CompilerOptions.RawTypeReference) != ProblemSeverities.Ignore){ |
502 |
scope.problemReporter().rawTypeReference(this.type, castType); |
503 |
} |
504 |
if ((this.bits & (ASTNode.UnnecessaryCast|ASTNode.DisableUnnecessaryCastCheck)) == ASTNode.UnnecessaryCast) { // unnecessary cast |
505 |
if (!isIndirectlyUsed()) // used for generic type inference or boxing ? |
506 |
scope.problemReporter().unnecessaryCast(this); |
507 |
} |
508 |
} |
496 |
} |
509 |
} else { // illegal cast |
497 |
} else { |
510 |
if ((castType.tagBits & TagBits.HasMissingType) == 0) { // no complaint if secondary error |
498 |
if (castType.isRawType() && scope.compilerOptions().getSeverity(CompilerOptions.RawTypeReference) != ProblemSeverities.Ignore){ |
511 |
scope.problemReporter().typeCastError(this, castType, expressionType); |
499 |
scope.problemReporter().rawTypeReference(this.type, castType); |
|
|
500 |
} |
501 |
if ((this.bits & (ASTNode.UnnecessaryCast|ASTNode.DisableUnnecessaryCastCheck)) == ASTNode.UnnecessaryCast) { // unnecessary cast |
502 |
if (!isIndirectlyUsed()) // used for generic type inference or boxing ? |
503 |
scope.problemReporter().unnecessaryCast(this); |
512 |
} |
504 |
} |
513 |
this.bits |= ASTNode.DisableUnnecessaryCastCheck; // disable further secondary diagnosis |
|
|
514 |
} |
505 |
} |
|
|
506 |
} else { // illegal cast |
507 |
if ((castType.tagBits & TagBits.HasMissingType) == 0) { // no complaint if secondary error |
508 |
scope.problemReporter().typeCastError(this, castType, expressionType); |
509 |
} |
510 |
this.bits |= ASTNode.DisableUnnecessaryCastCheck; // disable further secondary diagnosis |
515 |
} |
511 |
} |
516 |
this.resolvedType = castType.capture(scope, this.sourceEnd); |
|
|
517 |
if (exprContainCast) { |
518 |
checkNeedForCastCast(scope, this); |
519 |
} |
520 |
} |
512 |
} |
521 |
return this.resolvedType; |
513 |
this.resolvedType = castType.capture(scope, this.sourceEnd); |
522 |
} else { // expression as a cast |
514 |
if (exprContainCast) { |
523 |
TypeBinding expressionType = this.expression.resolveType(scope); |
515 |
checkNeedForCastCast(scope, this); |
524 |
if (expressionType == null) return null; |
516 |
} |
525 |
scope.problemReporter().invalidTypeReference(this.type); |
|
|
526 |
return null; |
527 |
} |
517 |
} |
|
|
518 |
return this.resolvedType; |
528 |
} |
519 |
} |
529 |
|
520 |
|
530 |
/** |
521 |
/** |