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