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