1212795Sdim//===--- Overload.h - C++ Overloading ---------------------------*- C++ -*-===// 2212795Sdim// 3212795Sdim// The LLVM Compiler Infrastructure 4212795Sdim// 5212795Sdim// This file is distributed under the University of Illinois Open Source 6212795Sdim// License. See LICENSE.TXT for details. 7212795Sdim// 8212795Sdim//===----------------------------------------------------------------------===// 9212795Sdim// 10212795Sdim// This file defines the data structures and types used in C++ 11212795Sdim// overload resolution. 12212795Sdim// 13212795Sdim//===----------------------------------------------------------------------===// 14212795Sdim 15212795Sdim#ifndef LLVM_CLANG_SEMA_OVERLOAD_H 16212795Sdim#define LLVM_CLANG_SEMA_OVERLOAD_H 17212795Sdim 18212795Sdim#include "clang/AST/Decl.h" 19212795Sdim#include "clang/AST/DeclTemplate.h" 20212795Sdim#include "clang/AST/Expr.h" 21212795Sdim#include "clang/AST/TemplateBase.h" 22212795Sdim#include "clang/AST/Type.h" 23212795Sdim#include "clang/AST/UnresolvedSet.h" 24226633Sdim#include "clang/Sema/SemaFixItUtils.h" 25263508Sdim#include "clang/Sema/TemplateDeduction.h" 26212795Sdim#include "llvm/ADT/SmallPtrSet.h" 27212795Sdim#include "llvm/ADT/SmallVector.h" 28234353Sdim#include "llvm/Support/Allocator.h" 29212795Sdim 30212795Sdimnamespace clang { 31212795Sdim class ASTContext; 32212795Sdim class CXXConstructorDecl; 33212795Sdim class CXXConversionDecl; 34212795Sdim class FunctionDecl; 35212795Sdim class Sema; 36212795Sdim 37212795Sdim /// OverloadingResult - Capture the result of performing overload 38212795Sdim /// resolution. 39212795Sdim enum OverloadingResult { 40212795Sdim OR_Success, ///< Overload resolution succeeded. 41212795Sdim OR_No_Viable_Function, ///< No viable function found. 42212795Sdim OR_Ambiguous, ///< Ambiguous candidates found. 43212795Sdim OR_Deleted ///< Succeeded, but refers to a deleted function. 44212795Sdim }; 45212795Sdim 46212795Sdim enum OverloadCandidateDisplayKind { 47212795Sdim /// Requests that all candidates be shown. Viable candidates will 48212795Sdim /// be printed first. 49212795Sdim OCD_AllCandidates, 50212795Sdim 51212795Sdim /// Requests that only viable candidates be shown. 52212795Sdim OCD_ViableCandidates 53212795Sdim }; 54212795Sdim 55212795Sdim /// ImplicitConversionKind - The kind of implicit conversion used to 56212795Sdim /// convert an argument to a parameter's type. The enumerator values 57212795Sdim /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that 58212795Sdim /// better conversion kinds have smaller values. 59212795Sdim enum ImplicitConversionKind { 60212795Sdim ICK_Identity = 0, ///< Identity conversion (no conversion) 61212795Sdim ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1) 62212795Sdim ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2) 63212795Sdim ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3) 64212795Sdim ICK_NoReturn_Adjustment, ///< Removal of noreturn from a type (Clang) 65212795Sdim ICK_Qualification, ///< Qualification conversions (C++ 4.4) 66212795Sdim ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5) 67212795Sdim ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6) 68212795Sdim ICK_Complex_Promotion, ///< Complex promotions (Clang extension) 69212795Sdim ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7) 70212795Sdim ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8) 71212795Sdim ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6) 72212795Sdim ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9) 73212795Sdim ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10) 74212795Sdim ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11) 75212795Sdim ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12) 76212795Sdim ICK_Compatible_Conversion, ///< Conversions between compatible types in C99 77212795Sdim ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics]) 78212795Sdim ICK_Vector_Conversion, ///< Vector conversions 79212795Sdim ICK_Vector_Splat, ///< A vector splat from an arithmetic type 80212795Sdim ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7) 81218893Sdim ICK_Block_Pointer_Conversion, ///< Block Pointer conversions 82249423Sdim ICK_TransparentUnionConversion, ///< Transparent Union Conversions 83224145Sdim ICK_Writeback_Conversion, ///< Objective-C ARC writeback conversion 84249423Sdim ICK_Zero_Event_Conversion, ///< Zero constant to event (OpenCL1.2 6.12.10) 85212795Sdim ICK_Num_Conversion_Kinds ///< The number of conversion kinds 86212795Sdim }; 87212795Sdim 88212795Sdim /// ImplicitConversionCategory - The category of an implicit 89212795Sdim /// conversion kind. The enumerator values match with Table 9 of 90212795Sdim /// (C++ 13.3.3.1.1) and are listed such that better conversion 91212795Sdim /// categories have smaller values. 92212795Sdim enum ImplicitConversionCategory { 93212795Sdim ICC_Identity = 0, ///< Identity 94212795Sdim ICC_Lvalue_Transformation, ///< Lvalue transformation 95212795Sdim ICC_Qualification_Adjustment, ///< Qualification adjustment 96212795Sdim ICC_Promotion, ///< Promotion 97212795Sdim ICC_Conversion ///< Conversion 98212795Sdim }; 99212795Sdim 100212795Sdim ImplicitConversionCategory 101212795Sdim GetConversionCategory(ImplicitConversionKind Kind); 102212795Sdim 103212795Sdim /// ImplicitConversionRank - The rank of an implicit conversion 104212795Sdim /// kind. The enumerator values match with Table 9 of (C++ 105212795Sdim /// 13.3.3.1.1) and are listed such that better conversion ranks 106212795Sdim /// have smaller values. 107212795Sdim enum ImplicitConversionRank { 108224145Sdim ICR_Exact_Match = 0, ///< Exact Match 109224145Sdim ICR_Promotion, ///< Promotion 110224145Sdim ICR_Conversion, ///< Conversion 111224145Sdim ICR_Complex_Real_Conversion, ///< Complex <-> Real conversion 112224145Sdim ICR_Writeback_Conversion ///< ObjC ARC writeback conversion 113212795Sdim }; 114212795Sdim 115212795Sdim ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind); 116212795Sdim 117234353Sdim /// NarrowingKind - The kind of narrowing conversion being performed by a 118234353Sdim /// standard conversion sequence according to C++11 [dcl.init.list]p7. 119234353Sdim enum NarrowingKind { 120234353Sdim /// Not a narrowing conversion. 121234353Sdim NK_Not_Narrowing, 122234353Sdim 123234353Sdim /// A narrowing conversion by virtue of the source and destination types. 124234353Sdim NK_Type_Narrowing, 125234353Sdim 126234353Sdim /// A narrowing conversion, because a constant expression got narrowed. 127234353Sdim NK_Constant_Narrowing, 128234353Sdim 129234353Sdim /// A narrowing conversion, because a non-constant-expression variable might 130234353Sdim /// have got narrowed. 131234353Sdim NK_Variable_Narrowing 132234353Sdim }; 133234353Sdim 134212795Sdim /// StandardConversionSequence - represents a standard conversion 135212795Sdim /// sequence (C++ 13.3.3.1.1). A standard conversion sequence 136212795Sdim /// contains between zero and three conversions. If a particular 137212795Sdim /// conversion is not needed, it will be set to the identity conversion 138212795Sdim /// (ICK_Identity). Note that the three conversions are 139212795Sdim /// specified as separate members (rather than in an array) so that 140212795Sdim /// we can keep the size of a standard conversion sequence to a 141212795Sdim /// single word. 142212795Sdim class StandardConversionSequence { 143212795Sdim public: 144212795Sdim /// First -- The first conversion can be an lvalue-to-rvalue 145212795Sdim /// conversion, array-to-pointer conversion, or 146212795Sdim /// function-to-pointer conversion. 147212795Sdim ImplicitConversionKind First : 8; 148212795Sdim 149212795Sdim /// Second - The second conversion can be an integral promotion, 150212795Sdim /// floating point promotion, integral conversion, floating point 151212795Sdim /// conversion, floating-integral conversion, pointer conversion, 152212795Sdim /// pointer-to-member conversion, or boolean conversion. 153212795Sdim ImplicitConversionKind Second : 8; 154212795Sdim 155212795Sdim /// Third - The third conversion can be a qualification conversion. 156212795Sdim ImplicitConversionKind Third : 8; 157212795Sdim 158218893Sdim /// \brief Whether this is the deprecated conversion of a 159212795Sdim /// string literal to a pointer to non-const character data 160212795Sdim /// (C++ 4.2p2). 161218893Sdim unsigned DeprecatedStringLiteralToCharPtr : 1; 162212795Sdim 163224145Sdim /// \brief Whether the qualification conversion involves a change in the 164224145Sdim /// Objective-C lifetime (for automatic reference counting). 165224145Sdim unsigned QualificationIncludesObjCLifetime : 1; 166224145Sdim 167212795Sdim /// IncompatibleObjC - Whether this is an Objective-C conversion 168212795Sdim /// that we should warn about (if we actually use it). 169218893Sdim unsigned IncompatibleObjC : 1; 170212795Sdim 171212795Sdim /// ReferenceBinding - True when this is a reference binding 172212795Sdim /// (C++ [over.ics.ref]). 173218893Sdim unsigned ReferenceBinding : 1; 174212795Sdim 175212795Sdim /// DirectBinding - True when this is a reference binding that is a 176212795Sdim /// direct binding (C++ [dcl.init.ref]). 177218893Sdim unsigned DirectBinding : 1; 178212795Sdim 179218893Sdim /// \brief Whether this is an lvalue reference binding (otherwise, it's 180218893Sdim /// an rvalue reference binding). 181218893Sdim unsigned IsLvalueReference : 1; 182218893Sdim 183218893Sdim /// \brief Whether we're binding to a function lvalue. 184218893Sdim unsigned BindsToFunctionLvalue : 1; 185218893Sdim 186218893Sdim /// \brief Whether we're binding to an rvalue. 187218893Sdim unsigned BindsToRvalue : 1; 188218893Sdim 189218893Sdim /// \brief Whether this binds an implicit object argument to a 190218893Sdim /// non-static member function without a ref-qualifier. 191218893Sdim unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1; 192218893Sdim 193224145Sdim /// \brief Whether this binds a reference to an object with a different 194224145Sdim /// Objective-C lifetime qualifier. 195224145Sdim unsigned ObjCLifetimeConversionBinding : 1; 196224145Sdim 197212795Sdim /// FromType - The type that this conversion is converting 198212795Sdim /// from. This is an opaque pointer that can be translated into a 199212795Sdim /// QualType. 200212795Sdim void *FromTypePtr; 201212795Sdim 202212795Sdim /// ToType - The types that this conversion is converting to in 203212795Sdim /// each step. This is an opaque pointer that can be translated 204212795Sdim /// into a QualType. 205212795Sdim void *ToTypePtrs[3]; 206212795Sdim 207212795Sdim /// CopyConstructor - The copy constructor that is used to perform 208212795Sdim /// this conversion, when the conversion is actually just the 209212795Sdim /// initialization of an object via copy constructor. Such 210212795Sdim /// conversions are either identity conversions or derived-to-base 211212795Sdim /// conversions. 212212795Sdim CXXConstructorDecl *CopyConstructor; 213212795Sdim 214212795Sdim void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } 215212795Sdim void setToType(unsigned Idx, QualType T) { 216212795Sdim assert(Idx < 3 && "To type index is out of range"); 217212795Sdim ToTypePtrs[Idx] = T.getAsOpaquePtr(); 218212795Sdim } 219212795Sdim void setAllToTypes(QualType T) { 220212795Sdim ToTypePtrs[0] = T.getAsOpaquePtr(); 221212795Sdim ToTypePtrs[1] = ToTypePtrs[0]; 222212795Sdim ToTypePtrs[2] = ToTypePtrs[0]; 223212795Sdim } 224212795Sdim 225212795Sdim QualType getFromType() const { 226212795Sdim return QualType::getFromOpaquePtr(FromTypePtr); 227212795Sdim } 228212795Sdim QualType getToType(unsigned Idx) const { 229212795Sdim assert(Idx < 3 && "To type index is out of range"); 230212795Sdim return QualType::getFromOpaquePtr(ToTypePtrs[Idx]); 231212795Sdim } 232212795Sdim 233212795Sdim void setAsIdentityConversion(); 234212795Sdim 235212795Sdim bool isIdentityConversion() const { 236223017Sdim return Second == ICK_Identity && Third == ICK_Identity; 237212795Sdim } 238212795Sdim 239212795Sdim ImplicitConversionRank getRank() const; 240234353Sdim NarrowingKind getNarrowingKind(ASTContext &Context, const Expr *Converted, 241234353Sdim APValue &ConstantValue, 242234353Sdim QualType &ConstantType) const; 243212795Sdim bool isPointerConversionToBool() const; 244212795Sdim bool isPointerConversionToVoidPointer(ASTContext& Context) const; 245263508Sdim void dump() const; 246212795Sdim }; 247212795Sdim 248212795Sdim /// UserDefinedConversionSequence - Represents a user-defined 249212795Sdim /// conversion sequence (C++ 13.3.3.1.2). 250212795Sdim struct UserDefinedConversionSequence { 251234353Sdim /// \brief Represents the standard conversion that occurs before 252234353Sdim /// the actual user-defined conversion. 253212795Sdim /// 254234353Sdim /// C++11 13.3.3.1.2p1: 255212795Sdim /// If the user-defined conversion is specified by a constructor 256212795Sdim /// (12.3.1), the initial standard conversion sequence converts 257212795Sdim /// the source type to the type required by the argument of the 258212795Sdim /// constructor. If the user-defined conversion is specified by 259212795Sdim /// a conversion function (12.3.2), the initial standard 260212795Sdim /// conversion sequence converts the source type to the implicit 261212795Sdim /// object parameter of the conversion function. 262212795Sdim StandardConversionSequence Before; 263212795Sdim 264212795Sdim /// EllipsisConversion - When this is true, it means user-defined 265212795Sdim /// conversion sequence starts with a ... (elipsis) conversion, instead of 266212795Sdim /// a standard conversion. In this case, 'Before' field must be ignored. 267212795Sdim // FIXME. I much rather put this as the first field. But there seems to be 268212795Sdim // a gcc code gen. bug which causes a crash in a test. Putting it here seems 269212795Sdim // to work around the crash. 270212795Sdim bool EllipsisConversion : 1; 271226633Sdim 272226633Sdim /// HadMultipleCandidates - When this is true, it means that the 273226633Sdim /// conversion function was resolved from an overloaded set having 274226633Sdim /// size greater than 1. 275226633Sdim bool HadMultipleCandidates : 1; 276226633Sdim 277212795Sdim /// After - Represents the standard conversion that occurs after 278212795Sdim /// the actual user-defined conversion. 279212795Sdim StandardConversionSequence After; 280212795Sdim 281212795Sdim /// ConversionFunction - The function that will perform the 282234353Sdim /// user-defined conversion. Null if the conversion is an 283234353Sdim /// aggregate initialization from an initializer list. 284212795Sdim FunctionDecl* ConversionFunction; 285212795Sdim 286218893Sdim /// \brief The declaration that we found via name lookup, which might be 287218893Sdim /// the same as \c ConversionFunction or it might be a using declaration 288218893Sdim /// that refers to \c ConversionFunction. 289226633Sdim DeclAccessPair FoundConversionFunction; 290234353Sdim 291263508Sdim void dump() const; 292212795Sdim }; 293212795Sdim 294212795Sdim /// Represents an ambiguous user-defined conversion sequence. 295212795Sdim struct AmbiguousConversionSequence { 296226633Sdim typedef SmallVector<FunctionDecl*, 4> ConversionSet; 297212795Sdim 298212795Sdim void *FromTypePtr; 299212795Sdim void *ToTypePtr; 300212795Sdim char Buffer[sizeof(ConversionSet)]; 301212795Sdim 302212795Sdim QualType getFromType() const { 303212795Sdim return QualType::getFromOpaquePtr(FromTypePtr); 304212795Sdim } 305212795Sdim QualType getToType() const { 306212795Sdim return QualType::getFromOpaquePtr(ToTypePtr); 307212795Sdim } 308212795Sdim void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } 309212795Sdim void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); } 310212795Sdim 311212795Sdim ConversionSet &conversions() { 312212795Sdim return *reinterpret_cast<ConversionSet*>(Buffer); 313212795Sdim } 314212795Sdim 315212795Sdim const ConversionSet &conversions() const { 316212795Sdim return *reinterpret_cast<const ConversionSet*>(Buffer); 317212795Sdim } 318212795Sdim 319212795Sdim void addConversion(FunctionDecl *D) { 320212795Sdim conversions().push_back(D); 321212795Sdim } 322212795Sdim 323212795Sdim typedef ConversionSet::iterator iterator; 324212795Sdim iterator begin() { return conversions().begin(); } 325212795Sdim iterator end() { return conversions().end(); } 326212795Sdim 327212795Sdim typedef ConversionSet::const_iterator const_iterator; 328212795Sdim const_iterator begin() const { return conversions().begin(); } 329212795Sdim const_iterator end() const { return conversions().end(); } 330212795Sdim 331212795Sdim void construct(); 332212795Sdim void destruct(); 333212795Sdim void copyFrom(const AmbiguousConversionSequence &); 334212795Sdim }; 335212795Sdim 336212795Sdim /// BadConversionSequence - Records information about an invalid 337212795Sdim /// conversion sequence. 338212795Sdim struct BadConversionSequence { 339212795Sdim enum FailureKind { 340212795Sdim no_conversion, 341212795Sdim unrelated_class, 342212795Sdim suppressed_user, 343218893Sdim bad_qualifiers, 344218893Sdim lvalue_ref_to_rvalue, 345218893Sdim rvalue_ref_to_lvalue 346212795Sdim }; 347212795Sdim 348212795Sdim // This can be null, e.g. for implicit object arguments. 349212795Sdim Expr *FromExpr; 350212795Sdim 351212795Sdim FailureKind Kind; 352212795Sdim 353212795Sdim private: 354212795Sdim // The type we're converting from (an opaque QualType). 355212795Sdim void *FromTy; 356212795Sdim 357212795Sdim // The type we're converting to (an opaque QualType). 358212795Sdim void *ToTy; 359212795Sdim 360212795Sdim public: 361212795Sdim void init(FailureKind K, Expr *From, QualType To) { 362212795Sdim init(K, From->getType(), To); 363212795Sdim FromExpr = From; 364212795Sdim } 365212795Sdim void init(FailureKind K, QualType From, QualType To) { 366212795Sdim Kind = K; 367212795Sdim FromExpr = 0; 368212795Sdim setFromType(From); 369212795Sdim setToType(To); 370212795Sdim } 371212795Sdim 372212795Sdim QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); } 373212795Sdim QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); } 374212795Sdim 375212795Sdim void setFromExpr(Expr *E) { 376212795Sdim FromExpr = E; 377212795Sdim setFromType(E->getType()); 378212795Sdim } 379212795Sdim void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); } 380212795Sdim void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); } 381212795Sdim }; 382212795Sdim 383212795Sdim /// ImplicitConversionSequence - Represents an implicit conversion 384212795Sdim /// sequence, which may be a standard conversion sequence 385212795Sdim /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2), 386212795Sdim /// or an ellipsis conversion sequence (C++ 13.3.3.1.3). 387212795Sdim class ImplicitConversionSequence { 388212795Sdim public: 389212795Sdim /// Kind - The kind of implicit conversion sequence. BadConversion 390212795Sdim /// specifies that there is no conversion from the source type to 391212795Sdim /// the target type. AmbiguousConversion represents the unique 392212795Sdim /// ambiguous conversion (C++0x [over.best.ics]p10). 393212795Sdim enum Kind { 394212795Sdim StandardConversion = 0, 395212795Sdim UserDefinedConversion, 396212795Sdim AmbiguousConversion, 397212795Sdim EllipsisConversion, 398212795Sdim BadConversion 399212795Sdim }; 400212795Sdim 401212795Sdim private: 402212795Sdim enum { 403212795Sdim Uninitialized = BadConversion + 1 404212795Sdim }; 405212795Sdim 406212795Sdim /// ConversionKind - The kind of implicit conversion sequence. 407234353Sdim unsigned ConversionKind : 30; 408212795Sdim 409234353Sdim /// \brief Whether the target is really a std::initializer_list, and the 410234353Sdim /// sequence only represents the worst element conversion. 411234353Sdim bool StdInitializerListElement : 1; 412234353Sdim 413212795Sdim void setKind(Kind K) { 414212795Sdim destruct(); 415212795Sdim ConversionKind = K; 416212795Sdim } 417212795Sdim 418212795Sdim void destruct() { 419212795Sdim if (ConversionKind == AmbiguousConversion) Ambiguous.destruct(); 420212795Sdim } 421212795Sdim 422212795Sdim public: 423212795Sdim union { 424212795Sdim /// When ConversionKind == StandardConversion, provides the 425212795Sdim /// details of the standard conversion sequence. 426212795Sdim StandardConversionSequence Standard; 427212795Sdim 428212795Sdim /// When ConversionKind == UserDefinedConversion, provides the 429212795Sdim /// details of the user-defined conversion sequence. 430212795Sdim UserDefinedConversionSequence UserDefined; 431212795Sdim 432212795Sdim /// When ConversionKind == AmbiguousConversion, provides the 433212795Sdim /// details of the ambiguous conversion. 434212795Sdim AmbiguousConversionSequence Ambiguous; 435212795Sdim 436212795Sdim /// When ConversionKind == BadConversion, provides the details 437212795Sdim /// of the bad conversion. 438212795Sdim BadConversionSequence Bad; 439212795Sdim }; 440212795Sdim 441263508Sdim ImplicitConversionSequence() 442263508Sdim : ConversionKind(Uninitialized), StdInitializerListElement(false) 443234353Sdim {} 444212795Sdim ~ImplicitConversionSequence() { 445212795Sdim destruct(); 446212795Sdim } 447212795Sdim ImplicitConversionSequence(const ImplicitConversionSequence &Other) 448263508Sdim : ConversionKind(Other.ConversionKind), 449234353Sdim StdInitializerListElement(Other.StdInitializerListElement) 450212795Sdim { 451212795Sdim switch (ConversionKind) { 452212795Sdim case Uninitialized: break; 453212795Sdim case StandardConversion: Standard = Other.Standard; break; 454212795Sdim case UserDefinedConversion: UserDefined = Other.UserDefined; break; 455212795Sdim case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break; 456212795Sdim case EllipsisConversion: break; 457212795Sdim case BadConversion: Bad = Other.Bad; break; 458212795Sdim } 459212795Sdim } 460212795Sdim 461212795Sdim ImplicitConversionSequence & 462212795Sdim operator=(const ImplicitConversionSequence &Other) { 463212795Sdim destruct(); 464212795Sdim new (this) ImplicitConversionSequence(Other); 465212795Sdim return *this; 466212795Sdim } 467212795Sdim 468212795Sdim Kind getKind() const { 469212795Sdim assert(isInitialized() && "querying uninitialized conversion"); 470212795Sdim return Kind(ConversionKind); 471212795Sdim } 472212795Sdim 473212795Sdim /// \brief Return a ranking of the implicit conversion sequence 474212795Sdim /// kind, where smaller ranks represent better conversion 475212795Sdim /// sequences. 476212795Sdim /// 477212795Sdim /// In particular, this routine gives user-defined conversion 478212795Sdim /// sequences and ambiguous conversion sequences the same rank, 479212795Sdim /// per C++ [over.best.ics]p10. 480212795Sdim unsigned getKindRank() const { 481212795Sdim switch (getKind()) { 482212795Sdim case StandardConversion: 483212795Sdim return 0; 484212795Sdim 485212795Sdim case UserDefinedConversion: 486212795Sdim case AmbiguousConversion: 487212795Sdim return 1; 488212795Sdim 489212795Sdim case EllipsisConversion: 490212795Sdim return 2; 491212795Sdim 492212795Sdim case BadConversion: 493212795Sdim return 3; 494212795Sdim } 495212795Sdim 496234353Sdim llvm_unreachable("Invalid ImplicitConversionSequence::Kind!"); 497212795Sdim } 498212795Sdim 499212795Sdim bool isBad() const { return getKind() == BadConversion; } 500212795Sdim bool isStandard() const { return getKind() == StandardConversion; } 501212795Sdim bool isEllipsis() const { return getKind() == EllipsisConversion; } 502212795Sdim bool isAmbiguous() const { return getKind() == AmbiguousConversion; } 503212795Sdim bool isUserDefined() const { return getKind() == UserDefinedConversion; } 504226633Sdim bool isFailure() const { return isBad() || isAmbiguous(); } 505212795Sdim 506212795Sdim /// Determines whether this conversion sequence has been 507212795Sdim /// initialized. Most operations should never need to query 508212795Sdim /// uninitialized conversions and should assert as above. 509212795Sdim bool isInitialized() const { return ConversionKind != Uninitialized; } 510212795Sdim 511212795Sdim /// Sets this sequence as a bad conversion for an explicit argument. 512212795Sdim void setBad(BadConversionSequence::FailureKind Failure, 513212795Sdim Expr *FromExpr, QualType ToType) { 514212795Sdim setKind(BadConversion); 515212795Sdim Bad.init(Failure, FromExpr, ToType); 516212795Sdim } 517212795Sdim 518212795Sdim /// Sets this sequence as a bad conversion for an implicit argument. 519212795Sdim void setBad(BadConversionSequence::FailureKind Failure, 520212795Sdim QualType FromType, QualType ToType) { 521212795Sdim setKind(BadConversion); 522212795Sdim Bad.init(Failure, FromType, ToType); 523212795Sdim } 524212795Sdim 525212795Sdim void setStandard() { setKind(StandardConversion); } 526212795Sdim void setEllipsis() { setKind(EllipsisConversion); } 527212795Sdim void setUserDefined() { setKind(UserDefinedConversion); } 528212795Sdim void setAmbiguous() { 529212795Sdim if (ConversionKind == AmbiguousConversion) return; 530212795Sdim ConversionKind = AmbiguousConversion; 531212795Sdim Ambiguous.construct(); 532212795Sdim } 533212795Sdim 534234353Sdim /// \brief Whether the target is really a std::initializer_list, and the 535234353Sdim /// sequence only represents the worst element conversion. 536234353Sdim bool isStdInitializerListElement() const { 537234353Sdim return StdInitializerListElement; 538234353Sdim } 539234353Sdim 540234353Sdim void setStdInitializerListElement(bool V = true) { 541234353Sdim StdInitializerListElement = V; 542234353Sdim } 543234353Sdim 544212795Sdim // The result of a comparison between implicit conversion 545212795Sdim // sequences. Use Sema::CompareImplicitConversionSequences to 546212795Sdim // actually perform the comparison. 547212795Sdim enum CompareKind { 548212795Sdim Better = -1, 549212795Sdim Indistinguishable = 0, 550212795Sdim Worse = 1 551212795Sdim }; 552212795Sdim 553212795Sdim void DiagnoseAmbiguousConversion(Sema &S, 554212795Sdim SourceLocation CaretLoc, 555212795Sdim const PartialDiagnostic &PDiag) const; 556212795Sdim 557263508Sdim void dump() const; 558212795Sdim }; 559212795Sdim 560212795Sdim enum OverloadFailureKind { 561212795Sdim ovl_fail_too_many_arguments, 562212795Sdim ovl_fail_too_few_arguments, 563212795Sdim ovl_fail_bad_conversion, 564212795Sdim ovl_fail_bad_deduction, 565212795Sdim 566212795Sdim /// This conversion candidate was not considered because it 567212795Sdim /// duplicates the work of a trivial or derived-to-base 568212795Sdim /// conversion. 569212795Sdim ovl_fail_trivial_conversion, 570212795Sdim 571212795Sdim /// This conversion candidate is not viable because its result 572212795Sdim /// type is not implicitly convertible to the desired type. 573212795Sdim ovl_fail_bad_final_conversion, 574212795Sdim 575212795Sdim /// This conversion function template specialization candidate is not 576212795Sdim /// viable because the final conversion was not an exact match. 577226633Sdim ovl_fail_final_conversion_not_exact, 578226633Sdim 579226633Sdim /// (CUDA) This candidate was not viable because the callee 580226633Sdim /// was not accessible from the caller's target (i.e. host->device, 581226633Sdim /// global->host, device->host). 582226633Sdim ovl_fail_bad_target 583212795Sdim }; 584212795Sdim 585212795Sdim /// OverloadCandidate - A single candidate in an overload set (C++ 13.3). 586212795Sdim struct OverloadCandidate { 587212795Sdim /// Function - The actual function that this candidate 588212795Sdim /// represents. When NULL, this is a built-in candidate 589212795Sdim /// (C++ [over.oper]) or a surrogate for a conversion to a 590212795Sdim /// function pointer or reference (C++ [over.call.object]). 591212795Sdim FunctionDecl *Function; 592212795Sdim 593212795Sdim /// FoundDecl - The original declaration that was looked up / 594212795Sdim /// invented / otherwise found, together with its access. 595212795Sdim /// Might be a UsingShadowDecl or a FunctionTemplateDecl. 596212795Sdim DeclAccessPair FoundDecl; 597212795Sdim 598212795Sdim // BuiltinTypes - Provides the return and parameter types of a 599212795Sdim // built-in overload candidate. Only valid when Function is NULL. 600212795Sdim struct { 601212795Sdim QualType ResultTy; 602212795Sdim QualType ParamTypes[3]; 603212795Sdim } BuiltinTypes; 604212795Sdim 605212795Sdim /// Surrogate - The conversion function for which this candidate 606212795Sdim /// is a surrogate, but only if IsSurrogate is true. 607212795Sdim CXXConversionDecl *Surrogate; 608212795Sdim 609212795Sdim /// Conversions - The conversion sequences used to convert the 610234353Sdim /// function arguments to the function parameters, the pointer points to a 611234353Sdim /// fixed size array with NumConversions elements. The memory is owned by 612234353Sdim /// the OverloadCandidateSet. 613234353Sdim ImplicitConversionSequence *Conversions; 614212795Sdim 615226633Sdim /// The FixIt hints which can be used to fix the Bad candidate. 616226633Sdim ConversionFixItGenerator Fix; 617226633Sdim 618234353Sdim /// NumConversions - The number of elements in the Conversions array. 619234353Sdim unsigned NumConversions; 620234353Sdim 621212795Sdim /// Viable - True to indicate that this overload candidate is viable. 622212795Sdim bool Viable; 623212795Sdim 624212795Sdim /// IsSurrogate - True to indicate that this candidate is a 625212795Sdim /// surrogate for a conversion to a function pointer or reference 626212795Sdim /// (C++ [over.call.object]). 627212795Sdim bool IsSurrogate; 628212795Sdim 629212795Sdim /// IgnoreObjectArgument - True to indicate that the first 630212795Sdim /// argument's conversion, which for this function represents the 631212795Sdim /// implicit object argument, should be ignored. This will be true 632212795Sdim /// when the candidate is a static member function (where the 633212795Sdim /// implicit object argument is just a placeholder) or a 634212795Sdim /// non-static member function when the call doesn't have an 635212795Sdim /// object argument. 636212795Sdim bool IgnoreObjectArgument; 637212795Sdim 638212795Sdim /// FailureKind - The reason why this candidate is not viable. 639212795Sdim /// Actually an OverloadFailureKind. 640212795Sdim unsigned char FailureKind; 641212795Sdim 642218893Sdim /// \brief The number of call arguments that were explicitly provided, 643218893Sdim /// to be used while performing partial ordering of function templates. 644218893Sdim unsigned ExplicitCallArguments; 645212795Sdim 646212795Sdim union { 647212795Sdim DeductionFailureInfo DeductionFailure; 648212795Sdim 649212795Sdim /// FinalConversion - For a conversion function (where Function is 650212795Sdim /// a CXXConversionDecl), the standard conversion that occurs 651212795Sdim /// after the call to the overload candidate to convert the result 652212795Sdim /// of calling the conversion function to the required type. 653212795Sdim StandardConversionSequence FinalConversion; 654212795Sdim }; 655212795Sdim 656212795Sdim /// hasAmbiguousConversion - Returns whether this overload 657212795Sdim /// candidate requires an ambiguous conversion or not. 658212795Sdim bool hasAmbiguousConversion() const { 659234353Sdim for (unsigned i = 0, e = NumConversions; i != e; ++i) { 660234353Sdim if (!Conversions[i].isInitialized()) return false; 661234353Sdim if (Conversions[i].isAmbiguous()) return true; 662212795Sdim } 663212795Sdim return false; 664212795Sdim } 665226633Sdim 666226633Sdim bool TryToFixBadConversion(unsigned Idx, Sema &S) { 667226633Sdim bool CanFix = Fix.tryToFixConversion( 668226633Sdim Conversions[Idx].Bad.FromExpr, 669226633Sdim Conversions[Idx].Bad.getFromType(), 670226633Sdim Conversions[Idx].Bad.getToType(), S); 671226633Sdim 672226633Sdim // If at least one conversion fails, the candidate cannot be fixed. 673226633Sdim if (!CanFix) 674226633Sdim Fix.clear(); 675226633Sdim 676226633Sdim return CanFix; 677226633Sdim } 678212795Sdim }; 679212795Sdim 680212795Sdim /// OverloadCandidateSet - A set of overload candidates, used in C++ 681212795Sdim /// overload resolution (C++ 13.3). 682234353Sdim class OverloadCandidateSet { 683234353Sdim SmallVector<OverloadCandidate, 16> Candidates; 684212795Sdim llvm::SmallPtrSet<Decl *, 16> Functions; 685212795Sdim 686234353Sdim // Allocator for OverloadCandidate::Conversions. We store the first few 687234353Sdim // elements inline to avoid allocation for small sets. 688234353Sdim llvm::BumpPtrAllocator ConversionSequenceAllocator; 689234353Sdim 690234353Sdim SourceLocation Loc; 691234353Sdim 692234353Sdim unsigned NumInlineSequences; 693234353Sdim char InlineSpace[16 * sizeof(ImplicitConversionSequence)]; 694234353Sdim 695243830Sdim OverloadCandidateSet(const OverloadCandidateSet &) LLVM_DELETED_FUNCTION; 696243830Sdim void operator=(const OverloadCandidateSet &) LLVM_DELETED_FUNCTION; 697243830Sdim 698243830Sdim void destroyCandidates(); 699243830Sdim 700212795Sdim public: 701234353Sdim OverloadCandidateSet(SourceLocation Loc) : Loc(Loc), NumInlineSequences(0){} 702243830Sdim ~OverloadCandidateSet() { destroyCandidates(); } 703212795Sdim 704212795Sdim SourceLocation getLocation() const { return Loc; } 705212795Sdim 706212795Sdim /// \brief Determine when this overload candidate will be new to the 707212795Sdim /// overload set. 708212795Sdim bool isNewCandidate(Decl *F) { 709212795Sdim return Functions.insert(F->getCanonicalDecl()); 710212795Sdim } 711212795Sdim 712212795Sdim /// \brief Clear out all of the candidates. 713212795Sdim void clear(); 714212795Sdim 715263508Sdim typedef SmallVectorImpl<OverloadCandidate>::iterator iterator; 716234353Sdim iterator begin() { return Candidates.begin(); } 717234353Sdim iterator end() { return Candidates.end(); } 718234353Sdim 719234353Sdim size_t size() const { return Candidates.size(); } 720234353Sdim bool empty() const { return Candidates.empty(); } 721234353Sdim 722234353Sdim /// \brief Add a new candidate with NumConversions conversion sequence slots 723234353Sdim /// to the overload set. 724234353Sdim OverloadCandidate &addCandidate(unsigned NumConversions = 0) { 725234353Sdim Candidates.push_back(OverloadCandidate()); 726234353Sdim OverloadCandidate &C = Candidates.back(); 727234353Sdim 728234353Sdim // Assign space from the inline array if there are enough free slots 729234353Sdim // available. 730234353Sdim if (NumConversions + NumInlineSequences <= 16) { 731234353Sdim ImplicitConversionSequence *I = 732234353Sdim (ImplicitConversionSequence*)InlineSpace; 733234353Sdim C.Conversions = &I[NumInlineSequences]; 734234353Sdim NumInlineSequences += NumConversions; 735234353Sdim } else { 736234353Sdim // Otherwise get memory from the allocator. 737234353Sdim C.Conversions = ConversionSequenceAllocator 738234353Sdim .Allocate<ImplicitConversionSequence>(NumConversions); 739234353Sdim } 740234353Sdim 741234353Sdim // Construct the new objects. 742234353Sdim for (unsigned i = 0; i != NumConversions; ++i) 743234353Sdim new (&C.Conversions[i]) ImplicitConversionSequence(); 744234353Sdim 745234353Sdim C.NumConversions = NumConversions; 746234353Sdim return C; 747234353Sdim } 748234353Sdim 749212795Sdim /// Find the best viable function on this overload set, if it exists. 750212795Sdim OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc, 751218893Sdim OverloadCandidateSet::iterator& Best, 752218893Sdim bool UserDefinedConversion = false); 753212795Sdim 754212795Sdim void NoteCandidates(Sema &S, 755212795Sdim OverloadCandidateDisplayKind OCD, 756249423Sdim ArrayRef<Expr *> Args, 757243830Sdim StringRef Opc = "", 758212795Sdim SourceLocation Loc = SourceLocation()); 759212795Sdim }; 760212795Sdim 761212795Sdim bool isBetterOverloadCandidate(Sema &S, 762212795Sdim const OverloadCandidate& Cand1, 763212795Sdim const OverloadCandidate& Cand2, 764218893Sdim SourceLocation Loc, 765218893Sdim bool UserDefinedConversion = false); 766212795Sdim} // end namespace clang 767212795Sdim 768212795Sdim#endif // LLVM_CLANG_SEMA_OVERLOAD_H 769