1 //===--- Overload.h - C++ Overloading ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the data structures and types used in C++
11 // overload resolution.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SEMA_OVERLOAD_H
16 #define LLVM_CLANG_SEMA_OVERLOAD_H
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/TemplateBase.h"
22 #include "clang/AST/Type.h"
23 #include "clang/AST/UnresolvedSet.h"
24 #include "clang/Sema/SemaFixItUtils.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/Allocator.h"
31 class CXXConstructorDecl;
32 class CXXConversionDecl;
36 /// OverloadingResult - Capture the result of performing overload
38 enum OverloadingResult {
39 OR_Success, ///< Overload resolution succeeded.
40 OR_No_Viable_Function, ///< No viable function found.
41 OR_Ambiguous, ///< Ambiguous candidates found.
42 OR_Deleted ///< Succeeded, but refers to a deleted function.
45 enum OverloadCandidateDisplayKind {
46 /// Requests that all candidates be shown. Viable candidates will
50 /// Requests that only viable candidates be shown.
54 /// ImplicitConversionKind - The kind of implicit conversion used to
55 /// convert an argument to a parameter's type. The enumerator values
56 /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that
57 /// better conversion kinds have smaller values.
58 enum ImplicitConversionKind {
59 ICK_Identity = 0, ///< Identity conversion (no conversion)
60 ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1)
61 ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2)
62 ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3)
63 ICK_NoReturn_Adjustment, ///< Removal of noreturn from a type (Clang)
64 ICK_Qualification, ///< Qualification conversions (C++ 4.4)
65 ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5)
66 ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6)
67 ICK_Complex_Promotion, ///< Complex promotions (Clang extension)
68 ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7)
69 ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8)
70 ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6)
71 ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9)
72 ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10)
73 ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11)
74 ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12)
75 ICK_Compatible_Conversion, ///< Conversions between compatible types in C99
76 ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics])
77 ICK_Vector_Conversion, ///< Vector conversions
78 ICK_Vector_Splat, ///< A vector splat from an arithmetic type
79 ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7)
80 ICK_Block_Pointer_Conversion, ///< Block Pointer conversions
81 ICK_TransparentUnionConversion, /// Transparent Union Conversions
82 ICK_Writeback_Conversion, ///< Objective-C ARC writeback conversion
83 ICK_Num_Conversion_Kinds ///< The number of conversion kinds
86 /// ImplicitConversionCategory - The category of an implicit
87 /// conversion kind. The enumerator values match with Table 9 of
88 /// (C++ 13.3.3.1.1) and are listed such that better conversion
89 /// categories have smaller values.
90 enum ImplicitConversionCategory {
91 ICC_Identity = 0, ///< Identity
92 ICC_Lvalue_Transformation, ///< Lvalue transformation
93 ICC_Qualification_Adjustment, ///< Qualification adjustment
94 ICC_Promotion, ///< Promotion
95 ICC_Conversion ///< Conversion
98 ImplicitConversionCategory
99 GetConversionCategory(ImplicitConversionKind Kind);
101 /// ImplicitConversionRank - The rank of an implicit conversion
102 /// kind. The enumerator values match with Table 9 of (C++
103 /// 13.3.3.1.1) and are listed such that better conversion ranks
104 /// have smaller values.
105 enum ImplicitConversionRank {
106 ICR_Exact_Match = 0, ///< Exact Match
107 ICR_Promotion, ///< Promotion
108 ICR_Conversion, ///< Conversion
109 ICR_Complex_Real_Conversion, ///< Complex <-> Real conversion
110 ICR_Writeback_Conversion ///< ObjC ARC writeback conversion
113 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
115 /// NarrowingKind - The kind of narrowing conversion being performed by a
116 /// standard conversion sequence according to C++11 [dcl.init.list]p7.
118 /// Not a narrowing conversion.
121 /// A narrowing conversion by virtue of the source and destination types.
124 /// A narrowing conversion, because a constant expression got narrowed.
125 NK_Constant_Narrowing,
127 /// A narrowing conversion, because a non-constant-expression variable might
128 /// have got narrowed.
129 NK_Variable_Narrowing
132 /// StandardConversionSequence - represents a standard conversion
133 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
134 /// contains between zero and three conversions. If a particular
135 /// conversion is not needed, it will be set to the identity conversion
136 /// (ICK_Identity). Note that the three conversions are
137 /// specified as separate members (rather than in an array) so that
138 /// we can keep the size of a standard conversion sequence to a
140 class StandardConversionSequence {
142 /// First -- The first conversion can be an lvalue-to-rvalue
143 /// conversion, array-to-pointer conversion, or
144 /// function-to-pointer conversion.
145 ImplicitConversionKind First : 8;
147 /// Second - The second conversion can be an integral promotion,
148 /// floating point promotion, integral conversion, floating point
149 /// conversion, floating-integral conversion, pointer conversion,
150 /// pointer-to-member conversion, or boolean conversion.
151 ImplicitConversionKind Second : 8;
153 /// Third - The third conversion can be a qualification conversion.
154 ImplicitConversionKind Third : 8;
156 /// \brief Whether this is the deprecated conversion of a
157 /// string literal to a pointer to non-const character data
159 unsigned DeprecatedStringLiteralToCharPtr : 1;
161 /// \brief Whether the qualification conversion involves a change in the
162 /// Objective-C lifetime (for automatic reference counting).
163 unsigned QualificationIncludesObjCLifetime : 1;
165 /// IncompatibleObjC - Whether this is an Objective-C conversion
166 /// that we should warn about (if we actually use it).
167 unsigned IncompatibleObjC : 1;
169 /// ReferenceBinding - True when this is a reference binding
170 /// (C++ [over.ics.ref]).
171 unsigned ReferenceBinding : 1;
173 /// DirectBinding - True when this is a reference binding that is a
174 /// direct binding (C++ [dcl.init.ref]).
175 unsigned DirectBinding : 1;
177 /// \brief Whether this is an lvalue reference binding (otherwise, it's
178 /// an rvalue reference binding).
179 unsigned IsLvalueReference : 1;
181 /// \brief Whether we're binding to a function lvalue.
182 unsigned BindsToFunctionLvalue : 1;
184 /// \brief Whether we're binding to an rvalue.
185 unsigned BindsToRvalue : 1;
187 /// \brief Whether this binds an implicit object argument to a
188 /// non-static member function without a ref-qualifier.
189 unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
191 /// \brief Whether this binds a reference to an object with a different
192 /// Objective-C lifetime qualifier.
193 unsigned ObjCLifetimeConversionBinding : 1;
195 /// FromType - The type that this conversion is converting
196 /// from. This is an opaque pointer that can be translated into a
200 /// ToType - The types that this conversion is converting to in
201 /// each step. This is an opaque pointer that can be translated
205 /// CopyConstructor - The copy constructor that is used to perform
206 /// this conversion, when the conversion is actually just the
207 /// initialization of an object via copy constructor. Such
208 /// conversions are either identity conversions or derived-to-base
210 CXXConstructorDecl *CopyConstructor;
212 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
213 void setToType(unsigned Idx, QualType T) {
214 assert(Idx < 3 && "To type index is out of range");
215 ToTypePtrs[Idx] = T.getAsOpaquePtr();
217 void setAllToTypes(QualType T) {
218 ToTypePtrs[0] = T.getAsOpaquePtr();
219 ToTypePtrs[1] = ToTypePtrs[0];
220 ToTypePtrs[2] = ToTypePtrs[0];
223 QualType getFromType() const {
224 return QualType::getFromOpaquePtr(FromTypePtr);
226 QualType getToType(unsigned Idx) const {
227 assert(Idx < 3 && "To type index is out of range");
228 return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
231 void setAsIdentityConversion();
233 bool isIdentityConversion() const {
234 return Second == ICK_Identity && Third == ICK_Identity;
237 ImplicitConversionRank getRank() const;
238 NarrowingKind getNarrowingKind(ASTContext &Context, const Expr *Converted,
239 APValue &ConstantValue,
240 QualType &ConstantType) const;
241 bool isPointerConversionToBool() const;
242 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
243 void DebugPrint() const;
246 /// UserDefinedConversionSequence - Represents a user-defined
247 /// conversion sequence (C++ 13.3.3.1.2).
248 struct UserDefinedConversionSequence {
249 /// \brief Represents the standard conversion that occurs before
250 /// the actual user-defined conversion.
252 /// C++11 13.3.3.1.2p1:
253 /// If the user-defined conversion is specified by a constructor
254 /// (12.3.1), the initial standard conversion sequence converts
255 /// the source type to the type required by the argument of the
256 /// constructor. If the user-defined conversion is specified by
257 /// a conversion function (12.3.2), the initial standard
258 /// conversion sequence converts the source type to the implicit
259 /// object parameter of the conversion function.
260 StandardConversionSequence Before;
262 /// EllipsisConversion - When this is true, it means user-defined
263 /// conversion sequence starts with a ... (elipsis) conversion, instead of
264 /// a standard conversion. In this case, 'Before' field must be ignored.
265 // FIXME. I much rather put this as the first field. But there seems to be
266 // a gcc code gen. bug which causes a crash in a test. Putting it here seems
267 // to work around the crash.
268 bool EllipsisConversion : 1;
270 /// HadMultipleCandidates - When this is true, it means that the
271 /// conversion function was resolved from an overloaded set having
272 /// size greater than 1.
273 bool HadMultipleCandidates : 1;
275 /// After - Represents the standard conversion that occurs after
276 /// the actual user-defined conversion.
277 StandardConversionSequence After;
279 /// ConversionFunction - The function that will perform the
280 /// user-defined conversion. Null if the conversion is an
281 /// aggregate initialization from an initializer list.
282 FunctionDecl* ConversionFunction;
284 /// \brief The declaration that we found via name lookup, which might be
285 /// the same as \c ConversionFunction or it might be a using declaration
286 /// that refers to \c ConversionFunction.
287 DeclAccessPair FoundConversionFunction;
289 void DebugPrint() const;
292 /// Represents an ambiguous user-defined conversion sequence.
293 struct AmbiguousConversionSequence {
294 typedef SmallVector<FunctionDecl*, 4> ConversionSet;
298 char Buffer[sizeof(ConversionSet)];
300 QualType getFromType() const {
301 return QualType::getFromOpaquePtr(FromTypePtr);
303 QualType getToType() const {
304 return QualType::getFromOpaquePtr(ToTypePtr);
306 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
307 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
309 ConversionSet &conversions() {
310 return *reinterpret_cast<ConversionSet*>(Buffer);
313 const ConversionSet &conversions() const {
314 return *reinterpret_cast<const ConversionSet*>(Buffer);
317 void addConversion(FunctionDecl *D) {
318 conversions().push_back(D);
321 typedef ConversionSet::iterator iterator;
322 iterator begin() { return conversions().begin(); }
323 iterator end() { return conversions().end(); }
325 typedef ConversionSet::const_iterator const_iterator;
326 const_iterator begin() const { return conversions().begin(); }
327 const_iterator end() const { return conversions().end(); }
331 void copyFrom(const AmbiguousConversionSequence &);
334 /// BadConversionSequence - Records information about an invalid
335 /// conversion sequence.
336 struct BadConversionSequence {
342 lvalue_ref_to_rvalue,
346 // This can be null, e.g. for implicit object arguments.
352 // The type we're converting from (an opaque QualType).
355 // The type we're converting to (an opaque QualType).
359 void init(FailureKind K, Expr *From, QualType To) {
360 init(K, From->getType(), To);
363 void init(FailureKind K, QualType From, QualType To) {
370 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
371 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
373 void setFromExpr(Expr *E) {
375 setFromType(E->getType());
377 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
378 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
381 /// ImplicitConversionSequence - Represents an implicit conversion
382 /// sequence, which may be a standard conversion sequence
383 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
384 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
385 class ImplicitConversionSequence {
387 /// Kind - The kind of implicit conversion sequence. BadConversion
388 /// specifies that there is no conversion from the source type to
389 /// the target type. AmbiguousConversion represents the unique
390 /// ambiguous conversion (C++0x [over.best.ics]p10).
392 StandardConversion = 0,
393 UserDefinedConversion,
401 Uninitialized = BadConversion + 1
404 /// ConversionKind - The kind of implicit conversion sequence.
405 unsigned ConversionKind : 30;
407 /// \brief Whether the argument is an initializer list.
408 bool ListInitializationSequence : 1;
410 /// \brief Whether the target is really a std::initializer_list, and the
411 /// sequence only represents the worst element conversion.
412 bool StdInitializerListElement : 1;
414 void setKind(Kind K) {
420 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
425 /// When ConversionKind == StandardConversion, provides the
426 /// details of the standard conversion sequence.
427 StandardConversionSequence Standard;
429 /// When ConversionKind == UserDefinedConversion, provides the
430 /// details of the user-defined conversion sequence.
431 UserDefinedConversionSequence UserDefined;
433 /// When ConversionKind == AmbiguousConversion, provides the
434 /// details of the ambiguous conversion.
435 AmbiguousConversionSequence Ambiguous;
437 /// When ConversionKind == BadConversion, provides the details
438 /// of the bad conversion.
439 BadConversionSequence Bad;
442 ImplicitConversionSequence()
443 : ConversionKind(Uninitialized), ListInitializationSequence(false),
444 StdInitializerListElement(false)
446 ~ImplicitConversionSequence() {
449 ImplicitConversionSequence(const ImplicitConversionSequence &Other)
450 : ConversionKind(Other.ConversionKind),
451 ListInitializationSequence(Other.ListInitializationSequence),
452 StdInitializerListElement(Other.StdInitializerListElement)
454 switch (ConversionKind) {
455 case Uninitialized: break;
456 case StandardConversion: Standard = Other.Standard; break;
457 case UserDefinedConversion: UserDefined = Other.UserDefined; break;
458 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
459 case EllipsisConversion: break;
460 case BadConversion: Bad = Other.Bad; break;
464 ImplicitConversionSequence &
465 operator=(const ImplicitConversionSequence &Other) {
467 new (this) ImplicitConversionSequence(Other);
471 Kind getKind() const {
472 assert(isInitialized() && "querying uninitialized conversion");
473 return Kind(ConversionKind);
476 /// \brief Return a ranking of the implicit conversion sequence
477 /// kind, where smaller ranks represent better conversion
480 /// In particular, this routine gives user-defined conversion
481 /// sequences and ambiguous conversion sequences the same rank,
482 /// per C++ [over.best.ics]p10.
483 unsigned getKindRank() const {
485 case StandardConversion:
488 case UserDefinedConversion:
489 case AmbiguousConversion:
492 case EllipsisConversion:
499 llvm_unreachable("Invalid ImplicitConversionSequence::Kind!");
502 bool isBad() const { return getKind() == BadConversion; }
503 bool isStandard() const { return getKind() == StandardConversion; }
504 bool isEllipsis() const { return getKind() == EllipsisConversion; }
505 bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
506 bool isUserDefined() const { return getKind() == UserDefinedConversion; }
507 bool isFailure() const { return isBad() || isAmbiguous(); }
509 /// Determines whether this conversion sequence has been
510 /// initialized. Most operations should never need to query
511 /// uninitialized conversions and should assert as above.
512 bool isInitialized() const { return ConversionKind != Uninitialized; }
514 /// Sets this sequence as a bad conversion for an explicit argument.
515 void setBad(BadConversionSequence::FailureKind Failure,
516 Expr *FromExpr, QualType ToType) {
517 setKind(BadConversion);
518 Bad.init(Failure, FromExpr, ToType);
521 /// Sets this sequence as a bad conversion for an implicit argument.
522 void setBad(BadConversionSequence::FailureKind Failure,
523 QualType FromType, QualType ToType) {
524 setKind(BadConversion);
525 Bad.init(Failure, FromType, ToType);
528 void setStandard() { setKind(StandardConversion); }
529 void setEllipsis() { setKind(EllipsisConversion); }
530 void setUserDefined() { setKind(UserDefinedConversion); }
531 void setAmbiguous() {
532 if (ConversionKind == AmbiguousConversion) return;
533 ConversionKind = AmbiguousConversion;
534 Ambiguous.construct();
537 /// \brief Whether this sequence was created by the rules of
538 /// list-initialization sequences.
539 bool isListInitializationSequence() const {
540 return ListInitializationSequence;
543 void setListInitializationSequence() {
544 ListInitializationSequence = true;
547 /// \brief Whether the target is really a std::initializer_list, and the
548 /// sequence only represents the worst element conversion.
549 bool isStdInitializerListElement() const {
550 return StdInitializerListElement;
553 void setStdInitializerListElement(bool V = true) {
554 StdInitializerListElement = V;
557 // The result of a comparison between implicit conversion
558 // sequences. Use Sema::CompareImplicitConversionSequences to
559 // actually perform the comparison.
562 Indistinguishable = 0,
566 void DiagnoseAmbiguousConversion(Sema &S,
567 SourceLocation CaretLoc,
568 const PartialDiagnostic &PDiag) const;
570 void DebugPrint() const;
573 enum OverloadFailureKind {
574 ovl_fail_too_many_arguments,
575 ovl_fail_too_few_arguments,
576 ovl_fail_bad_conversion,
577 ovl_fail_bad_deduction,
579 /// This conversion candidate was not considered because it
580 /// duplicates the work of a trivial or derived-to-base
582 ovl_fail_trivial_conversion,
584 /// This conversion candidate is not viable because its result
585 /// type is not implicitly convertible to the desired type.
586 ovl_fail_bad_final_conversion,
588 /// This conversion function template specialization candidate is not
589 /// viable because the final conversion was not an exact match.
590 ovl_fail_final_conversion_not_exact,
592 /// (CUDA) This candidate was not viable because the callee
593 /// was not accessible from the caller's target (i.e. host->device,
594 /// global->host, device->host).
598 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
599 struct OverloadCandidate {
600 /// Function - The actual function that this candidate
601 /// represents. When NULL, this is a built-in candidate
602 /// (C++ [over.oper]) or a surrogate for a conversion to a
603 /// function pointer or reference (C++ [over.call.object]).
604 FunctionDecl *Function;
606 /// FoundDecl - The original declaration that was looked up /
607 /// invented / otherwise found, together with its access.
608 /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
609 DeclAccessPair FoundDecl;
611 // BuiltinTypes - Provides the return and parameter types of a
612 // built-in overload candidate. Only valid when Function is NULL.
615 QualType ParamTypes[3];
618 /// Surrogate - The conversion function for which this candidate
619 /// is a surrogate, but only if IsSurrogate is true.
620 CXXConversionDecl *Surrogate;
622 /// Conversions - The conversion sequences used to convert the
623 /// function arguments to the function parameters, the pointer points to a
624 /// fixed size array with NumConversions elements. The memory is owned by
625 /// the OverloadCandidateSet.
626 ImplicitConversionSequence *Conversions;
628 /// The FixIt hints which can be used to fix the Bad candidate.
629 ConversionFixItGenerator Fix;
631 /// NumConversions - The number of elements in the Conversions array.
632 unsigned NumConversions;
634 /// Viable - True to indicate that this overload candidate is viable.
637 /// IsSurrogate - True to indicate that this candidate is a
638 /// surrogate for a conversion to a function pointer or reference
639 /// (C++ [over.call.object]).
642 /// IgnoreObjectArgument - True to indicate that the first
643 /// argument's conversion, which for this function represents the
644 /// implicit object argument, should be ignored. This will be true
645 /// when the candidate is a static member function (where the
646 /// implicit object argument is just a placeholder) or a
647 /// non-static member function when the call doesn't have an
649 bool IgnoreObjectArgument;
651 /// FailureKind - The reason why this candidate is not viable.
652 /// Actually an OverloadFailureKind.
653 unsigned char FailureKind;
655 /// \brief The number of call arguments that were explicitly provided,
656 /// to be used while performing partial ordering of function templates.
657 unsigned ExplicitCallArguments;
659 /// A structure used to record information about a failed
660 /// template argument deduction.
661 struct DeductionFailureInfo {
662 /// A Sema::TemplateDeductionResult.
665 /// \brief Indicates whether a diagnostic is stored in Diagnostic.
666 unsigned HasDiagnostic : 1;
668 /// \brief Opaque pointer containing additional data about
669 /// this deduction failure.
672 /// \brief A diagnostic indicating why deduction failed.
675 char Diagnostic[sizeof(PartialDiagnosticAt)];
678 /// \brief Retrieve the diagnostic which caused this deduction failure,
680 PartialDiagnosticAt *getSFINAEDiagnostic();
682 /// \brief Retrieve the template parameter this deduction failure
683 /// refers to, if any.
684 TemplateParameter getTemplateParameter();
686 /// \brief Retrieve the template argument list associated with this
687 /// deduction failure, if any.
688 TemplateArgumentList *getTemplateArgumentList();
690 /// \brief Return the first template argument this deduction failure
691 /// refers to, if any.
692 const TemplateArgument *getFirstArg();
694 /// \brief Return the second template argument this deduction failure
695 /// refers to, if any.
696 const TemplateArgument *getSecondArg();
698 /// \brief Free any memory associated with this deduction failure.
703 DeductionFailureInfo DeductionFailure;
705 /// FinalConversion - For a conversion function (where Function is
706 /// a CXXConversionDecl), the standard conversion that occurs
707 /// after the call to the overload candidate to convert the result
708 /// of calling the conversion function to the required type.
709 StandardConversionSequence FinalConversion;
712 /// hasAmbiguousConversion - Returns whether this overload
713 /// candidate requires an ambiguous conversion or not.
714 bool hasAmbiguousConversion() const {
715 for (unsigned i = 0, e = NumConversions; i != e; ++i) {
716 if (!Conversions[i].isInitialized()) return false;
717 if (Conversions[i].isAmbiguous()) return true;
722 bool TryToFixBadConversion(unsigned Idx, Sema &S) {
723 bool CanFix = Fix.tryToFixConversion(
724 Conversions[Idx].Bad.FromExpr,
725 Conversions[Idx].Bad.getFromType(),
726 Conversions[Idx].Bad.getToType(), S);
728 // If at least one conversion fails, the candidate cannot be fixed.
736 /// OverloadCandidateSet - A set of overload candidates, used in C++
737 /// overload resolution (C++ 13.3).
738 class OverloadCandidateSet {
739 SmallVector<OverloadCandidate, 16> Candidates;
740 llvm::SmallPtrSet<Decl *, 16> Functions;
742 // Allocator for OverloadCandidate::Conversions. We store the first few
743 // elements inline to avoid allocation for small sets.
744 llvm::BumpPtrAllocator ConversionSequenceAllocator;
748 unsigned NumInlineSequences;
749 char InlineSpace[16 * sizeof(ImplicitConversionSequence)];
751 OverloadCandidateSet(const OverloadCandidateSet &) LLVM_DELETED_FUNCTION;
752 void operator=(const OverloadCandidateSet &) LLVM_DELETED_FUNCTION;
754 void destroyCandidates();
757 OverloadCandidateSet(SourceLocation Loc) : Loc(Loc), NumInlineSequences(0){}
758 ~OverloadCandidateSet() { destroyCandidates(); }
760 SourceLocation getLocation() const { return Loc; }
762 /// \brief Determine when this overload candidate will be new to the
764 bool isNewCandidate(Decl *F) {
765 return Functions.insert(F->getCanonicalDecl());
768 /// \brief Clear out all of the candidates.
771 typedef SmallVector<OverloadCandidate, 16>::iterator iterator;
772 iterator begin() { return Candidates.begin(); }
773 iterator end() { return Candidates.end(); }
775 size_t size() const { return Candidates.size(); }
776 bool empty() const { return Candidates.empty(); }
778 /// \brief Add a new candidate with NumConversions conversion sequence slots
779 /// to the overload set.
780 OverloadCandidate &addCandidate(unsigned NumConversions = 0) {
781 Candidates.push_back(OverloadCandidate());
782 OverloadCandidate &C = Candidates.back();
784 // Assign space from the inline array if there are enough free slots
786 if (NumConversions + NumInlineSequences <= 16) {
787 ImplicitConversionSequence *I =
788 (ImplicitConversionSequence*)InlineSpace;
789 C.Conversions = &I[NumInlineSequences];
790 NumInlineSequences += NumConversions;
792 // Otherwise get memory from the allocator.
793 C.Conversions = ConversionSequenceAllocator
794 .Allocate<ImplicitConversionSequence>(NumConversions);
797 // Construct the new objects.
798 for (unsigned i = 0; i != NumConversions; ++i)
799 new (&C.Conversions[i]) ImplicitConversionSequence();
801 C.NumConversions = NumConversions;
805 /// Find the best viable function on this overload set, if it exists.
806 OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
807 OverloadCandidateSet::iterator& Best,
808 bool UserDefinedConversion = false);
810 void NoteCandidates(Sema &S,
811 OverloadCandidateDisplayKind OCD,
812 llvm::ArrayRef<Expr *> Args,
814 SourceLocation Loc = SourceLocation());
817 bool isBetterOverloadCandidate(Sema &S,
818 const OverloadCandidate& Cand1,
819 const OverloadCandidate& Cand2,
821 bool UserDefinedConversion = false);
822 } // end namespace clang
824 #endif // LLVM_CLANG_SEMA_OVERLOAD_H