clang API Documentation

Overload.h
Go to the documentation of this file.
00001 //===--- Overload.h - C++ Overloading ---------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the data structures and types used in C++
00011 // overload resolution.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_SEMA_OVERLOAD_H
00016 #define LLVM_CLANG_SEMA_OVERLOAD_H
00017 
00018 #include "clang/AST/Decl.h"
00019 #include "clang/AST/DeclTemplate.h"
00020 #include "clang/AST/Expr.h"
00021 #include "clang/AST/TemplateBase.h"
00022 #include "clang/AST/Type.h"
00023 #include "clang/AST/UnresolvedSet.h"
00024 #include "clang/Sema/SemaFixItUtils.h"
00025 #include "llvm/ADT/SmallPtrSet.h"
00026 #include "llvm/ADT/SmallVector.h"
00027 #include "llvm/Support/Allocator.h"
00028 
00029 namespace clang {
00030   class ASTContext;
00031   class CXXConstructorDecl;
00032   class CXXConversionDecl;
00033   class FunctionDecl;
00034   class Sema;
00035 
00036   /// OverloadingResult - Capture the result of performing overload
00037   /// resolution.
00038   enum OverloadingResult {
00039     OR_Success,             ///< Overload resolution succeeded.
00040     OR_No_Viable_Function,  ///< No viable function found.
00041     OR_Ambiguous,           ///< Ambiguous candidates found.
00042     OR_Deleted              ///< Succeeded, but refers to a deleted function.
00043   };
00044   
00045   enum OverloadCandidateDisplayKind {
00046     /// Requests that all candidates be shown.  Viable candidates will
00047     /// be printed first.
00048     OCD_AllCandidates,
00049 
00050     /// Requests that only viable candidates be shown.
00051     OCD_ViableCandidates
00052   };
00053 
00054   /// ImplicitConversionKind - The kind of implicit conversion used to
00055   /// convert an argument to a parameter's type. The enumerator values
00056   /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that
00057   /// better conversion kinds have smaller values.
00058   enum ImplicitConversionKind {
00059     ICK_Identity = 0,          ///< Identity conversion (no conversion)
00060     ICK_Lvalue_To_Rvalue,      ///< Lvalue-to-rvalue conversion (C++ 4.1)
00061     ICK_Array_To_Pointer,      ///< Array-to-pointer conversion (C++ 4.2)
00062     ICK_Function_To_Pointer,   ///< Function-to-pointer (C++ 4.3)
00063     ICK_NoReturn_Adjustment,   ///< Removal of noreturn from a type (Clang)
00064     ICK_Qualification,         ///< Qualification conversions (C++ 4.4)
00065     ICK_Integral_Promotion,    ///< Integral promotions (C++ 4.5)
00066     ICK_Floating_Promotion,    ///< Floating point promotions (C++ 4.6)
00067     ICK_Complex_Promotion,     ///< Complex promotions (Clang extension)
00068     ICK_Integral_Conversion,   ///< Integral conversions (C++ 4.7)
00069     ICK_Floating_Conversion,   ///< Floating point conversions (C++ 4.8)
00070     ICK_Complex_Conversion,    ///< Complex conversions (C99 6.3.1.6)
00071     ICK_Floating_Integral,     ///< Floating-integral conversions (C++ 4.9)
00072     ICK_Pointer_Conversion,    ///< Pointer conversions (C++ 4.10)
00073     ICK_Pointer_Member,        ///< Pointer-to-member conversions (C++ 4.11)
00074     ICK_Boolean_Conversion,    ///< Boolean conversions (C++ 4.12)
00075     ICK_Compatible_Conversion, ///< Conversions between compatible types in C99
00076     ICK_Derived_To_Base,       ///< Derived-to-base (C++ [over.best.ics])
00077     ICK_Vector_Conversion,     ///< Vector conversions
00078     ICK_Vector_Splat,          ///< A vector splat from an arithmetic type
00079     ICK_Complex_Real,          ///< Complex-real conversions (C99 6.3.1.7)
00080     ICK_Block_Pointer_Conversion,    ///< Block Pointer conversions 
00081     ICK_TransparentUnionConversion, /// Transparent Union Conversions
00082     ICK_Writeback_Conversion,  ///< Objective-C ARC writeback conversion
00083     ICK_Num_Conversion_Kinds   ///< The number of conversion kinds
00084   };
00085 
00086   /// ImplicitConversionCategory - The category of an implicit
00087   /// conversion kind. The enumerator values match with Table 9 of
00088   /// (C++ 13.3.3.1.1) and are listed such that better conversion
00089   /// categories have smaller values.
00090   enum ImplicitConversionCategory {
00091     ICC_Identity = 0,              ///< Identity
00092     ICC_Lvalue_Transformation,     ///< Lvalue transformation
00093     ICC_Qualification_Adjustment,  ///< Qualification adjustment
00094     ICC_Promotion,                 ///< Promotion
00095     ICC_Conversion                 ///< Conversion
00096   };
00097 
00098   ImplicitConversionCategory
00099   GetConversionCategory(ImplicitConversionKind Kind);
00100 
00101   /// ImplicitConversionRank - The rank of an implicit conversion
00102   /// kind. The enumerator values match with Table 9 of (C++
00103   /// 13.3.3.1.1) and are listed such that better conversion ranks
00104   /// have smaller values.
00105   enum ImplicitConversionRank {
00106     ICR_Exact_Match = 0,         ///< Exact Match
00107     ICR_Promotion,               ///< Promotion
00108     ICR_Conversion,              ///< Conversion
00109     ICR_Complex_Real_Conversion, ///< Complex <-> Real conversion
00110     ICR_Writeback_Conversion     ///< ObjC ARC writeback conversion
00111   };
00112 
00113   ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
00114 
00115   /// NarrowingKind - The kind of narrowing conversion being performed by a
00116   /// standard conversion sequence according to C++11 [dcl.init.list]p7.
00117   enum NarrowingKind {
00118     /// Not a narrowing conversion.
00119     NK_Not_Narrowing,
00120 
00121     /// A narrowing conversion by virtue of the source and destination types.
00122     NK_Type_Narrowing,
00123 
00124     /// A narrowing conversion, because a constant expression got narrowed.
00125     NK_Constant_Narrowing,
00126 
00127     /// A narrowing conversion, because a non-constant-expression variable might
00128     /// have got narrowed.
00129     NK_Variable_Narrowing
00130   };
00131 
00132   /// StandardConversionSequence - represents a standard conversion
00133   /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
00134   /// contains between zero and three conversions. If a particular
00135   /// conversion is not needed, it will be set to the identity conversion
00136   /// (ICK_Identity). Note that the three conversions are
00137   /// specified as separate members (rather than in an array) so that
00138   /// we can keep the size of a standard conversion sequence to a
00139   /// single word.
00140   class StandardConversionSequence {
00141   public:
00142     /// First -- The first conversion can be an lvalue-to-rvalue
00143     /// conversion, array-to-pointer conversion, or
00144     /// function-to-pointer conversion.
00145     ImplicitConversionKind First : 8;
00146 
00147     /// Second - The second conversion can be an integral promotion,
00148     /// floating point promotion, integral conversion, floating point
00149     /// conversion, floating-integral conversion, pointer conversion,
00150     /// pointer-to-member conversion, or boolean conversion.
00151     ImplicitConversionKind Second : 8;
00152 
00153     /// Third - The third conversion can be a qualification conversion.
00154     ImplicitConversionKind Third : 8;
00155 
00156     /// \brief Whether this is the deprecated conversion of a
00157     /// string literal to a pointer to non-const character data
00158     /// (C++ 4.2p2).
00159     unsigned DeprecatedStringLiteralToCharPtr : 1;
00160 
00161     /// \brief Whether the qualification conversion involves a change in the
00162     /// Objective-C lifetime (for automatic reference counting).
00163     unsigned QualificationIncludesObjCLifetime : 1;
00164     
00165     /// IncompatibleObjC - Whether this is an Objective-C conversion
00166     /// that we should warn about (if we actually use it).
00167     unsigned IncompatibleObjC : 1;
00168 
00169     /// ReferenceBinding - True when this is a reference binding
00170     /// (C++ [over.ics.ref]).
00171     unsigned ReferenceBinding : 1;
00172 
00173     /// DirectBinding - True when this is a reference binding that is a
00174     /// direct binding (C++ [dcl.init.ref]).
00175     unsigned DirectBinding : 1;
00176 
00177     /// \brief Whether this is an lvalue reference binding (otherwise, it's
00178     /// an rvalue reference binding).
00179     unsigned IsLvalueReference : 1;
00180     
00181     /// \brief Whether we're binding to a function lvalue.
00182     unsigned BindsToFunctionLvalue : 1;
00183     
00184     /// \brief Whether we're binding to an rvalue.
00185     unsigned BindsToRvalue : 1;
00186     
00187     /// \brief Whether this binds an implicit object argument to a 
00188     /// non-static member function without a ref-qualifier.
00189     unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
00190     
00191     /// \brief Whether this binds a reference to an object with a different
00192     /// Objective-C lifetime qualifier.
00193     unsigned ObjCLifetimeConversionBinding : 1;
00194     
00195     /// FromType - The type that this conversion is converting
00196     /// from. This is an opaque pointer that can be translated into a
00197     /// QualType.
00198     void *FromTypePtr;
00199 
00200     /// ToType - The types that this conversion is converting to in
00201     /// each step. This is an opaque pointer that can be translated
00202     /// into a QualType.
00203     void *ToTypePtrs[3];
00204 
00205     /// CopyConstructor - The copy constructor that is used to perform
00206     /// this conversion, when the conversion is actually just the
00207     /// initialization of an object via copy constructor. Such
00208     /// conversions are either identity conversions or derived-to-base
00209     /// conversions.
00210     CXXConstructorDecl *CopyConstructor;
00211 
00212     void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
00213     void setToType(unsigned Idx, QualType T) { 
00214       assert(Idx < 3 && "To type index is out of range");
00215       ToTypePtrs[Idx] = T.getAsOpaquePtr(); 
00216     }
00217     void setAllToTypes(QualType T) {
00218       ToTypePtrs[0] = T.getAsOpaquePtr(); 
00219       ToTypePtrs[1] = ToTypePtrs[0];
00220       ToTypePtrs[2] = ToTypePtrs[0];
00221     }
00222 
00223     QualType getFromType() const {
00224       return QualType::getFromOpaquePtr(FromTypePtr);
00225     }
00226     QualType getToType(unsigned Idx) const {
00227       assert(Idx < 3 && "To type index is out of range");
00228       return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
00229     }
00230 
00231     void setAsIdentityConversion();
00232     
00233     bool isIdentityConversion() const {
00234       return Second == ICK_Identity && Third == ICK_Identity;
00235     }
00236     
00237     ImplicitConversionRank getRank() const;
00238     NarrowingKind getNarrowingKind(ASTContext &Context, const Expr *Converted,
00239                                    APValue &ConstantValue,
00240                                    QualType &ConstantType) const;
00241     bool isPointerConversionToBool() const;
00242     bool isPointerConversionToVoidPointer(ASTContext& Context) const;
00243     void DebugPrint() const;
00244   };
00245 
00246   /// UserDefinedConversionSequence - Represents a user-defined
00247   /// conversion sequence (C++ 13.3.3.1.2).
00248   struct UserDefinedConversionSequence {
00249     /// \brief Represents the standard conversion that occurs before
00250     /// the actual user-defined conversion.
00251     ///
00252     /// C++11 13.3.3.1.2p1:
00253     ///   If the user-defined conversion is specified by a constructor
00254     ///   (12.3.1), the initial standard conversion sequence converts
00255     ///   the source type to the type required by the argument of the
00256     ///   constructor. If the user-defined conversion is specified by
00257     ///   a conversion function (12.3.2), the initial standard
00258     ///   conversion sequence converts the source type to the implicit
00259     ///   object parameter of the conversion function.
00260     StandardConversionSequence Before;
00261 
00262     /// EllipsisConversion - When this is true, it means user-defined
00263     /// conversion sequence starts with a ... (elipsis) conversion, instead of 
00264     /// a standard conversion. In this case, 'Before' field must be ignored.
00265     // FIXME. I much rather put this as the first field. But there seems to be
00266     // a gcc code gen. bug which causes a crash in a test. Putting it here seems
00267     // to work around the crash.
00268     bool EllipsisConversion : 1;
00269 
00270     /// HadMultipleCandidates - When this is true, it means that the
00271     /// conversion function was resolved from an overloaded set having
00272     /// size greater than 1.
00273     bool HadMultipleCandidates : 1;
00274 
00275     /// After - Represents the standard conversion that occurs after
00276     /// the actual user-defined conversion.
00277     StandardConversionSequence After;
00278 
00279     /// ConversionFunction - The function that will perform the
00280     /// user-defined conversion. Null if the conversion is an
00281     /// aggregate initialization from an initializer list.
00282     FunctionDecl* ConversionFunction;
00283 
00284     /// \brief The declaration that we found via name lookup, which might be
00285     /// the same as \c ConversionFunction or it might be a using declaration
00286     /// that refers to \c ConversionFunction.
00287     DeclAccessPair FoundConversionFunction;
00288 
00289     void DebugPrint() const;
00290   };
00291 
00292   /// Represents an ambiguous user-defined conversion sequence.
00293   struct AmbiguousConversionSequence {
00294     typedef SmallVector<FunctionDecl*, 4> ConversionSet;
00295 
00296     void *FromTypePtr;
00297     void *ToTypePtr;
00298     char Buffer[sizeof(ConversionSet)];
00299 
00300     QualType getFromType() const {
00301       return QualType::getFromOpaquePtr(FromTypePtr);
00302     }
00303     QualType getToType() const {
00304       return QualType::getFromOpaquePtr(ToTypePtr);
00305     }
00306     void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
00307     void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
00308 
00309     ConversionSet &conversions() {
00310       return *reinterpret_cast<ConversionSet*>(Buffer);
00311     }
00312 
00313     const ConversionSet &conversions() const {
00314       return *reinterpret_cast<const ConversionSet*>(Buffer);
00315     }
00316 
00317     void addConversion(FunctionDecl *D) {
00318       conversions().push_back(D);
00319     }
00320 
00321     typedef ConversionSet::iterator iterator;
00322     iterator begin() { return conversions().begin(); }
00323     iterator end() { return conversions().end(); }
00324 
00325     typedef ConversionSet::const_iterator const_iterator;
00326     const_iterator begin() const { return conversions().begin(); }
00327     const_iterator end() const { return conversions().end(); }
00328 
00329     void construct();
00330     void destruct();
00331     void copyFrom(const AmbiguousConversionSequence &);
00332   };
00333 
00334   /// BadConversionSequence - Records information about an invalid
00335   /// conversion sequence.
00336   struct BadConversionSequence {
00337     enum FailureKind {
00338       no_conversion,
00339       unrelated_class,
00340       suppressed_user,
00341       bad_qualifiers,
00342       lvalue_ref_to_rvalue,
00343       rvalue_ref_to_lvalue
00344     };
00345 
00346     // This can be null, e.g. for implicit object arguments.
00347     Expr *FromExpr;
00348 
00349     FailureKind Kind;
00350 
00351   private:
00352     // The type we're converting from (an opaque QualType).
00353     void *FromTy;
00354 
00355     // The type we're converting to (an opaque QualType).
00356     void *ToTy;
00357 
00358   public:
00359     void init(FailureKind K, Expr *From, QualType To) {
00360       init(K, From->getType(), To);
00361       FromExpr = From;
00362     }
00363     void init(FailureKind K, QualType From, QualType To) {
00364       Kind = K;
00365       FromExpr = 0;
00366       setFromType(From);
00367       setToType(To);
00368     }
00369 
00370     QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
00371     QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
00372 
00373     void setFromExpr(Expr *E) {
00374       FromExpr = E;
00375       setFromType(E->getType());
00376     }
00377     void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
00378     void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
00379   };
00380 
00381   /// ImplicitConversionSequence - Represents an implicit conversion
00382   /// sequence, which may be a standard conversion sequence
00383   /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
00384   /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
00385   class ImplicitConversionSequence {
00386   public:
00387     /// Kind - The kind of implicit conversion sequence. BadConversion
00388     /// specifies that there is no conversion from the source type to
00389     /// the target type.  AmbiguousConversion represents the unique
00390     /// ambiguous conversion (C++0x [over.best.ics]p10).
00391     enum Kind {
00392       StandardConversion = 0,
00393       UserDefinedConversion,
00394       AmbiguousConversion,
00395       EllipsisConversion,
00396       BadConversion
00397     };
00398 
00399   private:
00400     enum {
00401       Uninitialized = BadConversion + 1
00402     };
00403 
00404     /// ConversionKind - The kind of implicit conversion sequence.
00405     unsigned ConversionKind : 30;
00406 
00407     /// \brief Whether the argument is an initializer list.
00408     bool ListInitializationSequence : 1;
00409 
00410     /// \brief Whether the target is really a std::initializer_list, and the
00411     /// sequence only represents the worst element conversion.
00412     bool StdInitializerListElement : 1;
00413 
00414     void setKind(Kind K) {
00415       destruct();
00416       ConversionKind = K;
00417     }
00418 
00419     void destruct() {
00420       if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
00421     }
00422 
00423   public:
00424     union {
00425       /// When ConversionKind == StandardConversion, provides the
00426       /// details of the standard conversion sequence.
00427       StandardConversionSequence Standard;
00428 
00429       /// When ConversionKind == UserDefinedConversion, provides the
00430       /// details of the user-defined conversion sequence.
00431       UserDefinedConversionSequence UserDefined;
00432 
00433       /// When ConversionKind == AmbiguousConversion, provides the
00434       /// details of the ambiguous conversion.
00435       AmbiguousConversionSequence Ambiguous;
00436 
00437       /// When ConversionKind == BadConversion, provides the details
00438       /// of the bad conversion.
00439       BadConversionSequence Bad;
00440     };
00441 
00442     ImplicitConversionSequence() 
00443       : ConversionKind(Uninitialized), ListInitializationSequence(false),
00444         StdInitializerListElement(false)
00445     {}
00446     ~ImplicitConversionSequence() {
00447       destruct();
00448     }
00449     ImplicitConversionSequence(const ImplicitConversionSequence &Other)
00450       : ConversionKind(Other.ConversionKind), 
00451         ListInitializationSequence(Other.ListInitializationSequence),
00452         StdInitializerListElement(Other.StdInitializerListElement)
00453     {
00454       switch (ConversionKind) {
00455       case Uninitialized: break;
00456       case StandardConversion: Standard = Other.Standard; break;
00457       case UserDefinedConversion: UserDefined = Other.UserDefined; break;
00458       case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
00459       case EllipsisConversion: break;
00460       case BadConversion: Bad = Other.Bad; break;
00461       }
00462     }
00463 
00464     ImplicitConversionSequence &
00465         operator=(const ImplicitConversionSequence &Other) {
00466       destruct();
00467       new (this) ImplicitConversionSequence(Other);
00468       return *this;
00469     }
00470     
00471     Kind getKind() const {
00472       assert(isInitialized() && "querying uninitialized conversion");
00473       return Kind(ConversionKind);
00474     }
00475     
00476     /// \brief Return a ranking of the implicit conversion sequence
00477     /// kind, where smaller ranks represent better conversion
00478     /// sequences.
00479     ///
00480     /// In particular, this routine gives user-defined conversion
00481     /// sequences and ambiguous conversion sequences the same rank,
00482     /// per C++ [over.best.ics]p10.
00483     unsigned getKindRank() const {
00484       switch (getKind()) {
00485       case StandardConversion: 
00486         return 0;
00487 
00488       case UserDefinedConversion:
00489       case AmbiguousConversion: 
00490         return 1;
00491 
00492       case EllipsisConversion:
00493         return 2;
00494 
00495       case BadConversion:
00496         return 3;
00497       }
00498 
00499       llvm_unreachable("Invalid ImplicitConversionSequence::Kind!");
00500     }
00501 
00502     bool isBad() const { return getKind() == BadConversion; }
00503     bool isStandard() const { return getKind() == StandardConversion; }
00504     bool isEllipsis() const { return getKind() == EllipsisConversion; }
00505     bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
00506     bool isUserDefined() const { return getKind() == UserDefinedConversion; }
00507     bool isFailure() const { return isBad() || isAmbiguous(); }
00508 
00509     /// Determines whether this conversion sequence has been
00510     /// initialized.  Most operations should never need to query
00511     /// uninitialized conversions and should assert as above.
00512     bool isInitialized() const { return ConversionKind != Uninitialized; }
00513 
00514     /// Sets this sequence as a bad conversion for an explicit argument.
00515     void setBad(BadConversionSequence::FailureKind Failure,
00516                 Expr *FromExpr, QualType ToType) {
00517       setKind(BadConversion);
00518       Bad.init(Failure, FromExpr, ToType);
00519     }
00520 
00521     /// Sets this sequence as a bad conversion for an implicit argument.
00522     void setBad(BadConversionSequence::FailureKind Failure,
00523                 QualType FromType, QualType ToType) {
00524       setKind(BadConversion);
00525       Bad.init(Failure, FromType, ToType);
00526     }
00527 
00528     void setStandard() { setKind(StandardConversion); }
00529     void setEllipsis() { setKind(EllipsisConversion); }
00530     void setUserDefined() { setKind(UserDefinedConversion); }
00531     void setAmbiguous() {
00532       if (ConversionKind == AmbiguousConversion) return;
00533       ConversionKind = AmbiguousConversion;
00534       Ambiguous.construct();
00535     }
00536 
00537     /// \brief Whether this sequence was created by the rules of
00538     /// list-initialization sequences.
00539     bool isListInitializationSequence() const {
00540       return ListInitializationSequence;
00541     }
00542 
00543     void setListInitializationSequence() {
00544       ListInitializationSequence = true;
00545     }
00546 
00547     /// \brief Whether the target is really a std::initializer_list, and the
00548     /// sequence only represents the worst element conversion.
00549     bool isStdInitializerListElement() const {
00550       return StdInitializerListElement;
00551     }
00552 
00553     void setStdInitializerListElement(bool V = true) {
00554       StdInitializerListElement = V;
00555     }
00556 
00557     // The result of a comparison between implicit conversion
00558     // sequences. Use Sema::CompareImplicitConversionSequences to
00559     // actually perform the comparison.
00560     enum CompareKind {
00561       Better = -1,
00562       Indistinguishable = 0,
00563       Worse = 1
00564     };
00565 
00566     void DiagnoseAmbiguousConversion(Sema &S,
00567                                      SourceLocation CaretLoc,
00568                                      const PartialDiagnostic &PDiag) const;
00569 
00570     void DebugPrint() const;
00571   };
00572 
00573   enum OverloadFailureKind {
00574     ovl_fail_too_many_arguments,
00575     ovl_fail_too_few_arguments,
00576     ovl_fail_bad_conversion,
00577     ovl_fail_bad_deduction,
00578 
00579     /// This conversion candidate was not considered because it
00580     /// duplicates the work of a trivial or derived-to-base
00581     /// conversion.
00582     ovl_fail_trivial_conversion,
00583 
00584     /// This conversion candidate is not viable because its result
00585     /// type is not implicitly convertible to the desired type.
00586     ovl_fail_bad_final_conversion,
00587     
00588     /// This conversion function template specialization candidate is not 
00589     /// viable because the final conversion was not an exact match.
00590     ovl_fail_final_conversion_not_exact,
00591 
00592     /// (CUDA) This candidate was not viable because the callee
00593     /// was not accessible from the caller's target (i.e. host->device,
00594     /// global->host, device->host).
00595     ovl_fail_bad_target
00596   };
00597 
00598   /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
00599   struct OverloadCandidate {
00600     /// Function - The actual function that this candidate
00601     /// represents. When NULL, this is a built-in candidate
00602     /// (C++ [over.oper]) or a surrogate for a conversion to a
00603     /// function pointer or reference (C++ [over.call.object]).
00604     FunctionDecl *Function;
00605 
00606     /// FoundDecl - The original declaration that was looked up /
00607     /// invented / otherwise found, together with its access.
00608     /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
00609     DeclAccessPair FoundDecl;
00610 
00611     // BuiltinTypes - Provides the return and parameter types of a
00612     // built-in overload candidate. Only valid when Function is NULL.
00613     struct {
00614       QualType ResultTy;
00615       QualType ParamTypes[3];
00616     } BuiltinTypes;
00617 
00618     /// Surrogate - The conversion function for which this candidate
00619     /// is a surrogate, but only if IsSurrogate is true.
00620     CXXConversionDecl *Surrogate;
00621 
00622     /// Conversions - The conversion sequences used to convert the
00623     /// function arguments to the function parameters, the pointer points to a
00624     /// fixed size array with NumConversions elements. The memory is owned by
00625     /// the OverloadCandidateSet.
00626     ImplicitConversionSequence *Conversions;
00627 
00628     /// The FixIt hints which can be used to fix the Bad candidate.
00629     ConversionFixItGenerator Fix;
00630 
00631     /// NumConversions - The number of elements in the Conversions array.
00632     unsigned NumConversions;
00633 
00634     /// Viable - True to indicate that this overload candidate is viable.
00635     bool Viable;
00636 
00637     /// IsSurrogate - True to indicate that this candidate is a
00638     /// surrogate for a conversion to a function pointer or reference
00639     /// (C++ [over.call.object]).
00640     bool IsSurrogate;
00641 
00642     /// IgnoreObjectArgument - True to indicate that the first
00643     /// argument's conversion, which for this function represents the
00644     /// implicit object argument, should be ignored. This will be true
00645     /// when the candidate is a static member function (where the
00646     /// implicit object argument is just a placeholder) or a
00647     /// non-static member function when the call doesn't have an
00648     /// object argument.
00649     bool IgnoreObjectArgument;
00650 
00651     /// FailureKind - The reason why this candidate is not viable.
00652     /// Actually an OverloadFailureKind.
00653     unsigned char FailureKind;
00654 
00655     /// \brief The number of call arguments that were explicitly provided,
00656     /// to be used while performing partial ordering of function templates.
00657     unsigned ExplicitCallArguments;
00658     
00659     /// A structure used to record information about a failed
00660     /// template argument deduction.
00661     struct DeductionFailureInfo {
00662       /// A Sema::TemplateDeductionResult.
00663       unsigned Result : 8;
00664 
00665       /// \brief Indicates whether a diagnostic is stored in Diagnostic.
00666       unsigned HasDiagnostic : 1;
00667 
00668       /// \brief Opaque pointer containing additional data about
00669       /// this deduction failure.
00670       void *Data;
00671 
00672       /// \brief A diagnostic indicating why deduction failed.
00673       union {
00674         void *Align;
00675         char Diagnostic[sizeof(PartialDiagnosticAt)];
00676       };
00677 
00678       /// \brief Retrieve the diagnostic which caused this deduction failure,
00679       /// if any.
00680       PartialDiagnosticAt *getSFINAEDiagnostic();
00681       
00682       /// \brief Retrieve the template parameter this deduction failure
00683       /// refers to, if any.
00684       TemplateParameter getTemplateParameter();
00685       
00686       /// \brief Retrieve the template argument list associated with this
00687       /// deduction failure, if any.
00688       TemplateArgumentList *getTemplateArgumentList();
00689       
00690       /// \brief Return the first template argument this deduction failure
00691       /// refers to, if any.
00692       const TemplateArgument *getFirstArg();
00693 
00694       /// \brief Return the second template argument this deduction failure
00695       /// refers to, if any.
00696       const TemplateArgument *getSecondArg();
00697       
00698       /// \brief Free any memory associated with this deduction failure.
00699       void Destroy();
00700     };
00701 
00702     union {
00703       DeductionFailureInfo DeductionFailure;
00704       
00705       /// FinalConversion - For a conversion function (where Function is
00706       /// a CXXConversionDecl), the standard conversion that occurs
00707       /// after the call to the overload candidate to convert the result
00708       /// of calling the conversion function to the required type.
00709       StandardConversionSequence FinalConversion;
00710     };
00711 
00712     /// hasAmbiguousConversion - Returns whether this overload
00713     /// candidate requires an ambiguous conversion or not.
00714     bool hasAmbiguousConversion() const {
00715       for (unsigned i = 0, e = NumConversions; i != e; ++i) {
00716         if (!Conversions[i].isInitialized()) return false;
00717         if (Conversions[i].isAmbiguous()) return true;
00718       }
00719       return false;
00720     }
00721 
00722     bool TryToFixBadConversion(unsigned Idx, Sema &S) {
00723       bool CanFix = Fix.tryToFixConversion(
00724                       Conversions[Idx].Bad.FromExpr,
00725                       Conversions[Idx].Bad.getFromType(),
00726                       Conversions[Idx].Bad.getToType(), S);
00727 
00728       // If at least one conversion fails, the candidate cannot be fixed.
00729       if (!CanFix)
00730         Fix.clear();
00731 
00732       return CanFix;
00733     }
00734   };
00735 
00736   /// OverloadCandidateSet - A set of overload candidates, used in C++
00737   /// overload resolution (C++ 13.3).
00738   class OverloadCandidateSet {
00739     SmallVector<OverloadCandidate, 16> Candidates;
00740     llvm::SmallPtrSet<Decl *, 16> Functions;
00741 
00742     // Allocator for OverloadCandidate::Conversions. We store the first few
00743     // elements inline to avoid allocation for small sets.
00744     llvm::BumpPtrAllocator ConversionSequenceAllocator;
00745 
00746     SourceLocation Loc;
00747 
00748     unsigned NumInlineSequences;
00749     char InlineSpace[16 * sizeof(ImplicitConversionSequence)];
00750 
00751     OverloadCandidateSet(const OverloadCandidateSet &);
00752     OverloadCandidateSet &operator=(const OverloadCandidateSet &);
00753     
00754   public:
00755     OverloadCandidateSet(SourceLocation Loc) : Loc(Loc), NumInlineSequences(0){}
00756     ~OverloadCandidateSet() {
00757       for (iterator i = begin(), e = end(); i != e; ++i) {
00758         for (unsigned ii = 0, ie = i->NumConversions; ii != ie; ++ii)
00759           i->Conversions[ii].~ImplicitConversionSequence();
00760         if (i->FailureKind == ovl_fail_bad_deduction)
00761           i->DeductionFailure.Destroy();
00762       }
00763     }
00764 
00765     SourceLocation getLocation() const { return Loc; }
00766 
00767     /// \brief Determine when this overload candidate will be new to the
00768     /// overload set.
00769     bool isNewCandidate(Decl *F) { 
00770       return Functions.insert(F->getCanonicalDecl()); 
00771     }
00772 
00773     /// \brief Clear out all of the candidates.
00774     void clear();
00775 
00776     typedef SmallVector<OverloadCandidate, 16>::iterator iterator;
00777     iterator begin() { return Candidates.begin(); }
00778     iterator end() { return Candidates.end(); }
00779 
00780     size_t size() const { return Candidates.size(); }
00781     bool empty() const { return Candidates.empty(); }
00782 
00783     /// \brief Add a new candidate with NumConversions conversion sequence slots
00784     /// to the overload set.
00785     OverloadCandidate &addCandidate(unsigned NumConversions = 0) {
00786       Candidates.push_back(OverloadCandidate());
00787       OverloadCandidate &C = Candidates.back();
00788 
00789       // Assign space from the inline array if there are enough free slots
00790       // available.
00791       if (NumConversions + NumInlineSequences <= 16) {
00792         ImplicitConversionSequence *I =
00793           (ImplicitConversionSequence*)InlineSpace;
00794         C.Conversions = &I[NumInlineSequences];
00795         NumInlineSequences += NumConversions;
00796       } else {
00797         // Otherwise get memory from the allocator.
00798         C.Conversions = ConversionSequenceAllocator
00799                           .Allocate<ImplicitConversionSequence>(NumConversions);
00800       }
00801 
00802       // Construct the new objects.
00803       for (unsigned i = 0; i != NumConversions; ++i)
00804         new (&C.Conversions[i]) ImplicitConversionSequence();
00805 
00806       C.NumConversions = NumConversions;
00807       return C;
00808     }
00809 
00810     /// Find the best viable function on this overload set, if it exists.
00811     OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
00812                                          OverloadCandidateSet::iterator& Best,
00813                                          bool UserDefinedConversion = false);
00814 
00815     void NoteCandidates(Sema &S,
00816                         OverloadCandidateDisplayKind OCD,
00817                         llvm::ArrayRef<Expr *> Args,
00818                         const char *Opc = 0,
00819                         SourceLocation Loc = SourceLocation());
00820   };
00821 
00822   bool isBetterOverloadCandidate(Sema &S,
00823                                  const OverloadCandidate& Cand1,
00824                                  const OverloadCandidate& Cand2,
00825                                  SourceLocation Loc,
00826                                  bool UserDefinedConversion = false);
00827 } // end namespace clang
00828 
00829 #endif // LLVM_CLANG_SEMA_OVERLOAD_H