Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:00

0001 //===- Overload.h - C++ Overloading -----------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the data structures and types used in C++
0010 // overload resolution.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_SEMA_OVERLOAD_H
0015 #define LLVM_CLANG_SEMA_OVERLOAD_H
0016 
0017 #include "clang/AST/Decl.h"
0018 #include "clang/AST/DeclAccessPair.h"
0019 #include "clang/AST/DeclBase.h"
0020 #include "clang/AST/DeclCXX.h"
0021 #include "clang/AST/DeclTemplate.h"
0022 #include "clang/AST/Expr.h"
0023 #include "clang/AST/Type.h"
0024 #include "clang/Basic/LLVM.h"
0025 #include "clang/Basic/SourceLocation.h"
0026 #include "clang/Sema/SemaFixItUtils.h"
0027 #include "clang/Sema/TemplateDeduction.h"
0028 #include "llvm/ADT/ArrayRef.h"
0029 #include "llvm/ADT/STLExtras.h"
0030 #include "llvm/ADT/SmallPtrSet.h"
0031 #include "llvm/ADT/SmallVector.h"
0032 #include "llvm/ADT/StringRef.h"
0033 #include "llvm/Support/AlignOf.h"
0034 #include "llvm/Support/Allocator.h"
0035 #include "llvm/Support/Casting.h"
0036 #include "llvm/Support/ErrorHandling.h"
0037 #include <cassert>
0038 #include <cstddef>
0039 #include <cstdint>
0040 #include <utility>
0041 
0042 namespace clang {
0043 
0044 class APValue;
0045 class ASTContext;
0046 class Sema;
0047 
0048   /// OverloadingResult - Capture the result of performing overload
0049   /// resolution.
0050   enum OverloadingResult {
0051     /// Overload resolution succeeded.
0052     OR_Success,
0053 
0054     /// No viable function found.
0055     OR_No_Viable_Function,
0056 
0057     /// Ambiguous candidates found.
0058     OR_Ambiguous,
0059 
0060     /// Succeeded, but refers to a deleted function.
0061     OR_Deleted
0062   };
0063 
0064   enum OverloadCandidateDisplayKind {
0065     /// Requests that all candidates be shown.  Viable candidates will
0066     /// be printed first.
0067     OCD_AllCandidates,
0068 
0069     /// Requests that only viable candidates be shown.
0070     OCD_ViableCandidates,
0071 
0072     /// Requests that only tied-for-best candidates be shown.
0073     OCD_AmbiguousCandidates
0074   };
0075 
0076   /// The parameter ordering that will be used for the candidate. This is
0077   /// used to represent C++20 binary operator rewrites that reverse the order
0078   /// of the arguments. If the parameter ordering is Reversed, the Args list is
0079   /// reversed (but obviously the ParamDecls for the function are not).
0080   ///
0081   /// After forming an OverloadCandidate with reversed parameters, the list
0082   /// of conversions will (as always) be indexed by argument, so will be
0083   /// in reverse parameter order.
0084   enum class OverloadCandidateParamOrder : char { Normal, Reversed };
0085 
0086   /// The kinds of rewrite we perform on overload candidates. Note that the
0087   /// values here are chosen to serve as both bitflags and as a rank (lower
0088   /// values are preferred by overload resolution).
0089   enum OverloadCandidateRewriteKind : unsigned {
0090     /// Candidate is not a rewritten candidate.
0091     CRK_None = 0x0,
0092 
0093     /// Candidate is a rewritten candidate with a different operator name.
0094     CRK_DifferentOperator = 0x1,
0095 
0096     /// Candidate is a rewritten candidate with a reversed order of parameters.
0097     CRK_Reversed = 0x2,
0098   };
0099 
0100   /// ImplicitConversionKind - The kind of implicit conversion used to
0101   /// convert an argument to a parameter's type. The enumerator values
0102   /// match with the table titled 'Conversions' in [over.ics.scs] and are listed
0103   /// such that better conversion kinds have smaller values.
0104   enum ImplicitConversionKind {
0105     /// Identity conversion (no conversion)
0106     ICK_Identity = 0,
0107 
0108     /// Lvalue-to-rvalue conversion (C++ [conv.lval])
0109     ICK_Lvalue_To_Rvalue,
0110 
0111     /// Array-to-pointer conversion (C++ [conv.array])
0112     ICK_Array_To_Pointer,
0113 
0114     /// Function-to-pointer (C++ [conv.array])
0115     ICK_Function_To_Pointer,
0116 
0117     /// Function pointer conversion (C++17 [conv.fctptr])
0118     ICK_Function_Conversion,
0119 
0120     /// Qualification conversions (C++ [conv.qual])
0121     ICK_Qualification,
0122 
0123     /// Integral promotions (C++ [conv.prom])
0124     ICK_Integral_Promotion,
0125 
0126     /// Floating point promotions (C++ [conv.fpprom])
0127     ICK_Floating_Promotion,
0128 
0129     /// Complex promotions (Clang extension)
0130     ICK_Complex_Promotion,
0131 
0132     /// Integral conversions (C++ [conv.integral])
0133     ICK_Integral_Conversion,
0134 
0135     /// Floating point conversions (C++ [conv.double]
0136     ICK_Floating_Conversion,
0137 
0138     /// Complex conversions (C99 6.3.1.6)
0139     ICK_Complex_Conversion,
0140 
0141     /// Floating-integral conversions (C++ [conv.fpint])
0142     ICK_Floating_Integral,
0143 
0144     /// Pointer conversions (C++ [conv.ptr])
0145     ICK_Pointer_Conversion,
0146 
0147     /// Pointer-to-member conversions (C++ [conv.mem])
0148     ICK_Pointer_Member,
0149 
0150     /// Boolean conversions (C++ [conv.bool])
0151     ICK_Boolean_Conversion,
0152 
0153     /// Conversions between compatible types in C99
0154     ICK_Compatible_Conversion,
0155 
0156     /// Derived-to-base (C++ [over.best.ics])
0157     ICK_Derived_To_Base,
0158 
0159     /// Vector conversions
0160     ICK_Vector_Conversion,
0161 
0162     /// Arm SVE Vector conversions
0163     ICK_SVE_Vector_Conversion,
0164 
0165     /// RISC-V RVV Vector conversions
0166     ICK_RVV_Vector_Conversion,
0167 
0168     /// A vector splat from an arithmetic type
0169     ICK_Vector_Splat,
0170 
0171     /// Complex-real conversions (C99 6.3.1.7)
0172     ICK_Complex_Real,
0173 
0174     /// Block Pointer conversions
0175     ICK_Block_Pointer_Conversion,
0176 
0177     /// Transparent Union Conversions
0178     ICK_TransparentUnionConversion,
0179 
0180     /// Objective-C ARC writeback conversion
0181     ICK_Writeback_Conversion,
0182 
0183     /// Zero constant to event (OpenCL1.2 6.12.10)
0184     ICK_Zero_Event_Conversion,
0185 
0186     /// Zero constant to queue
0187     ICK_Zero_Queue_Conversion,
0188 
0189     /// Conversions allowed in C, but not C++
0190     ICK_C_Only_Conversion,
0191 
0192     /// C-only conversion between pointers with incompatible types
0193     ICK_Incompatible_Pointer_Conversion,
0194 
0195     /// Fixed point type conversions according to N1169.
0196     ICK_Fixed_Point_Conversion,
0197 
0198     /// HLSL vector truncation.
0199     ICK_HLSL_Vector_Truncation,
0200 
0201     /// HLSL non-decaying array rvalue cast.
0202     ICK_HLSL_Array_RValue,
0203 
0204     // HLSL vector splat from scalar or boolean type.
0205     ICK_HLSL_Vector_Splat,
0206 
0207     /// The number of conversion kinds
0208     ICK_Num_Conversion_Kinds,
0209   };
0210 
0211   /// ImplicitConversionRank - The rank of an implicit conversion
0212   /// kind. The enumerator values match with Table 9 of (C++
0213   /// 13.3.3.1.1) and are listed such that better conversion ranks
0214   /// have smaller values.
0215   enum ImplicitConversionRank {
0216     /// Exact Match
0217     ICR_Exact_Match = 0,
0218 
0219     /// HLSL Scalar Widening
0220     ICR_HLSL_Scalar_Widening,
0221 
0222     /// Promotion
0223     ICR_Promotion,
0224 
0225     /// HLSL Scalar Widening with promotion
0226     ICR_HLSL_Scalar_Widening_Promotion,
0227 
0228     /// Conversion
0229     ICR_Conversion,
0230 
0231     /// OpenCL Scalar Widening
0232     ICR_OCL_Scalar_Widening,
0233 
0234     /// HLSL Scalar Widening with conversion
0235     ICR_HLSL_Scalar_Widening_Conversion,
0236 
0237     /// Complex <-> Real conversion
0238     ICR_Complex_Real_Conversion,
0239 
0240     /// ObjC ARC writeback conversion
0241     ICR_Writeback_Conversion,
0242 
0243     /// Conversion only allowed in the C standard (e.g. void* to char*).
0244     ICR_C_Conversion,
0245 
0246     /// Conversion not allowed by the C standard, but that we accept as an
0247     /// extension anyway.
0248     ICR_C_Conversion_Extension,
0249 
0250     /// HLSL Matching Dimension Reduction
0251     ICR_HLSL_Dimension_Reduction,
0252 
0253     /// HLSL Dimension reduction with promotion
0254     ICR_HLSL_Dimension_Reduction_Promotion,
0255 
0256     /// HLSL Dimension reduction with conversion
0257     ICR_HLSL_Dimension_Reduction_Conversion,
0258   };
0259 
0260   ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
0261 
0262   ImplicitConversionRank
0263   GetDimensionConversionRank(ImplicitConversionRank Base,
0264                              ImplicitConversionKind Dimension);
0265 
0266   /// NarrowingKind - The kind of narrowing conversion being performed by a
0267   /// standard conversion sequence according to C++11 [dcl.init.list]p7.
0268   enum NarrowingKind {
0269     /// Not a narrowing conversion.
0270     NK_Not_Narrowing,
0271 
0272     /// A narrowing conversion by virtue of the source and destination types.
0273     NK_Type_Narrowing,
0274 
0275     /// A narrowing conversion, because a constant expression got narrowed.
0276     NK_Constant_Narrowing,
0277 
0278     /// A narrowing conversion, because a non-constant-expression variable might
0279     /// have got narrowed.
0280     NK_Variable_Narrowing,
0281 
0282     /// Cannot tell whether this is a narrowing conversion because the
0283     /// expression is value-dependent.
0284     NK_Dependent_Narrowing,
0285   };
0286 
0287   /// StandardConversionSequence - represents a standard conversion
0288   /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
0289   /// contains between zero and three conversions. If a particular
0290   /// conversion is not needed, it will be set to the identity conversion
0291   /// (ICK_Identity).
0292   class StandardConversionSequence {
0293   public:
0294     /// First -- The first conversion can be an lvalue-to-rvalue
0295     /// conversion, array-to-pointer conversion, or
0296     /// function-to-pointer conversion.
0297     ImplicitConversionKind First : 8;
0298 
0299     /// Second - The second conversion can be an integral promotion,
0300     /// floating point promotion, integral conversion, floating point
0301     /// conversion, floating-integral conversion, pointer conversion,
0302     /// pointer-to-member conversion, or boolean conversion.
0303     ImplicitConversionKind Second : 8;
0304 
0305     /// Dimension - Between the second and third conversion a vector or matrix
0306     /// dimension conversion may occur. If this is not ICK_Identity this
0307     /// conversion truncates the vector or matrix, or extends a scalar.
0308     ImplicitConversionKind Dimension : 8;
0309 
0310     /// Third - The third conversion can be a qualification conversion
0311     /// or a function conversion.
0312     ImplicitConversionKind Third : 8;
0313 
0314     /// Whether this is the deprecated conversion of a
0315     /// string literal to a pointer to non-const character data
0316     /// (C++ 4.2p2).
0317     LLVM_PREFERRED_TYPE(bool)
0318     unsigned DeprecatedStringLiteralToCharPtr : 1;
0319 
0320     /// Whether the qualification conversion involves a change in the
0321     /// Objective-C lifetime (for automatic reference counting).
0322     LLVM_PREFERRED_TYPE(bool)
0323     unsigned QualificationIncludesObjCLifetime : 1;
0324 
0325     /// IncompatibleObjC - Whether this is an Objective-C conversion
0326     /// that we should warn about (if we actually use it).
0327     LLVM_PREFERRED_TYPE(bool)
0328     unsigned IncompatibleObjC : 1;
0329 
0330     /// ReferenceBinding - True when this is a reference binding
0331     /// (C++ [over.ics.ref]).
0332     LLVM_PREFERRED_TYPE(bool)
0333     unsigned ReferenceBinding : 1;
0334 
0335     /// DirectBinding - True when this is a reference binding that is a
0336     /// direct binding (C++ [dcl.init.ref]).
0337     LLVM_PREFERRED_TYPE(bool)
0338     unsigned DirectBinding : 1;
0339 
0340     /// Whether this is an lvalue reference binding (otherwise, it's
0341     /// an rvalue reference binding).
0342     LLVM_PREFERRED_TYPE(bool)
0343     unsigned IsLvalueReference : 1;
0344 
0345     /// Whether we're binding to a function lvalue.
0346     LLVM_PREFERRED_TYPE(bool)
0347     unsigned BindsToFunctionLvalue : 1;
0348 
0349     /// Whether we're binding to an rvalue.
0350     LLVM_PREFERRED_TYPE(bool)
0351     unsigned BindsToRvalue : 1;
0352 
0353     /// Whether this binds an implicit object argument to a
0354     /// non-static member function without a ref-qualifier.
0355     LLVM_PREFERRED_TYPE(bool)
0356     unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
0357 
0358     /// Whether this binds a reference to an object with a different
0359     /// Objective-C lifetime qualifier.
0360     LLVM_PREFERRED_TYPE(bool)
0361     unsigned ObjCLifetimeConversionBinding : 1;
0362 
0363     /// FromType - The type that this conversion is converting
0364     /// from. This is an opaque pointer that can be translated into a
0365     /// QualType.
0366     void *FromTypePtr;
0367 
0368     /// ToType - The types that this conversion is converting to in
0369     /// each step. This is an opaque pointer that can be translated
0370     /// into a QualType.
0371     void *ToTypePtrs[3];
0372 
0373     /// CopyConstructor - The copy constructor that is used to perform
0374     /// this conversion, when the conversion is actually just the
0375     /// initialization of an object via copy constructor. Such
0376     /// conversions are either identity conversions or derived-to-base
0377     /// conversions.
0378     CXXConstructorDecl *CopyConstructor;
0379     DeclAccessPair FoundCopyConstructor;
0380 
0381     void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
0382 
0383     void setToType(unsigned Idx, QualType T) {
0384       assert(Idx < 3 && "To type index is out of range");
0385       ToTypePtrs[Idx] = T.getAsOpaquePtr();
0386     }
0387 
0388     void setAllToTypes(QualType T) {
0389       ToTypePtrs[0] = T.getAsOpaquePtr();
0390       ToTypePtrs[1] = ToTypePtrs[0];
0391       ToTypePtrs[2] = ToTypePtrs[0];
0392     }
0393 
0394     QualType getFromType() const {
0395       return QualType::getFromOpaquePtr(FromTypePtr);
0396     }
0397 
0398     QualType getToType(unsigned Idx) const {
0399       assert(Idx < 3 && "To type index is out of range");
0400       return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
0401     }
0402 
0403     void setAsIdentityConversion();
0404 
0405     bool isIdentityConversion() const {
0406       return Second == ICK_Identity && Dimension == ICK_Identity &&
0407              Third == ICK_Identity;
0408     }
0409 
0410     ImplicitConversionRank getRank() const;
0411     NarrowingKind
0412     getNarrowingKind(ASTContext &Context, const Expr *Converted,
0413                      APValue &ConstantValue, QualType &ConstantType,
0414                      bool IgnoreFloatToIntegralConversion = false) const;
0415     bool isPointerConversionToBool() const;
0416     bool isPointerConversionToVoidPointer(ASTContext& Context) const;
0417     void dump() const;
0418   };
0419 
0420   /// UserDefinedConversionSequence - Represents a user-defined
0421   /// conversion sequence (C++ 13.3.3.1.2).
0422   struct UserDefinedConversionSequence {
0423     /// Represents the standard conversion that occurs before
0424     /// the actual user-defined conversion.
0425     ///
0426     /// C++11 13.3.3.1.2p1:
0427     ///   If the user-defined conversion is specified by a constructor
0428     ///   (12.3.1), the initial standard conversion sequence converts
0429     ///   the source type to the type required by the argument of the
0430     ///   constructor. If the user-defined conversion is specified by
0431     ///   a conversion function (12.3.2), the initial standard
0432     ///   conversion sequence converts the source type to the implicit
0433     ///   object parameter of the conversion function.
0434     StandardConversionSequence Before;
0435 
0436     /// EllipsisConversion - When this is true, it means user-defined
0437     /// conversion sequence starts with a ... (ellipsis) conversion, instead of
0438     /// a standard conversion. In this case, 'Before' field must be ignored.
0439     // FIXME. I much rather put this as the first field. But there seems to be
0440     // a gcc code gen. bug which causes a crash in a test. Putting it here seems
0441     // to work around the crash.
0442     bool EllipsisConversion : 1;
0443 
0444     /// HadMultipleCandidates - When this is true, it means that the
0445     /// conversion function was resolved from an overloaded set having
0446     /// size greater than 1.
0447     bool HadMultipleCandidates : 1;
0448 
0449     /// After - Represents the standard conversion that occurs after
0450     /// the actual user-defined conversion.
0451     StandardConversionSequence After;
0452 
0453     /// ConversionFunction - The function that will perform the
0454     /// user-defined conversion. Null if the conversion is an
0455     /// aggregate initialization from an initializer list.
0456     FunctionDecl* ConversionFunction;
0457 
0458     /// The declaration that we found via name lookup, which might be
0459     /// the same as \c ConversionFunction or it might be a using declaration
0460     /// that refers to \c ConversionFunction.
0461     DeclAccessPair FoundConversionFunction;
0462 
0463     void dump() const;
0464   };
0465 
0466   /// Represents an ambiguous user-defined conversion sequence.
0467   struct AmbiguousConversionSequence {
0468     using ConversionSet =
0469         SmallVector<std::pair<NamedDecl *, FunctionDecl *>, 4>;
0470 
0471     void *FromTypePtr;
0472     void *ToTypePtr;
0473     char Buffer[sizeof(ConversionSet)];
0474 
0475     QualType getFromType() const {
0476       return QualType::getFromOpaquePtr(FromTypePtr);
0477     }
0478 
0479     QualType getToType() const {
0480       return QualType::getFromOpaquePtr(ToTypePtr);
0481     }
0482 
0483     void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
0484     void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
0485 
0486     ConversionSet &conversions() {
0487       return *reinterpret_cast<ConversionSet*>(Buffer);
0488     }
0489 
0490     const ConversionSet &conversions() const {
0491       return *reinterpret_cast<const ConversionSet*>(Buffer);
0492     }
0493 
0494     void addConversion(NamedDecl *Found, FunctionDecl *D) {
0495       conversions().push_back(std::make_pair(Found, D));
0496     }
0497 
0498     using iterator = ConversionSet::iterator;
0499 
0500     iterator begin() { return conversions().begin(); }
0501     iterator end() { return conversions().end(); }
0502 
0503     using const_iterator = ConversionSet::const_iterator;
0504 
0505     const_iterator begin() const { return conversions().begin(); }
0506     const_iterator end() const { return conversions().end(); }
0507 
0508     void construct();
0509     void destruct();
0510     void copyFrom(const AmbiguousConversionSequence &);
0511   };
0512 
0513   /// BadConversionSequence - Records information about an invalid
0514   /// conversion sequence.
0515   struct BadConversionSequence {
0516     enum FailureKind {
0517       no_conversion,
0518       unrelated_class,
0519       bad_qualifiers,
0520       lvalue_ref_to_rvalue,
0521       rvalue_ref_to_lvalue,
0522       too_few_initializers,
0523       too_many_initializers,
0524     };
0525 
0526     // This can be null, e.g. for implicit object arguments.
0527     Expr *FromExpr;
0528 
0529     FailureKind Kind;
0530 
0531   private:
0532     // The type we're converting from (an opaque QualType).
0533     void *FromTy;
0534 
0535     // The type we're converting to (an opaque QualType).
0536     void *ToTy;
0537 
0538   public:
0539     void init(FailureKind K, Expr *From, QualType To) {
0540       init(K, From->getType(), To);
0541       FromExpr = From;
0542     }
0543 
0544     void init(FailureKind K, QualType From, QualType To) {
0545       Kind = K;
0546       FromExpr = nullptr;
0547       setFromType(From);
0548       setToType(To);
0549     }
0550 
0551     QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
0552     QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
0553 
0554     void setFromExpr(Expr *E) {
0555       FromExpr = E;
0556       setFromType(E->getType());
0557     }
0558 
0559     void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
0560     void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
0561   };
0562 
0563   /// ImplicitConversionSequence - Represents an implicit conversion
0564   /// sequence, which may be a standard conversion sequence
0565   /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
0566   /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
0567   class ImplicitConversionSequence {
0568   public:
0569     /// Kind - The kind of implicit conversion sequence. BadConversion
0570     /// specifies that there is no conversion from the source type to
0571     /// the target type.  AmbiguousConversion represents the unique
0572     /// ambiguous conversion (C++0x [over.best.ics]p10).
0573     /// StaticObjectArgumentConversion represents the conversion rules for
0574     /// the synthesized first argument of calls to static member functions
0575     /// ([over.best.ics.general]p8).
0576     enum Kind {
0577       StandardConversion = 0,
0578       StaticObjectArgumentConversion,
0579       UserDefinedConversion,
0580       AmbiguousConversion,
0581       EllipsisConversion,
0582       BadConversion
0583     };
0584 
0585   private:
0586     enum {
0587       Uninitialized = BadConversion + 1
0588     };
0589 
0590     /// ConversionKind - The kind of implicit conversion sequence.
0591     LLVM_PREFERRED_TYPE(Kind)
0592     unsigned ConversionKind : 31;
0593 
0594     // Whether the initializer list was of an incomplete array.
0595     LLVM_PREFERRED_TYPE(bool)
0596     unsigned InitializerListOfIncompleteArray : 1;
0597 
0598     /// When initializing an array or std::initializer_list from an
0599     /// initializer-list, this is the array or std::initializer_list type being
0600     /// initialized. The remainder of the conversion sequence, including ToType,
0601     /// describe the worst conversion of an initializer to an element of the
0602     /// array or std::initializer_list. (Note, 'worst' is not well defined.)
0603     QualType InitializerListContainerType;
0604 
0605     void setKind(Kind K) {
0606       destruct();
0607       ConversionKind = K;
0608     }
0609 
0610     void destruct() {
0611       if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
0612     }
0613 
0614   public:
0615     union {
0616       /// When ConversionKind == StandardConversion, provides the
0617       /// details of the standard conversion sequence.
0618       StandardConversionSequence Standard;
0619 
0620       /// When ConversionKind == UserDefinedConversion, provides the
0621       /// details of the user-defined conversion sequence.
0622       UserDefinedConversionSequence UserDefined;
0623 
0624       /// When ConversionKind == AmbiguousConversion, provides the
0625       /// details of the ambiguous conversion.
0626       AmbiguousConversionSequence Ambiguous;
0627 
0628       /// When ConversionKind == BadConversion, provides the details
0629       /// of the bad conversion.
0630       BadConversionSequence Bad;
0631     };
0632 
0633     ImplicitConversionSequence()
0634         : ConversionKind(Uninitialized),
0635           InitializerListOfIncompleteArray(false) {
0636       Standard.setAsIdentityConversion();
0637     }
0638 
0639     ImplicitConversionSequence(const ImplicitConversionSequence &Other)
0640         : ConversionKind(Other.ConversionKind),
0641           InitializerListOfIncompleteArray(
0642               Other.InitializerListOfIncompleteArray),
0643           InitializerListContainerType(Other.InitializerListContainerType) {
0644       switch (ConversionKind) {
0645       case Uninitialized: break;
0646       case StandardConversion: Standard = Other.Standard; break;
0647       case StaticObjectArgumentConversion:
0648         break;
0649       case UserDefinedConversion: UserDefined = Other.UserDefined; break;
0650       case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
0651       case EllipsisConversion: break;
0652       case BadConversion: Bad = Other.Bad; break;
0653       }
0654     }
0655 
0656     ImplicitConversionSequence &
0657     operator=(const ImplicitConversionSequence &Other) {
0658       destruct();
0659       new (this) ImplicitConversionSequence(Other);
0660       return *this;
0661     }
0662 
0663     ~ImplicitConversionSequence() {
0664       destruct();
0665     }
0666 
0667     Kind getKind() const {
0668       assert(isInitialized() && "querying uninitialized conversion");
0669       return Kind(ConversionKind);
0670     }
0671 
0672     /// Return a ranking of the implicit conversion sequence
0673     /// kind, where smaller ranks represent better conversion
0674     /// sequences.
0675     ///
0676     /// In particular, this routine gives user-defined conversion
0677     /// sequences and ambiguous conversion sequences the same rank,
0678     /// per C++ [over.best.ics]p10.
0679     unsigned getKindRank() const {
0680       switch (getKind()) {
0681       case StandardConversion:
0682       case StaticObjectArgumentConversion:
0683         return 0;
0684 
0685       case UserDefinedConversion:
0686       case AmbiguousConversion:
0687         return 1;
0688 
0689       case EllipsisConversion:
0690         return 2;
0691 
0692       case BadConversion:
0693         return 3;
0694       }
0695 
0696       llvm_unreachable("Invalid ImplicitConversionSequence::Kind!");
0697     }
0698 
0699     bool isBad() const { return getKind() == BadConversion; }
0700     bool isStandard() const { return getKind() == StandardConversion; }
0701     bool isStaticObjectArgument() const {
0702       return getKind() == StaticObjectArgumentConversion;
0703     }
0704     bool isEllipsis() const { return getKind() == EllipsisConversion; }
0705     bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
0706     bool isUserDefined() const { return getKind() == UserDefinedConversion; }
0707     bool isFailure() const { return isBad() || isAmbiguous(); }
0708 
0709     /// Determines whether this conversion sequence has been
0710     /// initialized.  Most operations should never need to query
0711     /// uninitialized conversions and should assert as above.
0712     bool isInitialized() const { return ConversionKind != Uninitialized; }
0713 
0714     /// Sets this sequence as a bad conversion for an explicit argument.
0715     void setBad(BadConversionSequence::FailureKind Failure,
0716                 Expr *FromExpr, QualType ToType) {
0717       setKind(BadConversion);
0718       Bad.init(Failure, FromExpr, ToType);
0719     }
0720 
0721     /// Sets this sequence as a bad conversion for an implicit argument.
0722     void setBad(BadConversionSequence::FailureKind Failure,
0723                 QualType FromType, QualType ToType) {
0724       setKind(BadConversion);
0725       Bad.init(Failure, FromType, ToType);
0726     }
0727 
0728     void setStandard() { setKind(StandardConversion); }
0729     void setStaticObjectArgument() { setKind(StaticObjectArgumentConversion); }
0730     void setEllipsis() { setKind(EllipsisConversion); }
0731     void setUserDefined() { setKind(UserDefinedConversion); }
0732 
0733     void setAmbiguous() {
0734       if (ConversionKind == AmbiguousConversion) return;
0735       ConversionKind = AmbiguousConversion;
0736       Ambiguous.construct();
0737     }
0738 
0739     void setAsIdentityConversion(QualType T) {
0740       setStandard();
0741       Standard.setAsIdentityConversion();
0742       Standard.setFromType(T);
0743       Standard.setAllToTypes(T);
0744     }
0745 
0746     // True iff this is a conversion sequence from an initializer list to an
0747     // array or std::initializer.
0748     bool hasInitializerListContainerType() const {
0749       return !InitializerListContainerType.isNull();
0750     }
0751     void setInitializerListContainerType(QualType T, bool IA) {
0752       InitializerListContainerType = T;
0753       InitializerListOfIncompleteArray = IA;
0754     }
0755     bool isInitializerListOfIncompleteArray() const {
0756       return InitializerListOfIncompleteArray;
0757     }
0758     QualType getInitializerListContainerType() const {
0759       assert(hasInitializerListContainerType() &&
0760              "not initializer list container");
0761       return InitializerListContainerType;
0762     }
0763 
0764     /// Form an "implicit" conversion sequence from nullptr_t to bool, for a
0765     /// direct-initialization of a bool object from nullptr_t.
0766     static ImplicitConversionSequence getNullptrToBool(QualType SourceType,
0767                                                        QualType DestType,
0768                                                        bool NeedLValToRVal) {
0769       ImplicitConversionSequence ICS;
0770       ICS.setStandard();
0771       ICS.Standard.setAsIdentityConversion();
0772       ICS.Standard.setFromType(SourceType);
0773       if (NeedLValToRVal)
0774         ICS.Standard.First = ICK_Lvalue_To_Rvalue;
0775       ICS.Standard.setToType(0, SourceType);
0776       ICS.Standard.Second = ICK_Boolean_Conversion;
0777       ICS.Standard.setToType(1, DestType);
0778       ICS.Standard.setToType(2, DestType);
0779       return ICS;
0780     }
0781 
0782     // The result of a comparison between implicit conversion
0783     // sequences. Use Sema::CompareImplicitConversionSequences to
0784     // actually perform the comparison.
0785     enum CompareKind {
0786       Better = -1,
0787       Indistinguishable = 0,
0788       Worse = 1
0789     };
0790 
0791     void DiagnoseAmbiguousConversion(Sema &S,
0792                                      SourceLocation CaretLoc,
0793                                      const PartialDiagnostic &PDiag) const;
0794 
0795     void dump() const;
0796   };
0797 
0798   enum OverloadFailureKind {
0799     ovl_fail_too_many_arguments,
0800     ovl_fail_too_few_arguments,
0801     ovl_fail_bad_conversion,
0802     ovl_fail_bad_deduction,
0803 
0804     /// This conversion candidate was not considered because it
0805     /// duplicates the work of a trivial or derived-to-base
0806     /// conversion.
0807     ovl_fail_trivial_conversion,
0808 
0809     /// This conversion candidate was not considered because it is
0810     /// an illegal instantiation of a constructor temploid: it is
0811     /// callable with one argument, we only have one argument, and
0812     /// its first parameter type is exactly the type of the class.
0813     ///
0814     /// Defining such a constructor directly is illegal, and
0815     /// template-argument deduction is supposed to ignore such
0816     /// instantiations, but we can still get one with the right
0817     /// kind of implicit instantiation.
0818     ovl_fail_illegal_constructor,
0819 
0820     /// This conversion candidate is not viable because its result
0821     /// type is not implicitly convertible to the desired type.
0822     ovl_fail_bad_final_conversion,
0823 
0824     /// This conversion function template specialization candidate is not
0825     /// viable because the final conversion was not an exact match.
0826     ovl_fail_final_conversion_not_exact,
0827 
0828     /// (CUDA) This candidate was not viable because the callee
0829     /// was not accessible from the caller's target (i.e. host->device,
0830     /// global->host, device->host).
0831     ovl_fail_bad_target,
0832 
0833     /// This candidate function was not viable because an enable_if
0834     /// attribute disabled it.
0835     ovl_fail_enable_if,
0836 
0837     /// This candidate constructor or conversion function is explicit but
0838     /// the context doesn't permit explicit functions.
0839     ovl_fail_explicit,
0840 
0841     /// This candidate was not viable because its address could not be taken.
0842     ovl_fail_addr_not_available,
0843 
0844     /// This inherited constructor is not viable because it would slice the
0845     /// argument.
0846     ovl_fail_inhctor_slice,
0847 
0848     /// This candidate was not viable because it is a non-default multiversioned
0849     /// function.
0850     ovl_non_default_multiversion_function,
0851 
0852     /// This constructor/conversion candidate fail due to an address space
0853     /// mismatch between the object being constructed and the overload
0854     /// candidate.
0855     ovl_fail_object_addrspace_mismatch,
0856 
0857     /// This candidate was not viable because its associated constraints were
0858     /// not satisfied.
0859     ovl_fail_constraints_not_satisfied,
0860 
0861     /// This candidate was not viable because it has internal linkage and is
0862     /// from a different module unit than the use.
0863     ovl_fail_module_mismatched,
0864   };
0865 
0866   /// A list of implicit conversion sequences for the arguments of an
0867   /// OverloadCandidate.
0868   using ConversionSequenceList =
0869       llvm::MutableArrayRef<ImplicitConversionSequence>;
0870 
0871   /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
0872   struct OverloadCandidate {
0873     /// Function - The actual function that this candidate
0874     /// represents. When NULL, this is a built-in candidate
0875     /// (C++ [over.oper]) or a surrogate for a conversion to a
0876     /// function pointer or reference (C++ [over.call.object]).
0877     FunctionDecl *Function;
0878 
0879     /// FoundDecl - The original declaration that was looked up /
0880     /// invented / otherwise found, together with its access.
0881     /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
0882     DeclAccessPair FoundDecl;
0883 
0884     /// BuiltinParamTypes - Provides the parameter types of a built-in overload
0885     /// candidate. Only valid when Function is NULL.
0886     QualType BuiltinParamTypes[3];
0887 
0888     /// Surrogate - The conversion function for which this candidate
0889     /// is a surrogate, but only if IsSurrogate is true.
0890     CXXConversionDecl *Surrogate;
0891 
0892     /// The conversion sequences used to convert the function arguments
0893     /// to the function parameters. Note that these are indexed by argument,
0894     /// so may not match the parameter order of Function.
0895     ConversionSequenceList Conversions;
0896 
0897     /// The FixIt hints which can be used to fix the Bad candidate.
0898     ConversionFixItGenerator Fix;
0899 
0900     /// Viable - True to indicate that this overload candidate is viable.
0901     LLVM_PREFERRED_TYPE(bool)
0902     unsigned Viable : 1;
0903 
0904     /// Whether this candidate is the best viable function, or tied for being
0905     /// the best viable function.
0906     ///
0907     /// For an ambiguous overload resolution, indicates whether this candidate
0908     /// was part of the ambiguity kernel: the minimal non-empty set of viable
0909     /// candidates such that all elements of the ambiguity kernel are better
0910     /// than all viable candidates not in the ambiguity kernel.
0911     LLVM_PREFERRED_TYPE(bool)
0912     unsigned Best : 1;
0913 
0914     /// IsSurrogate - True to indicate that this candidate is a
0915     /// surrogate for a conversion to a function pointer or reference
0916     /// (C++ [over.call.object]).
0917     LLVM_PREFERRED_TYPE(bool)
0918     unsigned IsSurrogate : 1;
0919 
0920     /// IgnoreObjectArgument - True to indicate that the first
0921     /// argument's conversion, which for this function represents the
0922     /// implicit object argument, should be ignored. This will be true
0923     /// when the candidate is a static member function (where the
0924     /// implicit object argument is just a placeholder) or a
0925     /// non-static member function when the call doesn't have an
0926     /// object argument.
0927     LLVM_PREFERRED_TYPE(bool)
0928     unsigned IgnoreObjectArgument : 1;
0929 
0930     LLVM_PREFERRED_TYPE(bool)
0931     unsigned TookAddressOfOverload : 1;
0932 
0933     /// Have we matched any packs on the parameter side, versus any non-packs on
0934     /// the argument side, in a context where the opposite matching is also
0935     /// allowed?
0936     bool HasMatchedPackOnParmToNonPackOnArg : 1;
0937 
0938     /// True if the candidate was found using ADL.
0939     LLVM_PREFERRED_TYPE(CallExpr::ADLCallKind)
0940     unsigned IsADLCandidate : 1;
0941 
0942     /// Whether this is a rewritten candidate, and if so, of what kind?
0943     LLVM_PREFERRED_TYPE(OverloadCandidateRewriteKind)
0944     unsigned RewriteKind : 2;
0945 
0946     /// FailureKind - The reason why this candidate is not viable.
0947     /// Actually an OverloadFailureKind.
0948     unsigned char FailureKind;
0949 
0950     /// The number of call arguments that were explicitly provided,
0951     /// to be used while performing partial ordering of function templates.
0952     unsigned ExplicitCallArguments;
0953 
0954     union {
0955       DeductionFailureInfo DeductionFailure;
0956 
0957       /// FinalConversion - For a conversion function (where Function is
0958       /// a CXXConversionDecl), the standard conversion that occurs
0959       /// after the call to the overload candidate to convert the result
0960       /// of calling the conversion function to the required type.
0961       StandardConversionSequence FinalConversion;
0962     };
0963 
0964     /// Get RewriteKind value in OverloadCandidateRewriteKind type (This
0965     /// function is to workaround the spurious GCC bitfield enum warning)
0966     OverloadCandidateRewriteKind getRewriteKind() const {
0967       return static_cast<OverloadCandidateRewriteKind>(RewriteKind);
0968     }
0969 
0970     bool isReversed() const { return getRewriteKind() & CRK_Reversed; }
0971 
0972     /// hasAmbiguousConversion - Returns whether this overload
0973     /// candidate requires an ambiguous conversion or not.
0974     bool hasAmbiguousConversion() const {
0975       for (auto &C : Conversions) {
0976         if (!C.isInitialized()) return false;
0977         if (C.isAmbiguous()) return true;
0978       }
0979       return false;
0980     }
0981 
0982     bool TryToFixBadConversion(unsigned Idx, Sema &S) {
0983       bool CanFix = Fix.tryToFixConversion(
0984                       Conversions[Idx].Bad.FromExpr,
0985                       Conversions[Idx].Bad.getFromType(),
0986                       Conversions[Idx].Bad.getToType(), S);
0987 
0988       // If at least one conversion fails, the candidate cannot be fixed.
0989       if (!CanFix)
0990         Fix.clear();
0991 
0992       return CanFix;
0993     }
0994 
0995     unsigned getNumParams() const {
0996       if (IsSurrogate) {
0997         QualType STy = Surrogate->getConversionType();
0998         while (STy->isPointerOrReferenceType())
0999           STy = STy->getPointeeType();
1000         return STy->castAs<FunctionProtoType>()->getNumParams();
1001       }
1002       if (Function)
1003         return Function->getNumParams();
1004       return ExplicitCallArguments;
1005     }
1006 
1007     bool NotValidBecauseConstraintExprHasError() const;
1008 
1009   private:
1010     friend class OverloadCandidateSet;
1011     OverloadCandidate()
1012         : IsSurrogate(false), IgnoreObjectArgument(false),
1013           TookAddressOfOverload(false),
1014           HasMatchedPackOnParmToNonPackOnArg(false),
1015           IsADLCandidate(llvm::to_underlying(CallExpr::NotADL)),
1016           RewriteKind(CRK_None) {}
1017   };
1018 
1019   /// OverloadCandidateSet - A set of overload candidates, used in C++
1020   /// overload resolution (C++ 13.3).
1021   class OverloadCandidateSet {
1022   public:
1023     enum CandidateSetKind {
1024       /// Normal lookup.
1025       CSK_Normal,
1026 
1027       /// C++ [over.match.oper]:
1028       /// Lookup of operator function candidates in a call using operator
1029       /// syntax. Candidates that have no parameters of class type will be
1030       /// skipped unless there is a parameter of (reference to) enum type and
1031       /// the corresponding argument is of the same enum type.
1032       CSK_Operator,
1033 
1034       /// C++ [over.match.copy]:
1035       /// Copy-initialization of an object of class type by user-defined
1036       /// conversion.
1037       CSK_InitByUserDefinedConversion,
1038 
1039       /// C++ [over.match.ctor], [over.match.list]
1040       /// Initialization of an object of class type by constructor,
1041       /// using either a parenthesized or braced list of arguments.
1042       CSK_InitByConstructor,
1043 
1044       /// C++ [over.match.call.general]
1045       /// Resolve a call through the address of an overload set.
1046       CSK_AddressOfOverloadSet,
1047     };
1048 
1049     /// Information about operator rewrites to consider when adding operator
1050     /// functions to a candidate set.
1051     struct OperatorRewriteInfo {
1052       OperatorRewriteInfo()
1053           : OriginalOperator(OO_None), OpLoc(), AllowRewrittenCandidates(false) {}
1054       OperatorRewriteInfo(OverloadedOperatorKind Op, SourceLocation OpLoc,
1055                           bool AllowRewritten)
1056           : OriginalOperator(Op), OpLoc(OpLoc),
1057             AllowRewrittenCandidates(AllowRewritten) {}
1058 
1059       /// The original operator as written in the source.
1060       OverloadedOperatorKind OriginalOperator;
1061       /// The source location of the operator.
1062       SourceLocation OpLoc;
1063       /// Whether we should include rewritten candidates in the overload set.
1064       bool AllowRewrittenCandidates;
1065 
1066       /// Would use of this function result in a rewrite using a different
1067       /// operator?
1068       bool isRewrittenOperator(const FunctionDecl *FD) {
1069         return OriginalOperator &&
1070                FD->getDeclName().getCXXOverloadedOperator() != OriginalOperator;
1071       }
1072 
1073       bool isAcceptableCandidate(const FunctionDecl *FD) {
1074         if (!OriginalOperator)
1075           return true;
1076 
1077         // For an overloaded operator, we can have candidates with a different
1078         // name in our unqualified lookup set. Make sure we only consider the
1079         // ones we're supposed to.
1080         OverloadedOperatorKind OO =
1081             FD->getDeclName().getCXXOverloadedOperator();
1082         return OO && (OO == OriginalOperator ||
1083                       (AllowRewrittenCandidates &&
1084                        OO == getRewrittenOverloadedOperator(OriginalOperator)));
1085       }
1086 
1087       /// Determine the kind of rewrite that should be performed for this
1088       /// candidate.
1089       OverloadCandidateRewriteKind
1090       getRewriteKind(const FunctionDecl *FD, OverloadCandidateParamOrder PO) {
1091         OverloadCandidateRewriteKind CRK = CRK_None;
1092         if (isRewrittenOperator(FD))
1093           CRK = OverloadCandidateRewriteKind(CRK | CRK_DifferentOperator);
1094         if (PO == OverloadCandidateParamOrder::Reversed)
1095           CRK = OverloadCandidateRewriteKind(CRK | CRK_Reversed);
1096         return CRK;
1097       }
1098       /// Determines whether this operator could be implemented by a function
1099       /// with reversed parameter order.
1100       bool isReversible() {
1101         return AllowRewrittenCandidates && OriginalOperator &&
1102                (getRewrittenOverloadedOperator(OriginalOperator) != OO_None ||
1103                 allowsReversed(OriginalOperator));
1104       }
1105 
1106       /// Determine whether reversing parameter order is allowed for operator
1107       /// Op.
1108       bool allowsReversed(OverloadedOperatorKind Op);
1109 
1110       /// Determine whether we should add a rewritten candidate for \p FD with
1111       /// reversed parameter order.
1112       /// \param OriginalArgs are the original non reversed arguments.
1113       bool shouldAddReversed(Sema &S, ArrayRef<Expr *> OriginalArgs,
1114                              FunctionDecl *FD);
1115     };
1116 
1117   private:
1118     SmallVector<OverloadCandidate, 16> Candidates;
1119     llvm::SmallPtrSet<uintptr_t, 16> Functions;
1120 
1121     // Allocator for ConversionSequenceLists. We store the first few of these
1122     // inline to avoid allocation for small sets.
1123     llvm::BumpPtrAllocator SlabAllocator;
1124 
1125     SourceLocation Loc;
1126     CandidateSetKind Kind;
1127     OperatorRewriteInfo RewriteInfo;
1128 
1129     constexpr static unsigned NumInlineBytes =
1130         24 * sizeof(ImplicitConversionSequence);
1131     unsigned NumInlineBytesUsed = 0;
1132     alignas(void *) char InlineSpace[NumInlineBytes];
1133 
1134     // Address space of the object being constructed.
1135     LangAS DestAS = LangAS::Default;
1136 
1137     /// If we have space, allocates from inline storage. Otherwise, allocates
1138     /// from the slab allocator.
1139     /// FIXME: It would probably be nice to have a SmallBumpPtrAllocator
1140     /// instead.
1141     /// FIXME: Now that this only allocates ImplicitConversionSequences, do we
1142     /// want to un-generalize this?
1143     template <typename T>
1144     T *slabAllocate(unsigned N) {
1145       // It's simpler if this doesn't need to consider alignment.
1146       static_assert(alignof(T) == alignof(void *),
1147                     "Only works for pointer-aligned types.");
1148       static_assert(std::is_trivial<T>::value ||
1149                         std::is_same<ImplicitConversionSequence, T>::value,
1150                     "Add destruction logic to OverloadCandidateSet::clear().");
1151 
1152       unsigned NBytes = sizeof(T) * N;
1153       if (NBytes > NumInlineBytes - NumInlineBytesUsed)
1154         return SlabAllocator.Allocate<T>(N);
1155       char *FreeSpaceStart = InlineSpace + NumInlineBytesUsed;
1156       assert(uintptr_t(FreeSpaceStart) % alignof(void *) == 0 &&
1157              "Misaligned storage!");
1158 
1159       NumInlineBytesUsed += NBytes;
1160       return reinterpret_cast<T *>(FreeSpaceStart);
1161     }
1162 
1163     void destroyCandidates();
1164 
1165   public:
1166     OverloadCandidateSet(SourceLocation Loc, CandidateSetKind CSK,
1167                          OperatorRewriteInfo RewriteInfo = {})
1168         : Loc(Loc), Kind(CSK), RewriteInfo(RewriteInfo) {}
1169     OverloadCandidateSet(const OverloadCandidateSet &) = delete;
1170     OverloadCandidateSet &operator=(const OverloadCandidateSet &) = delete;
1171     ~OverloadCandidateSet() { destroyCandidates(); }
1172 
1173     SourceLocation getLocation() const { return Loc; }
1174     CandidateSetKind getKind() const { return Kind; }
1175     OperatorRewriteInfo getRewriteInfo() const { return RewriteInfo; }
1176 
1177     /// Whether diagnostics should be deferred.
1178     bool shouldDeferDiags(Sema &S, ArrayRef<Expr *> Args, SourceLocation OpLoc);
1179 
1180     /// Determine when this overload candidate will be new to the
1181     /// overload set.
1182     bool isNewCandidate(Decl *F, OverloadCandidateParamOrder PO =
1183                                      OverloadCandidateParamOrder::Normal) {
1184       uintptr_t Key = reinterpret_cast<uintptr_t>(F->getCanonicalDecl());
1185       Key |= static_cast<uintptr_t>(PO);
1186       return Functions.insert(Key).second;
1187     }
1188 
1189     /// Exclude a function from being considered by overload resolution.
1190     void exclude(Decl *F) {
1191       isNewCandidate(F, OverloadCandidateParamOrder::Normal);
1192       isNewCandidate(F, OverloadCandidateParamOrder::Reversed);
1193     }
1194 
1195     /// Clear out all of the candidates.
1196     void clear(CandidateSetKind CSK);
1197 
1198     using iterator = SmallVectorImpl<OverloadCandidate>::iterator;
1199 
1200     iterator begin() { return Candidates.begin(); }
1201     iterator end() { return Candidates.end(); }
1202 
1203     size_t size() const { return Candidates.size(); }
1204     bool empty() const { return Candidates.empty(); }
1205 
1206     /// Allocate storage for conversion sequences for NumConversions
1207     /// conversions.
1208     ConversionSequenceList
1209     allocateConversionSequences(unsigned NumConversions) {
1210       ImplicitConversionSequence *Conversions =
1211           slabAllocate<ImplicitConversionSequence>(NumConversions);
1212 
1213       // Construct the new objects.
1214       for (unsigned I = 0; I != NumConversions; ++I)
1215         new (&Conversions[I]) ImplicitConversionSequence();
1216 
1217       return ConversionSequenceList(Conversions, NumConversions);
1218     }
1219 
1220     /// Add a new candidate with NumConversions conversion sequence slots
1221     /// to the overload set.
1222     OverloadCandidate &addCandidate(unsigned NumConversions = 0,
1223                                     ConversionSequenceList Conversions = {}) {
1224       assert((Conversions.empty() || Conversions.size() == NumConversions) &&
1225              "preallocated conversion sequence has wrong length");
1226 
1227       Candidates.push_back(OverloadCandidate());
1228       OverloadCandidate &C = Candidates.back();
1229       C.Conversions = Conversions.empty()
1230                           ? allocateConversionSequences(NumConversions)
1231                           : Conversions;
1232       return C;
1233     }
1234 
1235     /// Find the best viable function on this overload set, if it exists.
1236     OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
1237                                          OverloadCandidateSet::iterator& Best);
1238 
1239     SmallVector<OverloadCandidate *, 32> CompleteCandidates(
1240         Sema &S, OverloadCandidateDisplayKind OCD, ArrayRef<Expr *> Args,
1241         SourceLocation OpLoc = SourceLocation(),
1242         llvm::function_ref<bool(OverloadCandidate &)> Filter =
1243             [](OverloadCandidate &) { return true; });
1244 
1245     void NoteCandidates(
1246         PartialDiagnosticAt PA, Sema &S, OverloadCandidateDisplayKind OCD,
1247         ArrayRef<Expr *> Args, StringRef Opc = "",
1248         SourceLocation Loc = SourceLocation(),
1249         llvm::function_ref<bool(OverloadCandidate &)> Filter =
1250             [](OverloadCandidate &) { return true; });
1251 
1252     void NoteCandidates(Sema &S, ArrayRef<Expr *> Args,
1253                         ArrayRef<OverloadCandidate *> Cands,
1254                         StringRef Opc = "",
1255                         SourceLocation OpLoc = SourceLocation());
1256 
1257     LangAS getDestAS() { return DestAS; }
1258 
1259     void setDestAS(LangAS AS) {
1260       assert((Kind == CSK_InitByConstructor ||
1261               Kind == CSK_InitByUserDefinedConversion) &&
1262              "can't set the destination address space when not constructing an "
1263              "object");
1264       DestAS = AS;
1265     }
1266 
1267   };
1268 
1269   bool isBetterOverloadCandidate(Sema &S,
1270                                  const OverloadCandidate &Cand1,
1271                                  const OverloadCandidate &Cand2,
1272                                  SourceLocation Loc,
1273                                  OverloadCandidateSet::CandidateSetKind Kind);
1274 
1275   struct ConstructorInfo {
1276     DeclAccessPair FoundDecl;
1277     CXXConstructorDecl *Constructor;
1278     FunctionTemplateDecl *ConstructorTmpl;
1279 
1280     explicit operator bool() const { return Constructor; }
1281   };
1282 
1283   // FIXME: Add an AddOverloadCandidate / AddTemplateOverloadCandidate overload
1284   // that takes one of these.
1285   inline ConstructorInfo getConstructorInfo(NamedDecl *ND) {
1286     if (isa<UsingDecl>(ND))
1287       return ConstructorInfo{};
1288 
1289     // For constructors, the access check is performed against the underlying
1290     // declaration, not the found declaration.
1291     auto *D = ND->getUnderlyingDecl();
1292     ConstructorInfo Info = {DeclAccessPair::make(ND, D->getAccess()), nullptr,
1293                             nullptr};
1294     Info.ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
1295     if (Info.ConstructorTmpl)
1296       D = Info.ConstructorTmpl->getTemplatedDecl();
1297     Info.Constructor = dyn_cast<CXXConstructorDecl>(D);
1298     return Info;
1299   }
1300 
1301   // Returns false if signature help is relevant despite number of arguments
1302   // exceeding parameters. Specifically, it returns false when
1303   // PartialOverloading is true and one of the following:
1304   // * Function is variadic
1305   // * Function is template variadic
1306   // * Function is an instantiation of template variadic function
1307   // The last case may seem strange. The idea is that if we added one more
1308   // argument, we'd end up with a function similar to Function. Since, in the
1309   // context of signature help and/or code completion, we do not know what the
1310   // type of the next argument (that the user is typing) will be, this is as
1311   // good candidate as we can get, despite the fact that it takes one less
1312   // parameter.
1313   bool shouldEnforceArgLimit(bool PartialOverloading, FunctionDecl *Function);
1314 
1315 } // namespace clang
1316 
1317 #endif // LLVM_CLANG_SEMA_OVERLOAD_H