-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang] Implement __reference_converts_from_temporary #91199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5627,6 +5627,77 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT, | |
static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceInfo *Lhs, | ||
const TypeSourceInfo *Rhs, SourceLocation KeyLoc); | ||
|
||
static ExprResult CheckConvertibilityForTypeTraits(Sema &Self, | ||
const TypeSourceInfo *Lhs, | ||
const TypeSourceInfo *Rhs, | ||
SourceLocation KeyLoc) { | ||
|
||
QualType LhsT = Lhs->getType(); | ||
QualType RhsT = Rhs->getType(); | ||
|
||
// C++0x [meta.rel]p4: | ||
// Given the following function prototype: | ||
// | ||
// template <class T> | ||
// typename add_rvalue_reference<T>::type create(); | ||
// | ||
// the predicate condition for a template specialization | ||
// is_convertible<From, To> shall be satisfied if and only if | ||
// the return expression in the following code would be | ||
// well-formed, including any implicit conversions to the return | ||
// type of the function: | ||
// | ||
// To test() { | ||
// return create<From>(); | ||
// } | ||
// | ||
// Access checking is performed as if in a context unrelated to To and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have a test for this access checking? I would expect something conversion ops/ctors that are protected, but they are friends of eachother. |
||
// From. Only the validity of the immediate context of the expression | ||
// of the return-statement (including conversions to the return type) | ||
// is considered. | ||
// | ||
// We model the initialization as a copy-initialization of a temporary | ||
// of the appropriate type, which for this expression is identical to the | ||
// return statement (since NRVO doesn't apply). | ||
|
||
// Functions aren't allowed to return function or array types. | ||
if (RhsT->isFunctionType() || RhsT->isArrayType()) | ||
return ExprError(); | ||
|
||
// A function definition requires a complete, non-abstract return type. | ||
if (!Self.isCompleteType(Rhs->getTypeLoc().getBeginLoc(), RhsT) || | ||
Self.isAbstractType(Rhs->getTypeLoc().getBeginLoc(), RhsT)) | ||
return ExprError(); | ||
|
||
// Compute the result of add_rvalue_reference. | ||
if (LhsT->isObjectType() || LhsT->isFunctionType()) | ||
LhsT = Self.Context.getRValueReferenceType(LhsT); | ||
|
||
// Build a fake source and destination for initialization. | ||
InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); | ||
OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), | ||
Expr::getValueKindForType(LhsT)); | ||
Expr *FromPtr = &From; | ||
InitializationKind Kind = | ||
InitializationKind::CreateCopy(KeyLoc, SourceLocation()); | ||
|
||
// Perform the initialization in an unevaluated context within a SFINAE | ||
// trap at translation unit scope. | ||
EnterExpressionEvaluationContext Unevaluated( | ||
Self, Sema::ExpressionEvaluationContext::Unevaluated); | ||
Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); | ||
Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); | ||
InitializationSequence Init(Self, To, Kind, FromPtr); | ||
if (Init.Failed()) | ||
return ExprError(); | ||
|
||
ExprResult Result = Init.Perform(Self, To, Kind, FromPtr); | ||
if (Result.isInvalid() || SFINAE.hasErrorOccurred()) | ||
return ExprError(); | ||
|
||
return Result; | ||
} | ||
|
||
static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, | ||
SourceLocation KWLoc, | ||
ArrayRef<TypeSourceInfo *> Args, | ||
|
@@ -5640,13 +5711,16 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, | |
|
||
// Evaluate ReferenceBindsToTemporary and ReferenceConstructsFromTemporary | ||
// alongside the IsConstructible traits to avoid duplication. | ||
if (Kind <= BTT_Last && Kind != BTT_ReferenceBindsToTemporary && Kind != BTT_ReferenceConstructsFromTemporary) | ||
if (Kind <= BTT_Last && Kind != BTT_ReferenceBindsToTemporary && | ||
Kind != BTT_ReferenceConstructsFromTemporary && | ||
Kind != BTT_ReferenceConvertsFromTemporary) | ||
return EvaluateBinaryTypeTrait(S, Kind, Args[0], | ||
Args[1], RParenLoc); | ||
|
||
switch (Kind) { | ||
case clang::BTT_ReferenceBindsToTemporary: | ||
case clang::BTT_ReferenceConstructsFromTemporary: | ||
case clang::BTT_ReferenceConvertsFromTemporary: | ||
case clang::TT_IsConstructible: | ||
case clang::TT_IsNothrowConstructible: | ||
case clang::TT_IsTriviallyConstructible: { | ||
|
@@ -5710,8 +5784,10 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, | |
Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl()); | ||
InitializedEntity To( | ||
InitializedEntity::InitializeTemporary(S.Context, Args[0])); | ||
InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc, | ||
RParenLoc)); | ||
InitializationKind InitKind( | ||
Kind == clang::BTT_ReferenceConvertsFromTemporary | ||
? InitializationKind::CreateCopy(KWLoc, KWLoc) | ||
: InitializationKind::CreateDirect(KWLoc, KWLoc, RParenLoc)); | ||
InitializationSequence Init(S, To, InitKind, ArgExprs); | ||
if (Init.Failed()) | ||
return false; | ||
|
@@ -5723,7 +5799,9 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, | |
if (Kind == clang::TT_IsConstructible) | ||
return true; | ||
|
||
if (Kind == clang::BTT_ReferenceBindsToTemporary || Kind == clang::BTT_ReferenceConstructsFromTemporary) { | ||
if (Kind == clang::BTT_ReferenceBindsToTemporary || | ||
Kind == clang::BTT_ReferenceConstructsFromTemporary || | ||
Kind == clang::BTT_ReferenceConvertsFromTemporary) { | ||
if (!T->isReferenceType()) | ||
return false; | ||
|
||
|
@@ -5737,9 +5815,12 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, | |
if (U->isReferenceType()) | ||
return false; | ||
|
||
TypeSourceInfo *TPtr = S.Context.CreateTypeSourceInfo(S.Context.getPointerType(S.BuiltinRemoveReference(T, UnaryTransformType::RemoveCVRef, {}))); | ||
TypeSourceInfo *UPtr = S.Context.CreateTypeSourceInfo(S.Context.getPointerType(S.BuiltinRemoveReference(U, UnaryTransformType::RemoveCVRef, {}))); | ||
return EvaluateBinaryTypeTrait(S, TypeTrait::BTT_IsConvertibleTo, UPtr, TPtr, RParenLoc); | ||
TypeSourceInfo *TPtr = S.Context.CreateTypeSourceInfo( | ||
S.Context.getPointerType(T.getNonReferenceType())); | ||
TypeSourceInfo *UPtr = S.Context.CreateTypeSourceInfo( | ||
S.Context.getPointerType(U.getNonReferenceType())); | ||
return !CheckConvertibilityForTypeTraits(S, UPtr, TPtr, RParenLoc) | ||
.isInvalid(); | ||
} | ||
|
||
if (Kind == clang::TT_IsNothrowConstructible) | ||
|
@@ -5945,68 +6026,12 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI | |
case BTT_IsConvertible: | ||
case BTT_IsConvertibleTo: | ||
case BTT_IsNothrowConvertible: { | ||
// C++0x [meta.rel]p4: | ||
// Given the following function prototype: | ||
// | ||
// template <class T> | ||
// typename add_rvalue_reference<T>::type create(); | ||
// | ||
// the predicate condition for a template specialization | ||
// is_convertible<From, To> shall be satisfied if and only if | ||
// the return expression in the following code would be | ||
// well-formed, including any implicit conversions to the return | ||
// type of the function: | ||
// | ||
// To test() { | ||
// return create<From>(); | ||
// } | ||
// | ||
// Access checking is performed as if in a context unrelated to To and | ||
// From. Only the validity of the immediate context of the expression | ||
// of the return-statement (including conversions to the return type) | ||
// is considered. | ||
// | ||
// We model the initialization as a copy-initialization of a temporary | ||
// of the appropriate type, which for this expression is identical to the | ||
// return statement (since NRVO doesn't apply). | ||
|
||
// Functions aren't allowed to return function or array types. | ||
if (RhsT->isFunctionType() || RhsT->isArrayType()) | ||
return false; | ||
|
||
// A return statement in a void function must have void type. | ||
if (RhsT->isVoidType()) | ||
return LhsT->isVoidType(); | ||
|
||
// A function definition requires a complete, non-abstract return type. | ||
if (!Self.isCompleteType(Rhs->getTypeLoc().getBeginLoc(), RhsT) || | ||
Self.isAbstractType(Rhs->getTypeLoc().getBeginLoc(), RhsT)) | ||
return false; | ||
|
||
// Compute the result of add_rvalue_reference. | ||
if (LhsT->isObjectType() || LhsT->isFunctionType()) | ||
LhsT = Self.Context.getRValueReferenceType(LhsT); | ||
|
||
// Build a fake source and destination for initialization. | ||
InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); | ||
OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), | ||
Expr::getValueKindForType(LhsT)); | ||
Expr *FromPtr = &From; | ||
InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, | ||
SourceLocation())); | ||
|
||
// Perform the initialization in an unevaluated context within a SFINAE | ||
// trap at translation unit scope. | ||
EnterExpressionEvaluationContext Unevaluated( | ||
Self, Sema::ExpressionEvaluationContext::Unevaluated); | ||
Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); | ||
Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); | ||
InitializationSequence Init(Self, To, Kind, FromPtr); | ||
if (Init.Failed()) | ||
return false; | ||
|
||
ExprResult Result = Init.Perform(Self, To, Kind, FromPtr); | ||
if (Result.isInvalid() || SFINAE.hasErrorOccurred()) | ||
ExprResult Result = | ||
CheckConvertibilityForTypeTraits(Self, Lhs, Rhs, KeyLoc); | ||
if (Result.isInvalid()) | ||
return false; | ||
|
||
if (BTT != BTT_IsNothrowConvertible) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why the change here? It seems the fallback is still valuable here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As they are new names, they were never structs. So there is no need for compatibility for old standard libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so was here unintentionally/without good reason? I guess that makes sense. SGTM.