1 //===- Overload.h - C++ Overloading -----------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the data structures and types used in C++ 10 // overload resolution. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_SEMA_OVERLOAD_H 15 #define LLVM_CLANG_SEMA_OVERLOAD_H 16 17 #include "clang/AST/Decl.h" 18 #include "clang/AST/DeclAccessPair.h" 19 #include "clang/AST/DeclBase.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclTemplate.h" 22 #include "clang/AST/Expr.h" 23 #include "clang/AST/Type.h" 24 #include "clang/Basic/LLVM.h" 25 #include "clang/Basic/SourceLocation.h" 26 #include "clang/Sema/SemaFixItUtils.h" 27 #include "clang/Sema/TemplateDeduction.h" 28 #include "llvm/ADT/ArrayRef.h" 29 #include "llvm/ADT/STLExtras.h" 30 #include "llvm/ADT/SmallPtrSet.h" 31 #include "llvm/ADT/SmallVector.h" 32 #include "llvm/ADT/StringRef.h" 33 #include "llvm/Support/AlignOf.h" 34 #include "llvm/Support/Allocator.h" 35 #include "llvm/Support/Casting.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include <cassert> 38 #include <cstddef> 39 #include <cstdint> 40 #include <utility> 41 42 namespace clang { 43 44 class APValue; 45 class ASTContext; 46 class Sema; 47 48 /// OverloadingResult - Capture the result of performing overload 49 /// resolution. 50 enum OverloadingResult { 51 /// Overload resolution succeeded. 52 OR_Success, 53 54 /// No viable function found. 55 OR_No_Viable_Function, 56 57 /// Ambiguous candidates found. 58 OR_Ambiguous, 59 60 /// Succeeded, but refers to a deleted function. 61 OR_Deleted 62 }; 63 64 enum OverloadCandidateDisplayKind { 65 /// Requests that all candidates be shown. Viable candidates will 66 /// be printed first. 67 OCD_AllCandidates, 68 69 /// Requests that only viable candidates be shown. 70 OCD_ViableCandidates, 71 72 /// Requests that only tied-for-best candidates be shown. 73 OCD_AmbiguousCandidates 74 }; 75 76 /// The parameter ordering that will be used for the candidate. This is 77 /// used to represent C++20 binary operator rewrites that reverse the order 78 /// of the arguments. If the parameter ordering is Reversed, the Args list is 79 /// reversed (but obviously the ParamDecls for the function are not). 80 /// 81 /// After forming an OverloadCandidate with reversed parameters, the list 82 /// of conversions will (as always) be indexed by argument, so will be 83 /// in reverse parameter order. 84 enum class OverloadCandidateParamOrder : char { Normal, Reversed }; 85 86 /// The kinds of rewrite we perform on overload candidates. Note that the 87 /// values here are chosen to serve as both bitflags and as a rank (lower 88 /// values are preferred by overload resolution). 89 enum OverloadCandidateRewriteKind : unsigned { 90 /// Candidate is not a rewritten candidate. 91 CRK_None = 0x0, 92 93 /// Candidate is a rewritten candidate with a different operator name. 94 CRK_DifferentOperator = 0x1, 95 96 /// Candidate is a rewritten candidate with a reversed order of parameters. 97 CRK_Reversed = 0x2, 98 }; 99 100 /// ImplicitConversionKind - The kind of implicit conversion used to 101 /// convert an argument to a parameter's type. The enumerator values 102 /// match with the table titled 'Conversions' in [over.ics.scs] and are listed 103 /// such that better conversion kinds have smaller values. 104 enum ImplicitConversionKind { 105 /// Identity conversion (no conversion) 106 ICK_Identity = 0, 107 108 /// Lvalue-to-rvalue conversion (C++ [conv.lval]) 109 ICK_Lvalue_To_Rvalue, 110 111 /// Array-to-pointer conversion (C++ [conv.array]) 112 ICK_Array_To_Pointer, 113 114 /// Function-to-pointer (C++ [conv.array]) 115 ICK_Function_To_Pointer, 116 117 /// Function pointer conversion (C++17 [conv.fctptr]) 118 ICK_Function_Conversion, 119 120 /// Qualification conversions (C++ [conv.qual]) 121 ICK_Qualification, 122 123 /// Integral promotions (C++ [conv.prom]) 124 ICK_Integral_Promotion, 125 126 /// Floating point promotions (C++ [conv.fpprom]) 127 ICK_Floating_Promotion, 128 129 /// Complex promotions (Clang extension) 130 ICK_Complex_Promotion, 131 132 /// Integral conversions (C++ [conv.integral]) 133 ICK_Integral_Conversion, 134 135 /// Floating point conversions (C++ [conv.double] 136 ICK_Floating_Conversion, 137 138 /// Complex conversions (C99 6.3.1.6) 139 ICK_Complex_Conversion, 140 141 /// Floating-integral conversions (C++ [conv.fpint]) 142 ICK_Floating_Integral, 143 144 /// Pointer conversions (C++ [conv.ptr]) 145 ICK_Pointer_Conversion, 146 147 /// Pointer-to-member conversions (C++ [conv.mem]) 148 ICK_Pointer_Member, 149 150 /// Boolean conversions (C++ [conv.bool]) 151 ICK_Boolean_Conversion, 152 153 /// Conversions between compatible types in C99 154 ICK_Compatible_Conversion, 155 156 /// Derived-to-base (C++ [over.best.ics]) 157 ICK_Derived_To_Base, 158 159 /// Vector conversions 160 ICK_Vector_Conversion, 161 162 /// Arm SVE Vector conversions 163 ICK_SVE_Vector_Conversion, 164 165 /// RISC-V RVV Vector conversions 166 ICK_RVV_Vector_Conversion, 167 168 /// A vector splat from an arithmetic type 169 ICK_Vector_Splat, 170 171 /// Complex-real conversions (C99 6.3.1.7) 172 ICK_Complex_Real, 173 174 /// Block Pointer conversions 175 ICK_Block_Pointer_Conversion, 176 177 /// Transparent Union Conversions 178 ICK_TransparentUnionConversion, 179 180 /// Objective-C ARC writeback conversion 181 ICK_Writeback_Conversion, 182 183 /// Zero constant to event (OpenCL1.2 6.12.10) 184 ICK_Zero_Event_Conversion, 185 186 /// Zero constant to queue 187 ICK_Zero_Queue_Conversion, 188 189 /// Conversions allowed in C, but not C++ 190 ICK_C_Only_Conversion, 191 192 /// C-only conversion between pointers with incompatible types 193 ICK_Incompatible_Pointer_Conversion, 194 195 /// Fixed point type conversions according to N1169. 196 ICK_Fixed_Point_Conversion, 197 198 /// HLSL vector truncation. 199 ICK_HLSL_Vector_Truncation, 200 201 /// The number of conversion kinds 202 ICK_Num_Conversion_Kinds, 203 }; 204 205 /// ImplicitConversionRank - The rank of an implicit conversion 206 /// kind. The enumerator values match with Table 9 of (C++ 207 /// 13.3.3.1.1) and are listed such that better conversion ranks 208 /// have smaller values. 209 enum ImplicitConversionRank { 210 /// Exact Match 211 ICR_Exact_Match = 0, 212 213 /// Promotion 214 ICR_Promotion, 215 216 /// Conversion 217 ICR_Conversion, 218 219 /// OpenCL Scalar Widening 220 ICR_OCL_Scalar_Widening, 221 222 /// Complex <-> Real conversion 223 ICR_Complex_Real_Conversion, 224 225 /// ObjC ARC writeback conversion 226 ICR_Writeback_Conversion, 227 228 /// Conversion only allowed in the C standard (e.g. void* to char*). 229 ICR_C_Conversion, 230 231 /// Conversion not allowed by the C standard, but that we accept as an 232 /// extension anyway. 233 ICR_C_Conversion_Extension 234 }; 235 236 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind); 237 238 /// NarrowingKind - The kind of narrowing conversion being performed by a 239 /// standard conversion sequence according to C++11 [dcl.init.list]p7. 240 enum NarrowingKind { 241 /// Not a narrowing conversion. 242 NK_Not_Narrowing, 243 244 /// A narrowing conversion by virtue of the source and destination types. 245 NK_Type_Narrowing, 246 247 /// A narrowing conversion, because a constant expression got narrowed. 248 NK_Constant_Narrowing, 249 250 /// A narrowing conversion, because a non-constant-expression variable might 251 /// have got narrowed. 252 NK_Variable_Narrowing, 253 254 /// Cannot tell whether this is a narrowing conversion because the 255 /// expression is value-dependent. 256 NK_Dependent_Narrowing, 257 }; 258 259 /// StandardConversionSequence - represents a standard conversion 260 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence 261 /// contains between zero and three conversions. If a particular 262 /// conversion is not needed, it will be set to the identity conversion 263 /// (ICK_Identity). 264 class StandardConversionSequence { 265 public: 266 /// First -- The first conversion can be an lvalue-to-rvalue 267 /// conversion, array-to-pointer conversion, or 268 /// function-to-pointer conversion. 269 ImplicitConversionKind First : 8; 270 271 /// Second - The second conversion can be an integral promotion, 272 /// floating point promotion, integral conversion, floating point 273 /// conversion, floating-integral conversion, pointer conversion, 274 /// pointer-to-member conversion, or boolean conversion. 275 ImplicitConversionKind Second : 8; 276 277 /// Element - Between the second and third conversion a vector or matrix 278 /// element conversion may occur. If this is not ICK_Identity this 279 /// conversion is applied element-wise to each element in the vector or 280 /// matrix. 281 ImplicitConversionKind Element : 8; 282 283 /// Third - The third conversion can be a qualification conversion 284 /// or a function conversion. 285 ImplicitConversionKind Third : 8; 286 287 /// Whether this is the deprecated conversion of a 288 /// string literal to a pointer to non-const character data 289 /// (C++ 4.2p2). 290 LLVM_PREFERRED_TYPE(bool) 291 unsigned DeprecatedStringLiteralToCharPtr : 1; 292 293 /// Whether the qualification conversion involves a change in the 294 /// Objective-C lifetime (for automatic reference counting). 295 LLVM_PREFERRED_TYPE(bool) 296 unsigned QualificationIncludesObjCLifetime : 1; 297 298 /// IncompatibleObjC - Whether this is an Objective-C conversion 299 /// that we should warn about (if we actually use it). 300 LLVM_PREFERRED_TYPE(bool) 301 unsigned IncompatibleObjC : 1; 302 303 /// ReferenceBinding - True when this is a reference binding 304 /// (C++ [over.ics.ref]). 305 LLVM_PREFERRED_TYPE(bool) 306 unsigned ReferenceBinding : 1; 307 308 /// DirectBinding - True when this is a reference binding that is a 309 /// direct binding (C++ [dcl.init.ref]). 310 LLVM_PREFERRED_TYPE(bool) 311 unsigned DirectBinding : 1; 312 313 /// Whether this is an lvalue reference binding (otherwise, it's 314 /// an rvalue reference binding). 315 LLVM_PREFERRED_TYPE(bool) 316 unsigned IsLvalueReference : 1; 317 318 /// Whether we're binding to a function lvalue. 319 LLVM_PREFERRED_TYPE(bool) 320 unsigned BindsToFunctionLvalue : 1; 321 322 /// Whether we're binding to an rvalue. 323 LLVM_PREFERRED_TYPE(bool) 324 unsigned BindsToRvalue : 1; 325 326 /// Whether this binds an implicit object argument to a 327 /// non-static member function without a ref-qualifier. 328 LLVM_PREFERRED_TYPE(bool) 329 unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1; 330 331 /// Whether this binds a reference to an object with a different 332 /// Objective-C lifetime qualifier. 333 LLVM_PREFERRED_TYPE(bool) 334 unsigned ObjCLifetimeConversionBinding : 1; 335 336 /// FromType - The type that this conversion is converting 337 /// from. This is an opaque pointer that can be translated into a 338 /// QualType. 339 void *FromTypePtr; 340 341 /// ToType - The types that this conversion is converting to in 342 /// each step. This is an opaque pointer that can be translated 343 /// into a QualType. 344 void *ToTypePtrs[3]; 345 346 /// CopyConstructor - The copy constructor that is used to perform 347 /// this conversion, when the conversion is actually just the 348 /// initialization of an object via copy constructor. Such 349 /// conversions are either identity conversions or derived-to-base 350 /// conversions. 351 CXXConstructorDecl *CopyConstructor; 352 DeclAccessPair FoundCopyConstructor; 353 setFromType(QualType T)354 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } 355 setToType(unsigned Idx,QualType T)356 void setToType(unsigned Idx, QualType T) { 357 assert(Idx < 3 && "To type index is out of range"); 358 ToTypePtrs[Idx] = T.getAsOpaquePtr(); 359 } 360 setAllToTypes(QualType T)361 void setAllToTypes(QualType T) { 362 ToTypePtrs[0] = T.getAsOpaquePtr(); 363 ToTypePtrs[1] = ToTypePtrs[0]; 364 ToTypePtrs[2] = ToTypePtrs[0]; 365 } 366 getFromType()367 QualType getFromType() const { 368 return QualType::getFromOpaquePtr(FromTypePtr); 369 } 370 getToType(unsigned Idx)371 QualType getToType(unsigned Idx) const { 372 assert(Idx < 3 && "To type index is out of range"); 373 return QualType::getFromOpaquePtr(ToTypePtrs[Idx]); 374 } 375 376 void setAsIdentityConversion(); 377 isIdentityConversion()378 bool isIdentityConversion() const { 379 return Second == ICK_Identity && Element == ICK_Identity && 380 Third == ICK_Identity; 381 } 382 383 ImplicitConversionRank getRank() const; 384 NarrowingKind 385 getNarrowingKind(ASTContext &Context, const Expr *Converted, 386 APValue &ConstantValue, QualType &ConstantType, 387 bool IgnoreFloatToIntegralConversion = false) const; 388 bool isPointerConversionToBool() const; 389 bool isPointerConversionToVoidPointer(ASTContext& Context) const; 390 void dump() const; 391 }; 392 393 /// UserDefinedConversionSequence - Represents a user-defined 394 /// conversion sequence (C++ 13.3.3.1.2). 395 struct UserDefinedConversionSequence { 396 /// Represents the standard conversion that occurs before 397 /// the actual user-defined conversion. 398 /// 399 /// C++11 13.3.3.1.2p1: 400 /// If the user-defined conversion is specified by a constructor 401 /// (12.3.1), the initial standard conversion sequence converts 402 /// the source type to the type required by the argument of the 403 /// constructor. If the user-defined conversion is specified by 404 /// a conversion function (12.3.2), the initial standard 405 /// conversion sequence converts the source type to the implicit 406 /// object parameter of the conversion function. 407 StandardConversionSequence Before; 408 409 /// EllipsisConversion - When this is true, it means user-defined 410 /// conversion sequence starts with a ... (ellipsis) conversion, instead of 411 /// a standard conversion. In this case, 'Before' field must be ignored. 412 // FIXME. I much rather put this as the first field. But there seems to be 413 // a gcc code gen. bug which causes a crash in a test. Putting it here seems 414 // to work around the crash. 415 bool EllipsisConversion : 1; 416 417 /// HadMultipleCandidates - When this is true, it means that the 418 /// conversion function was resolved from an overloaded set having 419 /// size greater than 1. 420 bool HadMultipleCandidates : 1; 421 422 /// After - Represents the standard conversion that occurs after 423 /// the actual user-defined conversion. 424 StandardConversionSequence After; 425 426 /// ConversionFunction - The function that will perform the 427 /// user-defined conversion. Null if the conversion is an 428 /// aggregate initialization from an initializer list. 429 FunctionDecl* ConversionFunction; 430 431 /// The declaration that we found via name lookup, which might be 432 /// the same as \c ConversionFunction or it might be a using declaration 433 /// that refers to \c ConversionFunction. 434 DeclAccessPair FoundConversionFunction; 435 436 void dump() const; 437 }; 438 439 /// Represents an ambiguous user-defined conversion sequence. 440 struct AmbiguousConversionSequence { 441 using ConversionSet = 442 SmallVector<std::pair<NamedDecl *, FunctionDecl *>, 4>; 443 444 void *FromTypePtr; 445 void *ToTypePtr; 446 char Buffer[sizeof(ConversionSet)]; 447 getFromTypeAmbiguousConversionSequence448 QualType getFromType() const { 449 return QualType::getFromOpaquePtr(FromTypePtr); 450 } 451 getToTypeAmbiguousConversionSequence452 QualType getToType() const { 453 return QualType::getFromOpaquePtr(ToTypePtr); 454 } 455 setFromTypeAmbiguousConversionSequence456 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } setToTypeAmbiguousConversionSequence457 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); } 458 conversionsAmbiguousConversionSequence459 ConversionSet &conversions() { 460 return *reinterpret_cast<ConversionSet*>(Buffer); 461 } 462 conversionsAmbiguousConversionSequence463 const ConversionSet &conversions() const { 464 return *reinterpret_cast<const ConversionSet*>(Buffer); 465 } 466 addConversionAmbiguousConversionSequence467 void addConversion(NamedDecl *Found, FunctionDecl *D) { 468 conversions().push_back(std::make_pair(Found, D)); 469 } 470 471 using iterator = ConversionSet::iterator; 472 beginAmbiguousConversionSequence473 iterator begin() { return conversions().begin(); } endAmbiguousConversionSequence474 iterator end() { return conversions().end(); } 475 476 using const_iterator = ConversionSet::const_iterator; 477 beginAmbiguousConversionSequence478 const_iterator begin() const { return conversions().begin(); } endAmbiguousConversionSequence479 const_iterator end() const { return conversions().end(); } 480 481 void construct(); 482 void destruct(); 483 void copyFrom(const AmbiguousConversionSequence &); 484 }; 485 486 /// BadConversionSequence - Records information about an invalid 487 /// conversion sequence. 488 struct BadConversionSequence { 489 enum FailureKind { 490 no_conversion, 491 unrelated_class, 492 bad_qualifiers, 493 lvalue_ref_to_rvalue, 494 rvalue_ref_to_lvalue, 495 too_few_initializers, 496 too_many_initializers, 497 }; 498 499 // This can be null, e.g. for implicit object arguments. 500 Expr *FromExpr; 501 502 FailureKind Kind; 503 504 private: 505 // The type we're converting from (an opaque QualType). 506 void *FromTy; 507 508 // The type we're converting to (an opaque QualType). 509 void *ToTy; 510 511 public: initBadConversionSequence512 void init(FailureKind K, Expr *From, QualType To) { 513 init(K, From->getType(), To); 514 FromExpr = From; 515 } 516 initBadConversionSequence517 void init(FailureKind K, QualType From, QualType To) { 518 Kind = K; 519 FromExpr = nullptr; 520 setFromType(From); 521 setToType(To); 522 } 523 getFromTypeBadConversionSequence524 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); } getToTypeBadConversionSequence525 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); } 526 setFromExprBadConversionSequence527 void setFromExpr(Expr *E) { 528 FromExpr = E; 529 setFromType(E->getType()); 530 } 531 setFromTypeBadConversionSequence532 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); } setToTypeBadConversionSequence533 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); } 534 }; 535 536 /// ImplicitConversionSequence - Represents an implicit conversion 537 /// sequence, which may be a standard conversion sequence 538 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2), 539 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3). 540 class ImplicitConversionSequence { 541 public: 542 /// Kind - The kind of implicit conversion sequence. BadConversion 543 /// specifies that there is no conversion from the source type to 544 /// the target type. AmbiguousConversion represents the unique 545 /// ambiguous conversion (C++0x [over.best.ics]p10). 546 /// StaticObjectArgumentConversion represents the conversion rules for 547 /// the synthesized first argument of calls to static member functions 548 /// ([over.best.ics.general]p8). 549 enum Kind { 550 StandardConversion = 0, 551 StaticObjectArgumentConversion, 552 UserDefinedConversion, 553 AmbiguousConversion, 554 EllipsisConversion, 555 BadConversion 556 }; 557 558 private: 559 enum { 560 Uninitialized = BadConversion + 1 561 }; 562 563 /// ConversionKind - The kind of implicit conversion sequence. 564 LLVM_PREFERRED_TYPE(Kind) 565 unsigned ConversionKind : 31; 566 567 // Whether the initializer list was of an incomplete array. 568 LLVM_PREFERRED_TYPE(bool) 569 unsigned InitializerListOfIncompleteArray : 1; 570 571 /// When initializing an array or std::initializer_list from an 572 /// initializer-list, this is the array or std::initializer_list type being 573 /// initialized. The remainder of the conversion sequence, including ToType, 574 /// describe the worst conversion of an initializer to an element of the 575 /// array or std::initializer_list. (Note, 'worst' is not well defined.) 576 QualType InitializerListContainerType; 577 setKind(Kind K)578 void setKind(Kind K) { 579 destruct(); 580 ConversionKind = K; 581 } 582 destruct()583 void destruct() { 584 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct(); 585 } 586 587 public: 588 union { 589 /// When ConversionKind == StandardConversion, provides the 590 /// details of the standard conversion sequence. 591 StandardConversionSequence Standard; 592 593 /// When ConversionKind == UserDefinedConversion, provides the 594 /// details of the user-defined conversion sequence. 595 UserDefinedConversionSequence UserDefined; 596 597 /// When ConversionKind == AmbiguousConversion, provides the 598 /// details of the ambiguous conversion. 599 AmbiguousConversionSequence Ambiguous; 600 601 /// When ConversionKind == BadConversion, provides the details 602 /// of the bad conversion. 603 BadConversionSequence Bad; 604 }; 605 ImplicitConversionSequence()606 ImplicitConversionSequence() 607 : ConversionKind(Uninitialized), 608 InitializerListOfIncompleteArray(false) { 609 Standard.setAsIdentityConversion(); 610 } 611 ImplicitConversionSequence(const ImplicitConversionSequence & Other)612 ImplicitConversionSequence(const ImplicitConversionSequence &Other) 613 : ConversionKind(Other.ConversionKind), 614 InitializerListOfIncompleteArray( 615 Other.InitializerListOfIncompleteArray), 616 InitializerListContainerType(Other.InitializerListContainerType) { 617 switch (ConversionKind) { 618 case Uninitialized: break; 619 case StandardConversion: Standard = Other.Standard; break; 620 case StaticObjectArgumentConversion: 621 break; 622 case UserDefinedConversion: UserDefined = Other.UserDefined; break; 623 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break; 624 case EllipsisConversion: break; 625 case BadConversion: Bad = Other.Bad; break; 626 } 627 } 628 629 ImplicitConversionSequence & 630 operator=(const ImplicitConversionSequence &Other) { 631 destruct(); 632 new (this) ImplicitConversionSequence(Other); 633 return *this; 634 } 635 ~ImplicitConversionSequence()636 ~ImplicitConversionSequence() { 637 destruct(); 638 } 639 getKind()640 Kind getKind() const { 641 assert(isInitialized() && "querying uninitialized conversion"); 642 return Kind(ConversionKind); 643 } 644 645 /// Return a ranking of the implicit conversion sequence 646 /// kind, where smaller ranks represent better conversion 647 /// sequences. 648 /// 649 /// In particular, this routine gives user-defined conversion 650 /// sequences and ambiguous conversion sequences the same rank, 651 /// per C++ [over.best.ics]p10. getKindRank()652 unsigned getKindRank() const { 653 switch (getKind()) { 654 case StandardConversion: 655 case StaticObjectArgumentConversion: 656 return 0; 657 658 case UserDefinedConversion: 659 case AmbiguousConversion: 660 return 1; 661 662 case EllipsisConversion: 663 return 2; 664 665 case BadConversion: 666 return 3; 667 } 668 669 llvm_unreachable("Invalid ImplicitConversionSequence::Kind!"); 670 } 671 isBad()672 bool isBad() const { return getKind() == BadConversion; } isStandard()673 bool isStandard() const { return getKind() == StandardConversion; } isStaticObjectArgument()674 bool isStaticObjectArgument() const { 675 return getKind() == StaticObjectArgumentConversion; 676 } isEllipsis()677 bool isEllipsis() const { return getKind() == EllipsisConversion; } isAmbiguous()678 bool isAmbiguous() const { return getKind() == AmbiguousConversion; } isUserDefined()679 bool isUserDefined() const { return getKind() == UserDefinedConversion; } isFailure()680 bool isFailure() const { return isBad() || isAmbiguous(); } 681 682 /// Determines whether this conversion sequence has been 683 /// initialized. Most operations should never need to query 684 /// uninitialized conversions and should assert as above. isInitialized()685 bool isInitialized() const { return ConversionKind != Uninitialized; } 686 687 /// Sets this sequence as a bad conversion for an explicit argument. setBad(BadConversionSequence::FailureKind Failure,Expr * FromExpr,QualType ToType)688 void setBad(BadConversionSequence::FailureKind Failure, 689 Expr *FromExpr, QualType ToType) { 690 setKind(BadConversion); 691 Bad.init(Failure, FromExpr, ToType); 692 } 693 694 /// Sets this sequence as a bad conversion for an implicit argument. setBad(BadConversionSequence::FailureKind Failure,QualType FromType,QualType ToType)695 void setBad(BadConversionSequence::FailureKind Failure, 696 QualType FromType, QualType ToType) { 697 setKind(BadConversion); 698 Bad.init(Failure, FromType, ToType); 699 } 700 setStandard()701 void setStandard() { setKind(StandardConversion); } setStaticObjectArgument()702 void setStaticObjectArgument() { setKind(StaticObjectArgumentConversion); } setEllipsis()703 void setEllipsis() { setKind(EllipsisConversion); } setUserDefined()704 void setUserDefined() { setKind(UserDefinedConversion); } 705 setAmbiguous()706 void setAmbiguous() { 707 if (ConversionKind == AmbiguousConversion) return; 708 ConversionKind = AmbiguousConversion; 709 Ambiguous.construct(); 710 } 711 setAsIdentityConversion(QualType T)712 void setAsIdentityConversion(QualType T) { 713 setStandard(); 714 Standard.setAsIdentityConversion(); 715 Standard.setFromType(T); 716 Standard.setAllToTypes(T); 717 } 718 719 // True iff this is a conversion sequence from an initializer list to an 720 // array or std::initializer. hasInitializerListContainerType()721 bool hasInitializerListContainerType() const { 722 return !InitializerListContainerType.isNull(); 723 } setInitializerListContainerType(QualType T,bool IA)724 void setInitializerListContainerType(QualType T, bool IA) { 725 InitializerListContainerType = T; 726 InitializerListOfIncompleteArray = IA; 727 } isInitializerListOfIncompleteArray()728 bool isInitializerListOfIncompleteArray() const { 729 return InitializerListOfIncompleteArray; 730 } getInitializerListContainerType()731 QualType getInitializerListContainerType() const { 732 assert(hasInitializerListContainerType() && 733 "not initializer list container"); 734 return InitializerListContainerType; 735 } 736 737 /// Form an "implicit" conversion sequence from nullptr_t to bool, for a 738 /// direct-initialization of a bool object from nullptr_t. getNullptrToBool(QualType SourceType,QualType DestType,bool NeedLValToRVal)739 static ImplicitConversionSequence getNullptrToBool(QualType SourceType, 740 QualType DestType, 741 bool NeedLValToRVal) { 742 ImplicitConversionSequence ICS; 743 ICS.setStandard(); 744 ICS.Standard.setAsIdentityConversion(); 745 ICS.Standard.setFromType(SourceType); 746 if (NeedLValToRVal) 747 ICS.Standard.First = ICK_Lvalue_To_Rvalue; 748 ICS.Standard.setToType(0, SourceType); 749 ICS.Standard.Second = ICK_Boolean_Conversion; 750 ICS.Standard.setToType(1, DestType); 751 ICS.Standard.setToType(2, DestType); 752 return ICS; 753 } 754 755 // The result of a comparison between implicit conversion 756 // sequences. Use Sema::CompareImplicitConversionSequences to 757 // actually perform the comparison. 758 enum CompareKind { 759 Better = -1, 760 Indistinguishable = 0, 761 Worse = 1 762 }; 763 764 void DiagnoseAmbiguousConversion(Sema &S, 765 SourceLocation CaretLoc, 766 const PartialDiagnostic &PDiag) const; 767 768 void dump() const; 769 }; 770 771 enum OverloadFailureKind { 772 ovl_fail_too_many_arguments, 773 ovl_fail_too_few_arguments, 774 ovl_fail_bad_conversion, 775 ovl_fail_bad_deduction, 776 777 /// This conversion candidate was not considered because it 778 /// duplicates the work of a trivial or derived-to-base 779 /// conversion. 780 ovl_fail_trivial_conversion, 781 782 /// This conversion candidate was not considered because it is 783 /// an illegal instantiation of a constructor temploid: it is 784 /// callable with one argument, we only have one argument, and 785 /// its first parameter type is exactly the type of the class. 786 /// 787 /// Defining such a constructor directly is illegal, and 788 /// template-argument deduction is supposed to ignore such 789 /// instantiations, but we can still get one with the right 790 /// kind of implicit instantiation. 791 ovl_fail_illegal_constructor, 792 793 /// This conversion candidate is not viable because its result 794 /// type is not implicitly convertible to the desired type. 795 ovl_fail_bad_final_conversion, 796 797 /// This conversion function template specialization candidate is not 798 /// viable because the final conversion was not an exact match. 799 ovl_fail_final_conversion_not_exact, 800 801 /// (CUDA) This candidate was not viable because the callee 802 /// was not accessible from the caller's target (i.e. host->device, 803 /// global->host, device->host). 804 ovl_fail_bad_target, 805 806 /// This candidate function was not viable because an enable_if 807 /// attribute disabled it. 808 ovl_fail_enable_if, 809 810 /// This candidate constructor or conversion function is explicit but 811 /// the context doesn't permit explicit functions. 812 ovl_fail_explicit, 813 814 /// This candidate was not viable because its address could not be taken. 815 ovl_fail_addr_not_available, 816 817 /// This inherited constructor is not viable because it would slice the 818 /// argument. 819 ovl_fail_inhctor_slice, 820 821 /// This candidate was not viable because it is a non-default multiversioned 822 /// function. 823 ovl_non_default_multiversion_function, 824 825 /// This constructor/conversion candidate fail due to an address space 826 /// mismatch between the object being constructed and the overload 827 /// candidate. 828 ovl_fail_object_addrspace_mismatch, 829 830 /// This candidate was not viable because its associated constraints were 831 /// not satisfied. 832 ovl_fail_constraints_not_satisfied, 833 834 /// This candidate was not viable because it has internal linkage and is 835 /// from a different module unit than the use. 836 ovl_fail_module_mismatched, 837 }; 838 839 /// A list of implicit conversion sequences for the arguments of an 840 /// OverloadCandidate. 841 using ConversionSequenceList = 842 llvm::MutableArrayRef<ImplicitConversionSequence>; 843 844 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3). 845 struct OverloadCandidate { 846 /// Function - The actual function that this candidate 847 /// represents. When NULL, this is a built-in candidate 848 /// (C++ [over.oper]) or a surrogate for a conversion to a 849 /// function pointer or reference (C++ [over.call.object]). 850 FunctionDecl *Function; 851 852 /// FoundDecl - The original declaration that was looked up / 853 /// invented / otherwise found, together with its access. 854 /// Might be a UsingShadowDecl or a FunctionTemplateDecl. 855 DeclAccessPair FoundDecl; 856 857 /// BuiltinParamTypes - Provides the parameter types of a built-in overload 858 /// candidate. Only valid when Function is NULL. 859 QualType BuiltinParamTypes[3]; 860 861 /// Surrogate - The conversion function for which this candidate 862 /// is a surrogate, but only if IsSurrogate is true. 863 CXXConversionDecl *Surrogate; 864 865 /// The conversion sequences used to convert the function arguments 866 /// to the function parameters. Note that these are indexed by argument, 867 /// so may not match the parameter order of Function. 868 ConversionSequenceList Conversions; 869 870 /// The FixIt hints which can be used to fix the Bad candidate. 871 ConversionFixItGenerator Fix; 872 873 /// Viable - True to indicate that this overload candidate is viable. 874 bool Viable : 1; 875 876 /// Whether this candidate is the best viable function, or tied for being 877 /// the best viable function. 878 /// 879 /// For an ambiguous overload resolution, indicates whether this candidate 880 /// was part of the ambiguity kernel: the minimal non-empty set of viable 881 /// candidates such that all elements of the ambiguity kernel are better 882 /// than all viable candidates not in the ambiguity kernel. 883 bool Best : 1; 884 885 /// IsSurrogate - True to indicate that this candidate is a 886 /// surrogate for a conversion to a function pointer or reference 887 /// (C++ [over.call.object]). 888 bool IsSurrogate : 1; 889 890 /// IgnoreObjectArgument - True to indicate that the first 891 /// argument's conversion, which for this function represents the 892 /// implicit object argument, should be ignored. This will be true 893 /// when the candidate is a static member function (where the 894 /// implicit object argument is just a placeholder) or a 895 /// non-static member function when the call doesn't have an 896 /// object argument. 897 bool IgnoreObjectArgument : 1; 898 899 /// True if the candidate was found using ADL. 900 CallExpr::ADLCallKind IsADLCandidate : 1; 901 902 /// Whether this is a rewritten candidate, and if so, of what kind? 903 LLVM_PREFERRED_TYPE(OverloadCandidateRewriteKind) 904 unsigned RewriteKind : 2; 905 906 /// FailureKind - The reason why this candidate is not viable. 907 /// Actually an OverloadFailureKind. 908 unsigned char FailureKind; 909 910 /// The number of call arguments that were explicitly provided, 911 /// to be used while performing partial ordering of function templates. 912 unsigned ExplicitCallArguments; 913 914 union { 915 DeductionFailureInfo DeductionFailure; 916 917 /// FinalConversion - For a conversion function (where Function is 918 /// a CXXConversionDecl), the standard conversion that occurs 919 /// after the call to the overload candidate to convert the result 920 /// of calling the conversion function to the required type. 921 StandardConversionSequence FinalConversion; 922 }; 923 924 /// Get RewriteKind value in OverloadCandidateRewriteKind type (This 925 /// function is to workaround the spurious GCC bitfield enum warning) getRewriteKindOverloadCandidate926 OverloadCandidateRewriteKind getRewriteKind() const { 927 return static_cast<OverloadCandidateRewriteKind>(RewriteKind); 928 } 929 isReversedOverloadCandidate930 bool isReversed() const { return getRewriteKind() & CRK_Reversed; } 931 932 /// hasAmbiguousConversion - Returns whether this overload 933 /// candidate requires an ambiguous conversion or not. hasAmbiguousConversionOverloadCandidate934 bool hasAmbiguousConversion() const { 935 for (auto &C : Conversions) { 936 if (!C.isInitialized()) return false; 937 if (C.isAmbiguous()) return true; 938 } 939 return false; 940 } 941 TryToFixBadConversionOverloadCandidate942 bool TryToFixBadConversion(unsigned Idx, Sema &S) { 943 bool CanFix = Fix.tryToFixConversion( 944 Conversions[Idx].Bad.FromExpr, 945 Conversions[Idx].Bad.getFromType(), 946 Conversions[Idx].Bad.getToType(), S); 947 948 // If at least one conversion fails, the candidate cannot be fixed. 949 if (!CanFix) 950 Fix.clear(); 951 952 return CanFix; 953 } 954 getNumParamsOverloadCandidate955 unsigned getNumParams() const { 956 if (IsSurrogate) { 957 QualType STy = Surrogate->getConversionType(); 958 while (STy->isPointerType() || STy->isReferenceType()) 959 STy = STy->getPointeeType(); 960 return STy->castAs<FunctionProtoType>()->getNumParams(); 961 } 962 if (Function) 963 return Function->getNumParams(); 964 return ExplicitCallArguments; 965 } 966 967 bool NotValidBecauseConstraintExprHasError() const; 968 969 private: 970 friend class OverloadCandidateSet; OverloadCandidateOverloadCandidate971 OverloadCandidate() 972 : IsSurrogate(false), IsADLCandidate(CallExpr::NotADL), RewriteKind(CRK_None) {} 973 }; 974 975 /// OverloadCandidateSet - A set of overload candidates, used in C++ 976 /// overload resolution (C++ 13.3). 977 class OverloadCandidateSet { 978 public: 979 enum CandidateSetKind { 980 /// Normal lookup. 981 CSK_Normal, 982 983 /// C++ [over.match.oper]: 984 /// Lookup of operator function candidates in a call using operator 985 /// syntax. Candidates that have no parameters of class type will be 986 /// skipped unless there is a parameter of (reference to) enum type and 987 /// the corresponding argument is of the same enum type. 988 CSK_Operator, 989 990 /// C++ [over.match.copy]: 991 /// Copy-initialization of an object of class type by user-defined 992 /// conversion. 993 CSK_InitByUserDefinedConversion, 994 995 /// C++ [over.match.ctor], [over.match.list] 996 /// Initialization of an object of class type by constructor, 997 /// using either a parenthesized or braced list of arguments. 998 CSK_InitByConstructor, 999 }; 1000 1001 /// Information about operator rewrites to consider when adding operator 1002 /// functions to a candidate set. 1003 struct OperatorRewriteInfo { OperatorRewriteInfoOperatorRewriteInfo1004 OperatorRewriteInfo() 1005 : OriginalOperator(OO_None), OpLoc(), AllowRewrittenCandidates(false) {} OperatorRewriteInfoOperatorRewriteInfo1006 OperatorRewriteInfo(OverloadedOperatorKind Op, SourceLocation OpLoc, 1007 bool AllowRewritten) 1008 : OriginalOperator(Op), OpLoc(OpLoc), 1009 AllowRewrittenCandidates(AllowRewritten) {} 1010 1011 /// The original operator as written in the source. 1012 OverloadedOperatorKind OriginalOperator; 1013 /// The source location of the operator. 1014 SourceLocation OpLoc; 1015 /// Whether we should include rewritten candidates in the overload set. 1016 bool AllowRewrittenCandidates; 1017 1018 /// Would use of this function result in a rewrite using a different 1019 /// operator? isRewrittenOperatorOperatorRewriteInfo1020 bool isRewrittenOperator(const FunctionDecl *FD) { 1021 return OriginalOperator && 1022 FD->getDeclName().getCXXOverloadedOperator() != OriginalOperator; 1023 } 1024 isAcceptableCandidateOperatorRewriteInfo1025 bool isAcceptableCandidate(const FunctionDecl *FD) { 1026 if (!OriginalOperator) 1027 return true; 1028 1029 // For an overloaded operator, we can have candidates with a different 1030 // name in our unqualified lookup set. Make sure we only consider the 1031 // ones we're supposed to. 1032 OverloadedOperatorKind OO = 1033 FD->getDeclName().getCXXOverloadedOperator(); 1034 return OO && (OO == OriginalOperator || 1035 (AllowRewrittenCandidates && 1036 OO == getRewrittenOverloadedOperator(OriginalOperator))); 1037 } 1038 1039 /// Determine the kind of rewrite that should be performed for this 1040 /// candidate. 1041 OverloadCandidateRewriteKind getRewriteKindOperatorRewriteInfo1042 getRewriteKind(const FunctionDecl *FD, OverloadCandidateParamOrder PO) { 1043 OverloadCandidateRewriteKind CRK = CRK_None; 1044 if (isRewrittenOperator(FD)) 1045 CRK = OverloadCandidateRewriteKind(CRK | CRK_DifferentOperator); 1046 if (PO == OverloadCandidateParamOrder::Reversed) 1047 CRK = OverloadCandidateRewriteKind(CRK | CRK_Reversed); 1048 return CRK; 1049 } 1050 /// Determines whether this operator could be implemented by a function 1051 /// with reversed parameter order. isReversibleOperatorRewriteInfo1052 bool isReversible() { 1053 return AllowRewrittenCandidates && OriginalOperator && 1054 (getRewrittenOverloadedOperator(OriginalOperator) != OO_None || 1055 allowsReversed(OriginalOperator)); 1056 } 1057 1058 /// Determine whether reversing parameter order is allowed for operator 1059 /// Op. 1060 bool allowsReversed(OverloadedOperatorKind Op); 1061 1062 /// Determine whether we should add a rewritten candidate for \p FD with 1063 /// reversed parameter order. 1064 /// \param OriginalArgs are the original non reversed arguments. 1065 bool shouldAddReversed(Sema &S, ArrayRef<Expr *> OriginalArgs, 1066 FunctionDecl *FD); 1067 }; 1068 1069 private: 1070 SmallVector<OverloadCandidate, 16> Candidates; 1071 llvm::SmallPtrSet<uintptr_t, 16> Functions; 1072 1073 // Allocator for ConversionSequenceLists. We store the first few of these 1074 // inline to avoid allocation for small sets. 1075 llvm::BumpPtrAllocator SlabAllocator; 1076 1077 SourceLocation Loc; 1078 CandidateSetKind Kind; 1079 OperatorRewriteInfo RewriteInfo; 1080 1081 constexpr static unsigned NumInlineBytes = 1082 24 * sizeof(ImplicitConversionSequence); 1083 unsigned NumInlineBytesUsed = 0; 1084 alignas(void *) char InlineSpace[NumInlineBytes]; 1085 1086 // Address space of the object being constructed. 1087 LangAS DestAS = LangAS::Default; 1088 1089 /// If we have space, allocates from inline storage. Otherwise, allocates 1090 /// from the slab allocator. 1091 /// FIXME: It would probably be nice to have a SmallBumpPtrAllocator 1092 /// instead. 1093 /// FIXME: Now that this only allocates ImplicitConversionSequences, do we 1094 /// want to un-generalize this? 1095 template <typename T> slabAllocate(unsigned N)1096 T *slabAllocate(unsigned N) { 1097 // It's simpler if this doesn't need to consider alignment. 1098 static_assert(alignof(T) == alignof(void *), 1099 "Only works for pointer-aligned types."); 1100 static_assert(std::is_trivial<T>::value || 1101 std::is_same<ImplicitConversionSequence, T>::value, 1102 "Add destruction logic to OverloadCandidateSet::clear()."); 1103 1104 unsigned NBytes = sizeof(T) * N; 1105 if (NBytes > NumInlineBytes - NumInlineBytesUsed) 1106 return SlabAllocator.Allocate<T>(N); 1107 char *FreeSpaceStart = InlineSpace + NumInlineBytesUsed; 1108 assert(uintptr_t(FreeSpaceStart) % alignof(void *) == 0 && 1109 "Misaligned storage!"); 1110 1111 NumInlineBytesUsed += NBytes; 1112 return reinterpret_cast<T *>(FreeSpaceStart); 1113 } 1114 1115 void destroyCandidates(); 1116 1117 public: 1118 OverloadCandidateSet(SourceLocation Loc, CandidateSetKind CSK, 1119 OperatorRewriteInfo RewriteInfo = {}) Loc(Loc)1120 : Loc(Loc), Kind(CSK), RewriteInfo(RewriteInfo) {} 1121 OverloadCandidateSet(const OverloadCandidateSet &) = delete; 1122 OverloadCandidateSet &operator=(const OverloadCandidateSet &) = delete; ~OverloadCandidateSet()1123 ~OverloadCandidateSet() { destroyCandidates(); } 1124 getLocation()1125 SourceLocation getLocation() const { return Loc; } getKind()1126 CandidateSetKind getKind() const { return Kind; } getRewriteInfo()1127 OperatorRewriteInfo getRewriteInfo() const { return RewriteInfo; } 1128 1129 /// Whether diagnostics should be deferred. 1130 bool shouldDeferDiags(Sema &S, ArrayRef<Expr *> Args, SourceLocation OpLoc); 1131 1132 /// Determine when this overload candidate will be new to the 1133 /// overload set. 1134 bool isNewCandidate(Decl *F, OverloadCandidateParamOrder PO = 1135 OverloadCandidateParamOrder::Normal) { 1136 uintptr_t Key = reinterpret_cast<uintptr_t>(F->getCanonicalDecl()); 1137 Key |= static_cast<uintptr_t>(PO); 1138 return Functions.insert(Key).second; 1139 } 1140 1141 /// Exclude a function from being considered by overload resolution. exclude(Decl * F)1142 void exclude(Decl *F) { 1143 isNewCandidate(F, OverloadCandidateParamOrder::Normal); 1144 isNewCandidate(F, OverloadCandidateParamOrder::Reversed); 1145 } 1146 1147 /// Clear out all of the candidates. 1148 void clear(CandidateSetKind CSK); 1149 1150 using iterator = SmallVectorImpl<OverloadCandidate>::iterator; 1151 begin()1152 iterator begin() { return Candidates.begin(); } end()1153 iterator end() { return Candidates.end(); } 1154 size()1155 size_t size() const { return Candidates.size(); } empty()1156 bool empty() const { return Candidates.empty(); } 1157 1158 /// Allocate storage for conversion sequences for NumConversions 1159 /// conversions. 1160 ConversionSequenceList allocateConversionSequences(unsigned NumConversions)1161 allocateConversionSequences(unsigned NumConversions) { 1162 ImplicitConversionSequence *Conversions = 1163 slabAllocate<ImplicitConversionSequence>(NumConversions); 1164 1165 // Construct the new objects. 1166 for (unsigned I = 0; I != NumConversions; ++I) 1167 new (&Conversions[I]) ImplicitConversionSequence(); 1168 1169 return ConversionSequenceList(Conversions, NumConversions); 1170 } 1171 1172 /// Add a new candidate with NumConversions conversion sequence slots 1173 /// to the overload set. 1174 OverloadCandidate & 1175 addCandidate(unsigned NumConversions = 0, 1176 ConversionSequenceList Conversions = std::nullopt) { 1177 assert((Conversions.empty() || Conversions.size() == NumConversions) && 1178 "preallocated conversion sequence has wrong length"); 1179 1180 Candidates.push_back(OverloadCandidate()); 1181 OverloadCandidate &C = Candidates.back(); 1182 C.Conversions = Conversions.empty() 1183 ? allocateConversionSequences(NumConversions) 1184 : Conversions; 1185 return C; 1186 } 1187 1188 /// Find the best viable function on this overload set, if it exists. 1189 OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc, 1190 OverloadCandidateSet::iterator& Best); 1191 1192 SmallVector<OverloadCandidate *, 32> CompleteCandidates( 1193 Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args, 1194 SourceLocation OpLoc = SourceLocation(), 1195 llvm::function_ref<bool(OverloadCandidate &)> Filter = 1196 [](OverloadCandidate &) { return true; }); 1197 1198 void NoteCandidates( 1199 PartialDiagnosticAt PA, Sema &S, OverloadCandidateDisplayKind OCD, 1200 ArrayRef<Expr *> Args, StringRef Opc = "", 1201 SourceLocation Loc = SourceLocation(), 1202 llvm::function_ref<bool(OverloadCandidate &)> Filter = 1203 [](OverloadCandidate &) { return true; }); 1204 1205 void NoteCandidates(Sema &S, ArrayRef<Expr *> Args, 1206 ArrayRef<OverloadCandidate *> Cands, 1207 StringRef Opc = "", 1208 SourceLocation OpLoc = SourceLocation()); 1209 getDestAS()1210 LangAS getDestAS() { return DestAS; } 1211 setDestAS(LangAS AS)1212 void setDestAS(LangAS AS) { 1213 assert((Kind == CSK_InitByConstructor || 1214 Kind == CSK_InitByUserDefinedConversion) && 1215 "can't set the destination address space when not constructing an " 1216 "object"); 1217 DestAS = AS; 1218 } 1219 1220 }; 1221 1222 bool isBetterOverloadCandidate(Sema &S, 1223 const OverloadCandidate &Cand1, 1224 const OverloadCandidate &Cand2, 1225 SourceLocation Loc, 1226 OverloadCandidateSet::CandidateSetKind Kind); 1227 1228 struct ConstructorInfo { 1229 DeclAccessPair FoundDecl; 1230 CXXConstructorDecl *Constructor; 1231 FunctionTemplateDecl *ConstructorTmpl; 1232 1233 explicit operator bool() const { return Constructor; } 1234 }; 1235 1236 // FIXME: Add an AddOverloadCandidate / AddTemplateOverloadCandidate overload 1237 // that takes one of these. getConstructorInfo(NamedDecl * ND)1238 inline ConstructorInfo getConstructorInfo(NamedDecl *ND) { 1239 if (isa<UsingDecl>(ND)) 1240 return ConstructorInfo{}; 1241 1242 // For constructors, the access check is performed against the underlying 1243 // declaration, not the found declaration. 1244 auto *D = ND->getUnderlyingDecl(); 1245 ConstructorInfo Info = {DeclAccessPair::make(ND, D->getAccess()), nullptr, 1246 nullptr}; 1247 Info.ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); 1248 if (Info.ConstructorTmpl) 1249 D = Info.ConstructorTmpl->getTemplatedDecl(); 1250 Info.Constructor = dyn_cast<CXXConstructorDecl>(D); 1251 return Info; 1252 } 1253 1254 // Returns false if signature help is relevant despite number of arguments 1255 // exceeding parameters. Specifically, it returns false when 1256 // PartialOverloading is true and one of the following: 1257 // * Function is variadic 1258 // * Function is template variadic 1259 // * Function is an instantiation of template variadic function 1260 // The last case may seem strange. The idea is that if we added one more 1261 // argument, we'd end up with a function similar to Function. Since, in the 1262 // context of signature help and/or code completion, we do not know what the 1263 // type of the next argument (that the user is typing) will be, this is as 1264 // good candidate as we can get, despite the fact that it takes one less 1265 // parameter. 1266 bool shouldEnforceArgLimit(bool PartialOverloading, FunctionDecl *Function); 1267 1268 } // namespace clang 1269 1270 #endif // LLVM_CLANG_SEMA_OVERLOAD_H 1271