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