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