@@ -6972,6 +6972,7 @@ void Sema::AddOverloadCandidate(
6972
6972
Candidate.IsSurrogate = false;
6973
6973
Candidate.IsADLCandidate = IsADLCandidate;
6974
6974
Candidate.IgnoreObjectArgument = false;
6975
+ Candidate.TookAddressOfOverload = false;
6975
6976
Candidate.ExplicitCallArguments = Args.size();
6976
6977
6977
6978
// Explicit functions are not actually candidates at all if we're not
@@ -7546,10 +7547,24 @@ Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl,
7546
7547
CandidateSet.getRewriteInfo().getRewriteKind(Method, PO);
7547
7548
Candidate.IsSurrogate = false;
7548
7549
Candidate.IgnoreObjectArgument = false;
7550
+ Candidate.TookAddressOfOverload =
7551
+ CandidateSet.getKind() == OverloadCandidateSet::CSK_AddressOfOverloadSet;
7549
7552
Candidate.ExplicitCallArguments = Args.size();
7550
7553
7551
- unsigned NumParams = Method->getNumExplicitParams();
7552
- unsigned ExplicitOffset = Method->isExplicitObjectMemberFunction() ? 1 : 0;
7554
+ bool IgnoreExplicitObject =
7555
+ (Method->isExplicitObjectMemberFunction() &&
7556
+ CandidateSet.getKind() ==
7557
+ OverloadCandidateSet::CSK_AddressOfOverloadSet);
7558
+ bool ImplicitObjectMethodTreatedAsStatic =
7559
+ CandidateSet.getKind() ==
7560
+ OverloadCandidateSet::CSK_AddressOfOverloadSet &&
7561
+ Method->isImplicitObjectMemberFunction();
7562
+
7563
+ unsigned ExplicitOffset =
7564
+ !IgnoreExplicitObject && Method->isExplicitObjectMemberFunction() ? 1 : 0;
7565
+
7566
+ unsigned NumParams = Method->getNumParams() - ExplicitOffset +
7567
+ int(ImplicitObjectMethodTreatedAsStatic);
7553
7568
7554
7569
// (C++ 13.3.2p2): A candidate function having fewer than m
7555
7570
// parameters is viable only if it has an ellipsis in its parameter
@@ -7567,7 +7582,10 @@ Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl,
7567
7582
// (8.3.6). For the purposes of overload resolution, the
7568
7583
// parameter list is truncated on the right, so that there are
7569
7584
// exactly m parameters.
7570
- unsigned MinRequiredArgs = Method->getMinRequiredExplicitArguments();
7585
+ unsigned MinRequiredArgs = Method->getMinRequiredArguments() -
7586
+ ExplicitOffset +
7587
+ int(ImplicitObjectMethodTreatedAsStatic);
7588
+
7571
7589
if (Args.size() < MinRequiredArgs && !PartialOverloading) {
7572
7590
// Not enough arguments.
7573
7591
Candidate.Viable = false;
@@ -7637,7 +7655,14 @@ Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl,
7637
7655
// exist for each argument an implicit conversion sequence
7638
7656
// (13.3.3.1) that converts that argument to the corresponding
7639
7657
// parameter of F.
7640
- QualType ParamType = Proto->getParamType(ArgIdx + ExplicitOffset);
7658
+ QualType ParamType;
7659
+ if (ImplicitObjectMethodTreatedAsStatic) {
7660
+ ParamType = ArgIdx == 0
7661
+ ? Method->getFunctionObjectParameterReferenceType()
7662
+ : Proto->getParamType(ArgIdx - 1);
7663
+ } else {
7664
+ ParamType = Proto->getParamType(ArgIdx + ExplicitOffset);
7665
+ }
7641
7666
Candidate.Conversions[ConvIdx]
7642
7667
= TryCopyInitialization(*this, Args[ArgIdx], ParamType,
7643
7668
SuppressUserConversions,
@@ -7718,6 +7743,7 @@ void Sema::AddMethodTemplateCandidate(
7718
7743
Candidate.IgnoreObjectArgument =
7719
7744
cast<CXXMethodDecl>(Candidate.Function)->isStatic() ||
7720
7745
ObjectType.isNull();
7746
+ Candidate.TookAddressOfOverload = false;
7721
7747
Candidate.ExplicitCallArguments = Args.size();
7722
7748
if (Result == TemplateDeductionResult::NonDependentConversionFailure)
7723
7749
Candidate.FailureKind = ovl_fail_bad_conversion;
@@ -7808,6 +7834,7 @@ void Sema::AddTemplateOverloadCandidate(
7808
7834
Candidate.IgnoreObjectArgument =
7809
7835
isa<CXXMethodDecl>(Candidate.Function) &&
7810
7836
!isa<CXXConstructorDecl>(Candidate.Function);
7837
+ Candidate.TookAddressOfOverload = false;
7811
7838
Candidate.ExplicitCallArguments = Args.size();
7812
7839
if (Result == TemplateDeductionResult::NonDependentConversionFailure)
7813
7840
Candidate.FailureKind = ovl_fail_bad_conversion;
@@ -7999,6 +8026,7 @@ void Sema::AddConversionCandidate(
7999
8026
Candidate.Function = Conversion;
8000
8027
Candidate.IsSurrogate = false;
8001
8028
Candidate.IgnoreObjectArgument = false;
8029
+ Candidate.TookAddressOfOverload = false;
8002
8030
Candidate.FinalConversion.setAsIdentityConversion();
8003
8031
Candidate.FinalConversion.setFromType(ConvType);
8004
8032
Candidate.FinalConversion.setAllToTypes(ToType);
@@ -8201,6 +8229,7 @@ void Sema::AddTemplateConversionCandidate(
8201
8229
Candidate.FailureKind = ovl_fail_bad_deduction;
8202
8230
Candidate.IsSurrogate = false;
8203
8231
Candidate.IgnoreObjectArgument = false;
8232
+ Candidate.TookAddressOfOverload = false;
8204
8233
Candidate.ExplicitCallArguments = 1;
8205
8234
Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result,
8206
8235
Info);
@@ -8241,6 +8270,7 @@ void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion,
8241
8270
Candidate.Viable = true;
8242
8271
Candidate.IsSurrogate = true;
8243
8272
Candidate.IgnoreObjectArgument = false;
8273
+ Candidate.TookAddressOfOverload = false;
8244
8274
Candidate.ExplicitCallArguments = Args.size();
8245
8275
8246
8276
// Determine the implicit conversion sequence for the implicit
@@ -8466,6 +8496,7 @@ void Sema::AddBuiltinCandidate(QualType *ParamTys, ArrayRef<Expr *> Args,
8466
8496
Candidate.Function = nullptr;
8467
8497
Candidate.IsSurrogate = false;
8468
8498
Candidate.IgnoreObjectArgument = false;
8499
+ Candidate.TookAddressOfOverload = false;
8469
8500
std::copy(ParamTys, ParamTys + Args.size(), Candidate.BuiltinParamTypes);
8470
8501
8471
8502
// Determine the implicit conversion sequences for each of the
@@ -10930,6 +10961,12 @@ OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc,
10930
10961
if (Best->Function && Best->Function->isDeleted())
10931
10962
return OR_Deleted;
10932
10963
10964
+ if (auto *M = dyn_cast_or_null<CXXMethodDecl>(Best->Function);
10965
+ Kind == CSK_AddressOfOverloadSet && M &&
10966
+ M->isImplicitObjectMemberFunction()) {
10967
+ return OR_No_Viable_Function;
10968
+ }
10969
+
10933
10970
if (!EquivalentCands.empty())
10934
10971
S.diagnoseEquivalentInternalLinkageDeclarations(Loc, Best->Function,
10935
10972
EquivalentCands);
@@ -11517,9 +11554,10 @@ static void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand,
11517
11554
/// candidates. This is not covered by the more general DiagnoseArityMismatch()
11518
11555
/// over a candidate in any candidate set.
11519
11556
static bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand,
11520
- unsigned NumArgs) {
11557
+ unsigned NumArgs, bool IsAddressOf = false ) {
11521
11558
FunctionDecl *Fn = Cand->Function;
11522
- unsigned MinParams = Fn->getMinRequiredArguments();
11559
+ unsigned MinParams = Fn->getMinRequiredExplicitArguments() +
11560
+ ((IsAddressOf && !Fn->isStatic()) ? 1 : 0);
11523
11561
11524
11562
// With invalid overloaded operators, it's possible that we think we
11525
11563
// have an arity mismatch when in fact it looks like we have the
@@ -11547,7 +11585,8 @@ static bool CheckArityMismatch(Sema &S, OverloadCandidate *Cand,
11547
11585
11548
11586
/// General arity mismatch diagnosis over a candidate in a candidate set.
11549
11587
static void DiagnoseArityMismatch(Sema &S, NamedDecl *Found, Decl *D,
11550
- unsigned NumFormalArgs) {
11588
+ unsigned NumFormalArgs,
11589
+ bool IsAddressOf = false) {
11551
11590
assert(isa<FunctionDecl>(D) &&
11552
11591
"The templated declaration should at least be a function"
11553
11592
" when diagnosing bad template argument deduction due to too many"
@@ -11557,12 +11596,17 @@ static void DiagnoseArityMismatch(Sema &S, NamedDecl *Found, Decl *D,
11557
11596
11558
11597
// TODO: treat calls to a missing default constructor as a special case
11559
11598
const auto *FnTy = Fn->getType()->castAs<FunctionProtoType>();
11560
- unsigned MinParams = Fn->getMinRequiredExplicitArguments();
11599
+ unsigned MinParams = Fn->getMinRequiredExplicitArguments() +
11600
+ ((IsAddressOf && !Fn->isStatic()) ? 1 : 0);
11561
11601
11562
11602
// at least / at most / exactly
11563
- bool HasExplicitObjectParam = Fn->hasCXXExplicitFunctionObjectParameter();
11564
- unsigned ParamCount = FnTy->getNumParams() - (HasExplicitObjectParam ? 1 : 0);
11603
+ bool HasExplicitObjectParam =
11604
+ !IsAddressOf && Fn->hasCXXExplicitFunctionObjectParameter();
11605
+
11606
+ unsigned ParamCount =
11607
+ Fn->getNumNonObjectParams() + ((IsAddressOf && !Fn->isStatic()) ? 1 : 0);
11565
11608
unsigned mode, modeCount;
11609
+
11566
11610
if (NumFormalArgs < MinParams) {
11567
11611
if (MinParams != ParamCount || FnTy->isVariadic() ||
11568
11612
FnTy->isTemplateVariadic())
@@ -11582,7 +11626,7 @@ static void DiagnoseArityMismatch(Sema &S, NamedDecl *Found, Decl *D,
11582
11626
std::pair<OverloadCandidateKind, OverloadCandidateSelect> FnKindPair =
11583
11627
ClassifyOverloadCandidate(S, Found, Fn, CRK_None, Description);
11584
11628
11585
- if (modeCount == 1 &&
11629
+ if (modeCount == 1 && !IsAddressOf &&
11586
11630
Fn->getParamDecl(HasExplicitObjectParam ? 1 : 0)->getDeclName())
11587
11631
S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one)
11588
11632
<< (unsigned)FnKindPair.first << (unsigned)FnKindPair.second
@@ -11601,8 +11645,9 @@ static void DiagnoseArityMismatch(Sema &S, NamedDecl *Found, Decl *D,
11601
11645
/// Arity mismatch diagnosis specific to a function overload candidate.
11602
11646
static void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand,
11603
11647
unsigned NumFormalArgs) {
11604
- if (!CheckArityMismatch(S, Cand, NumFormalArgs))
11605
- DiagnoseArityMismatch(S, Cand->FoundDecl, Cand->Function, NumFormalArgs);
11648
+ if (!CheckArityMismatch(S, Cand, NumFormalArgs, Cand->TookAddressOfOverload))
11649
+ DiagnoseArityMismatch(S, Cand->FoundDecl, Cand->Function, NumFormalArgs,
11650
+ Cand->TookAddressOfOverload);
11606
11651
}
11607
11652
11608
11653
static TemplateDecl *getDescribedTemplate(Decl *Templated) {
@@ -12042,6 +12087,13 @@ static void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand,
12042
12087
Cand->FailureKind != ovl_fail_bad_conversion)
12043
12088
return;
12044
12089
12090
+ // Skip implicit member functions when trying to resolve
12091
+ // the address of a an overload set for a function pointer.
12092
+ if (Cand->TookAddressOfOverload &&
12093
+ !Cand->Function->hasCXXExplicitFunctionObjectParameter() &&
12094
+ !Cand->Function->isStatic())
12095
+ return;
12096
+
12045
12097
// Note deleted candidates, but only if they're viable.
12046
12098
if (Cand->Viable) {
12047
12099
if (Fn->isDeleted()) {
@@ -14085,6 +14137,21 @@ static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn,
14085
14137
}
14086
14138
14087
14139
case OR_No_Viable_Function: {
14140
+ if (*Best != CandidateSet->end() &&
14141
+ CandidateSet->getKind() ==
14142
+ clang::OverloadCandidateSet::CSK_AddressOfOverloadSet) {
14143
+ if (CXXMethodDecl *M =
14144
+ dyn_cast_if_present<CXXMethodDecl>((*Best)->Function);
14145
+ M && M->isImplicitObjectMemberFunction()) {
14146
+ CandidateSet->NoteCandidates(
14147
+ PartialDiagnosticAt(
14148
+ Fn->getBeginLoc(),
14149
+ SemaRef.PDiag(diag::err_member_call_without_object) << 0 << M),
14150
+ SemaRef, OCD_AmbiguousCandidates, Args);
14151
+ return ExprError();
14152
+ }
14153
+ }
14154
+
14088
14155
// Try to recover by looking for viable functions which the user might
14089
14156
// have meant to call.
14090
14157
ExprResult Recovery = BuildRecoveryCallExpr(SemaRef, S, Fn, ULE, LParenLoc,
@@ -14176,8 +14243,10 @@ ExprResult Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn,
14176
14243
Expr *ExecConfig,
14177
14244
bool AllowTypoCorrection,
14178
14245
bool CalleesAddressIsTaken) {
14179
- OverloadCandidateSet CandidateSet(Fn->getExprLoc(),
14180
- OverloadCandidateSet::CSK_Normal);
14246
+ OverloadCandidateSet CandidateSet(
14247
+ Fn->getExprLoc(), CalleesAddressIsTaken
14248
+ ? OverloadCandidateSet::CSK_AddressOfOverloadSet
14249
+ : OverloadCandidateSet::CSK_Normal);
14181
14250
ExprResult result;
14182
14251
14183
14252
if (buildOverloadedCallSet(S, Fn, ULE, Args, LParenLoc, &CandidateSet,
@@ -16342,9 +16411,9 @@ ExprResult Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found,
16342
16411
assert(UnOp->getOpcode() == UO_AddrOf &&
16343
16412
"Can only take the address of an overloaded function");
16344
16413
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) {
16345
- if (Method->isStatic ()) {
16346
- // Do nothing: static member functions aren't any different
16347
- // from non-member functions .
16414
+ if (! Method->isImplicitObjectMemberFunction ()) {
16415
+ // Do nothing: the address of static and
16416
+ // explicit object member functions is a ( non-member) function pointer .
16348
16417
} else {
16349
16418
// Fix the subexpression, which really has to be an
16350
16419
// UnresolvedLookupExpr holding an overloaded member function
@@ -16402,7 +16471,10 @@ ExprResult Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found,
16402
16471
}
16403
16472
16404
16473
QualType Type = Fn->getType();
16405
- ExprValueKind ValueKind = getLangOpts().CPlusPlus ? VK_LValue : VK_PRValue;
16474
+ ExprValueKind ValueKind =
16475
+ getLangOpts().CPlusPlus && !Fn->hasCXXExplicitFunctionObjectParameter()
16476
+ ? VK_LValue
16477
+ : VK_PRValue;
16406
16478
16407
16479
// FIXME: Duplicated from BuildDeclarationNameExpr.
16408
16480
if (unsigned BID = Fn->getBuiltinID()) {
0 commit comments