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