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