diff options
author | George Burgess IV <george.burgess.iv@gmail.com> | 2015-10-16 01:17:38 +0000 |
---|---|---|
committer | George Burgess IV <george.burgess.iv@gmail.com> | 2015-10-16 01:17:38 +0000 |
commit | 2a6150d93290b8b1e73a577226bd9ee4d298a703 (patch) | |
tree | 4892ee0509946c602ce887977c5528a7a987157b /clang/lib/Sema/SemaOverload.cpp | |
parent | 47d118e9f275f3d6d811ad77cac3bdff8d8c2d57 (diff) | |
download | bcm5719-llvm-2a6150d93290b8b1e73a577226bd9ee4d298a703.tar.gz bcm5719-llvm-2a6150d93290b8b1e73a577226bd9ee4d298a703.zip |
[Sema] Fix address-of + enable_if overloading logic
Previously, our logic when taking the address of an overloaded function
would not consider enable_if attributes, so long as all of the enable_if
conditions on a given candidate were true. So, two functions with
identical signatures (one with enable_if attributes, the other without),
would be considered equally good overloads. If we were calling the
function instead of taking its address, then the function with enable_if
attributes would be preferred.
This patch makes us prefer the candidate with enable_if regardless of if
we're calling or taking the address of an overloaded function.
Differential Revision: http://reviews.llvm.org/D13795
llvm-svn: 250486
Diffstat (limited to 'clang/lib/Sema/SemaOverload.cpp')
-rw-r--r-- | clang/lib/Sema/SemaOverload.cpp | 150 |
1 files changed, 93 insertions, 57 deletions
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 44cb166f25b..1ad43a35925 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -5855,23 +5855,31 @@ ObjCMethodDecl *Sema::SelectBestMethod(Selector Sel, MultiExprArg Args, return nullptr; } -static bool IsNotEnableIfAttr(Attr *A) { return !isa<EnableIfAttr>(A); } +// specific_attr_iterator iterates over enable_if attributes in reverse, and +// enable_if is order-sensitive. As a result, we need to reverse things +// sometimes. Size of 4 elements is arbitrary. +static SmallVector<EnableIfAttr *, 4> +getOrderedEnableIfAttrs(const FunctionDecl *Function) { + SmallVector<EnableIfAttr *, 4> Result; + if (!Function->hasAttrs()) + return Result; + + const auto &FuncAttrs = Function->getAttrs(); + for (Attr *Attr : FuncAttrs) + if (auto *EnableIf = dyn_cast<EnableIfAttr>(Attr)) + Result.push_back(EnableIf); + + std::reverse(Result.begin(), Result.end()); + return Result; +} EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args, bool MissingImplicitThis) { - // FIXME: specific_attr_iterator<EnableIfAttr> iterates in reverse order, but - // we need to find the first failing one. - if (!Function->hasAttrs()) - return nullptr; - AttrVec Attrs = Function->getAttrs(); - AttrVec::iterator E = std::remove_if(Attrs.begin(), Attrs.end(), - IsNotEnableIfAttr); - if (Attrs.begin() == E) + auto EnableIfAttrs = getOrderedEnableIfAttrs(Function); + if (EnableIfAttrs.empty()) return nullptr; - std::reverse(Attrs.begin(), E); SFINAETrap Trap(*this); - SmallVector<Expr *, 16> ConvertedArgs; bool InitializationFailed = false; bool ContainsValueDependentExpr = false; @@ -5908,7 +5916,7 @@ EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args, } if (InitializationFailed || Trap.hasErrorOccurred()) - return cast<EnableIfAttr>(Attrs[0]); + return EnableIfAttrs[0]; // Push default arguments if needed. if (!Function->isVariadic() && Args.size() < Function->getNumParams()) { @@ -5929,12 +5937,11 @@ EnableIfAttr *Sema::CheckEnableIf(FunctionDecl *Function, ArrayRef<Expr *> Args, } if (InitializationFailed || Trap.hasErrorOccurred()) - return cast<EnableIfAttr>(Attrs[0]); + return EnableIfAttrs[0]; } - for (AttrVec::iterator I = Attrs.begin(); I != E; ++I) { + for (auto *EIA : EnableIfAttrs) { APValue Result; - EnableIfAttr *EIA = cast<EnableIfAttr>(*I); if (EIA->getCond()->isValueDependent()) { // Don't even try now, we'll examine it after instantiation. continue; @@ -8397,6 +8404,44 @@ Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, } } +// Determines whether Cand1 is "better" in terms of its enable_if attrs than +// Cand2 for overloading. This function assumes that all of the enable_if attrs +// on Cand1 and Cand2 have conditions that evaluate to true. +// +// Cand1's set of enable_if attributes are said to be "better" than Cand2's iff +// Cand1's first N enable_if attributes have precisely the same conditions as +// Cand2's first N enable_if attributes (where N = the number of enable_if +// attributes on Cand2), and Cand1 has more than N enable_if attributes. +static bool hasBetterEnableIfAttrs(Sema &S, const FunctionDecl *Cand1, + const FunctionDecl *Cand2) { + + // FIXME: The next several lines are just + // specific_attr_iterator<EnableIfAttr> but going in declaration order, + // instead of reverse order which is how they're stored in the AST. + auto Cand1Attrs = getOrderedEnableIfAttrs(Cand1); + auto Cand2Attrs = getOrderedEnableIfAttrs(Cand2); + + // Candidate 1 is better if it has strictly more attributes and + // the common sequence is identical. + if (Cand1Attrs.size() <= Cand2Attrs.size()) + return false; + + auto Cand1I = Cand1Attrs.begin(); + llvm::FoldingSetNodeID Cand1ID, Cand2ID; + for (auto &Cand2A : Cand2Attrs) { + Cand1ID.clear(); + Cand2ID.clear(); + + auto &Cand1A = *Cand1I++; + Cand1A->getCond()->Profile(Cand1ID, S.getASTContext(), true); + Cand2A->getCond()->Profile(Cand2ID, S.getASTContext(), true); + if (Cand1ID != Cand2ID) + return false; + } + + return true; +} + /// isBetterOverloadCandidate - Determines whether the first overload /// candidate is a better candidate than the second (C++ 13.3.3p1). bool clang::isBetterOverloadCandidate(Sema &S, const OverloadCandidate &Cand1, @@ -8507,47 +8552,8 @@ bool clang::isBetterOverloadCandidate(Sema &S, const OverloadCandidate &Cand1, // Check for enable_if value-based overload resolution. if (Cand1.Function && Cand2.Function && (Cand1.Function->hasAttr<EnableIfAttr>() || - Cand2.Function->hasAttr<EnableIfAttr>())) { - // FIXME: The next several lines are just - // specific_attr_iterator<EnableIfAttr> but going in declaration order, - // instead of reverse order which is how they're stored in the AST. - AttrVec Cand1Attrs; - if (Cand1.Function->hasAttrs()) { - Cand1Attrs = Cand1.Function->getAttrs(); - Cand1Attrs.erase(std::remove_if(Cand1Attrs.begin(), Cand1Attrs.end(), - IsNotEnableIfAttr), - Cand1Attrs.end()); - std::reverse(Cand1Attrs.begin(), Cand1Attrs.end()); - } - - AttrVec Cand2Attrs; - if (Cand2.Function->hasAttrs()) { - Cand2Attrs = Cand2.Function->getAttrs(); - Cand2Attrs.erase(std::remove_if(Cand2Attrs.begin(), Cand2Attrs.end(), - IsNotEnableIfAttr), - Cand2Attrs.end()); - std::reverse(Cand2Attrs.begin(), Cand2Attrs.end()); - } - - // Candidate 1 is better if it has strictly more attributes and - // the common sequence is identical. - if (Cand1Attrs.size() <= Cand2Attrs.size()) - return false; - - auto Cand1I = Cand1Attrs.begin(); - for (auto &Cand2A : Cand2Attrs) { - auto &Cand1A = *Cand1I++; - llvm::FoldingSetNodeID Cand1ID, Cand2ID; - cast<EnableIfAttr>(Cand1A)->getCond()->Profile(Cand1ID, - S.getASTContext(), true); - cast<EnableIfAttr>(Cand2A)->getCond()->Profile(Cand2ID, - S.getASTContext(), true); - if (Cand1ID != Cand2ID) - return false; - } - - return true; - } + Cand2.Function->hasAttr<EnableIfAttr>())) + return hasBetterEnableIfAttrs(S, Cand1.Function, Cand2.Function); if (S.getLangOpts().CUDA && S.getLangOpts().CUDATargetOverloads && Cand1.Function && Cand2.Function) { @@ -9986,7 +9992,7 @@ public: if (FindAllFunctionsThatMatchTargetTypeExactly()) { // C++ [over.over]p4: // If more than one function is selected, [...] - if (Matches.size() > 1) { + if (Matches.size() > 1 && !eliminiateSuboptimalOverloadCandidates()) { if (FoundNonTemplateFunction) EliminateAllTemplateMatches(); else @@ -10002,6 +10008,36 @@ public: bool hasComplained() const { return HasComplained; } private: + // Is A considered a better overload candidate for the desired type than B? + bool isBetterCandidate(const FunctionDecl *A, const FunctionDecl *B) { + return hasBetterEnableIfAttrs(S, A, B); + } + + // Returns true if we've eliminated any (read: all but one) candidates, false + // otherwise. + bool eliminiateSuboptimalOverloadCandidates() { + // Same algorithm as overload resolution -- one pass to pick the "best", + // another pass to be sure that nothing is better than the best. + auto Best = Matches.begin(); + for (auto I = Matches.begin()+1, E = Matches.end(); I != E; ++I) + if (isBetterCandidate(I->second, Best->second)) + Best = I; + + const FunctionDecl *BestFn = Best->second; + auto IsBestOrInferiorToBest = [this, BestFn]( + const std::pair<DeclAccessPair, FunctionDecl *> &Pair) { + return BestFn == Pair.second || isBetterCandidate(BestFn, Pair.second); + }; + + // Note: We explicitly leave Matches unmodified if there isn't a clear best + // option, so we can potentially give the user a better error + if (!std::all_of(Matches.begin(), Matches.end(), IsBestOrInferiorToBest)) + return false; + Matches[0] = *Best; + Matches.resize(1); + return true; + } + bool isTargetTypeAFunction() const { return TargetFunctionType->isFunctionType(); } |