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 "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
29 class CXXConstructorDecl;
30 class CXXConversionDecl;
34 /// OverloadingResult - Capture the result of performing overload
36 enum OverloadingResult {
37 OR_Success, ///< Overload resolution succeeded.
38 OR_No_Viable_Function, ///< No viable function found.
39 OR_Ambiguous, ///< Ambiguous candidates found.
40 OR_Deleted ///< Succeeded, but refers to a deleted function.
43 enum OverloadCandidateDisplayKind {
44 /// Requests that all candidates be shown. Viable candidates will
48 /// Requests that only viable candidates be shown.
52 /// ImplicitConversionKind - The kind of implicit conversion used to
53 /// convert an argument to a parameter's type. The enumerator values
54 /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that
55 /// better conversion kinds have smaller values.
56 enum ImplicitConversionKind {
57 ICK_Identity = 0, ///< Identity conversion (no conversion)
58 ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1)
59 ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2)
60 ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3)
61 ICK_NoReturn_Adjustment, ///< Removal of noreturn from a type (Clang)
62 ICK_Qualification, ///< Qualification conversions (C++ 4.4)
63 ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5)
64 ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6)
65 ICK_Complex_Promotion, ///< Complex promotions (Clang extension)
66 ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7)
67 ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8)
68 ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6)
69 ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9)
70 ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10)
71 ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11)
72 ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12)
73 ICK_Compatible_Conversion, ///< Conversions between compatible types in C99
74 ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics])
75 ICK_Vector_Conversion, ///< Vector conversions
76 ICK_Vector_Splat, ///< A vector splat from an arithmetic type
77 ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7)
78 ICK_Block_Pointer_Conversion, ///< Block Pointer conversions
79 ICK_TransparentUnionConversion, /// Transparent Union Conversions
80 ICK_Num_Conversion_Kinds ///< The number of conversion kinds
83 /// ImplicitConversionCategory - The category of an implicit
84 /// conversion kind. The enumerator values match with Table 9 of
85 /// (C++ 13.3.3.1.1) and are listed such that better conversion
86 /// categories have smaller values.
87 enum ImplicitConversionCategory {
88 ICC_Identity = 0, ///< Identity
89 ICC_Lvalue_Transformation, ///< Lvalue transformation
90 ICC_Qualification_Adjustment, ///< Qualification adjustment
91 ICC_Promotion, ///< Promotion
92 ICC_Conversion ///< Conversion
95 ImplicitConversionCategory
96 GetConversionCategory(ImplicitConversionKind Kind);
98 /// ImplicitConversionRank - The rank of an implicit conversion
99 /// kind. The enumerator values match with Table 9 of (C++
100 /// 13.3.3.1.1) and are listed such that better conversion ranks
101 /// have smaller values.
102 enum ImplicitConversionRank {
103 ICR_Exact_Match = 0, ///< Exact Match
104 ICR_Promotion, ///< Promotion
105 ICR_Conversion, ///< Conversion
106 ICR_Complex_Real_Conversion ///< Complex <-> Real conversion
109 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
111 /// StandardConversionSequence - represents a standard conversion
112 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
113 /// contains between zero and three conversions. If a particular
114 /// conversion is not needed, it will be set to the identity conversion
115 /// (ICK_Identity). Note that the three conversions are
116 /// specified as separate members (rather than in an array) so that
117 /// we can keep the size of a standard conversion sequence to a
119 class StandardConversionSequence {
121 /// First -- The first conversion can be an lvalue-to-rvalue
122 /// conversion, array-to-pointer conversion, or
123 /// function-to-pointer conversion.
124 ImplicitConversionKind First : 8;
126 /// Second - The second conversion can be an integral promotion,
127 /// floating point promotion, integral conversion, floating point
128 /// conversion, floating-integral conversion, pointer conversion,
129 /// pointer-to-member conversion, or boolean conversion.
130 ImplicitConversionKind Second : 8;
132 /// Third - The third conversion can be a qualification conversion.
133 ImplicitConversionKind Third : 8;
135 /// \brief Whether this is the deprecated conversion of a
136 /// string literal to a pointer to non-const character data
138 unsigned DeprecatedStringLiteralToCharPtr : 1;
140 /// IncompatibleObjC - Whether this is an Objective-C conversion
141 /// that we should warn about (if we actually use it).
142 unsigned IncompatibleObjC : 1;
144 /// ReferenceBinding - True when this is a reference binding
145 /// (C++ [over.ics.ref]).
146 unsigned ReferenceBinding : 1;
148 /// DirectBinding - True when this is a reference binding that is a
149 /// direct binding (C++ [dcl.init.ref]).
150 unsigned DirectBinding : 1;
152 /// \brief Whether this is an lvalue reference binding (otherwise, it's
153 /// an rvalue reference binding).
154 unsigned IsLvalueReference : 1;
156 /// \brief Whether we're binding to a function lvalue.
157 unsigned BindsToFunctionLvalue : 1;
159 /// \brief Whether we're binding to an rvalue.
160 unsigned BindsToRvalue : 1;
162 /// \brief Whether this binds an implicit object argument to a
163 /// non-static member function without a ref-qualifier.
164 unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
166 /// FromType - The type that this conversion is converting
167 /// from. This is an opaque pointer that can be translated into a
171 /// ToType - The types that this conversion is converting to in
172 /// each step. This is an opaque pointer that can be translated
176 /// CopyConstructor - The copy constructor that is used to perform
177 /// this conversion, when the conversion is actually just the
178 /// initialization of an object via copy constructor. Such
179 /// conversions are either identity conversions or derived-to-base
181 CXXConstructorDecl *CopyConstructor;
183 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
184 void setToType(unsigned Idx, QualType T) {
185 assert(Idx < 3 && "To type index is out of range");
186 ToTypePtrs[Idx] = T.getAsOpaquePtr();
188 void setAllToTypes(QualType T) {
189 ToTypePtrs[0] = T.getAsOpaquePtr();
190 ToTypePtrs[1] = ToTypePtrs[0];
191 ToTypePtrs[2] = ToTypePtrs[0];
194 QualType getFromType() const {
195 return QualType::getFromOpaquePtr(FromTypePtr);
197 QualType getToType(unsigned Idx) const {
198 assert(Idx < 3 && "To type index is out of range");
199 return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
202 void setAsIdentityConversion();
204 bool isIdentityConversion() const {
205 return Second == ICK_Identity && Third == ICK_Identity;
208 ImplicitConversionRank getRank() const;
209 bool isPointerConversionToBool() const;
210 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
211 void DebugPrint() const;
214 /// UserDefinedConversionSequence - Represents a user-defined
215 /// conversion sequence (C++ 13.3.3.1.2).
216 struct UserDefinedConversionSequence {
217 /// Before - Represents the standard conversion that occurs before
218 /// the actual user-defined conversion. (C++ 13.3.3.1.2p1):
220 /// If the user-defined conversion is specified by a constructor
221 /// (12.3.1), the initial standard conversion sequence converts
222 /// the source type to the type required by the argument of the
223 /// constructor. If the user-defined conversion is specified by
224 /// a conversion function (12.3.2), the initial standard
225 /// conversion sequence converts the source type to the implicit
226 /// object parameter of the conversion function.
227 StandardConversionSequence Before;
229 /// EllipsisConversion - When this is true, it means user-defined
230 /// conversion sequence starts with a ... (elipsis) conversion, instead of
231 /// a standard conversion. In this case, 'Before' field must be ignored.
232 // FIXME. I much rather put this as the first field. But there seems to be
233 // a gcc code gen. bug which causes a crash in a test. Putting it here seems
234 // to work around the crash.
235 bool EllipsisConversion : 1;
237 /// After - Represents the standard conversion that occurs after
238 /// the actual user-defined conversion.
239 StandardConversionSequence After;
241 /// ConversionFunction - The function that will perform the
242 /// user-defined conversion.
243 FunctionDecl* ConversionFunction;
245 /// \brief The declaration that we found via name lookup, which might be
246 /// the same as \c ConversionFunction or it might be a using declaration
247 /// that refers to \c ConversionFunction.
248 NamedDecl *FoundConversionFunction;
250 void DebugPrint() const;
253 /// Represents an ambiguous user-defined conversion sequence.
254 struct AmbiguousConversionSequence {
255 typedef llvm::SmallVector<FunctionDecl*, 4> ConversionSet;
259 char Buffer[sizeof(ConversionSet)];
261 QualType getFromType() const {
262 return QualType::getFromOpaquePtr(FromTypePtr);
264 QualType getToType() const {
265 return QualType::getFromOpaquePtr(ToTypePtr);
267 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
268 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
270 ConversionSet &conversions() {
271 return *reinterpret_cast<ConversionSet*>(Buffer);
274 const ConversionSet &conversions() const {
275 return *reinterpret_cast<const ConversionSet*>(Buffer);
278 void addConversion(FunctionDecl *D) {
279 conversions().push_back(D);
282 typedef ConversionSet::iterator iterator;
283 iterator begin() { return conversions().begin(); }
284 iterator end() { return conversions().end(); }
286 typedef ConversionSet::const_iterator const_iterator;
287 const_iterator begin() const { return conversions().begin(); }
288 const_iterator end() const { return conversions().end(); }
292 void copyFrom(const AmbiguousConversionSequence &);
295 /// BadConversionSequence - Records information about an invalid
296 /// conversion sequence.
297 struct BadConversionSequence {
303 lvalue_ref_to_rvalue,
307 // This can be null, e.g. for implicit object arguments.
313 // The type we're converting from (an opaque QualType).
316 // The type we're converting to (an opaque QualType).
320 void init(FailureKind K, Expr *From, QualType To) {
321 init(K, From->getType(), To);
324 void init(FailureKind K, QualType From, QualType To) {
331 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
332 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
334 void setFromExpr(Expr *E) {
336 setFromType(E->getType());
338 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
339 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
342 /// ImplicitConversionSequence - Represents an implicit conversion
343 /// sequence, which may be a standard conversion sequence
344 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
345 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
346 class ImplicitConversionSequence {
348 /// Kind - The kind of implicit conversion sequence. BadConversion
349 /// specifies that there is no conversion from the source type to
350 /// the target type. AmbiguousConversion represents the unique
351 /// ambiguous conversion (C++0x [over.best.ics]p10).
353 StandardConversion = 0,
354 UserDefinedConversion,
362 Uninitialized = BadConversion + 1
365 /// ConversionKind - The kind of implicit conversion sequence.
366 unsigned ConversionKind;
368 void setKind(Kind K) {
374 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
379 /// When ConversionKind == StandardConversion, provides the
380 /// details of the standard conversion sequence.
381 StandardConversionSequence Standard;
383 /// When ConversionKind == UserDefinedConversion, provides the
384 /// details of the user-defined conversion sequence.
385 UserDefinedConversionSequence UserDefined;
387 /// When ConversionKind == AmbiguousConversion, provides the
388 /// details of the ambiguous conversion.
389 AmbiguousConversionSequence Ambiguous;
391 /// When ConversionKind == BadConversion, provides the details
392 /// of the bad conversion.
393 BadConversionSequence Bad;
396 ImplicitConversionSequence() : ConversionKind(Uninitialized) {}
397 ~ImplicitConversionSequence() {
400 ImplicitConversionSequence(const ImplicitConversionSequence &Other)
401 : ConversionKind(Other.ConversionKind)
403 switch (ConversionKind) {
404 case Uninitialized: break;
405 case StandardConversion: Standard = Other.Standard; break;
406 case UserDefinedConversion: UserDefined = Other.UserDefined; break;
407 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
408 case EllipsisConversion: break;
409 case BadConversion: Bad = Other.Bad; break;
413 ImplicitConversionSequence &
414 operator=(const ImplicitConversionSequence &Other) {
416 new (this) ImplicitConversionSequence(Other);
420 Kind getKind() const {
421 assert(isInitialized() && "querying uninitialized conversion");
422 return Kind(ConversionKind);
425 /// \brief Return a ranking of the implicit conversion sequence
426 /// kind, where smaller ranks represent better conversion
429 /// In particular, this routine gives user-defined conversion
430 /// sequences and ambiguous conversion sequences the same rank,
431 /// per C++ [over.best.ics]p10.
432 unsigned getKindRank() const {
434 case StandardConversion:
437 case UserDefinedConversion:
438 case AmbiguousConversion:
441 case EllipsisConversion:
451 bool isBad() const { return getKind() == BadConversion; }
452 bool isStandard() const { return getKind() == StandardConversion; }
453 bool isEllipsis() const { return getKind() == EllipsisConversion; }
454 bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
455 bool isUserDefined() const { return getKind() == UserDefinedConversion; }
457 /// Determines whether this conversion sequence has been
458 /// initialized. Most operations should never need to query
459 /// uninitialized conversions and should assert as above.
460 bool isInitialized() const { return ConversionKind != Uninitialized; }
462 /// Sets this sequence as a bad conversion for an explicit argument.
463 void setBad(BadConversionSequence::FailureKind Failure,
464 Expr *FromExpr, QualType ToType) {
465 setKind(BadConversion);
466 Bad.init(Failure, FromExpr, ToType);
469 /// Sets this sequence as a bad conversion for an implicit argument.
470 void setBad(BadConversionSequence::FailureKind Failure,
471 QualType FromType, QualType ToType) {
472 setKind(BadConversion);
473 Bad.init(Failure, FromType, ToType);
476 void setStandard() { setKind(StandardConversion); }
477 void setEllipsis() { setKind(EllipsisConversion); }
478 void setUserDefined() { setKind(UserDefinedConversion); }
479 void setAmbiguous() {
480 if (ConversionKind == AmbiguousConversion) return;
481 ConversionKind = AmbiguousConversion;
482 Ambiguous.construct();
485 // The result of a comparison between implicit conversion
486 // sequences. Use Sema::CompareImplicitConversionSequences to
487 // actually perform the comparison.
490 Indistinguishable = 0,
494 void DiagnoseAmbiguousConversion(Sema &S,
495 SourceLocation CaretLoc,
496 const PartialDiagnostic &PDiag) const;
498 void DebugPrint() const;
501 enum OverloadFailureKind {
502 ovl_fail_too_many_arguments,
503 ovl_fail_too_few_arguments,
504 ovl_fail_bad_conversion,
505 ovl_fail_bad_deduction,
507 /// This conversion candidate was not considered because it
508 /// duplicates the work of a trivial or derived-to-base
510 ovl_fail_trivial_conversion,
512 /// This conversion candidate is not viable because its result
513 /// type is not implicitly convertible to the desired type.
514 ovl_fail_bad_final_conversion,
516 /// This conversion function template specialization candidate is not
517 /// viable because the final conversion was not an exact match.
518 ovl_fail_final_conversion_not_exact
521 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
522 struct OverloadCandidate {
523 /// Function - The actual function that this candidate
524 /// represents. When NULL, this is a built-in candidate
525 /// (C++ [over.oper]) or a surrogate for a conversion to a
526 /// function pointer or reference (C++ [over.call.object]).
527 FunctionDecl *Function;
529 /// FoundDecl - The original declaration that was looked up /
530 /// invented / otherwise found, together with its access.
531 /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
532 DeclAccessPair FoundDecl;
534 // BuiltinTypes - Provides the return and parameter types of a
535 // built-in overload candidate. Only valid when Function is NULL.
538 QualType ParamTypes[3];
541 /// Surrogate - The conversion function for which this candidate
542 /// is a surrogate, but only if IsSurrogate is true.
543 CXXConversionDecl *Surrogate;
545 /// Conversions - The conversion sequences used to convert the
546 /// function arguments to the function parameters.
547 llvm::SmallVector<ImplicitConversionSequence, 4> Conversions;
549 /// Viable - True to indicate that this overload candidate is viable.
552 /// IsSurrogate - True to indicate that this candidate is a
553 /// surrogate for a conversion to a function pointer or reference
554 /// (C++ [over.call.object]).
557 /// IgnoreObjectArgument - True to indicate that the first
558 /// argument's conversion, which for this function represents the
559 /// implicit object argument, should be ignored. This will be true
560 /// when the candidate is a static member function (where the
561 /// implicit object argument is just a placeholder) or a
562 /// non-static member function when the call doesn't have an
564 bool IgnoreObjectArgument;
566 /// FailureKind - The reason why this candidate is not viable.
567 /// Actually an OverloadFailureKind.
568 unsigned char FailureKind;
570 /// \brief The number of call arguments that were explicitly provided,
571 /// to be used while performing partial ordering of function templates.
572 unsigned ExplicitCallArguments;
574 /// A structure used to record information about a failed
575 /// template argument deduction.
576 struct DeductionFailureInfo {
577 // A Sema::TemplateDeductionResult.
580 /// \brief Opaque pointer containing additional data about
581 /// this deduction failure.
584 /// \brief Retrieve the template parameter this deduction failure
585 /// refers to, if any.
586 TemplateParameter getTemplateParameter();
588 /// \brief Retrieve the template argument list associated with this
589 /// deduction failure, if any.
590 TemplateArgumentList *getTemplateArgumentList();
592 /// \brief Return the first template argument this deduction failure
593 /// refers to, if any.
594 const TemplateArgument *getFirstArg();
596 /// \brief Return the second template argument this deduction failure
597 /// refers to, if any.
598 const TemplateArgument *getSecondArg();
600 /// \brief Free any memory associated with this deduction failure.
605 DeductionFailureInfo DeductionFailure;
607 /// FinalConversion - For a conversion function (where Function is
608 /// a CXXConversionDecl), the standard conversion that occurs
609 /// after the call to the overload candidate to convert the result
610 /// of calling the conversion function to the required type.
611 StandardConversionSequence FinalConversion;
614 /// hasAmbiguousConversion - Returns whether this overload
615 /// candidate requires an ambiguous conversion or not.
616 bool hasAmbiguousConversion() const {
617 for (llvm::SmallVectorImpl<ImplicitConversionSequence>::const_iterator
618 I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
619 if (!I->isInitialized()) return false;
620 if (I->isAmbiguous()) return true;
626 /// OverloadCandidateSet - A set of overload candidates, used in C++
627 /// overload resolution (C++ 13.3).
628 class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> {
629 typedef llvm::SmallVector<OverloadCandidate, 16> inherited;
630 llvm::SmallPtrSet<Decl *, 16> Functions;
634 OverloadCandidateSet(const OverloadCandidateSet &);
635 OverloadCandidateSet &operator=(const OverloadCandidateSet &);
638 OverloadCandidateSet(SourceLocation Loc) : Loc(Loc) {}
640 SourceLocation getLocation() const { return Loc; }
642 /// \brief Determine when this overload candidate will be new to the
644 bool isNewCandidate(Decl *F) {
645 return Functions.insert(F->getCanonicalDecl());
648 /// \brief Clear out all of the candidates.
651 /// Find the best viable function on this overload set, if it exists.
652 OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
653 OverloadCandidateSet::iterator& Best,
654 bool UserDefinedConversion = false);
656 void NoteCandidates(Sema &S,
657 OverloadCandidateDisplayKind OCD,
658 Expr **Args, unsigned NumArgs,
660 SourceLocation Loc = SourceLocation());
663 bool isBetterOverloadCandidate(Sema &S,
664 const OverloadCandidate& Cand1,
665 const OverloadCandidate& Cand2,
667 bool UserDefinedConversion = false);
668 } // end namespace clang
670 #endif // LLVM_CLANG_SEMA_OVERLOAD_H