Overload.h revision 09dd3798e100ace40defdc5541277502346213f2
1//===--- Overload.h - C++ Overloading ---------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the data structures and types used in C++
11// overload resolution.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_SEMA_OVERLOAD_H
16#define LLVM_CLANG_SEMA_OVERLOAD_H
17
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"
28
29namespace clang {
30  class ASTContext;
31  class CXXConstructorDecl;
32  class CXXConversionDecl;
33  class FunctionDecl;
34  class Sema;
35
36  /// OverloadingResult - Capture the result of performing overload
37  /// resolution.
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.
43  };
44
45  enum OverloadCandidateDisplayKind {
46    /// Requests that all candidates be shown.  Viable candidates will
47    /// be printed first.
48    OCD_AllCandidates,
49
50    /// Requests that only viable candidates be shown.
51    OCD_ViableCandidates
52  };
53
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
84  };
85
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
96  };
97
98  ImplicitConversionCategory
99  GetConversionCategory(ImplicitConversionKind Kind);
100
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
111  };
112
113  ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
114
115  /// StandardConversionSequence - represents a standard conversion
116  /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
117  /// contains between zero and three conversions. If a particular
118  /// conversion is not needed, it will be set to the identity conversion
119  /// (ICK_Identity). Note that the three conversions are
120  /// specified as separate members (rather than in an array) so that
121  /// we can keep the size of a standard conversion sequence to a
122  /// single word.
123  class StandardConversionSequence {
124  public:
125    /// First -- The first conversion can be an lvalue-to-rvalue
126    /// conversion, array-to-pointer conversion, or
127    /// function-to-pointer conversion.
128    ImplicitConversionKind First : 8;
129
130    /// Second - The second conversion can be an integral promotion,
131    /// floating point promotion, integral conversion, floating point
132    /// conversion, floating-integral conversion, pointer conversion,
133    /// pointer-to-member conversion, or boolean conversion.
134    ImplicitConversionKind Second : 8;
135
136    /// Third - The third conversion can be a qualification conversion.
137    ImplicitConversionKind Third : 8;
138
139    /// \brief Whether this is the deprecated conversion of a
140    /// string literal to a pointer to non-const character data
141    /// (C++ 4.2p2).
142    unsigned DeprecatedStringLiteralToCharPtr : 1;
143
144    /// \brief Whether the qualification conversion involves a change in the
145    /// Objective-C lifetime (for automatic reference counting).
146    unsigned QualificationIncludesObjCLifetime : 1;
147
148    /// IncompatibleObjC - Whether this is an Objective-C conversion
149    /// that we should warn about (if we actually use it).
150    unsigned IncompatibleObjC : 1;
151
152    /// ReferenceBinding - True when this is a reference binding
153    /// (C++ [over.ics.ref]).
154    unsigned ReferenceBinding : 1;
155
156    /// DirectBinding - True when this is a reference binding that is a
157    /// direct binding (C++ [dcl.init.ref]).
158    unsigned DirectBinding : 1;
159
160    /// \brief Whether this is an lvalue reference binding (otherwise, it's
161    /// an rvalue reference binding).
162    unsigned IsLvalueReference : 1;
163
164    /// \brief Whether we're binding to a function lvalue.
165    unsigned BindsToFunctionLvalue : 1;
166
167    /// \brief Whether we're binding to an rvalue.
168    unsigned BindsToRvalue : 1;
169
170    /// \brief Whether this binds an implicit object argument to a
171    /// non-static member function without a ref-qualifier.
172    unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
173
174    /// \brief Whether this binds a reference to an object with a different
175    /// Objective-C lifetime qualifier.
176    unsigned ObjCLifetimeConversionBinding : 1;
177
178    /// FromType - The type that this conversion is converting
179    /// from. This is an opaque pointer that can be translated into a
180    /// QualType.
181    void *FromTypePtr;
182
183    /// ToType - The types that this conversion is converting to in
184    /// each step. This is an opaque pointer that can be translated
185    /// into a QualType.
186    void *ToTypePtrs[3];
187
188    /// CopyConstructor - The copy constructor that is used to perform
189    /// this conversion, when the conversion is actually just the
190    /// initialization of an object via copy constructor. Such
191    /// conversions are either identity conversions or derived-to-base
192    /// conversions.
193    CXXConstructorDecl *CopyConstructor;
194
195    void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
196    void setToType(unsigned Idx, QualType T) {
197      assert(Idx < 3 && "To type index is out of range");
198      ToTypePtrs[Idx] = T.getAsOpaquePtr();
199    }
200    void setAllToTypes(QualType T) {
201      ToTypePtrs[0] = T.getAsOpaquePtr();
202      ToTypePtrs[1] = ToTypePtrs[0];
203      ToTypePtrs[2] = ToTypePtrs[0];
204    }
205
206    QualType getFromType() const {
207      return QualType::getFromOpaquePtr(FromTypePtr);
208    }
209    QualType getToType(unsigned Idx) const {
210      assert(Idx < 3 && "To type index is out of range");
211      return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
212    }
213
214    void setAsIdentityConversion();
215
216    bool isIdentityConversion() const {
217      return Second == ICK_Identity && Third == ICK_Identity;
218    }
219
220    ImplicitConversionRank getRank() const;
221    bool isPointerConversionToBool() const;
222    bool isPointerConversionToVoidPointer(ASTContext& Context) const;
223    void DebugPrint() const;
224  };
225
226  /// UserDefinedConversionSequence - Represents a user-defined
227  /// conversion sequence (C++ 13.3.3.1.2).
228  struct UserDefinedConversionSequence {
229    /// \brief Represents the standard conversion that occurs before
230    /// the actual user-defined conversion.
231    ///
232    /// C++11 13.3.3.1.2p1:
233    ///   If the user-defined conversion is specified by a constructor
234    ///   (12.3.1), the initial standard conversion sequence converts
235    ///   the source type to the type required by the argument of the
236    ///   constructor. If the user-defined conversion is specified by
237    ///   a conversion function (12.3.2), the initial standard
238    ///   conversion sequence converts the source type to the implicit
239    ///   object parameter of the conversion function.
240    StandardConversionSequence Before;
241
242    /// EllipsisConversion - When this is true, it means user-defined
243    /// conversion sequence starts with a ... (elipsis) conversion, instead of
244    /// a standard conversion. In this case, 'Before' field must be ignored.
245    // FIXME. I much rather put this as the first field. But there seems to be
246    // a gcc code gen. bug which causes a crash in a test. Putting it here seems
247    // to work around the crash.
248    bool EllipsisConversion : 1;
249
250    /// HadMultipleCandidates - When this is true, it means that the
251    /// conversion function was resolved from an overloaded set having
252    /// size greater than 1.
253    bool HadMultipleCandidates : 1;
254
255    /// After - Represents the standard conversion that occurs after
256    /// the actual user-defined conversion.
257    StandardConversionSequence After;
258
259    /// ConversionFunction - The function that will perform the
260    /// user-defined conversion. Null if the conversion is an
261    /// aggregate initialization from an initializer list.
262    FunctionDecl* ConversionFunction;
263
264    /// \brief The declaration that we found via name lookup, which might be
265    /// the same as \c ConversionFunction or it might be a using declaration
266    /// that refers to \c ConversionFunction.
267    DeclAccessPair FoundConversionFunction;
268
269    void DebugPrint() const;
270  };
271
272  /// Represents an ambiguous user-defined conversion sequence.
273  struct AmbiguousConversionSequence {
274    typedef SmallVector<FunctionDecl*, 4> ConversionSet;
275
276    void *FromTypePtr;
277    void *ToTypePtr;
278    char Buffer[sizeof(ConversionSet)];
279
280    QualType getFromType() const {
281      return QualType::getFromOpaquePtr(FromTypePtr);
282    }
283    QualType getToType() const {
284      return QualType::getFromOpaquePtr(ToTypePtr);
285    }
286    void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
287    void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
288
289    ConversionSet &conversions() {
290      return *reinterpret_cast<ConversionSet*>(Buffer);
291    }
292
293    const ConversionSet &conversions() const {
294      return *reinterpret_cast<const ConversionSet*>(Buffer);
295    }
296
297    void addConversion(FunctionDecl *D) {
298      conversions().push_back(D);
299    }
300
301    typedef ConversionSet::iterator iterator;
302    iterator begin() { return conversions().begin(); }
303    iterator end() { return conversions().end(); }
304
305    typedef ConversionSet::const_iterator const_iterator;
306    const_iterator begin() const { return conversions().begin(); }
307    const_iterator end() const { return conversions().end(); }
308
309    void construct();
310    void destruct();
311    void copyFrom(const AmbiguousConversionSequence &);
312  };
313
314  /// BadConversionSequence - Records information about an invalid
315  /// conversion sequence.
316  struct BadConversionSequence {
317    enum FailureKind {
318      no_conversion,
319      unrelated_class,
320      suppressed_user,
321      bad_qualifiers,
322      lvalue_ref_to_rvalue,
323      rvalue_ref_to_lvalue
324    };
325
326    // This can be null, e.g. for implicit object arguments.
327    Expr *FromExpr;
328
329    FailureKind Kind;
330
331  private:
332    // The type we're converting from (an opaque QualType).
333    void *FromTy;
334
335    // The type we're converting to (an opaque QualType).
336    void *ToTy;
337
338  public:
339    void init(FailureKind K, Expr *From, QualType To) {
340      init(K, From->getType(), To);
341      FromExpr = From;
342    }
343    void init(FailureKind K, QualType From, QualType To) {
344      Kind = K;
345      FromExpr = 0;
346      setFromType(From);
347      setToType(To);
348    }
349
350    QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
351    QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
352
353    void setFromExpr(Expr *E) {
354      FromExpr = E;
355      setFromType(E->getType());
356    }
357    void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
358    void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
359  };
360
361  /// ImplicitConversionSequence - Represents an implicit conversion
362  /// sequence, which may be a standard conversion sequence
363  /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
364  /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
365  class ImplicitConversionSequence {
366  public:
367    /// Kind - The kind of implicit conversion sequence. BadConversion
368    /// specifies that there is no conversion from the source type to
369    /// the target type.  AmbiguousConversion represents the unique
370    /// ambiguous conversion (C++0x [over.best.ics]p10).
371    enum Kind {
372      StandardConversion = 0,
373      UserDefinedConversion,
374      AmbiguousConversion,
375      EllipsisConversion,
376      BadConversion
377    };
378
379  private:
380    enum {
381      Uninitialized = BadConversion + 1
382    };
383
384    /// ConversionKind - The kind of implicit conversion sequence.
385    unsigned ConversionKind : 31;
386
387    /// \brief Whether the argument is an initializer list.
388    bool ListInitializationSequence : 1;
389
390    void setKind(Kind K) {
391      destruct();
392      ConversionKind = K;
393    }
394
395    void destruct() {
396      if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
397    }
398
399  public:
400    union {
401      /// When ConversionKind == StandardConversion, provides the
402      /// details of the standard conversion sequence.
403      StandardConversionSequence Standard;
404
405      /// When ConversionKind == UserDefinedConversion, provides the
406      /// details of the user-defined conversion sequence.
407      UserDefinedConversionSequence UserDefined;
408
409      /// When ConversionKind == AmbiguousConversion, provides the
410      /// details of the ambiguous conversion.
411      AmbiguousConversionSequence Ambiguous;
412
413      /// When ConversionKind == BadConversion, provides the details
414      /// of the bad conversion.
415      BadConversionSequence Bad;
416    };
417
418    ImplicitConversionSequence()
419      : ConversionKind(Uninitialized), ListInitializationSequence(false) {}
420    ~ImplicitConversionSequence() {
421      destruct();
422    }
423    ImplicitConversionSequence(const ImplicitConversionSequence &Other)
424      : ConversionKind(Other.ConversionKind),
425        ListInitializationSequence(Other.ListInitializationSequence)
426    {
427      switch (ConversionKind) {
428      case Uninitialized: break;
429      case StandardConversion: Standard = Other.Standard; break;
430      case UserDefinedConversion: UserDefined = Other.UserDefined; break;
431      case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
432      case EllipsisConversion: break;
433      case BadConversion: Bad = Other.Bad; break;
434      }
435    }
436
437    ImplicitConversionSequence &
438        operator=(const ImplicitConversionSequence &Other) {
439      destruct();
440      new (this) ImplicitConversionSequence(Other);
441      return *this;
442    }
443
444    Kind getKind() const {
445      assert(isInitialized() && "querying uninitialized conversion");
446      return Kind(ConversionKind);
447    }
448
449    /// \brief Return a ranking of the implicit conversion sequence
450    /// kind, where smaller ranks represent better conversion
451    /// sequences.
452    ///
453    /// In particular, this routine gives user-defined conversion
454    /// sequences and ambiguous conversion sequences the same rank,
455    /// per C++ [over.best.ics]p10.
456    unsigned getKindRank() const {
457      switch (getKind()) {
458      case StandardConversion:
459        return 0;
460
461      case UserDefinedConversion:
462      case AmbiguousConversion:
463        return 1;
464
465      case EllipsisConversion:
466        return 2;
467
468      case BadConversion:
469        return 3;
470      }
471
472      return 3;
473    }
474
475    bool isBad() const { return getKind() == BadConversion; }
476    bool isStandard() const { return getKind() == StandardConversion; }
477    bool isEllipsis() const { return getKind() == EllipsisConversion; }
478    bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
479    bool isUserDefined() const { return getKind() == UserDefinedConversion; }
480    bool isFailure() const { return isBad() || isAmbiguous(); }
481
482    /// Determines whether this conversion sequence has been
483    /// initialized.  Most operations should never need to query
484    /// uninitialized conversions and should assert as above.
485    bool isInitialized() const { return ConversionKind != Uninitialized; }
486
487    /// Sets this sequence as a bad conversion for an explicit argument.
488    void setBad(BadConversionSequence::FailureKind Failure,
489                Expr *FromExpr, QualType ToType) {
490      setKind(BadConversion);
491      Bad.init(Failure, FromExpr, ToType);
492    }
493
494    /// Sets this sequence as a bad conversion for an implicit argument.
495    void setBad(BadConversionSequence::FailureKind Failure,
496                QualType FromType, QualType ToType) {
497      setKind(BadConversion);
498      Bad.init(Failure, FromType, ToType);
499    }
500
501    void setStandard() { setKind(StandardConversion); }
502    void setEllipsis() { setKind(EllipsisConversion); }
503    void setUserDefined() { setKind(UserDefinedConversion); }
504    void setAmbiguous() {
505      if (ConversionKind == AmbiguousConversion) return;
506      ConversionKind = AmbiguousConversion;
507      Ambiguous.construct();
508    }
509
510    /// \brief Whether this sequence was created by the rules of
511    /// list-initialization sequences.
512    bool isListInitializationSequence() const {
513      return ListInitializationSequence;
514    }
515
516    void setListInitializationSequence() {
517      ListInitializationSequence = true;
518    }
519
520    // The result of a comparison between implicit conversion
521    // sequences. Use Sema::CompareImplicitConversionSequences to
522    // actually perform the comparison.
523    enum CompareKind {
524      Better = -1,
525      Indistinguishable = 0,
526      Worse = 1
527    };
528
529    void DiagnoseAmbiguousConversion(Sema &S,
530                                     SourceLocation CaretLoc,
531                                     const PartialDiagnostic &PDiag) const;
532
533    void DebugPrint() const;
534  };
535
536  enum OverloadFailureKind {
537    ovl_fail_too_many_arguments,
538    ovl_fail_too_few_arguments,
539    ovl_fail_bad_conversion,
540    ovl_fail_bad_deduction,
541
542    /// This conversion candidate was not considered because it
543    /// duplicates the work of a trivial or derived-to-base
544    /// conversion.
545    ovl_fail_trivial_conversion,
546
547    /// This conversion candidate is not viable because its result
548    /// type is not implicitly convertible to the desired type.
549    ovl_fail_bad_final_conversion,
550
551    /// This conversion function template specialization candidate is not
552    /// viable because the final conversion was not an exact match.
553    ovl_fail_final_conversion_not_exact,
554
555    /// (CUDA) This candidate was not viable because the callee
556    /// was not accessible from the caller's target (i.e. host->device,
557    /// global->host, device->host).
558    ovl_fail_bad_target
559  };
560
561  /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
562  struct OverloadCandidate {
563    /// Function - The actual function that this candidate
564    /// represents. When NULL, this is a built-in candidate
565    /// (C++ [over.oper]) or a surrogate for a conversion to a
566    /// function pointer or reference (C++ [over.call.object]).
567    FunctionDecl *Function;
568
569    /// FoundDecl - The original declaration that was looked up /
570    /// invented / otherwise found, together with its access.
571    /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
572    DeclAccessPair FoundDecl;
573
574    // BuiltinTypes - Provides the return and parameter types of a
575    // built-in overload candidate. Only valid when Function is NULL.
576    struct {
577      QualType ResultTy;
578      QualType ParamTypes[3];
579    } BuiltinTypes;
580
581    /// Surrogate - The conversion function for which this candidate
582    /// is a surrogate, but only if IsSurrogate is true.
583    CXXConversionDecl *Surrogate;
584
585    /// Conversions - The conversion sequences used to convert the
586    /// function arguments to the function parameters, the pointer points to a
587    /// fixed size array with NumConversions elements. The memory is owned by
588    /// the OverloadCandidateSet.
589    ImplicitConversionSequence *Conversions;
590
591    /// The FixIt hints which can be used to fix the Bad candidate.
592    ConversionFixItGenerator Fix;
593
594    /// NumConversions - The number of elements in the Conversions array.
595    unsigned NumConversions;
596
597    /// Viable - True to indicate that this overload candidate is viable.
598    bool Viable;
599
600    /// IsSurrogate - True to indicate that this candidate is a
601    /// surrogate for a conversion to a function pointer or reference
602    /// (C++ [over.call.object]).
603    bool IsSurrogate;
604
605    /// IgnoreObjectArgument - True to indicate that the first
606    /// argument's conversion, which for this function represents the
607    /// implicit object argument, should be ignored. This will be true
608    /// when the candidate is a static member function (where the
609    /// implicit object argument is just a placeholder) or a
610    /// non-static member function when the call doesn't have an
611    /// object argument.
612    bool IgnoreObjectArgument;
613
614    /// FailureKind - The reason why this candidate is not viable.
615    /// Actually an OverloadFailureKind.
616    unsigned char FailureKind;
617
618    /// \brief The number of call arguments that were explicitly provided,
619    /// to be used while performing partial ordering of function templates.
620    unsigned ExplicitCallArguments;
621
622    /// A structure used to record information about a failed
623    /// template argument deduction.
624    struct DeductionFailureInfo {
625      // A Sema::TemplateDeductionResult.
626      unsigned Result;
627
628      /// \brief Opaque pointer containing additional data about
629      /// this deduction failure.
630      void *Data;
631
632      /// \brief Retrieve the template parameter this deduction failure
633      /// refers to, if any.
634      TemplateParameter getTemplateParameter();
635
636      /// \brief Retrieve the template argument list associated with this
637      /// deduction failure, if any.
638      TemplateArgumentList *getTemplateArgumentList();
639
640      /// \brief Return the first template argument this deduction failure
641      /// refers to, if any.
642      const TemplateArgument *getFirstArg();
643
644      /// \brief Return the second template argument this deduction failure
645      /// refers to, if any.
646      const TemplateArgument *getSecondArg();
647
648      /// \brief Free any memory associated with this deduction failure.
649      void Destroy();
650    };
651
652    union {
653      DeductionFailureInfo DeductionFailure;
654
655      /// FinalConversion - For a conversion function (where Function is
656      /// a CXXConversionDecl), the standard conversion that occurs
657      /// after the call to the overload candidate to convert the result
658      /// of calling the conversion function to the required type.
659      StandardConversionSequence FinalConversion;
660    };
661
662    ~OverloadCandidate() {
663      for (unsigned i = 0, e = NumConversions; i != e; ++i)
664        Conversions[i].~ImplicitConversionSequence();
665    }
666
667    /// hasAmbiguousConversion - Returns whether this overload
668    /// candidate requires an ambiguous conversion or not.
669    bool hasAmbiguousConversion() const {
670      for (unsigned i = 0, e = NumConversions; i != e; ++i) {
671        if (!Conversions[i].isInitialized()) return false;
672        if (Conversions[i].isAmbiguous()) return true;
673      }
674      return false;
675    }
676
677    bool TryToFixBadConversion(unsigned Idx, Sema &S) {
678      bool CanFix = Fix.tryToFixConversion(
679                      Conversions[Idx].Bad.FromExpr,
680                      Conversions[Idx].Bad.getFromType(),
681                      Conversions[Idx].Bad.getToType(), S);
682
683      // If at least one conversion fails, the candidate cannot be fixed.
684      if (!CanFix)
685        Fix.clear();
686
687      return CanFix;
688    }
689  };
690
691  /// OverloadCandidateSet - A set of overload candidates, used in C++
692  /// overload resolution (C++ 13.3).
693  class OverloadCandidateSet {
694    SmallVector<OverloadCandidate, 16> Candidates;
695    llvm::SmallPtrSet<Decl *, 16> Functions;
696
697    // Allocator for OverloadCandidate::Conversions. We store the first few
698    // elements inline to avoid allocation for small sets.
699    llvm::BumpPtrAllocator ConversionSequenceAllocator;
700    size_t InlineSpace[16*sizeof(ImplicitConversionSequence) / sizeof(size_t)];
701    unsigned NumInlineSequences;
702
703    SourceLocation Loc;
704
705    OverloadCandidateSet(const OverloadCandidateSet &);
706    OverloadCandidateSet &operator=(const OverloadCandidateSet &);
707
708  public:
709    OverloadCandidateSet(SourceLocation Loc) : NumInlineSequences(0), Loc(Loc){}
710
711    SourceLocation getLocation() const { return Loc; }
712
713    /// \brief Determine when this overload candidate will be new to the
714    /// overload set.
715    bool isNewCandidate(Decl *F) {
716      return Functions.insert(F->getCanonicalDecl());
717    }
718
719    /// \brief Clear out all of the candidates.
720    void clear();
721
722    typedef SmallVector<OverloadCandidate, 16>::iterator iterator;
723    iterator begin() { return Candidates.begin(); }
724    iterator end() { return Candidates.end(); }
725
726    size_t size() const { return Candidates.size(); }
727    bool empty() const { return Candidates.empty(); }
728
729    /// \brief Add a new candidate with NumConversions conversion sequence slots
730    /// to the overload set.
731    OverloadCandidate &addCandidate(unsigned NumConversions = 0) {
732      Candidates.push_back(OverloadCandidate());
733      OverloadCandidate &C = Candidates.back();
734
735      // Assign space from the inline array if there are enough free slots
736      // available.
737      if (NumConversions + NumInlineSequences < 16) {
738        ImplicitConversionSequence *I =
739          (ImplicitConversionSequence*)InlineSpace;
740        C.Conversions = &I[NumInlineSequences];
741        NumInlineSequences += NumConversions;
742      } else {
743        // Otherwise get memory from the allocator.
744        C.Conversions = ConversionSequenceAllocator
745                          .Allocate<ImplicitConversionSequence>(NumConversions);
746      }
747
748      // Construct the new objects.
749      for (unsigned i = 0; i != NumConversions; ++i)
750        new (&C.Conversions[i]) ImplicitConversionSequence();
751
752      C.NumConversions = NumConversions;
753      return C;
754    }
755
756    /// Find the best viable function on this overload set, if it exists.
757    OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
758                                         OverloadCandidateSet::iterator& Best,
759                                         bool UserDefinedConversion = false);
760
761    void NoteCandidates(Sema &S,
762                        OverloadCandidateDisplayKind OCD,
763                        Expr **Args, unsigned NumArgs,
764                        const char *Opc = 0,
765                        SourceLocation Loc = SourceLocation());
766  };
767
768  bool isBetterOverloadCandidate(Sema &S,
769                                 const OverloadCandidate& Cand1,
770                                 const OverloadCandidate& Cand2,
771                                 SourceLocation Loc,
772                                 bool UserDefinedConversion = false);
773} // end namespace clang
774
775#endif // LLVM_CLANG_SEMA_OVERLOAD_H
776