1 //===--- Overload.h - C++ Overloading ---------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the data structures and types used in C++ 11 // overload resolution. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_SEMA_OVERLOAD_H 16 #define LLVM_CLANG_SEMA_OVERLOAD_H 17 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclTemplate.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/TemplateBase.h" 22 #include "clang/AST/Type.h" 23 #include "clang/AST/UnresolvedSet.h" 24 #include "clang/Sema/SemaFixItUtils.h" 25 #include "llvm/ADT/SmallPtrSet.h" 26 #include "llvm/ADT/SmallVector.h" 27 #include "llvm/Support/Allocator.h" 28 29 namespace clang { 30 class ASTContext; 31 class CXXConstructorDecl; 32 class CXXConversionDecl; 33 class FunctionDecl; 34 class Sema; 35 36 /// OverloadingResult - Capture the result of performing overload 37 /// resolution. 38 enum OverloadingResult { 39 OR_Success, ///< Overload resolution succeeded. 40 OR_No_Viable_Function, ///< No viable function found. 41 OR_Ambiguous, ///< Ambiguous candidates found. 42 OR_Deleted ///< Succeeded, but refers to a deleted function. 43 }; 44 45 enum OverloadCandidateDisplayKind { 46 /// Requests that all candidates be shown. Viable candidates will 47 /// be printed first. 48 OCD_AllCandidates, 49 50 /// Requests that only viable candidates be shown. 51 OCD_ViableCandidates 52 }; 53 54 /// ImplicitConversionKind - The kind of implicit conversion used to 55 /// convert an argument to a parameter's type. The enumerator values 56 /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that 57 /// better conversion kinds have smaller values. 58 enum ImplicitConversionKind { 59 ICK_Identity = 0, ///< Identity conversion (no conversion) 60 ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1) 61 ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2) 62 ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3) 63 ICK_NoReturn_Adjustment, ///< Removal of noreturn from a type (Clang) 64 ICK_Qualification, ///< Qualification conversions (C++ 4.4) 65 ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5) 66 ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6) 67 ICK_Complex_Promotion, ///< Complex promotions (Clang extension) 68 ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7) 69 ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8) 70 ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6) 71 ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9) 72 ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10) 73 ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11) 74 ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12) 75 ICK_Compatible_Conversion, ///< Conversions between compatible types in C99 76 ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics]) 77 ICK_Vector_Conversion, ///< Vector conversions 78 ICK_Vector_Splat, ///< A vector splat from an arithmetic type 79 ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7) 80 ICK_Block_Pointer_Conversion, ///< Block Pointer conversions 81 ICK_TransparentUnionConversion, /// Transparent Union Conversions 82 ICK_Writeback_Conversion, ///< Objective-C ARC writeback conversion 83 ICK_Num_Conversion_Kinds ///< The number of conversion kinds 84 }; 85 86 /// ImplicitConversionCategory - The category of an implicit 87 /// conversion kind. The enumerator values match with Table 9 of 88 /// (C++ 13.3.3.1.1) and are listed such that better conversion 89 /// categories have smaller values. 90 enum ImplicitConversionCategory { 91 ICC_Identity = 0, ///< Identity 92 ICC_Lvalue_Transformation, ///< Lvalue transformation 93 ICC_Qualification_Adjustment, ///< Qualification adjustment 94 ICC_Promotion, ///< Promotion 95 ICC_Conversion ///< Conversion 96 }; 97 98 ImplicitConversionCategory 99 GetConversionCategory(ImplicitConversionKind Kind); 100 101 /// ImplicitConversionRank - The rank of an implicit conversion 102 /// kind. The enumerator values match with Table 9 of (C++ 103 /// 13.3.3.1.1) and are listed such that better conversion ranks 104 /// have smaller values. 105 enum ImplicitConversionRank { 106 ICR_Exact_Match = 0, ///< Exact Match 107 ICR_Promotion, ///< Promotion 108 ICR_Conversion, ///< Conversion 109 ICR_Complex_Real_Conversion, ///< Complex <-> Real conversion 110 ICR_Writeback_Conversion ///< ObjC ARC writeback conversion 111 }; 112 113 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind); 114 115 /// NarrowingKind - The kind of narrowing conversion being performed by a 116 /// standard conversion sequence according to C++11 [dcl.init.list]p7. 117 enum NarrowingKind { 118 /// Not a narrowing conversion. 119 NK_Not_Narrowing, 120 121 /// A narrowing conversion by virtue of the source and destination types. 122 NK_Type_Narrowing, 123 124 /// A narrowing conversion, because a constant expression got narrowed. 125 NK_Constant_Narrowing, 126 127 /// A narrowing conversion, because a non-constant-expression variable might 128 /// have got narrowed. 129 NK_Variable_Narrowing 130 }; 131 132 /// StandardConversionSequence - represents a standard conversion 133 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence 134 /// contains between zero and three conversions. If a particular 135 /// conversion is not needed, it will be set to the identity conversion 136 /// (ICK_Identity). Note that the three conversions are 137 /// specified as separate members (rather than in an array) so that 138 /// we can keep the size of a standard conversion sequence to a 139 /// single word. 140 class StandardConversionSequence { 141 public: 142 /// First -- The first conversion can be an lvalue-to-rvalue 143 /// conversion, array-to-pointer conversion, or 144 /// function-to-pointer conversion. 145 ImplicitConversionKind First : 8; 146 147 /// Second - The second conversion can be an integral promotion, 148 /// floating point promotion, integral conversion, floating point 149 /// conversion, floating-integral conversion, pointer conversion, 150 /// pointer-to-member conversion, or boolean conversion. 151 ImplicitConversionKind Second : 8; 152 153 /// Third - The third conversion can be a qualification conversion. 154 ImplicitConversionKind Third : 8; 155 156 /// \brief Whether this is the deprecated conversion of a 157 /// string literal to a pointer to non-const character data 158 /// (C++ 4.2p2). 159 unsigned DeprecatedStringLiteralToCharPtr : 1; 160 161 /// \brief Whether the qualification conversion involves a change in the 162 /// Objective-C lifetime (for automatic reference counting). 163 unsigned QualificationIncludesObjCLifetime : 1; 164 165 /// IncompatibleObjC - Whether this is an Objective-C conversion 166 /// that we should warn about (if we actually use it). 167 unsigned IncompatibleObjC : 1; 168 169 /// ReferenceBinding - True when this is a reference binding 170 /// (C++ [over.ics.ref]). 171 unsigned ReferenceBinding : 1; 172 173 /// DirectBinding - True when this is a reference binding that is a 174 /// direct binding (C++ [dcl.init.ref]). 175 unsigned DirectBinding : 1; 176 177 /// \brief Whether this is an lvalue reference binding (otherwise, it's 178 /// an rvalue reference binding). 179 unsigned IsLvalueReference : 1; 180 181 /// \brief Whether we're binding to a function lvalue. 182 unsigned BindsToFunctionLvalue : 1; 183 184 /// \brief Whether we're binding to an rvalue. 185 unsigned BindsToRvalue : 1; 186 187 /// \brief Whether this binds an implicit object argument to a 188 /// non-static member function without a ref-qualifier. 189 unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1; 190 191 /// \brief Whether this binds a reference to an object with a different 192 /// Objective-C lifetime qualifier. 193 unsigned ObjCLifetimeConversionBinding : 1; 194 195 /// FromType - The type that this conversion is converting 196 /// from. This is an opaque pointer that can be translated into a 197 /// QualType. 198 void *FromTypePtr; 199 200 /// ToType - The types that this conversion is converting to in 201 /// each step. This is an opaque pointer that can be translated 202 /// into a QualType. 203 void *ToTypePtrs[3]; 204 205 /// CopyConstructor - The copy constructor that is used to perform 206 /// this conversion, when the conversion is actually just the 207 /// initialization of an object via copy constructor. Such 208 /// conversions are either identity conversions or derived-to-base 209 /// conversions. 210 CXXConstructorDecl *CopyConstructor; 211 setFromType(QualType T)212 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } setToType(unsigned Idx,QualType T)213 void setToType(unsigned Idx, QualType T) { 214 assert(Idx < 3 && "To type index is out of range"); 215 ToTypePtrs[Idx] = T.getAsOpaquePtr(); 216 } setAllToTypes(QualType T)217 void setAllToTypes(QualType T) { 218 ToTypePtrs[0] = T.getAsOpaquePtr(); 219 ToTypePtrs[1] = ToTypePtrs[0]; 220 ToTypePtrs[2] = ToTypePtrs[0]; 221 } 222 getFromType()223 QualType getFromType() const { 224 return QualType::getFromOpaquePtr(FromTypePtr); 225 } getToType(unsigned Idx)226 QualType getToType(unsigned Idx) const { 227 assert(Idx < 3 && "To type index is out of range"); 228 return QualType::getFromOpaquePtr(ToTypePtrs[Idx]); 229 } 230 231 void setAsIdentityConversion(); 232 isIdentityConversion()233 bool isIdentityConversion() const { 234 return Second == ICK_Identity && Third == ICK_Identity; 235 } 236 237 ImplicitConversionRank getRank() const; 238 NarrowingKind getNarrowingKind(ASTContext &Context, const Expr *Converted, 239 APValue &ConstantValue, 240 QualType &ConstantType) const; 241 bool isPointerConversionToBool() const; 242 bool isPointerConversionToVoidPointer(ASTContext& Context) const; 243 void DebugPrint() const; 244 }; 245 246 /// UserDefinedConversionSequence - Represents a user-defined 247 /// conversion sequence (C++ 13.3.3.1.2). 248 struct UserDefinedConversionSequence { 249 /// \brief Represents the standard conversion that occurs before 250 /// the actual user-defined conversion. 251 /// 252 /// C++11 13.3.3.1.2p1: 253 /// If the user-defined conversion is specified by a constructor 254 /// (12.3.1), the initial standard conversion sequence converts 255 /// the source type to the type required by the argument of the 256 /// constructor. If the user-defined conversion is specified by 257 /// a conversion function (12.3.2), the initial standard 258 /// conversion sequence converts the source type to the implicit 259 /// object parameter of the conversion function. 260 StandardConversionSequence Before; 261 262 /// EllipsisConversion - When this is true, it means user-defined 263 /// conversion sequence starts with a ... (elipsis) conversion, instead of 264 /// a standard conversion. In this case, 'Before' field must be ignored. 265 // FIXME. I much rather put this as the first field. But there seems to be 266 // a gcc code gen. bug which causes a crash in a test. Putting it here seems 267 // to work around the crash. 268 bool EllipsisConversion : 1; 269 270 /// HadMultipleCandidates - When this is true, it means that the 271 /// conversion function was resolved from an overloaded set having 272 /// size greater than 1. 273 bool HadMultipleCandidates : 1; 274 275 /// After - Represents the standard conversion that occurs after 276 /// the actual user-defined conversion. 277 StandardConversionSequence After; 278 279 /// ConversionFunction - The function that will perform the 280 /// user-defined conversion. Null if the conversion is an 281 /// aggregate initialization from an initializer list. 282 FunctionDecl* ConversionFunction; 283 284 /// \brief The declaration that we found via name lookup, which might be 285 /// the same as \c ConversionFunction or it might be a using declaration 286 /// that refers to \c ConversionFunction. 287 DeclAccessPair FoundConversionFunction; 288 289 void DebugPrint() const; 290 }; 291 292 /// Represents an ambiguous user-defined conversion sequence. 293 struct AmbiguousConversionSequence { 294 typedef SmallVector<FunctionDecl*, 4> ConversionSet; 295 296 void *FromTypePtr; 297 void *ToTypePtr; 298 char Buffer[sizeof(ConversionSet)]; 299 getFromTypeAmbiguousConversionSequence300 QualType getFromType() const { 301 return QualType::getFromOpaquePtr(FromTypePtr); 302 } getToTypeAmbiguousConversionSequence303 QualType getToType() const { 304 return QualType::getFromOpaquePtr(ToTypePtr); 305 } setFromTypeAmbiguousConversionSequence306 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } setToTypeAmbiguousConversionSequence307 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); } 308 conversionsAmbiguousConversionSequence309 ConversionSet &conversions() { 310 return *reinterpret_cast<ConversionSet*>(Buffer); 311 } 312 conversionsAmbiguousConversionSequence313 const ConversionSet &conversions() const { 314 return *reinterpret_cast<const ConversionSet*>(Buffer); 315 } 316 addConversionAmbiguousConversionSequence317 void addConversion(FunctionDecl *D) { 318 conversions().push_back(D); 319 } 320 321 typedef ConversionSet::iterator iterator; beginAmbiguousConversionSequence322 iterator begin() { return conversions().begin(); } endAmbiguousConversionSequence323 iterator end() { return conversions().end(); } 324 325 typedef ConversionSet::const_iterator const_iterator; beginAmbiguousConversionSequence326 const_iterator begin() const { return conversions().begin(); } endAmbiguousConversionSequence327 const_iterator end() const { return conversions().end(); } 328 329 void construct(); 330 void destruct(); 331 void copyFrom(const AmbiguousConversionSequence &); 332 }; 333 334 /// BadConversionSequence - Records information about an invalid 335 /// conversion sequence. 336 struct BadConversionSequence { 337 enum FailureKind { 338 no_conversion, 339 unrelated_class, 340 suppressed_user, 341 bad_qualifiers, 342 lvalue_ref_to_rvalue, 343 rvalue_ref_to_lvalue 344 }; 345 346 // This can be null, e.g. for implicit object arguments. 347 Expr *FromExpr; 348 349 FailureKind Kind; 350 351 private: 352 // The type we're converting from (an opaque QualType). 353 void *FromTy; 354 355 // The type we're converting to (an opaque QualType). 356 void *ToTy; 357 358 public: initBadConversionSequence359 void init(FailureKind K, Expr *From, QualType To) { 360 init(K, From->getType(), To); 361 FromExpr = From; 362 } initBadConversionSequence363 void init(FailureKind K, QualType From, QualType To) { 364 Kind = K; 365 FromExpr = 0; 366 setFromType(From); 367 setToType(To); 368 } 369 getFromTypeBadConversionSequence370 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); } getToTypeBadConversionSequence371 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); } 372 setFromExprBadConversionSequence373 void setFromExpr(Expr *E) { 374 FromExpr = E; 375 setFromType(E->getType()); 376 } setFromTypeBadConversionSequence377 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); } setToTypeBadConversionSequence378 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); } 379 }; 380 381 /// ImplicitConversionSequence - Represents an implicit conversion 382 /// sequence, which may be a standard conversion sequence 383 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2), 384 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3). 385 class ImplicitConversionSequence { 386 public: 387 /// Kind - The kind of implicit conversion sequence. BadConversion 388 /// specifies that there is no conversion from the source type to 389 /// the target type. AmbiguousConversion represents the unique 390 /// ambiguous conversion (C++0x [over.best.ics]p10). 391 enum Kind { 392 StandardConversion = 0, 393 UserDefinedConversion, 394 AmbiguousConversion, 395 EllipsisConversion, 396 BadConversion 397 }; 398 399 private: 400 enum { 401 Uninitialized = BadConversion + 1 402 }; 403 404 /// ConversionKind - The kind of implicit conversion sequence. 405 unsigned ConversionKind : 30; 406 407 /// \brief Whether the argument is an initializer list. 408 bool ListInitializationSequence : 1; 409 410 /// \brief Whether the target is really a std::initializer_list, and the 411 /// sequence only represents the worst element conversion. 412 bool StdInitializerListElement : 1; 413 setKind(Kind K)414 void setKind(Kind K) { 415 destruct(); 416 ConversionKind = K; 417 } 418 destruct()419 void destruct() { 420 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct(); 421 } 422 423 public: 424 union { 425 /// When ConversionKind == StandardConversion, provides the 426 /// details of the standard conversion sequence. 427 StandardConversionSequence Standard; 428 429 /// When ConversionKind == UserDefinedConversion, provides the 430 /// details of the user-defined conversion sequence. 431 UserDefinedConversionSequence UserDefined; 432 433 /// When ConversionKind == AmbiguousConversion, provides the 434 /// details of the ambiguous conversion. 435 AmbiguousConversionSequence Ambiguous; 436 437 /// When ConversionKind == BadConversion, provides the details 438 /// of the bad conversion. 439 BadConversionSequence Bad; 440 }; 441 ImplicitConversionSequence()442 ImplicitConversionSequence() 443 : ConversionKind(Uninitialized), ListInitializationSequence(false), 444 StdInitializerListElement(false) 445 {} ~ImplicitConversionSequence()446 ~ImplicitConversionSequence() { 447 destruct(); 448 } ImplicitConversionSequence(const ImplicitConversionSequence & Other)449 ImplicitConversionSequence(const ImplicitConversionSequence &Other) 450 : ConversionKind(Other.ConversionKind), 451 ListInitializationSequence(Other.ListInitializationSequence), 452 StdInitializerListElement(Other.StdInitializerListElement) 453 { 454 switch (ConversionKind) { 455 case Uninitialized: break; 456 case StandardConversion: Standard = Other.Standard; break; 457 case UserDefinedConversion: UserDefined = Other.UserDefined; break; 458 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break; 459 case EllipsisConversion: break; 460 case BadConversion: Bad = Other.Bad; break; 461 } 462 } 463 464 ImplicitConversionSequence & 465 operator=(const ImplicitConversionSequence &Other) { 466 destruct(); 467 new (this) ImplicitConversionSequence(Other); 468 return *this; 469 } 470 getKind()471 Kind getKind() const { 472 assert(isInitialized() && "querying uninitialized conversion"); 473 return Kind(ConversionKind); 474 } 475 476 /// \brief Return a ranking of the implicit conversion sequence 477 /// kind, where smaller ranks represent better conversion 478 /// sequences. 479 /// 480 /// In particular, this routine gives user-defined conversion 481 /// sequences and ambiguous conversion sequences the same rank, 482 /// per C++ [over.best.ics]p10. getKindRank()483 unsigned getKindRank() const { 484 switch (getKind()) { 485 case StandardConversion: 486 return 0; 487 488 case UserDefinedConversion: 489 case AmbiguousConversion: 490 return 1; 491 492 case EllipsisConversion: 493 return 2; 494 495 case BadConversion: 496 return 3; 497 } 498 499 llvm_unreachable("Invalid ImplicitConversionSequence::Kind!"); 500 } 501 isBad()502 bool isBad() const { return getKind() == BadConversion; } isStandard()503 bool isStandard() const { return getKind() == StandardConversion; } isEllipsis()504 bool isEllipsis() const { return getKind() == EllipsisConversion; } isAmbiguous()505 bool isAmbiguous() const { return getKind() == AmbiguousConversion; } isUserDefined()506 bool isUserDefined() const { return getKind() == UserDefinedConversion; } isFailure()507 bool isFailure() const { return isBad() || isAmbiguous(); } 508 509 /// Determines whether this conversion sequence has been 510 /// initialized. Most operations should never need to query 511 /// uninitialized conversions and should assert as above. isInitialized()512 bool isInitialized() const { return ConversionKind != Uninitialized; } 513 514 /// Sets this sequence as a bad conversion for an explicit argument. setBad(BadConversionSequence::FailureKind Failure,Expr * FromExpr,QualType ToType)515 void setBad(BadConversionSequence::FailureKind Failure, 516 Expr *FromExpr, QualType ToType) { 517 setKind(BadConversion); 518 Bad.init(Failure, FromExpr, ToType); 519 } 520 521 /// Sets this sequence as a bad conversion for an implicit argument. setBad(BadConversionSequence::FailureKind Failure,QualType FromType,QualType ToType)522 void setBad(BadConversionSequence::FailureKind Failure, 523 QualType FromType, QualType ToType) { 524 setKind(BadConversion); 525 Bad.init(Failure, FromType, ToType); 526 } 527 setStandard()528 void setStandard() { setKind(StandardConversion); } setEllipsis()529 void setEllipsis() { setKind(EllipsisConversion); } setUserDefined()530 void setUserDefined() { setKind(UserDefinedConversion); } setAmbiguous()531 void setAmbiguous() { 532 if (ConversionKind == AmbiguousConversion) return; 533 ConversionKind = AmbiguousConversion; 534 Ambiguous.construct(); 535 } 536 537 /// \brief Whether this sequence was created by the rules of 538 /// list-initialization sequences. isListInitializationSequence()539 bool isListInitializationSequence() const { 540 return ListInitializationSequence; 541 } 542 setListInitializationSequence()543 void setListInitializationSequence() { 544 ListInitializationSequence = true; 545 } 546 547 /// \brief Whether the target is really a std::initializer_list, and the 548 /// sequence only represents the worst element conversion. isStdInitializerListElement()549 bool isStdInitializerListElement() const { 550 return StdInitializerListElement; 551 } 552 553 void setStdInitializerListElement(bool V = true) { 554 StdInitializerListElement = V; 555 } 556 557 // The result of a comparison between implicit conversion 558 // sequences. Use Sema::CompareImplicitConversionSequences to 559 // actually perform the comparison. 560 enum CompareKind { 561 Better = -1, 562 Indistinguishable = 0, 563 Worse = 1 564 }; 565 566 void DiagnoseAmbiguousConversion(Sema &S, 567 SourceLocation CaretLoc, 568 const PartialDiagnostic &PDiag) const; 569 570 void DebugPrint() const; 571 }; 572 573 enum OverloadFailureKind { 574 ovl_fail_too_many_arguments, 575 ovl_fail_too_few_arguments, 576 ovl_fail_bad_conversion, 577 ovl_fail_bad_deduction, 578 579 /// This conversion candidate was not considered because it 580 /// duplicates the work of a trivial or derived-to-base 581 /// conversion. 582 ovl_fail_trivial_conversion, 583 584 /// This conversion candidate is not viable because its result 585 /// type is not implicitly convertible to the desired type. 586 ovl_fail_bad_final_conversion, 587 588 /// This conversion function template specialization candidate is not 589 /// viable because the final conversion was not an exact match. 590 ovl_fail_final_conversion_not_exact, 591 592 /// (CUDA) This candidate was not viable because the callee 593 /// was not accessible from the caller's target (i.e. host->device, 594 /// global->host, device->host). 595 ovl_fail_bad_target 596 }; 597 598 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3). 599 struct OverloadCandidate { 600 /// Function - The actual function that this candidate 601 /// represents. When NULL, this is a built-in candidate 602 /// (C++ [over.oper]) or a surrogate for a conversion to a 603 /// function pointer or reference (C++ [over.call.object]). 604 FunctionDecl *Function; 605 606 /// FoundDecl - The original declaration that was looked up / 607 /// invented / otherwise found, together with its access. 608 /// Might be a UsingShadowDecl or a FunctionTemplateDecl. 609 DeclAccessPair FoundDecl; 610 611 // BuiltinTypes - Provides the return and parameter types of a 612 // built-in overload candidate. Only valid when Function is NULL. 613 struct { 614 QualType ResultTy; 615 QualType ParamTypes[3]; 616 } BuiltinTypes; 617 618 /// Surrogate - The conversion function for which this candidate 619 /// is a surrogate, but only if IsSurrogate is true. 620 CXXConversionDecl *Surrogate; 621 622 /// Conversions - The conversion sequences used to convert the 623 /// function arguments to the function parameters, the pointer points to a 624 /// fixed size array with NumConversions elements. The memory is owned by 625 /// the OverloadCandidateSet. 626 ImplicitConversionSequence *Conversions; 627 628 /// The FixIt hints which can be used to fix the Bad candidate. 629 ConversionFixItGenerator Fix; 630 631 /// NumConversions - The number of elements in the Conversions array. 632 unsigned NumConversions; 633 634 /// Viable - True to indicate that this overload candidate is viable. 635 bool Viable; 636 637 /// IsSurrogate - True to indicate that this candidate is a 638 /// surrogate for a conversion to a function pointer or reference 639 /// (C++ [over.call.object]). 640 bool IsSurrogate; 641 642 /// IgnoreObjectArgument - True to indicate that the first 643 /// argument's conversion, which for this function represents the 644 /// implicit object argument, should be ignored. This will be true 645 /// when the candidate is a static member function (where the 646 /// implicit object argument is just a placeholder) or a 647 /// non-static member function when the call doesn't have an 648 /// object argument. 649 bool IgnoreObjectArgument; 650 651 /// FailureKind - The reason why this candidate is not viable. 652 /// Actually an OverloadFailureKind. 653 unsigned char FailureKind; 654 655 /// \brief The number of call arguments that were explicitly provided, 656 /// to be used while performing partial ordering of function templates. 657 unsigned ExplicitCallArguments; 658 659 /// A structure used to record information about a failed 660 /// template argument deduction. 661 struct DeductionFailureInfo { 662 /// A Sema::TemplateDeductionResult. 663 unsigned Result : 8; 664 665 /// \brief Indicates whether a diagnostic is stored in Diagnostic. 666 unsigned HasDiagnostic : 1; 667 668 /// \brief Opaque pointer containing additional data about 669 /// this deduction failure. 670 void *Data; 671 672 /// \brief A diagnostic indicating why deduction failed. 673 union { 674 void *Align; 675 char Diagnostic[sizeof(PartialDiagnosticAt)]; 676 }; 677 678 /// \brief Retrieve the diagnostic which caused this deduction failure, 679 /// if any. 680 PartialDiagnosticAt *getSFINAEDiagnostic(); 681 682 /// \brief Retrieve the template parameter this deduction failure 683 /// refers to, if any. 684 TemplateParameter getTemplateParameter(); 685 686 /// \brief Retrieve the template argument list associated with this 687 /// deduction failure, if any. 688 TemplateArgumentList *getTemplateArgumentList(); 689 690 /// \brief Return the first template argument this deduction failure 691 /// refers to, if any. 692 const TemplateArgument *getFirstArg(); 693 694 /// \brief Return the second template argument this deduction failure 695 /// refers to, if any. 696 const TemplateArgument *getSecondArg(); 697 698 /// \brief Free any memory associated with this deduction failure. 699 void Destroy(); 700 }; 701 702 union { 703 DeductionFailureInfo DeductionFailure; 704 705 /// FinalConversion - For a conversion function (where Function is 706 /// a CXXConversionDecl), the standard conversion that occurs 707 /// after the call to the overload candidate to convert the result 708 /// of calling the conversion function to the required type. 709 StandardConversionSequence FinalConversion; 710 }; 711 712 /// hasAmbiguousConversion - Returns whether this overload 713 /// candidate requires an ambiguous conversion or not. hasAmbiguousConversionOverloadCandidate714 bool hasAmbiguousConversion() const { 715 for (unsigned i = 0, e = NumConversions; i != e; ++i) { 716 if (!Conversions[i].isInitialized()) return false; 717 if (Conversions[i].isAmbiguous()) return true; 718 } 719 return false; 720 } 721 TryToFixBadConversionOverloadCandidate722 bool TryToFixBadConversion(unsigned Idx, Sema &S) { 723 bool CanFix = Fix.tryToFixConversion( 724 Conversions[Idx].Bad.FromExpr, 725 Conversions[Idx].Bad.getFromType(), 726 Conversions[Idx].Bad.getToType(), S); 727 728 // If at least one conversion fails, the candidate cannot be fixed. 729 if (!CanFix) 730 Fix.clear(); 731 732 return CanFix; 733 } 734 }; 735 736 /// OverloadCandidateSet - A set of overload candidates, used in C++ 737 /// overload resolution (C++ 13.3). 738 class OverloadCandidateSet { 739 SmallVector<OverloadCandidate, 16> Candidates; 740 llvm::SmallPtrSet<Decl *, 16> Functions; 741 742 // Allocator for OverloadCandidate::Conversions. We store the first few 743 // elements inline to avoid allocation for small sets. 744 llvm::BumpPtrAllocator ConversionSequenceAllocator; 745 746 SourceLocation Loc; 747 748 unsigned NumInlineSequences; 749 char InlineSpace[16 * sizeof(ImplicitConversionSequence)]; 750 751 OverloadCandidateSet(const OverloadCandidateSet &); 752 OverloadCandidateSet &operator=(const OverloadCandidateSet &); 753 754 public: OverloadCandidateSet(SourceLocation Loc)755 OverloadCandidateSet(SourceLocation Loc) : Loc(Loc), NumInlineSequences(0){} ~OverloadCandidateSet()756 ~OverloadCandidateSet() { clear(); } 757 getLocation()758 SourceLocation getLocation() const { return Loc; } 759 760 /// \brief Determine when this overload candidate will be new to the 761 /// overload set. isNewCandidate(Decl * F)762 bool isNewCandidate(Decl *F) { 763 return Functions.insert(F->getCanonicalDecl()); 764 } 765 766 /// \brief Clear out all of the candidates. 767 void clear(); 768 769 typedef SmallVector<OverloadCandidate, 16>::iterator iterator; begin()770 iterator begin() { return Candidates.begin(); } end()771 iterator end() { return Candidates.end(); } 772 size()773 size_t size() const { return Candidates.size(); } empty()774 bool empty() const { return Candidates.empty(); } 775 776 /// \brief Add a new candidate with NumConversions conversion sequence slots 777 /// to the overload set. 778 OverloadCandidate &addCandidate(unsigned NumConversions = 0) { 779 Candidates.push_back(OverloadCandidate()); 780 OverloadCandidate &C = Candidates.back(); 781 782 // Assign space from the inline array if there are enough free slots 783 // available. 784 if (NumConversions + NumInlineSequences <= 16) { 785 ImplicitConversionSequence *I = 786 (ImplicitConversionSequence*)InlineSpace; 787 C.Conversions = &I[NumInlineSequences]; 788 NumInlineSequences += NumConversions; 789 } else { 790 // Otherwise get memory from the allocator. 791 C.Conversions = ConversionSequenceAllocator 792 .Allocate<ImplicitConversionSequence>(NumConversions); 793 } 794 795 // Construct the new objects. 796 for (unsigned i = 0; i != NumConversions; ++i) 797 new (&C.Conversions[i]) ImplicitConversionSequence(); 798 799 C.NumConversions = NumConversions; 800 return C; 801 } 802 803 /// Find the best viable function on this overload set, if it exists. 804 OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc, 805 OverloadCandidateSet::iterator& Best, 806 bool UserDefinedConversion = false); 807 808 void NoteCandidates(Sema &S, 809 OverloadCandidateDisplayKind OCD, 810 llvm::ArrayRef<Expr *> Args, 811 const char *Opc = 0, 812 SourceLocation Loc = SourceLocation()); 813 }; 814 815 bool isBetterOverloadCandidate(Sema &S, 816 const OverloadCandidate& Cand1, 817 const OverloadCandidate& Cand2, 818 SourceLocation Loc, 819 bool UserDefinedConversion = false); 820 } // end namespace clang 821 822 #endif // LLVM_CLANG_SEMA_OVERLOAD_H 823