diff options
Diffstat (limited to 'lib/Sema/SemaOverload.cpp')
-rw-r--r-- | lib/Sema/SemaOverload.cpp | 448 |
1 files changed, 270 insertions, 178 deletions
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 9111878..89d495d 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -1,4 +1,4 @@ -//===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// +//===--- SemaOverload.cpp - C++ Overloading -------------------------------===// // // The LLVM Compiler Infrastructure // @@ -11,13 +11,7 @@ // //===----------------------------------------------------------------------===// -#include "clang/Sema/SemaInternal.h" -#include "clang/Sema/Lookup.h" -#include "clang/Sema/Initialization.h" -#include "clang/Sema/Template.h" -#include "clang/Sema/TemplateDeduction.h" -#include "clang/Basic/Diagnostic.h" -#include "clang/Lex/Preprocessor.h" +#include "clang/Sema/Overload.h" #include "clang/AST/ASTContext.h" #include "clang/AST/CXXInheritance.h" #include "clang/AST/DeclObjC.h" @@ -25,26 +19,37 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/TypeOrdering.h" +#include "clang/Basic/Diagnostic.h" #include "clang/Basic/PartialDiagnostic.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Sema/Initialization.h" +#include "clang/Sema/Lookup.h" +#include "clang/Sema/SemaInternal.h" +#include "clang/Sema/Template.h" +#include "clang/Sema/TemplateDeduction.h" #include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallString.h" -#include "llvm/ADT/STLExtras.h" #include <algorithm> namespace clang { using namespace sema; -/// A convenience routine for creating a decayed reference to a -/// function. +/// A convenience routine for creating a decayed reference to a function. static ExprResult -CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, bool HadMultipleCandidates, +CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, NamedDecl *FoundDecl, + bool HadMultipleCandidates, SourceLocation Loc = SourceLocation(), const DeclarationNameLoc &LocInfo = DeclarationNameLoc()){ DeclRefExpr *DRE = new (S.Context) DeclRefExpr(Fn, false, Fn->getType(), VK_LValue, Loc, LocInfo); if (HadMultipleCandidates) DRE->setHadMultipleCandidates(true); + + S.MarkDeclRefReferenced(DRE); + S.DiagnoseUseOfDecl(FoundDecl, Loc); + ExprResult E = S.Owned(DRE); E = S.DefaultFunctionArrayConversion(E.take()); if (E.isInvalid()) @@ -535,12 +540,16 @@ AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { namespace { // Structure used by OverloadCandidate::DeductionFailureInfo to store - // template parameter and template argument information. - struct DFIParamWithArguments { - TemplateParameter Param; + // template argument information. + struct DFIArguments { TemplateArgument FirstArg; TemplateArgument SecondArg; }; + // Structure used by OverloadCandidate::DeductionFailureInfo to store + // template parameter and template argument information. + struct DFIParamWithArguments : DFIArguments { + TemplateParameter Param; + }; } /// \brief Convert from Sema's representation of template deduction information @@ -566,6 +575,15 @@ static MakeDeductionFailureInfo(ASTContext &Context, Result.Data = Info.Param.getOpaqueValue(); break; + case Sema::TDK_NonDeducedMismatch: { + // FIXME: Should allocate from normal heap so that we can free this later. + DFIArguments *Saved = new (Context) DFIArguments; + Saved->FirstArg = Info.FirstArg; + Saved->SecondArg = Info.SecondArg; + Result.Data = Saved; + break; + } + case Sema::TDK_Inconsistent: case Sema::TDK_Underqualified: { // FIXME: Should allocate from normal heap so that we can free this later. @@ -587,8 +605,11 @@ static MakeDeductionFailureInfo(ASTContext &Context, } break; - case Sema::TDK_NonDeducedMismatch: case Sema::TDK_FailedOverloadResolution: + Result.Data = Info.Expression; + break; + + case Sema::TDK_MiscellaneousDeductionFailure: break; } @@ -604,10 +625,12 @@ void OverloadCandidate::DeductionFailureInfo::Destroy() { case Sema::TDK_TooManyArguments: case Sema::TDK_TooFewArguments: case Sema::TDK_InvalidExplicitArguments: + case Sema::TDK_FailedOverloadResolution: break; case Sema::TDK_Inconsistent: case Sema::TDK_Underqualified: + case Sema::TDK_NonDeducedMismatch: // FIXME: Destroy the data? Data = 0; break; @@ -622,8 +645,7 @@ void OverloadCandidate::DeductionFailureInfo::Destroy() { break; // Unhandled - case Sema::TDK_NonDeducedMismatch: - case Sema::TDK_FailedOverloadResolution: + case Sema::TDK_MiscellaneousDeductionFailure: break; } } @@ -644,6 +666,8 @@ OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { case Sema::TDK_TooManyArguments: case Sema::TDK_TooFewArguments: case Sema::TDK_SubstitutionFailure: + case Sema::TDK_NonDeducedMismatch: + case Sema::TDK_FailedOverloadResolution: return TemplateParameter(); case Sema::TDK_Incomplete: @@ -655,8 +679,7 @@ OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { return static_cast<DFIParamWithArguments*>(Data)->Param; // Unhandled - case Sema::TDK_NonDeducedMismatch: - case Sema::TDK_FailedOverloadResolution: + case Sema::TDK_MiscellaneousDeductionFailure: break; } @@ -666,24 +689,25 @@ OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { TemplateArgumentList * OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { switch (static_cast<Sema::TemplateDeductionResult>(Result)) { - case Sema::TDK_Success: - case Sema::TDK_Invalid: - case Sema::TDK_InstantiationDepth: - case Sema::TDK_TooManyArguments: - case Sema::TDK_TooFewArguments: - case Sema::TDK_Incomplete: - case Sema::TDK_InvalidExplicitArguments: - case Sema::TDK_Inconsistent: - case Sema::TDK_Underqualified: - return 0; + case Sema::TDK_Success: + case Sema::TDK_Invalid: + case Sema::TDK_InstantiationDepth: + case Sema::TDK_TooManyArguments: + case Sema::TDK_TooFewArguments: + case Sema::TDK_Incomplete: + case Sema::TDK_InvalidExplicitArguments: + case Sema::TDK_Inconsistent: + case Sema::TDK_Underqualified: + case Sema::TDK_NonDeducedMismatch: + case Sema::TDK_FailedOverloadResolution: + return 0; - case Sema::TDK_SubstitutionFailure: - return static_cast<TemplateArgumentList*>(Data); + case Sema::TDK_SubstitutionFailure: + return static_cast<TemplateArgumentList*>(Data); - // Unhandled - case Sema::TDK_NonDeducedMismatch: - case Sema::TDK_FailedOverloadResolution: - break; + // Unhandled + case Sema::TDK_MiscellaneousDeductionFailure: + break; } return 0; @@ -699,15 +723,16 @@ const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { case Sema::TDK_TooFewArguments: case Sema::TDK_InvalidExplicitArguments: case Sema::TDK_SubstitutionFailure: + case Sema::TDK_FailedOverloadResolution: return 0; case Sema::TDK_Inconsistent: case Sema::TDK_Underqualified: - return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; + case Sema::TDK_NonDeducedMismatch: + return &static_cast<DFIArguments*>(Data)->FirstArg; // Unhandled - case Sema::TDK_NonDeducedMismatch: - case Sema::TDK_FailedOverloadResolution: + case Sema::TDK_MiscellaneousDeductionFailure: break; } @@ -725,21 +750,31 @@ OverloadCandidate::DeductionFailureInfo::getSecondArg() { case Sema::TDK_TooFewArguments: case Sema::TDK_InvalidExplicitArguments: case Sema::TDK_SubstitutionFailure: + case Sema::TDK_FailedOverloadResolution: return 0; case Sema::TDK_Inconsistent: case Sema::TDK_Underqualified: - return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; + case Sema::TDK_NonDeducedMismatch: + return &static_cast<DFIArguments*>(Data)->SecondArg; // Unhandled - case Sema::TDK_NonDeducedMismatch: - case Sema::TDK_FailedOverloadResolution: + case Sema::TDK_MiscellaneousDeductionFailure: break; } return 0; } +Expr * +OverloadCandidate::DeductionFailureInfo::getExpr() { + if (static_cast<Sema::TemplateDeductionResult>(Result) == + Sema::TDK_FailedOverloadResolution) + return static_cast<Expr*>(Data); + + return 0; +} + void OverloadCandidateSet::destroyCandidates() { for (iterator i = begin(), e = end(); i != e; ++i) { for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii) @@ -885,7 +920,8 @@ Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, // function templates hide function templates with different // return types or template parameter lists. bool UseMemberUsingDeclRules = - (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); + (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord() && + !New->getFriendObjectKind(); if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { @@ -929,13 +965,21 @@ Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, return Ovl_Overload; } -bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, - bool UseUsingDeclRules) { - // If both of the functions are extern "C", then they are not - // overloads. - if (Old->isExternC() && New->isExternC()) +static bool canBeOverloaded(const FunctionDecl &D) { + if (D.getAttr<OverloadableAttr>()) + return true; + if (D.isExternC()) + return false; + + // Main cannot be overloaded (basic.start.main). + if (D.isMain()) return false; + return true; +} + +static bool shouldTryToOverload(Sema &S, FunctionDecl *New, FunctionDecl *Old, + bool UseUsingDeclRules) { FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); @@ -946,8 +990,8 @@ bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, return true; // Is the function New an overload of the function Old? - QualType OldQType = Context.getCanonicalType(Old->getType()); - QualType NewQType = Context.getCanonicalType(New->getType()); + QualType OldQType = S.Context.getCanonicalType(Old->getType()); + QualType NewQType = S.Context.getCanonicalType(New->getType()); // Compare the signatures (C++ 1.3.10) of the two functions to // determine whether they are overloads. If we find any mismatch @@ -968,7 +1012,7 @@ bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, if (OldQType != NewQType && (OldType->getNumArgs() != NewType->getNumArgs() || OldType->isVariadic() != NewType->isVariadic() || - !FunctionArgTypesAreEqual(OldType, NewType))) + !S.FunctionArgTypesAreEqual(OldType, NewType))) return true; // C++ [temp.over.link]p4: @@ -984,9 +1028,9 @@ bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, // However, we don't consider either of these when deciding whether // a member introduced by a shadow declaration is hidden. if (!UseUsingDeclRules && NewTemplate && - (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), - OldTemplate->getTemplateParameters(), - false, TPL_TemplateMatch) || + (!S.TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), + OldTemplate->getTemplateParameters(), + false, S.TPL_TemplateMatch) || OldType->getResultType() != NewType->getResultType())) return true; @@ -998,34 +1042,55 @@ bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, // 13.1p2). While not part of the definition of the signature, // this check is important to determine whether these functions // can be overloaded. - CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); - CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); + CXXMethodDecl *OldMethod = dyn_cast<CXXMethodDecl>(Old); + CXXMethodDecl *NewMethod = dyn_cast<CXXMethodDecl>(New); if (OldMethod && NewMethod && - !OldMethod->isStatic() && !NewMethod->isStatic() && - (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || - OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { - if (!UseUsingDeclRules && - OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && - (OldMethod->getRefQualifier() == RQ_None || - NewMethod->getRefQualifier() == RQ_None)) { - // C++0x [over.load]p2: - // - Member function declarations with the same name and the same - // parameter-type-list as well as member function template - // declarations with the same name, the same parameter-type-list, and - // the same template parameter lists cannot be overloaded if any of - // them, but not all, have a ref-qualifier (8.3.5). - Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) - << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); - Diag(OldMethod->getLocation(), diag::note_previous_declaration); + !OldMethod->isStatic() && !NewMethod->isStatic()) { + if (OldMethod->getRefQualifier() != NewMethod->getRefQualifier()) { + if (!UseUsingDeclRules && + (OldMethod->getRefQualifier() == RQ_None || + NewMethod->getRefQualifier() == RQ_None)) { + // C++0x [over.load]p2: + // - Member function declarations with the same name and the same + // parameter-type-list as well as member function template + // declarations with the same name, the same parameter-type-list, and + // the same template parameter lists cannot be overloaded if any of + // them, but not all, have a ref-qualifier (8.3.5). + S.Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) + << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); + S.Diag(OldMethod->getLocation(), diag::note_previous_declaration); + } + return true; } - return true; + // We may not have applied the implicit const for a constexpr member + // function yet (because we haven't yet resolved whether this is a static + // or non-static member function). Add it now, on the assumption that this + // is a redeclaration of OldMethod. + unsigned NewQuals = NewMethod->getTypeQualifiers(); + if (NewMethod->isConstexpr() && !isa<CXXConstructorDecl>(NewMethod)) + NewQuals |= Qualifiers::Const; + if (OldMethod->getTypeQualifiers() != NewQuals) + return true; } // The signatures match; this is not an overload. return false; } +bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, + bool UseUsingDeclRules) { + if (!shouldTryToOverload(*this, New, Old, UseUsingDeclRules)) + return false; + + // If both of the functions are extern "C", then they are not + // overloads. + if (!canBeOverloaded(*Old) && !canBeOverloaded(*New)) + return false; + + return true; +} + /// \brief Checks availability of the function depending on the current /// function context. Inside an unavailable function, unavailability is ignored. /// @@ -1577,6 +1642,11 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, // tryAtomicConversion has updated the standard conversion sequence // appropriately. return true; + } else if (ToType->isEventT() && + From->isIntegerConstantExpr(S.getASTContext()) && + (From->EvaluateKnownConstInt(S.getASTContext()) == 0)) { + SCS.Second = ICK_Zero_Event_Conversion; + FromType = ToType; } else { // No second conversion required. SCS.Second = ICK_Identity; @@ -1606,9 +1676,7 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, CanonTo = S.Context.getCanonicalType(ToType); if (CanonFrom.getLocalUnqualifiedType() == CanonTo.getLocalUnqualifiedType() && - (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() - || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr() - || CanonFrom.getObjCLifetime() != CanonTo.getObjCLifetime())) { + CanonFrom.getLocalQualifiers() != CanonTo.getLocalQualifiers()) { FromType = ToType; CanonFrom = CanonTo; } @@ -1818,7 +1886,8 @@ bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { return true; // Half can be promoted to float. - if (FromBuiltin->getKind() == BuiltinType::Half && + if (!getLangOpts().NativeHalfType && + FromBuiltin->getKind() == BuiltinType::Half && ToBuiltin->getKind() == BuiltinType::Float) return true; } @@ -2880,8 +2949,8 @@ IsInitializerListConstructorConversion(Sema &S, Expr *From, QualType ToType, UserDefinedConversionSequence &User, OverloadCandidateSet &CandidateSet, bool AllowExplicit) { - DeclContext::lookup_iterator Con, ConEnd; - for (llvm::tie(Con, ConEnd) = S.LookupConstructors(To); + DeclContext::lookup_result R = S.LookupConstructors(To); + for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); Con != ConEnd; ++Con) { NamedDecl *D = *Con; DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); @@ -2980,7 +3049,7 @@ IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, S.IsDerivedFrom(From->getType(), ToType))) ConstructorsOnly = true; - S.RequireCompleteType(From->getLocStart(), ToType, 0); + S.RequireCompleteType(From->getExprLoc(), ToType, 0); // RequireCompleteType may have returned true due to some invalid decl // during template instantiation, but ToType may be complete enough now // to try to recover. @@ -3008,8 +3077,8 @@ IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, ListInitializing = true; } - DeclContext::lookup_iterator Con, ConEnd; - for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); + DeclContext::lookup_result R = S.LookupConstructors(ToRecordDecl); + for (DeclContext::lookup_iterator Con = R.begin(), ConEnd = R.end(); Con != ConEnd; ++Con) { NamedDecl *D = *Con; DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); @@ -3065,10 +3134,11 @@ IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, if (CXXRecordDecl *FromRecordDecl = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { // Add all of the conversion functions as candidates. - const UnresolvedSetImpl *Conversions - = FromRecordDecl->getVisibleConversionFunctions(); - for (UnresolvedSetImpl::iterator I = Conversions->begin(), - E = Conversions->end(); I != E; ++I) { + std::pair<CXXRecordDecl::conversion_iterator, + CXXRecordDecl::conversion_iterator> + Conversions = FromRecordDecl->getVisibleConversionFunctions(); + for (CXXRecordDecl::conversion_iterator + I = Conversions.first, E = Conversions.second; I != E; ++I) { DeclAccessPair FoundDecl = I.getPair(); NamedDecl *D = FoundDecl.getDecl(); CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); @@ -3198,7 +3268,7 @@ static ImplicitConversionSequence::CompareKind compareConversionFunctions(Sema &S, FunctionDecl *Function1, FunctionDecl *Function2) { - if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus0x) + if (!S.getLangOpts().ObjC1 || !S.getLangOpts().CPlusPlus11) return ImplicitConversionSequence::Indistinguishable; // Objective-C++: @@ -3868,6 +3938,15 @@ CompareDerivedToBaseConversions(Sema &S, return ImplicitConversionSequence::Indistinguishable; } +/// \brief Determine whether the given type is valid, e.g., it is not an invalid +/// C++ class. +static bool isTypeValid(QualType T) { + if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) + return !Record->isInvalidDecl(); + + return true; +} + /// CompareReferenceRelationship - Compare the two types T1 and T2 to /// determine whether they are reference-related, /// reference-compatible, reference-compatible with added @@ -3901,7 +3980,8 @@ Sema::CompareReferenceRelationship(SourceLocation Loc, if (UnqualT1 == UnqualT2) { // Nothing to do. } else if (!RequireCompleteType(Loc, OrigT2, 0) && - IsDerivedFrom(UnqualT2, UnqualT1)) + isTypeValid(UnqualT1) && isTypeValid(UnqualT2) && + IsDerivedFrom(UnqualT2, UnqualT1)) DerivedToBase = true; else if (UnqualT1->isObjCObjectOrInterfaceType() && UnqualT2->isObjCObjectOrInterfaceType() && @@ -3959,10 +4039,11 @@ FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); OverloadCandidateSet CandidateSet(DeclLoc); - const UnresolvedSetImpl *Conversions - = T2RecordDecl->getVisibleConversionFunctions(); - for (UnresolvedSetImpl::iterator I = Conversions->begin(), - E = Conversions->end(); I != E; ++I) { + std::pair<CXXRecordDecl::conversion_iterator, + CXXRecordDecl::conversion_iterator> + Conversions = T2RecordDecl->getVisibleConversionFunctions(); + for (CXXRecordDecl::conversion_iterator + I = Conversions.first, E = Conversions.second; I != E; ++I) { NamedDecl *D = *I; CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); if (isa<UsingShadowDecl>(D)) @@ -4213,7 +4294,7 @@ TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, // allow the use of rvalue references in C++98/03 for the benefit of // standard library implementors; therefore, we need the xvalue check here. ICS.Standard.DirectBinding = - S.getLangOpts().CPlusPlus0x || + S.getLangOpts().CPlusPlus11 || (InitCategory.isPRValue() && !T2->isRecordType()); ICS.Standard.IsLvalueReference = !isRValRef; ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); @@ -4374,7 +4455,7 @@ TryListConversion(Sema &S, InitListExpr *From, QualType ToType, bool toStdInitializerList = false; QualType X; if (ToType->isArrayType()) - X = S.Context.getBaseElementType(ToType); + X = S.Context.getAsArrayType(ToType)->getElementType(); else toStdInitializerList = S.isStdInitializerList(ToType, &X); if (!X.isNull()) { @@ -4595,7 +4676,7 @@ static bool TryCopyInitialization(const CanQualType FromQTy, /// parameter of the given member function (@c Method) from the /// expression @p From. static ImplicitConversionSequence -TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, +TryObjectArgumentInitialization(Sema &S, QualType FromType, Expr::Classification FromClassification, CXXMethodDecl *Method, CXXRecordDecl *ActingContext) { @@ -4611,7 +4692,6 @@ TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, ImplicitConversionSequence ICS; // We need to have an object of class type. - QualType FromType = OrigFromType; if (const PointerType *PT = FromType->getAs<PointerType>()) { FromType = PT->getPointeeType(); @@ -4646,7 +4726,7 @@ TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, != FromTypeCanon.getLocalCVRQualifiers() && !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { ICS.setBad(BadConversionSequence::bad_qualifiers, - OrigFromType, ImplicitParamType); + FromType, ImplicitParamType); return ICS; } @@ -4811,6 +4891,7 @@ static bool CheckConvertedConstantConversions(Sema &S, case ICK_Identity: case ICK_Integral_Promotion: case ICK_Integral_Conversion: + case ICK_Zero_Event_Conversion: return true; case ICK_Boolean_Conversion: @@ -4857,7 +4938,7 @@ static bool CheckConvertedConstantConversions(Sema &S, ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, llvm::APSInt &Value, CCEKind CCE) { - assert(LangOpts.CPlusPlus0x && "converted constant expression outside C++11"); + assert(LangOpts.CPlusPlus11 && "converted constant expression outside C++11"); assert(T->isIntegralOrEnumerationType() && "unexpected converted const type"); if (checkPlaceholderForOverload(*this, From)) @@ -4939,7 +5020,7 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, } // Check the expression is a constant expression. - llvm::SmallVector<PartialDiagnosticAt, 8> Notes; + SmallVector<PartialDiagnosticAt, 8> Notes; Expr::EvalResult Eval; Eval.Diag = &Notes; @@ -5104,15 +5185,15 @@ Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, // Look for a conversion to an integral or enumeration type. UnresolvedSet<4> ViableConversions; UnresolvedSet<4> ExplicitConversions; - const UnresolvedSetImpl *Conversions + std::pair<CXXRecordDecl::conversion_iterator, + CXXRecordDecl::conversion_iterator> Conversions = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); - bool HadMultipleCandidates = (Conversions->size() > 1); + bool HadMultipleCandidates + = (std::distance(Conversions.first, Conversions.second) > 1); - for (UnresolvedSetImpl::iterator I = Conversions->begin(), - E = Conversions->end(); - I != E; - ++I) { + for (CXXRecordDecl::conversion_iterator + I = Conversions.first, E = Conversions.second; I != E; ++I) { if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) { if (isIntegralOrEnumerationType( @@ -5230,7 +5311,7 @@ Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, void Sema::AddOverloadCandidate(FunctionDecl *Function, DeclAccessPair FoundDecl, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet& CandidateSet, bool SuppressUserConversions, bool PartialOverloading, @@ -5353,7 +5434,7 @@ Sema::AddOverloadCandidate(FunctionDecl *Function, /// \brief Add all of the function declarations in the given function set to /// the overload canddiate set. void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet& CandidateSet, bool SuppressUserConversions, TemplateArgumentListInfo *ExplicitTemplateArgs) { @@ -5428,7 +5509,7 @@ void Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext, QualType ObjectType, Expr::Classification ObjectClassification, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet& CandidateSet, bool SuppressUserConversions) { const FunctionProtoType* Proto @@ -5532,7 +5613,7 @@ Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ObjectType, Expr::Classification ObjectClassification, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet& CandidateSet, bool SuppressUserConversions) { if (!CandidateSet.isNewCandidate(MethodTmpl)) @@ -5582,7 +5663,7 @@ void Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, DeclAccessPair FoundDecl, TemplateArgumentListInfo *ExplicitTemplateArgs, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet& CandidateSet, bool SuppressUserConversions) { if (!CandidateSet.isNewCandidate(FunctionTemplate)) @@ -5816,7 +5897,7 @@ void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, CXXRecordDecl *ActingContext, const FunctionProtoType *Proto, Expr *Object, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet& CandidateSet) { if (!CandidateSet.isNewCandidate(Conversion)) return; @@ -6286,10 +6367,11 @@ BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, return; CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); - const UnresolvedSetImpl *Conversions - = ClassDecl->getVisibleConversionFunctions(); - for (UnresolvedSetImpl::iterator I = Conversions->begin(), - E = Conversions->end(); I != E; ++I) { + std::pair<CXXRecordDecl::conversion_iterator, + CXXRecordDecl::conversion_iterator> + Conversions = ClassDecl->getVisibleConversionFunctions(); + for (CXXRecordDecl::conversion_iterator + I = Conversions.first, E = Conversions.second; I != E; ++I) { NamedDecl *D = I.getDecl(); if (isa<UsingShadowDecl>(D)) D = cast<UsingShadowDecl>(D)->getTargetDecl(); @@ -6355,11 +6437,12 @@ static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { if (!ClassDecl->hasDefinition()) return VRQuals; - const UnresolvedSetImpl *Conversions = - ClassDecl->getVisibleConversionFunctions(); + std::pair<CXXRecordDecl::conversion_iterator, + CXXRecordDecl::conversion_iterator> + Conversions = ClassDecl->getVisibleConversionFunctions(); - for (UnresolvedSetImpl::iterator I = Conversions->begin(), - E = Conversions->end(); I != E; ++I) { + for (CXXRecordDecl::conversion_iterator + I = Conversions.first, E = Conversions.second; I != E; ++I) { NamedDecl *D = I.getDecl(); if (isa<UsingShadowDecl>(D)) D = cast<UsingShadowDecl>(D)->getTargetDecl(); @@ -7449,7 +7532,7 @@ public: S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); } - if (S.getLangOpts().CPlusPlus0x) { + if (S.getLangOpts().CPlusPlus11) { for (BuiltinCandidateTypeSet::iterator Enum = CandidateTypes[ArgIdx].enumeration_begin(), EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); @@ -7667,7 +7750,7 @@ Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, void Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, bool Operator, SourceLocation Loc, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, TemplateArgumentListInfo *ExplicitTemplateArgs, OverloadCandidateSet& CandidateSet, bool PartialOverloading) { @@ -8380,7 +8463,7 @@ void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, case Sema::TDK_SubstitutionFailure: { // Format the template argument list into the argument string. - llvm::SmallString<128> TemplateArgString; + SmallString<128> TemplateArgString; if (TemplateArgumentList *Args = Cand->DeductionFailure.getTemplateArgumentList()) { TemplateArgString = " "; @@ -8402,7 +8485,7 @@ void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, // Format the SFINAE diagnostic into the argument string. // FIXME: Add a general mechanism to include a PartialDiagnostic *'s // formatted message in another diagnostic. - llvm::SmallString<128> SFINAEArgString; + SmallString<128> SFINAEArgString; SourceRange R; if (PDiag) { SFINAEArgString = ": "; @@ -8416,10 +8499,25 @@ void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, return; } + case Sema::TDK_FailedOverloadResolution: { + OverloadExpr::FindResult R = + OverloadExpr::find(Cand->DeductionFailure.getExpr()); + S.Diag(Fn->getLocation(), + diag::note_ovl_candidate_failed_overload_resolution) + << R.Expression->getName(); + return; + } + + case Sema::TDK_NonDeducedMismatch: + // FIXME: Provide a source location to indicate what we couldn't match. + S.Diag(Fn->getLocation(), diag::note_ovl_candidate_non_deduced_mismatch) + << *Cand->DeductionFailure.getFirstArg() + << *Cand->DeductionFailure.getSecondArg(); + return; + // TODO: diagnose these individually, then kill off // note_ovl_candidate_bad_deduction, which is uselessly vague. - case Sema::TDK_NonDeducedMismatch: - case Sema::TDK_FailedOverloadResolution: + case Sema::TDK_MiscellaneousDeductionFailure: S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); MaybeEmitInheritedConstructorNote(S, Fn); return; @@ -8597,6 +8695,7 @@ RankDeductionFailure(const OverloadCandidate::DeductionFailureInfo &DFI) { case Sema::TDK_SubstitutionFailure: case Sema::TDK_NonDeducedMismatch: + case Sema::TDK_MiscellaneousDeductionFailure: return 3; case Sema::TDK_InstantiationDepth: @@ -8721,7 +8820,7 @@ struct CompareOverloadCandidatesForDisplay { /// CompleteNonViableCandidate - Normally, overload resolution only /// computes up to the first. Produces the FixIt set if possible. void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, - llvm::ArrayRef<Expr *> Args) { + ArrayRef<Expr *> Args) { assert(!Cand->Viable); // Don't do anything on failures other than bad conversion. @@ -8809,7 +8908,7 @@ void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, /// set. void OverloadCandidateSet::NoteCandidates(Sema &S, OverloadCandidateDisplayKind OCD, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, StringRef Opc, SourceLocation OpLoc) { // Sort the candidates by viability and position. Sorting directly would @@ -9400,7 +9499,7 @@ bool Sema::ResolveAndFixSingleFunctionTemplateSpecialization( static void AddOverloadedCallCandidate(Sema &S, DeclAccessPair FoundDecl, TemplateArgumentListInfo *ExplicitTemplateArgs, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet &CandidateSet, bool PartialOverloading, bool KnownValid) { @@ -9431,7 +9530,7 @@ static void AddOverloadedCallCandidate(Sema &S, /// \brief Add the overload candidates named by callee and/or found by argument /// dependent lookup to the given overload set. void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, - llvm::ArrayRef<Expr *> Args, + ArrayRef<Expr *> Args, OverloadCandidateSet &CandidateSet, bool PartialOverloading) { @@ -9495,7 +9594,7 @@ static bool DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, const CXXScopeSpec &SS, LookupResult &R, TemplateArgumentListInfo *ExplicitTemplateArgs, - llvm::ArrayRef<Expr *> Args) { + ArrayRef<Expr *> Args) { if (SemaRef.ActiveTemplateInstantiations.empty() || !SS.isEmpty()) return false; @@ -9536,18 +9635,23 @@ DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, SemaRef.FindAssociatedClassesAndNamespaces(FnLoc, Args, AssociatedNamespaces, AssociatedClasses); - // Never suggest declaring a function within namespace 'std'. Sema::AssociatedNamespaceSet SuggestedNamespaces; - if (DeclContext *Std = SemaRef.getStdNamespace()) { - for (Sema::AssociatedNamespaceSet::iterator - it = AssociatedNamespaces.begin(), - end = AssociatedNamespaces.end(); it != end; ++it) { - if (!Std->Encloses(*it)) - SuggestedNamespaces.insert(*it); - } - } else { - // Lacking the 'std::' namespace, use all of the associated namespaces. - SuggestedNamespaces = AssociatedNamespaces; + DeclContext *Std = SemaRef.getStdNamespace(); + for (Sema::AssociatedNamespaceSet::iterator + it = AssociatedNamespaces.begin(), + end = AssociatedNamespaces.end(); it != end; ++it) { + // Never suggest declaring a function within namespace 'std'. + if (Std && Std->Encloses(*it)) + continue; + + // Never suggest declaring a function within a namespace with a reserved + // name, like __gnu_cxx. + NamespaceDecl *NS = dyn_cast<NamespaceDecl>(*it); + if (NS && + NS->getQualifiedNameAsString().find("__") != std::string::npos) + continue; + + SuggestedNamespaces.insert(*it); } SemaRef.Diag(R.getNameLoc(), diag::err_not_found_by_two_phase_lookup) @@ -9587,7 +9691,7 @@ DiagnoseTwoPhaseLookup(Sema &SemaRef, SourceLocation FnLoc, static bool DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, SourceLocation OpLoc, - llvm::ArrayRef<Expr *> Args) { + ArrayRef<Expr *> Args) { DeclarationName OpName = SemaRef.Context.DeclarationNames.getCXXOperatorName(Op); LookupResult R(SemaRef, OpName, OpLoc, Sema::LookupOperatorName); @@ -9827,7 +9931,6 @@ static ExprResult FinishOverloadedCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, switch (OverloadResult) { case OR_Success: { FunctionDecl *FDecl = (*Best)->Function; - SemaRef.MarkFunctionReferenced(Fn->getExprLoc(), FDecl); SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc()); Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); @@ -10015,8 +10118,6 @@ Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, // We matched an overloaded operator. Build a call to that // operator. - MarkFunctionReferenced(OpLoc, FnDecl); - // Convert the arguments. if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); @@ -10040,15 +10141,13 @@ Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, Input = InputInit.take(); } - DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); - // Determine the result type. QualType ResultTy = FnDecl->getResultType(); ExprValueKind VK = Expr::getValueKindForType(ResultTy); ResultTy = ResultTy.getNonLValueExprType(Context); // Build the actual expression node. - ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, + ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, Best->FoundDecl, HadMultipleCandidates, OpLoc); if (FnExpr.isInvalid()) return ExprError(); @@ -10240,8 +10339,6 @@ Sema::CreateOverloadedBinOp(SourceLocation OpLoc, // We matched an overloaded operator. Build a call to that // operator. - MarkFunctionReferenced(OpLoc, FnDecl); - // Convert the arguments. if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { // Best->Access is only meaningful for class members. @@ -10282,8 +10379,6 @@ Sema::CreateOverloadedBinOp(SourceLocation OpLoc, Args[1] = RHS = Arg1.takeAs<Expr>(); } - DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); - // Determine the result type. QualType ResultTy = FnDecl->getResultType(); ExprValueKind VK = Expr::getValueKindForType(ResultTy); @@ -10291,6 +10386,7 @@ Sema::CreateOverloadedBinOp(SourceLocation OpLoc, // Build the actual expression node. ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, + Best->FoundDecl, HadMultipleCandidates, OpLoc); if (FnExpr.isInvalid()) return ExprError(); @@ -10304,6 +10400,13 @@ Sema::CreateOverloadedBinOp(SourceLocation OpLoc, FnDecl)) return ExprError(); + ArrayRef<const Expr *> ArgsArray(Args, 2); + // Cut off the implicit 'this'. + if (isa<CXXMethodDecl>(FnDecl)) + ArgsArray = ArgsArray.slice(1); + checkCall(FnDecl, ArgsArray, 0, isa<CXXMethodDecl>(FnDecl), OpLoc, + TheCall->getSourceRange(), VariadicDoesNotApply); + return MaybeBindToTemporary(TheCall); } else { // We matched a built-in operator. Convert the arguments, then @@ -10376,16 +10479,13 @@ Sema::CreateOverloadedBinOp(SourceLocation OpLoc, if (isImplicitlyDeleted(Best->Function)) { CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); Diag(OpLoc, diag::err_ovl_deleted_special_oper) - << getSpecialMember(Method) - << BinaryOperator::getOpcodeStr(Opc) - << getDeletedOrUnavailableSuffix(Best->Function); + << Context.getRecordType(Method->getParent()) + << getSpecialMember(Method); - if (getSpecialMember(Method) != CXXInvalid) { - // The user probably meant to call this special member. Just - // explain why it's deleted. - NoteDeletedFunction(Method); - return ExprError(); - } + // The user probably meant to call this special member. Just + // explain why it's deleted. + NoteDeletedFunction(Method); + return ExprError(); } else { Diag(OpLoc, diag::err_ovl_deleted_oper) << Best->Function->isDeleted() @@ -10463,10 +10563,7 @@ Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, // We matched an overloaded operator. Build a call to that // operator. - MarkFunctionReferenced(LLoc, FnDecl); - CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); - DiagnoseUseOfDecl(Best->FoundDecl, LLoc); // Convert the arguments. CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); @@ -10498,6 +10595,7 @@ Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, DeclarationNameInfo OpLocInfo(OpName, LLoc); OpLocInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); ExprResult FnExpr = CreateFunctionRefExpr(*this, FnDecl, + Best->FoundDecl, HadMultipleCandidates, OpLocInfo.getLoc(), OpLocInfo.getInfo()); @@ -10715,7 +10813,6 @@ Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, Best)) { case OR_Success: Method = cast<CXXMethodDecl>(Best->Function); - MarkFunctionReferenced(UnresExpr->getMemberLoc(), Method); FoundDecl = Best->FoundDecl; CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); @@ -10880,10 +10977,11 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, // functions for each conversion function declared in an // accessible base class provided the function is not hidden // within T by another intervening declaration. - const UnresolvedSetImpl *Conversions + std::pair<CXXRecordDecl::conversion_iterator, + CXXRecordDecl::conversion_iterator> Conversions = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); - for (UnresolvedSetImpl::iterator I = Conversions->begin(), - E = Conversions->end(); I != E; ++I) { + for (CXXRecordDecl::conversion_iterator + I = Conversions.first, E = Conversions.second; I != E; ++I) { NamedDecl *D = *I; CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); if (isa<UsingShadowDecl>(D)) @@ -10989,9 +11087,7 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, RParenLoc); } - MarkFunctionReferenced(LParenLoc, Best->Function); CheckMemberOperatorAccess(LParenLoc, Object.get(), 0, Best->FoundDecl); - DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); // We found an overloaded operator(). Build a CXXOperatorCallExpr // that calls this method, using Object for the implicit object @@ -11025,7 +11121,7 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Obj, DeclarationNameInfo OpLocInfo( Context.DeclarationNames.getCXXOperatorName(OO_Call), LParenLoc); OpLocInfo.setCXXOperatorNameRange(SourceRange(LParenLoc, RParenLoc)); - ExprResult NewFn = CreateFunctionRefExpr(*this, Method, + ExprResult NewFn = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, HadMultipleCandidates, OpLocInfo.getLoc(), OpLocInfo.getInfo()); @@ -11190,9 +11286,7 @@ Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { return ExprError(); } - MarkFunctionReferenced(OpLoc, Best->Function); CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); - DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); // Convert the object parameter. CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); @@ -11204,7 +11298,7 @@ Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { Base = BaseResult.take(); // Build the operator call. - ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, + ExprResult FnExpr = CreateFunctionRefExpr(*this, Method, Best->FoundDecl, HadMultipleCandidates, OpLoc); if (FnExpr.isInvalid()) return ExprError(); @@ -11259,10 +11353,8 @@ ExprResult Sema::BuildLiteralOperatorCall(LookupResult &R, } FunctionDecl *FD = Best->Function; - MarkFunctionReferenced(UDSuffixLoc, FD); - DiagnoseUseOfDecl(Best->FoundDecl, UDSuffixLoc); - - ExprResult Fn = CreateFunctionRefExpr(*this, FD, HadMultipleCandidates, + ExprResult Fn = CreateFunctionRefExpr(*this, FD, Best->FoundDecl, + HadMultipleCandidates, SuffixInfo.getLoc(), SuffixInfo.getInfo()); if (Fn.isInvalid()) |