-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[Clang] Implement CWG2813: Class member access with prvalues #120223
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
cor3ntin
merged 20 commits into
llvm:main
from
cor3ntin:prvalue-explicit-object-member-function-call
Dec 18, 2024
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e53dfbc
[Clang] Implement CWG2813
MitalAshok edac756
fix clangd AST test
MitalAshok a353728
Run [[nodiscard]] test in C++23
MitalAshok cc2c637
Change __declspec(property) handling; Refactor code; Tests
MitalAshok 3fe511a
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 9108fd0
anonymous namespace -> static
MitalAshok 1ff7db1
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 087a9f0
Update cwg28xx.cpp
MitalAshok e839e95
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 8e72680
Fix test failure
MitalAshok ef71fb7
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 95440d6
Add example from #100341
MitalAshok 78a3115
Retarget clang 20
MitalAshok 2bc3149
Merge remote-tracking branch 'llvm_be_very_careful/main' into prvalue…
cor3ntin 6b34aa8
fix merge
cor3ntin fb9570c
Apply my own review feedback
cor3ntin dafbff1
format
cor3ntin 948a7eb
produce glvalue bases for implicit object member functions access
cor3ntin 85634bc
format
cor3ntin ab2b46e
Apply Erich's feedback
cor3ntin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1003,15 +1003,6 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, | |
: !isDependentScopeSpecifier(SS) || computeDeclContext(SS)) && | ||
"dependent lookup context that isn't the current instantiation?"); | ||
|
||
// C++1z [expr.ref]p2: | ||
// For the first option (dot) the first expression shall be a glvalue [...] | ||
if (!IsArrow && BaseExpr && BaseExpr->isPRValue()) { | ||
ExprResult Converted = TemporaryMaterializationConversion(BaseExpr); | ||
if (Converted.isInvalid()) | ||
return ExprError(); | ||
BaseExpr = Converted.get(); | ||
} | ||
|
||
const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); | ||
DeclarationName MemberName = MemberNameInfo.getName(); | ||
SourceLocation MemberLoc = MemberNameInfo.getLoc(); | ||
|
@@ -1128,26 +1119,68 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, | |
BaseExpr = BuildCXXThisExpr(Loc, BaseExprType, /*IsImplicit=*/true); | ||
} | ||
|
||
// C++17 [expr.ref]p2, per CWG2813: | ||
// For the first option (dot), if the id-expression names a static member or | ||
// an enumerator, the first expression is a discarded-value expression; if | ||
// the id-expression names a non-static data member, the first expression | ||
// shall be a glvalue. | ||
auto MakeDiscardedValue = [&] { | ||
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. I would prefer some more descriptive names here? Perhaps |
||
assert(getLangOpts().CPlusPlus && | ||
"Static member / member enumerator outside of C++"); | ||
if (IsArrow) | ||
return false; | ||
ExprResult Converted = IgnoredValueConversions(BaseExpr); | ||
if (Converted.isInvalid()) | ||
return true; | ||
BaseExpr = Converted.get(); | ||
DiagnoseDiscardedExprMarkedNodiscard(BaseExpr); | ||
return false; | ||
}; | ||
auto MakeGLValue = [&] { | ||
if (IsArrow || !BaseExpr->isPRValue()) | ||
return false; | ||
ExprResult Converted = TemporaryMaterializationConversion(BaseExpr); | ||
if (Converted.isInvalid()) | ||
return true; | ||
BaseExpr = Converted.get(); | ||
return false; | ||
}; | ||
|
||
// Check the use of this member. | ||
if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) | ||
return ExprError(); | ||
|
||
if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) | ||
if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { | ||
if (MakeGLValue()) | ||
return ExprError(); | ||
return BuildFieldReferenceExpr(BaseExpr, IsArrow, OpLoc, SS, FD, FoundDecl, | ||
MemberNameInfo); | ||
} | ||
|
||
if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl)) | ||
if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl)) { | ||
// No temporaries are materialized for property references yet. | ||
// They might be materialized when this is transformed into a member call. | ||
// Note that this is slightly different behaviour from MSVC which doesn't | ||
// implement CWG2813 yet: MSVC might materialize an extra temporary if the | ||
// getter or setter function is an explicit object member function. | ||
return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD, | ||
MemberNameInfo); | ||
} | ||
|
||
if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) | ||
if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) { | ||
if (MakeGLValue()) | ||
return ExprError(); | ||
// We may have found a field within an anonymous union or struct | ||
// (C++ [class.union]). | ||
return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, | ||
FoundDecl, BaseExpr, | ||
OpLoc); | ||
} | ||
|
||
// Static data member | ||
if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { | ||
if (MakeDiscardedValue()) | ||
return ExprError(); | ||
return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, | ||
SS.getWithLocInContext(Context), TemplateKWLoc, Var, | ||
FoundDecl, /*HadMultipleCandidates=*/false, | ||
|
@@ -1162,6 +1195,9 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, | |
valueKind = VK_PRValue; | ||
type = Context.BoundMemberTy; | ||
Sirraide marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// Static member function | ||
if (MakeDiscardedValue()) | ||
return ExprError(); | ||
valueKind = VK_LValue; | ||
type = MemberFn->getType(); | ||
} | ||
|
@@ -1174,13 +1210,17 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, | |
assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); | ||
|
||
if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { | ||
if (MakeDiscardedValue()) | ||
return ExprError(); | ||
return BuildMemberExpr( | ||
BaseExpr, IsArrow, OpLoc, SS.getWithLocInContext(Context), | ||
TemplateKWLoc, Enum, FoundDecl, /*HadMultipleCandidates=*/false, | ||
MemberNameInfo, Enum->getType(), VK_PRValue, OK_Ordinary); | ||
} | ||
|
||
if (VarTemplateDecl *VarTempl = dyn_cast<VarTemplateDecl>(MemberDecl)) { | ||
if (MakeDiscardedValue()) | ||
return ExprError(); | ||
if (!TemplateArgs) { | ||
diagnoseMissingTemplateArguments( | ||
SS, /*TemplateKeyword=*/TemplateKWLoc.isValid(), VarTempl, MemberLoc); | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.