Changeset View
Changeset View
Standalone View
Standalone View
cfe/trunk/lib/Sema/SemaCast.cpp
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | struct CastOperation { | ||||
// Top-level semantics-checking routines. | // Top-level semantics-checking routines. | ||||
void CheckConstCast(); | void CheckConstCast(); | ||||
void CheckReinterpretCast(); | void CheckReinterpretCast(); | ||||
void CheckStaticCast(); | void CheckStaticCast(); | ||||
void CheckDynamicCast(); | void CheckDynamicCast(); | ||||
void CheckCXXCStyleCast(bool FunctionalCast, bool ListInitialization); | void CheckCXXCStyleCast(bool FunctionalCast, bool ListInitialization); | ||||
void CheckCStyleCast(); | void CheckCStyleCast(); | ||||
void CheckBuiltinBitCast(); | |||||
void updatePartOfExplicitCastFlags(CastExpr *CE) { | void updatePartOfExplicitCastFlags(CastExpr *CE) { | ||||
// Walk down from the CE to the OrigSrcExpr, and mark all immediate | // Walk down from the CE to the OrigSrcExpr, and mark all immediate | ||||
// ImplicitCastExpr's as being part of ExplicitCastExpr. The original CE | // ImplicitCastExpr's as being part of ExplicitCastExpr. The original CE | ||||
// (which is a ExplicitCastExpr), and the OrigSrcExpr are not touched. | // (which is a ExplicitCastExpr), and the OrigSrcExpr are not touched. | ||||
for (; auto *ICE = dyn_cast<ImplicitCastExpr>(CE->getSubExpr()); CE = ICE) | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(CE->getSubExpr()); CE = ICE) | ||||
ICE->setIsPartOfExplicitCast(true); | ICE->setIsPartOfExplicitCast(true); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 228 Lines • ▼ Show 20 Lines | return Op.complete(CXXStaticCastExpr::Create(Context, Op.ResultType, | ||||
Op.ValueKind, Op.Kind, Op.SrcExpr.get(), | Op.ValueKind, Op.Kind, Op.SrcExpr.get(), | ||||
&Op.BasePath, DestTInfo, | &Op.BasePath, DestTInfo, | ||||
OpLoc, Parens.getEnd(), | OpLoc, Parens.getEnd(), | ||||
AngleBrackets)); | AngleBrackets)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
ExprResult Sema::ActOnBuiltinBitCastExpr(SourceLocation KWLoc, Declarator &D, | |||||
ExprResult Operand, | |||||
SourceLocation RParenLoc) { | |||||
assert(!D.isInvalidType()); | |||||
TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, Operand.get()->getType()); | |||||
if (D.isInvalidType()) | |||||
return ExprError(); | |||||
return BuildBuiltinBitCastExpr(KWLoc, TInfo, Operand.get(), RParenLoc); | |||||
} | |||||
ExprResult Sema::BuildBuiltinBitCastExpr(SourceLocation KWLoc, | |||||
TypeSourceInfo *TSI, Expr *Operand, | |||||
SourceLocation RParenLoc) { | |||||
CastOperation Op(*this, TSI->getType(), Operand); | |||||
Op.OpRange = SourceRange(KWLoc, RParenLoc); | |||||
TypeLoc TL = TSI->getTypeLoc(); | |||||
Op.DestRange = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); | |||||
if (!Operand->isTypeDependent() && !TSI->getType()->isDependentType()) { | |||||
Op.CheckBuiltinBitCast(); | |||||
if (Op.SrcExpr.isInvalid()) | |||||
return ExprError(); | |||||
} | |||||
BuiltinBitCastExpr *BCE = | |||||
new (Context) BuiltinBitCastExpr(Op.ResultType, Op.ValueKind, Op.Kind, | |||||
Op.SrcExpr.get(), TSI, KWLoc, RParenLoc); | |||||
return Op.complete(BCE); | |||||
} | |||||
/// Try to diagnose a failed overloaded cast. Returns true if | /// Try to diagnose a failed overloaded cast. Returns true if | ||||
/// diagnostics were emitted. | /// diagnostics were emitted. | ||||
static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT, | static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT, | ||||
SourceRange range, Expr *src, | SourceRange range, Expr *src, | ||||
QualType destType, | QualType destType, | ||||
bool listInitialization) { | bool listInitialization) { | ||||
switch (CT) { | switch (CT) { | ||||
// These cast kinds don't consider user-defined conversions. | // These cast kinds don't consider user-defined conversions. | ||||
▲ Show 20 Lines • Show All 2,417 Lines • ▼ Show 20 Lines | void CastOperation::CheckCStyleCast() { | ||||
Kind = Self.PrepareScalarCast(SrcExpr, DestType); | Kind = Self.PrepareScalarCast(SrcExpr, DestType); | ||||
if (SrcExpr.isInvalid()) | if (SrcExpr.isInvalid()) | ||||
return; | return; | ||||
if (Kind == CK_BitCast) | if (Kind == CK_BitCast) | ||||
checkCastAlign(); | checkCastAlign(); | ||||
} | } | ||||
void CastOperation::CheckBuiltinBitCast() { | |||||
QualType SrcType = SrcExpr.get()->getType(); | |||||
if (SrcExpr.get()->isRValue()) | |||||
SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcType, SrcExpr.get(), | |||||
/*IsLValueReference=*/false); | |||||
CharUnits DestSize = Self.Context.getTypeSizeInChars(DestType); | |||||
CharUnits SourceSize = Self.Context.getTypeSizeInChars(SrcType); | |||||
if (DestSize != SourceSize) { | |||||
Self.Diag(OpRange.getBegin(), diag::err_bit_cast_type_size_mismatch) | |||||
<< (int)SourceSize.getQuantity() << (int)DestSize.getQuantity(); | |||||
SrcExpr = ExprError(); | |||||
return; | |||||
} | |||||
if (!DestType.isTriviallyCopyableType(Self.Context)) { | |||||
Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable) | |||||
<< 1; | |||||
SrcExpr = ExprError(); | |||||
return; | |||||
} | |||||
if (!SrcType.isTriviallyCopyableType(Self.Context)) { | |||||
Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable) | |||||
<< 0; | |||||
SrcExpr = ExprError(); | |||||
return; | |||||
} | |||||
if (Self.Context.hasSameUnqualifiedType(DestType, SrcType)) { | |||||
Kind = CK_NoOp; | |||||
return; | |||||
} | |||||
Kind = CK_LValueToRValueBitCast; | |||||
} | |||||
/// DiagnoseCastQual - Warn whenever casts discards a qualifiers, be it either | /// DiagnoseCastQual - Warn whenever casts discards a qualifiers, be it either | ||||
/// const, volatile or both. | /// const, volatile or both. | ||||
static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr, | static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr, | ||||
QualType DestType) { | QualType DestType) { | ||||
if (SrcExpr.isInvalid()) | if (SrcExpr.isInvalid()) | ||||
return; | return; | ||||
QualType SrcType = SrcExpr.get()->getType(); | QualType SrcType = SrcExpr.get()->getType(); | ||||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |