clang API Documentation
#include "clang/Sema/SemaInternal.h"#include "clang/Sema/Initialization.h"#include "clang/AST/ExprCXX.h"#include "clang/AST/ExprObjC.h"#include "clang/AST/ASTContext.h"#include "clang/AST/CXXInheritance.h"#include "clang/Basic/PartialDiagnostic.h"#include "llvm/ADT/SmallVector.h"#include <set>
Go to the source code of this file.
Enumerations | |
| enum | TryCastResult { TC_NotApplicable, TC_Success, TC_Failed } |
| enum | CastType { CT_Const, CT_Static, CT_Reinterpret, CT_Dynamic, CT_CStyle, CT_Functional } |
Functions | |
| static bool | CastsAwayConstness (Sema &Self, QualType SrcType, QualType DestType, bool CheckCVR, bool CheckObjCLifetime) |
| static TryCastResult | TryLValueToRValueCast (Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, CastKind &Kind, CXXCastPath &BasePath, unsigned &msg) |
| Tests whether a conversion according to N2844 is valid. | |
| static TryCastResult | TryStaticReferenceDowncast (Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, const SourceRange &OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath) |
| Tests whether a conversion according to C++ 5.2.9p5 is valid. | |
| static TryCastResult | TryStaticPointerDowncast (Sema &Self, QualType SrcType, QualType DestType, bool CStyle, const SourceRange &OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath) |
| Tests whether a conversion according to C++ 5.2.9p8 is valid. | |
| static TryCastResult | TryStaticDowncast (Sema &Self, CanQualType SrcType, CanQualType DestType, bool CStyle, const SourceRange &OpRange, QualType OrigSrcType, QualType OrigDestType, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath) |
| static TryCastResult | TryStaticMemberPointerUpcast (Sema &Self, ExprResult &SrcExpr, QualType SrcType, QualType DestType, bool CStyle, const SourceRange &OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath) |
| static TryCastResult | TryStaticImplicitCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, Sema::CheckedConversionKind CCK, const SourceRange &OpRange, unsigned &msg, CastKind &Kind) |
| static TryCastResult | TryStaticCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, Sema::CheckedConversionKind CCK, const SourceRange &OpRange, unsigned &msg, CastKind &Kind, CXXCastPath &BasePath) |
| static TryCastResult | TryConstCast (Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, unsigned &msg) |
| static TryCastResult | TryReinterpretCast (Sema &Self, ExprResult &SrcExpr, QualType DestType, bool CStyle, const SourceRange &OpRange, unsigned &msg, CastKind &Kind) |
| static bool | tryDiagnoseOverloadedCast (Sema &S, CastType CT, SourceRange range, Expr *src, QualType destType) |
| static void | diagnoseBadCast (Sema &S, unsigned msg, CastType castType, SourceRange opRange, Expr *src, QualType destType) |
| Diagnose a failed cast. | |
| static bool | UnwrapDissimilarPointerTypes (QualType &T1, QualType &T2) |
| enum CastType |
| CT_Const |
const_cast |
| CT_Static |
static_cast |
| CT_Reinterpret |
reinterpret_cast |
| CT_Dynamic |
dynamic_cast |
| CT_CStyle |
(Type)expr |
| CT_Functional |
Type(expr). |
Definition at line 34 of file SemaCXXCast.cpp.
| enum TryCastResult |
| TC_NotApplicable |
The cast method is not applicable. |
| TC_Success |
The cast method is appropriate and successful. |
| TC_Failed |
The cast method is appropriate, but failed. A diagnostic has been emitted. |
Definition at line 27 of file SemaCXXCast.cpp.
| static bool CastsAwayConstness | ( | Sema & | Self, | |
| QualType | SrcType, | |||
| QualType | DestType, | |||
| bool | CheckCVR, | |||
| bool | CheckObjCLifetime | |||
| ) | [static] |
CastsAwayConstness - Check if the pointer conversion from SrcType to DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by the cast checkers. Both arguments must denote pointer (possibly to member) types.
| CheckCVR | Whether to check for const/volatile/restrict qualifiers. | |
| CheckObjCLifetime | Whether to check Objective-C lifetime qualifiers. |
Definition at line 439 of file SemaCXXCast.cpp.
References clang::Qualifiers::compatiblyIncludesObjCLifetime(), clang::Sema::Context, clang::ASTContext::getCanonicalType(), clang::Qualifiers::getCVRQualifiers(), clang::ASTContext::getLangOptions(), clang::ASTContext::getPointerType(), clang::ASTContext::getQualifiedType(), clang::ASTContext::getUnqualifiedArrayType(), clang::Sema::IsQualificationConversion(), clang::Qualifiers::setCVRQualifiers(), UnwrapDissimilarPointerTypes(), and clang::ASTContext::VoidTy.
Referenced by TryReinterpretCast().
| static void diagnoseBadCast | ( | Sema & | S, | |
| unsigned | msg, | |||
| CastType | castType, | |||
| SourceRange | opRange, | |||
| Expr * | src, | |||
| QualType | destType | |||
| ) | [static] |
Diagnose a failed cast.
Definition at line 359 of file SemaCXXCast.cpp.
References clang::ASTContext::BoundMemberTy, clang::Sema::CheckPlaceholderExpr(), clang::Sema::Context, clang::Sema::Diag(), clang::SourceRange::getBegin(), clang::Stmt::getSourceRange(), clang::Expr::getType(), and tryDiagnoseOverloadedCast().
| static TryCastResult TryConstCast | ( | Sema & | Self, | |
| Expr * | SrcExpr, | |||
| QualType | DestType, | |||
| bool | CStyle, | |||
| unsigned & | msg | |||
| ) | [static] |
TryConstCast - See if a const_cast from source to destination is allowed, and perform it if it is.
Definition at line 1317 of file SemaCXXCast.cpp.
References clang::Sema::Context, clang::ASTContext::getCanonicalType(), clang::ASTContext::getPointerType(), clang::Expr::getType(), clang::ASTContext::getUnqualifiedArrayType(), clang::Expr::isLValue(), clang::Qualifiers::removeCVRQualifiers(), TC_NotApplicable, TC_Success, and clang::ASTContext::UnwrapSimilarPointerTypes().
| static bool tryDiagnoseOverloadedCast | ( | Sema & | S, | |
| CastType | CT, | |||
| SourceRange | range, | |||
| Expr * | src, | |||
| QualType | destType | |||
| ) | [static] |
Try to diagnose a failed overloaded cast. Returns true if diagnostics were emitted.
Definition at line 284 of file SemaCXXCast.cpp.
References clang::InitializationKind::CreateCast(), clang::InitializationKind::CreateCStyleCast(), clang::InitializationKind::CreateFunctionalCast(), CT_Const, CT_CStyle, CT_Dynamic, CT_Functional, CT_Reinterpret, CT_Static, clang::Sema::Diag(), clang::InitializationSequence::Failed(), clang::InitializationSequence::FK_ConstructorOverloadFailed, clang::InitializationSequence::FK_UserConversionOverloadFailed, clang::SourceRange::getBegin(), clang::InitializationSequence::getFailedCandidateSet(), clang::InitializationSequence::getFailedOverloadResult(), clang::InitializationSequence::getFailureKind(), clang::Stmt::getSourceRange(), clang::Expr::getType(), clang::InitializedEntity::InitializeTemporary(), clang::OverloadCandidateSet::NoteCandidates(), clang::OCD_AllCandidates, clang::OCD_ViableCandidates, clang::OR_Ambiguous, clang::OR_Deleted, clang::OR_No_Viable_Function, and clang::OR_Success.
Referenced by diagnoseBadCast().
| TryCastResult TryLValueToRValueCast | ( | Sema & | Self, | |
| Expr * | SrcExpr, | |||
| QualType | DestType, | |||
| bool | CStyle, | |||
| CastKind & | Kind, | |||
| CXXCastPath & | BasePath, | |||
| unsigned & | msg | |||
| ) | [static] |
Tests whether a conversion according to N2844 is valid.
Definition at line 921 of file SemaCXXCast.cpp.
References clang::Sema::BuildBasePathArray(), clang::CK_DerivedToBase, clang::CK_NoOp, clang::Sema::CompareReferenceRelationship(), clang::Stmt::getLocStart(), clang::ReferenceType::getPointeeType(), clang::Expr::getType(), clang::QualType::getUnqualifiedType(), clang::Sema::IsDerivedFrom(), clang::Expr::isGLValue(), TC_Failed, TC_NotApplicable, and TC_Success.
Referenced by TryStaticCast().
| static TryCastResult TryReinterpretCast | ( | Sema & | Self, | |
| ExprResult & | SrcExpr, | |||
| QualType | DestType, | |||
| bool | CStyle, | |||
| const SourceRange & | OpRange, | |||
| unsigned & | msg, | |||
| CastKind & | Kind | |||
| ) | [static] |
Definition at line 1445 of file SemaCXXCast.cpp.
References CastsAwayConstness(), clang::Sema::CheckCompatibleReinterpretCast(), clang::CK_AnyPointerToBlockPointerCast, clang::CK_BitCast, clang::CK_IntegralToPointer, clang::CK_LValueBitCast, clang::CK_NoOp, clang::CK_PointerToIntegral, clang::Sema::Context, clang::Sema::Diag(), clang::ExprError(), clang::ActionResult< PtrTy, CompressInvalid >::get(), clang::SourceRange::getBegin(), clang::ASTContext::getCanonicalType(), clang::Sema::getLangOptions(), clang::Expr::getObjectKind(), clang::MemberPointerType::getPointeeType(), clang::ASTContext::getPointerType(), clang::Stmt::getSourceRange(), clang::Expr::getType(), clang::ASTContext::getTypeSize(), clang::Expr::getValueKindForType(), clang::Expr::isLValue(), clang::ActionResult< PtrTy, CompressInvalid >::isUsable(), clang::move(), clang::OK_BitField, clang::OK_ObjCProperty, clang::OK_Ordinary, clang::OK_VectorComponent, clang::ASTContext::OverloadTy, clang::Sema::PrepareCastToObjCObjectPointer(), clang::Sema::ResolveAndFixSingleFunctionTemplateSpecialization(), TC_Failed, TC_NotApplicable, TC_Success, and clang::VK_RValue.
| static TryCastResult TryStaticCast | ( | Sema & | Self, | |
| ExprResult & | SrcExpr, | |||
| QualType | DestType, | |||
| Sema::CheckedConversionKind | CCK, | |||
| const SourceRange & | OpRange, | |||
| unsigned & | msg, | |||
| CastKind & | Kind, | |||
| CXXCastPath & | BasePath | |||
| ) | [static] |
TryStaticCast - Check if a static cast can be performed, and do so if possible. If CStyle, ignore access restrictions on hierarchy casting and casting away constness.
Definition at line 753 of file SemaCXXCast.cpp.
References clang::CK_AnyPointerToBlockPointerCast, clang::CK_BitCast, clang::CK_CPointerToObjCPointerCast, clang::CK_FloatingToIntegral, clang::CK_IntegralCast, clang::CK_IntegralToBoolean, clang::CK_IntegralToFloating, clang::Qualifiers::compatiblyIncludes(), clang::Sema::Context, clang::ActionResult< PtrTy, CompressInvalid >::get(), clang::ASTContext::getCanonicalType(), clang::PointerType::getPointeeType(), clang::QualType::getQualifiers(), clang::Expr::getType(), clang::ActionResult< PtrTy, CompressInvalid >::isInvalid(), clang::Qualifiers::removeObjCGCAttr(), clang::Qualifiers::removeObjCLifetime(), TC_Failed, TC_NotApplicable, TC_Success, TryLValueToRValueCast(), TryStaticImplicitCast(), TryStaticMemberPointerUpcast(), TryStaticPointerDowncast(), and TryStaticReferenceDowncast().
| TryCastResult TryStaticDowncast | ( | Sema & | Self, | |
| CanQualType | SrcType, | |||
| CanQualType | DestType, | |||
| bool | CStyle, | |||
| const SourceRange & | OpRange, | |||
| QualType | OrigSrcType, | |||
| QualType | OrigDestType, | |||
| unsigned & | msg, | |||
| CastKind & | Kind, | |||
| CXXCastPath & | BasePath | |||
| ) | [static] |
TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and TryStaticPointerDowncast. Tests whether a static downcast from SrcType to DestType is possible and allowed.
Definition at line 1041 of file SemaCXXCast.cpp.
References AR_accessible, AR_dependent, AR_inaccessible, clang::CXXBasePaths::begin(), clang::Sema::BuildBasePathArray(), clang::Sema::CheckBaseClassAccess(), clang::CK_BaseToDerived, clang::CXXBasePaths::clear(), clang::Sema::Diag(), clang::CXXBasePaths::end(), clang::CXXBasePaths::front(), clang::CanQual< T >::getAs(), clang::SourceRange::getBegin(), clang::CXXBasePaths::getDetectedVirtual(), clang::CanQual< T >::getUnqualifiedType(), clang::CXXBasePaths::isAmbiguous(), clang::CanQual< T >::isAtLeastAsQualifiedAs(), clang::Sema::IsDerivedFrom(), clang::CXXBasePaths::isRecordingPaths(), clang::Sema::PDiag(), clang::Sema::RequireCompleteType(), clang::CXXBasePaths::setRecordingPaths(), TC_Failed, TC_NotApplicable, and TC_Success.
Referenced by TryStaticPointerDowncast(), and TryStaticReferenceDowncast().
| TryCastResult TryStaticImplicitCast | ( | Sema & | Self, | |
| ExprResult & | SrcExpr, | |||
| QualType | DestType, | |||
| Sema::CheckedConversionKind | CCK, | |||
| const SourceRange & | OpRange, | |||
| unsigned & | msg, | |||
| CastKind & | Kind | |||
| ) | [static] |
TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2 is valid:
An expression e can be explicitly converted to a type T using a static_cast if the declaration "T t(e);" is well-formed [...].
Definition at line 1267 of file SemaCXXCast.cpp.
References clang::CK_ConstructorConversion, clang::CK_NoOp, clang::InitializationSequence::Failed(), clang::ActionResult< PtrTy, CompressInvalid >::get(), clang::SourceRange::getBegin(), clang::InitializationSequence::isConstructorInitialization(), clang::ActionResult< PtrTy, CompressInvalid >::isInvalid(), clang::move(), clang::InitializationSequence::Perform(), clang::Sema::RequireCompleteType(), TC_Failed, TC_NotApplicable, and TC_Success.
Referenced by TryStaticCast().
| TryCastResult TryStaticMemberPointerUpcast | ( | Sema & | Self, | |
| ExprResult & | SrcExpr, | |||
| QualType | SrcType, | |||
| QualType | DestType, | |||
| bool | CStyle, | |||
| const SourceRange & | OpRange, | |||
| unsigned & | msg, | |||
| CastKind & | Kind, | |||
| CXXCastPath & | BasePath | |||
| ) | [static] |
TryStaticMemberPointerUpcast - Tests whether a conversion according to C++ 5.2.9p9 is valid:
An rvalue of type "pointer to member of D of type cv1 T" can be converted to an rvalue of type "pointer to member of B of type cv2 T", where B is a base class of D [...].
Definition at line 1156 of file SemaCXXCast.cpp.
References AR_accessible, AR_dependent, AR_inaccessible, clang::Sema::BuildBasePathArray(), clang::Sema::CheckBaseClassAccess(), clang::CK_DerivedToBaseMemberPointer, clang::Sema::Context, clang::Sema::Diag(), clang::Sema::FixOverloadedFunctionReference(), clang::ActionResult< PtrTy, CompressInvalid >::get(), clang::Sema::getAmbiguousPathsDisplayString(), clang::SourceRange::getBegin(), clang::ASTContext::getCanonicalType(), clang::MemberPointerType::getClass(), clang::ASTContext::getMemberPointerType(), clang::CXXMethodDecl::getParent(), clang::MemberPointerType::getPointeeType(), clang::Expr::getType(), clang::ASTContext::getTypeDeclType(), clang::ASTContext::hasSameUnqualifiedType(), clang::Sema::IsDerivedFrom(), clang::ActionResult< PtrTy, CompressInvalid >::isUsable(), clang::ASTContext::OverloadTy, clang::Sema::ResolveAddressOfOverloadedFunction(), TC_Failed, TC_NotApplicable, and TC_Success.
Referenced by TryStaticCast().
| TryCastResult TryStaticPointerDowncast | ( | Sema & | Self, | |
| QualType | SrcType, | |||
| QualType | DestType, | |||
| bool | CStyle, | |||
| const SourceRange & | OpRange, | |||
| unsigned & | msg, | |||
| CastKind & | Kind, | |||
| CXXCastPath & | BasePath | |||
| ) | [static] |
Tests whether a conversion according to C++ 5.2.9p8 is valid.
Definition at line 1007 of file SemaCXXCast.cpp.
References clang::Sema::Context, clang::ASTContext::getCanonicalType(), clang::PointerType::getPointeeType(), TC_NotApplicable, and TryStaticDowncast().
Referenced by TryStaticCast().
| TryCastResult TryStaticReferenceDowncast | ( | Sema & | Self, | |
| Expr * | SrcExpr, | |||
| QualType | DestType, | |||
| bool | CStyle, | |||
| const SourceRange & | OpRange, | |||
| unsigned & | msg, | |||
| CastKind & | Kind, | |||
| CXXCastPath & | BasePath | |||
| ) | [static] |
Tests whether a conversion according to C++ 5.2.9p5 is valid.
Definition at line 972 of file SemaCXXCast.cpp.
References clang::Sema::Context, clang::ASTContext::getCanonicalType(), clang::ReferenceType::getPointeeType(), clang::Expr::getType(), clang::Expr::isLValue(), clang::Type::isRValueReferenceType(), TC_NotApplicable, and TryStaticDowncast().
Referenced by TryStaticCast().
UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes, this removes one level of indirection from both types, provided that they're the same kind of pointer (plain or to-member). Unlike the Sema function, this one doesn't care if the two pointers-to-member don't point into the same class. This is because CastsAwayConstness doesn't care.
Definition at line 379 of file SemaCXXCast.cpp.
References clang::BlockPointerType::getPointeeType(), clang::MemberPointerType::getPointeeType(), clang::ObjCObjectPointerType::getPointeeType(), and clang::PointerType::getPointeeType().
Referenced by CastsAwayConstness().