blob: cf1694382613d8079b5c063cb4c8a318120bc53b [file] [log] [blame]
Douglas Gregor8e9bebd2008-10-21 16:13:35 +00001//===--- 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
Douglas Gregor20093b42009-12-09 23:02:17 +000018#include "clang/AST/Decl.h"
Douglas Gregora9333192010-05-08 17:41:32 +000019#include "clang/AST/DeclTemplate.h"
John McCalladbb8f82010-01-13 09:16:55 +000020#include "clang/AST/Expr.h"
Douglas Gregora9333192010-05-08 17:41:32 +000021#include "clang/AST/TemplateBase.h"
Douglas Gregor20093b42009-12-09 23:02:17 +000022#include "clang/AST/Type.h"
John McCall9aa472c2010-03-19 07:35:19 +000023#include "clang/AST/UnresolvedSet.h"
Douglas Gregor20093b42009-12-09 23:02:17 +000024#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000025#include "llvm/ADT/SmallVector.h"
26
27namespace clang {
Douglas Gregor20093b42009-12-09 23:02:17 +000028 class ASTContext;
Douglas Gregor225c41e2008-11-03 19:09:14 +000029 class CXXConstructorDecl;
Douglas Gregor20093b42009-12-09 23:02:17 +000030 class CXXConversionDecl;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000031 class FunctionDecl;
32
Douglas Gregor20093b42009-12-09 23:02:17 +000033 /// OverloadingResult - Capture the result of performing overload
34 /// resolution.
35 enum OverloadingResult {
36 OR_Success, ///< Overload resolution succeeded.
37 OR_No_Viable_Function, ///< No viable function found.
38 OR_Ambiguous, ///< Ambiguous candidates found.
Sean Huntc39f6972010-04-07 22:52:07 +000039 OR_Deleted ///< Succeeded, but refers to a deleted function.
Douglas Gregor20093b42009-12-09 23:02:17 +000040 };
41
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000042 /// ImplicitConversionKind - The kind of implicit conversion used to
43 /// convert an argument to a parameter's type. The enumerator values
44 /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that
45 /// better conversion kinds have smaller values.
46 enum ImplicitConversionKind {
Douglas Gregorf9201e02009-02-11 23:02:49 +000047 ICK_Identity = 0, ///< Identity conversion (no conversion)
48 ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1)
49 ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2)
50 ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3)
Douglas Gregor43c79c22009-12-09 00:47:37 +000051 ICK_NoReturn_Adjustment, ///< Removal of noreturn from a type (Clang)
Douglas Gregorf9201e02009-02-11 23:02:49 +000052 ICK_Qualification, ///< Qualification conversions (C++ 4.4)
53 ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5)
54 ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6)
Douglas Gregor5cdf8212009-02-12 00:15:05 +000055 ICK_Complex_Promotion, ///< Complex promotions (Clang extension)
Douglas Gregorf9201e02009-02-11 23:02:49 +000056 ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7)
57 ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8)
Douglas Gregor5cdf8212009-02-12 00:15:05 +000058 ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6)
Douglas Gregorf9201e02009-02-11 23:02:49 +000059 ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9)
60 ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10)
61 ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11)
62 ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12)
63 ICK_Compatible_Conversion, ///< Conversions between compatible types in C99
64 ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics])
Chandler Carruth23a370f2010-02-25 07:20:54 +000065 ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7)
Douglas Gregorf9201e02009-02-11 23:02:49 +000066 ICK_Num_Conversion_Kinds ///< The number of conversion kinds
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000067 };
68
69 /// ImplicitConversionCategory - The category of an implicit
70 /// conversion kind. The enumerator values match with Table 9 of
71 /// (C++ 13.3.3.1.1) and are listed such that better conversion
72 /// categories have smaller values.
73 enum ImplicitConversionCategory {
74 ICC_Identity = 0, ///< Identity
75 ICC_Lvalue_Transformation, ///< Lvalue transformation
76 ICC_Qualification_Adjustment, ///< Qualification adjustment
77 ICC_Promotion, ///< Promotion
78 ICC_Conversion ///< Conversion
79 };
80
Mike Stump1eb44332009-09-09 15:08:12 +000081 ImplicitConversionCategory
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000082 GetConversionCategory(ImplicitConversionKind Kind);
83
84 /// ImplicitConversionRank - The rank of an implicit conversion
85 /// kind. The enumerator values match with Table 9 of (C++
86 /// 13.3.3.1.1) and are listed such that better conversion ranks
87 /// have smaller values.
88 enum ImplicitConversionRank {
Chandler Carruth23a370f2010-02-25 07:20:54 +000089 ICR_Exact_Match = 0, ///< Exact Match
90 ICR_Promotion, ///< Promotion
91 ICR_Conversion, ///< Conversion
92 ICR_Complex_Real_Conversion ///< Complex <-> Real conversion
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000093 };
94
95 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
96
97 /// StandardConversionSequence - represents a standard conversion
98 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
99 /// contains between zero and three conversions. If a particular
100 /// conversion is not needed, it will be set to the identity conversion
101 /// (ICK_Identity). Note that the three conversions are
102 /// specified as separate members (rather than in an array) so that
103 /// we can keep the size of a standard conversion sequence to a
104 /// single word.
105 struct StandardConversionSequence {
106 /// First -- The first conversion can be an lvalue-to-rvalue
107 /// conversion, array-to-pointer conversion, or
108 /// function-to-pointer conversion.
109 ImplicitConversionKind First : 8;
110
111 /// Second - The second conversion can be an integral promotion,
112 /// floating point promotion, integral conversion, floating point
113 /// conversion, floating-integral conversion, pointer conversion,
114 /// pointer-to-member conversion, or boolean conversion.
115 ImplicitConversionKind Second : 8;
116
117 /// Third - The third conversion can be a qualification conversion.
118 ImplicitConversionKind Third : 8;
119
Douglas Gregor15da57e2008-10-29 02:00:59 +0000120 /// Deprecated - Whether this the deprecated conversion of a
Mike Stump1eb44332009-09-09 15:08:12 +0000121 /// string literal to a pointer to non-const character data
Douglas Gregor15da57e2008-10-29 02:00:59 +0000122 /// (C++ 4.2p2).
Douglas Gregora9bff302010-02-28 18:30:25 +0000123 bool DeprecatedStringLiteralToCharPtr : 1;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000124
Douglas Gregor45920e82008-12-19 17:40:08 +0000125 /// IncompatibleObjC - Whether this is an Objective-C conversion
126 /// that we should warn about (if we actually use it).
127 bool IncompatibleObjC : 1;
128
Mike Stump1eb44332009-09-09 15:08:12 +0000129 /// ReferenceBinding - True when this is a reference binding
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000130 /// (C++ [over.ics.ref]).
131 bool ReferenceBinding : 1;
132
Mike Stump1eb44332009-09-09 15:08:12 +0000133 /// DirectBinding - True when this is a reference binding that is a
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000134 /// direct binding (C++ [dcl.init.ref]).
135 bool DirectBinding : 1;
136
Sebastian Redla9845802009-03-29 15:27:50 +0000137 /// RRefBinding - True when this is a reference binding of an rvalue
138 /// reference to an rvalue (C++0x [over.ics.rank]p3b4).
139 bool RRefBinding : 1;
140
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000141 /// FromType - The type that this conversion is converting
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000142 /// from. This is an opaque pointer that can be translated into a
143 /// QualType.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000144 void *FromTypePtr;
145
Douglas Gregorad323a82010-01-27 03:51:04 +0000146 /// ToType - The types that this conversion is converting to in
147 /// each step. This is an opaque pointer that can be translated
148 /// into a QualType.
149 void *ToTypePtrs[3];
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000150
Douglas Gregor225c41e2008-11-03 19:09:14 +0000151 /// CopyConstructor - The copy constructor that is used to perform
152 /// this conversion, when the conversion is actually just the
153 /// initialization of an object via copy constructor. Such
154 /// conversions are either identity conversions or derived-to-base
155 /// conversions.
156 CXXConstructorDecl *CopyConstructor;
157
John McCall1d318332010-01-12 00:44:57 +0000158 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
Douglas Gregorad323a82010-01-27 03:51:04 +0000159 void setToType(unsigned Idx, QualType T) {
160 assert(Idx < 3 && "To type index is out of range");
161 ToTypePtrs[Idx] = T.getAsOpaquePtr();
162 }
163 void setAllToTypes(QualType T) {
164 ToTypePtrs[0] = T.getAsOpaquePtr();
165 ToTypePtrs[1] = ToTypePtrs[0];
166 ToTypePtrs[2] = ToTypePtrs[0];
167 }
168
John McCall1d318332010-01-12 00:44:57 +0000169 QualType getFromType() const {
170 return QualType::getFromOpaquePtr(FromTypePtr);
171 }
Douglas Gregorad323a82010-01-27 03:51:04 +0000172 QualType getToType(unsigned Idx) const {
173 assert(Idx < 3 && "To type index is out of range");
174 return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
John McCall1d318332010-01-12 00:44:57 +0000175 }
176
Mike Stump1eb44332009-09-09 15:08:12 +0000177 void setAsIdentityConversion();
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000178 ImplicitConversionRank getRank() const;
179 bool isPointerConversionToBool() const;
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000180 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000181 void DebugPrint() const;
182 };
183
184 /// UserDefinedConversionSequence - Represents a user-defined
185 /// conversion sequence (C++ 13.3.3.1.2).
186 struct UserDefinedConversionSequence {
187 /// Before - Represents the standard conversion that occurs before
188 /// the actual user-defined conversion. (C++ 13.3.3.1.2p1):
189 ///
190 /// If the user-defined conversion is specified by a constructor
191 /// (12.3.1), the initial standard conversion sequence converts
192 /// the source type to the type required by the argument of the
193 /// constructor. If the user-defined conversion is specified by
194 /// a conversion function (12.3.2), the initial standard
195 /// conversion sequence converts the source type to the implicit
196 /// object parameter of the conversion function.
197 StandardConversionSequence Before;
198
Fariborz Jahanian966256a2009-11-06 00:23:08 +0000199 /// EllipsisConversion - When this is true, it means user-defined
200 /// conversion sequence starts with a ... (elipsis) conversion, instead of
201 /// a standard conversion. In this case, 'Before' field must be ignored.
202 // FIXME. I much rather put this as the first field. But there seems to be
203 // a gcc code gen. bug which causes a crash in a test. Putting it here seems
204 // to work around the crash.
205 bool EllipsisConversion : 1;
206
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000207 /// After - Represents the standard conversion that occurs after
208 /// the actual user-defined conversion.
209 StandardConversionSequence After;
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000211 /// ConversionFunction - The function that will perform the
212 /// user-defined conversion.
213 FunctionDecl* ConversionFunction;
214
215 void DebugPrint() const;
216 };
217
John McCall1d318332010-01-12 00:44:57 +0000218 /// Represents an ambiguous user-defined conversion sequence.
219 struct AmbiguousConversionSequence {
220 typedef llvm::SmallVector<FunctionDecl*, 4> ConversionSet;
221
222 void *FromTypePtr;
223 void *ToTypePtr;
224 char Buffer[sizeof(ConversionSet)];
225
226 QualType getFromType() const {
227 return QualType::getFromOpaquePtr(FromTypePtr);
228 }
229 QualType getToType() const {
230 return QualType::getFromOpaquePtr(ToTypePtr);
231 }
232 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
233 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
234
235 ConversionSet &conversions() {
236 return *reinterpret_cast<ConversionSet*>(Buffer);
237 }
238
239 const ConversionSet &conversions() const {
240 return *reinterpret_cast<const ConversionSet*>(Buffer);
241 }
242
243 void addConversion(FunctionDecl *D) {
244 conversions().push_back(D);
245 }
246
247 typedef ConversionSet::iterator iterator;
248 iterator begin() { return conversions().begin(); }
249 iterator end() { return conversions().end(); }
250
251 typedef ConversionSet::const_iterator const_iterator;
252 const_iterator begin() const { return conversions().begin(); }
253 const_iterator end() const { return conversions().end(); }
254
255 void construct();
256 void destruct();
257 void copyFrom(const AmbiguousConversionSequence &);
258 };
259
John McCalladbb8f82010-01-13 09:16:55 +0000260 /// BadConversionSequence - Records information about an invalid
261 /// conversion sequence.
262 struct BadConversionSequence {
263 enum FailureKind {
264 no_conversion,
265 unrelated_class,
266 suppressed_user,
267 bad_qualifiers
268 };
269
270 // This can be null, e.g. for implicit object arguments.
271 Expr *FromExpr;
272
273 FailureKind Kind;
274
275 private:
276 // The type we're converting from (an opaque QualType).
277 void *FromTy;
278
279 // The type we're converting to (an opaque QualType).
280 void *ToTy;
281
282 public:
283 void init(FailureKind K, Expr *From, QualType To) {
284 init(K, From->getType(), To);
285 FromExpr = From;
286 }
287 void init(FailureKind K, QualType From, QualType To) {
288 Kind = K;
289 FromExpr = 0;
290 setFromType(From);
291 setToType(To);
292 }
293
294 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
295 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
296
297 void setFromExpr(Expr *E) {
298 FromExpr = E;
299 setFromType(E->getType());
300 }
301 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
302 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
303 };
304
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000305 /// ImplicitConversionSequence - Represents an implicit conversion
Mike Stump1eb44332009-09-09 15:08:12 +0000306 /// sequence, which may be a standard conversion sequence
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000307 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000308 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
309 struct ImplicitConversionSequence {
310 /// Kind - The kind of implicit conversion sequence. BadConversion
311 /// specifies that there is no conversion from the source type to
John McCall1d318332010-01-12 00:44:57 +0000312 /// the target type. AmbiguousConversion represents the unique
313 /// ambiguous conversion (C++0x [over.best.ics]p10).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000314 enum Kind {
315 StandardConversion = 0,
316 UserDefinedConversion,
John McCall1d318332010-01-12 00:44:57 +0000317 AmbiguousConversion,
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000318 EllipsisConversion,
319 BadConversion
320 };
321
John McCall1d318332010-01-12 00:44:57 +0000322 private:
John McCallb1bdc622010-02-25 01:37:24 +0000323 enum {
324 Uninitialized = BadConversion + 1
325 };
326
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000327 /// ConversionKind - The kind of implicit conversion sequence.
John McCallb1bdc622010-02-25 01:37:24 +0000328 unsigned ConversionKind;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000329
John McCall1d318332010-01-12 00:44:57 +0000330 void setKind(Kind K) {
John McCallb1bdc622010-02-25 01:37:24 +0000331 destruct();
John McCall1d318332010-01-12 00:44:57 +0000332 ConversionKind = K;
333 }
334
John McCallb1bdc622010-02-25 01:37:24 +0000335 void destruct() {
336 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
337 }
338
John McCall1d318332010-01-12 00:44:57 +0000339 public:
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000340 union {
341 /// When ConversionKind == StandardConversion, provides the
342 /// details of the standard conversion sequence.
343 StandardConversionSequence Standard;
344
345 /// When ConversionKind == UserDefinedConversion, provides the
346 /// details of the user-defined conversion sequence.
347 UserDefinedConversionSequence UserDefined;
John McCall1d318332010-01-12 00:44:57 +0000348
349 /// When ConversionKind == AmbiguousConversion, provides the
350 /// details of the ambiguous conversion.
351 AmbiguousConversionSequence Ambiguous;
John McCalladbb8f82010-01-13 09:16:55 +0000352
353 /// When ConversionKind == BadConversion, provides the details
354 /// of the bad conversion.
355 BadConversionSequence Bad;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000356 };
John McCall1d318332010-01-12 00:44:57 +0000357
John McCallb1bdc622010-02-25 01:37:24 +0000358 ImplicitConversionSequence() : ConversionKind(Uninitialized) {}
John McCall1d318332010-01-12 00:44:57 +0000359 ~ImplicitConversionSequence() {
John McCallb1bdc622010-02-25 01:37:24 +0000360 destruct();
John McCall1d318332010-01-12 00:44:57 +0000361 }
362 ImplicitConversionSequence(const ImplicitConversionSequence &Other)
363 : ConversionKind(Other.ConversionKind)
364 {
365 switch (ConversionKind) {
John McCallb1bdc622010-02-25 01:37:24 +0000366 case Uninitialized: break;
John McCall1d318332010-01-12 00:44:57 +0000367 case StandardConversion: Standard = Other.Standard; break;
368 case UserDefinedConversion: UserDefined = Other.UserDefined; break;
369 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
370 case EllipsisConversion: break;
John McCalladbb8f82010-01-13 09:16:55 +0000371 case BadConversion: Bad = Other.Bad; break;
John McCall1d318332010-01-12 00:44:57 +0000372 }
373 }
374
375 ImplicitConversionSequence &
376 operator=(const ImplicitConversionSequence &Other) {
John McCallb1bdc622010-02-25 01:37:24 +0000377 destruct();
John McCall1d318332010-01-12 00:44:57 +0000378 new (this) ImplicitConversionSequence(Other);
379 return *this;
380 }
Fariborz Jahanianb1663d02009-09-23 00:58:07 +0000381
John McCallb1bdc622010-02-25 01:37:24 +0000382 Kind getKind() const {
383 assert(isInitialized() && "querying uninitialized conversion");
384 return Kind(ConversionKind);
385 }
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +0000386
387 /// \brief Return a ranking of the implicit conversion sequence
388 /// kind, where smaller ranks represent better conversion
389 /// sequences.
390 ///
391 /// In particular, this routine gives user-defined conversion
392 /// sequences and ambiguous conversion sequences the same rank,
393 /// per C++ [over.best.ics]p10.
394 unsigned getKindRank() const {
395 switch (getKind()) {
396 case StandardConversion:
397 return 0;
398
399 case UserDefinedConversion:
400 case AmbiguousConversion:
401 return 1;
402
403 case EllipsisConversion:
404 return 2;
405
406 case BadConversion:
407 return 3;
408 }
409
410 return 3;
411 }
412
John McCallb1bdc622010-02-25 01:37:24 +0000413 bool isBad() const { return getKind() == BadConversion; }
414 bool isStandard() const { return getKind() == StandardConversion; }
415 bool isEllipsis() const { return getKind() == EllipsisConversion; }
416 bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
417 bool isUserDefined() const { return getKind() == UserDefinedConversion; }
418
419 /// Determines whether this conversion sequence has been
420 /// initialized. Most operations should never need to query
421 /// uninitialized conversions and should assert as above.
422 bool isInitialized() const { return ConversionKind != Uninitialized; }
423
424 /// Sets this sequence as a bad conversion for an explicit argument.
425 void setBad(BadConversionSequence::FailureKind Failure,
426 Expr *FromExpr, QualType ToType) {
427 setKind(BadConversion);
428 Bad.init(Failure, FromExpr, ToType);
John McCall1d318332010-01-12 00:44:57 +0000429 }
430
John McCallb1bdc622010-02-25 01:37:24 +0000431 /// Sets this sequence as a bad conversion for an implicit argument.
432 void setBad(BadConversionSequence::FailureKind Failure,
433 QualType FromType, QualType ToType) {
434 setKind(BadConversion);
435 Bad.init(Failure, FromType, ToType);
436 }
437
John McCall1d318332010-01-12 00:44:57 +0000438 void setStandard() { setKind(StandardConversion); }
439 void setEllipsis() { setKind(EllipsisConversion); }
440 void setUserDefined() { setKind(UserDefinedConversion); }
441 void setAmbiguous() {
John McCallb1bdc622010-02-25 01:37:24 +0000442 if (ConversionKind == AmbiguousConversion) return;
John McCall1d318332010-01-12 00:44:57 +0000443 ConversionKind = AmbiguousConversion;
444 Ambiguous.construct();
445 }
446
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000447 // The result of a comparison between implicit conversion
448 // sequences. Use Sema::CompareImplicitConversionSequences to
449 // actually perform the comparison.
450 enum CompareKind {
Douglas Gregor57373262008-10-22 14:17:15 +0000451 Better = -1,
452 Indistinguishable = 0,
453 Worse = 1
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000454 };
455
456 void DebugPrint() const;
457 };
458
John McCalladbb8f82010-01-13 09:16:55 +0000459 enum OverloadFailureKind {
460 ovl_fail_too_many_arguments,
461 ovl_fail_too_few_arguments,
462 ovl_fail_bad_conversion,
John McCall717e8912010-01-23 05:17:32 +0000463 ovl_fail_bad_deduction,
464
465 /// This conversion candidate was not considered because it
466 /// duplicates the work of a trivial or derived-to-base
467 /// conversion.
468 ovl_fail_trivial_conversion,
469
470 /// This conversion candidate is not viable because its result
471 /// type is not implicitly convertible to the desired type.
Douglas Gregorc520c842010-04-12 23:42:09 +0000472 ovl_fail_bad_final_conversion,
473
474 /// This conversion function template specialization candidate is not
475 /// viable because the final conversion was not an exact match.
476 ovl_fail_final_conversion_not_exact
John McCalladbb8f82010-01-13 09:16:55 +0000477 };
478
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000479 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
480 struct OverloadCandidate {
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000481 /// Function - The actual function that this candidate
Mike Stump1eb44332009-09-09 15:08:12 +0000482 /// represents. When NULL, this is a built-in candidate
483 /// (C++ [over.oper]) or a surrogate for a conversion to a
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000484 /// function pointer or reference (C++ [over.call.object]).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000485 FunctionDecl *Function;
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000486
John McCall9aa472c2010-03-19 07:35:19 +0000487 /// FoundDecl - The original declaration that was looked up /
488 /// invented / otherwise found, together with its access.
489 /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
490 DeclAccessPair FoundDecl;
491
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000492 // BuiltinTypes - Provides the return and parameter types of a
493 // built-in overload candidate. Only valid when Function is NULL.
494 struct {
495 QualType ResultTy;
496 QualType ParamTypes[3];
497 } BuiltinTypes;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000499 /// Surrogate - The conversion function for which this candidate
500 /// is a surrogate, but only if IsSurrogate is true.
501 CXXConversionDecl *Surrogate;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000502
503 /// Conversions - The conversion sequences used to convert the
504 /// function arguments to the function parameters.
505 llvm::SmallVector<ImplicitConversionSequence, 4> Conversions;
506
507 /// Viable - True to indicate that this overload candidate is viable.
508 bool Viable;
Douglas Gregorf1991ea2008-11-07 22:36:19 +0000509
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000510 /// IsSurrogate - True to indicate that this candidate is a
511 /// surrogate for a conversion to a function pointer or reference
512 /// (C++ [over.call.object]).
513 bool IsSurrogate;
514
Douglas Gregor88a35142008-12-22 05:46:06 +0000515 /// IgnoreObjectArgument - True to indicate that the first
516 /// argument's conversion, which for this function represents the
517 /// implicit object argument, should be ignored. This will be true
518 /// when the candidate is a static member function (where the
519 /// implicit object argument is just a placeholder) or a
520 /// non-static member function when the call doesn't have an
521 /// object argument.
522 bool IgnoreObjectArgument;
523
John McCalladbb8f82010-01-13 09:16:55 +0000524 /// FailureKind - The reason why this candidate is not viable.
525 /// Actually an OverloadFailureKind.
526 unsigned char FailureKind;
527
John McCall342fec42010-02-01 18:53:26 +0000528 /// A structure used to record information about a failed
529 /// template argument deduction.
530 struct DeductionFailureInfo {
531 // A Sema::TemplateDeductionResult.
532 unsigned Result;
533
Douglas Gregora9333192010-05-08 17:41:32 +0000534 /// \brief Opaque pointer containing additional data about
535 /// this deduction failure.
536 void *Data;
537
538 /// \brief Retrieve the template parameter this deduction failure
539 /// refers to, if any.
540 TemplateParameter getTemplateParameter();
541
Douglas Gregorec20f462010-05-08 20:07:26 +0000542 /// \brief Retrieve the template argument list associated with this
543 /// deduction failure, if any.
544 TemplateArgumentList *getTemplateArgumentList();
545
Douglas Gregora9333192010-05-08 17:41:32 +0000546 /// \brief Return the first template argument this deduction failure
547 /// refers to, if any.
548 const TemplateArgument *getFirstArg();
549
550 /// \brief Return the second template argument this deduction failure
551 /// refers to, if any.
552 const TemplateArgument *getSecondArg();
553
554 /// \brief Free any memory associated with this deduction failure.
555 void Destroy();
John McCall342fec42010-02-01 18:53:26 +0000556 };
557
558 union {
559 DeductionFailureInfo DeductionFailure;
560
561 /// FinalConversion - For a conversion function (where Function is
562 /// a CXXConversionDecl), the standard conversion that occurs
563 /// after the call to the overload candidate to convert the result
564 /// of calling the conversion function to the required type.
565 StandardConversionSequence FinalConversion;
566 };
John McCall1d318332010-01-12 00:44:57 +0000567
568 /// hasAmbiguousConversion - Returns whether this overload
569 /// candidate requires an ambiguous conversion or not.
570 bool hasAmbiguousConversion() const {
571 for (llvm::SmallVectorImpl<ImplicitConversionSequence>::const_iterator
572 I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
John McCallb1bdc622010-02-25 01:37:24 +0000573 if (!I->isInitialized()) return false;
John McCall1d318332010-01-12 00:44:57 +0000574 if (I->isAmbiguous()) return true;
575 }
576 return false;
577 }
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000578 };
579
580 /// OverloadCandidateSet - A set of overload candidates, used in C++
581 /// overload resolution (C++ 13.3).
Douglas Gregor3f396022009-09-28 04:47:19 +0000582 class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> {
Douglas Gregor20093b42009-12-09 23:02:17 +0000583 typedef llvm::SmallVector<OverloadCandidate, 16> inherited;
Douglas Gregor3f396022009-09-28 04:47:19 +0000584 llvm::SmallPtrSet<Decl *, 16> Functions;
John McCall5769d612010-02-08 23:07:23 +0000585
586 SourceLocation Loc;
Douglas Gregor0ca4c582010-05-08 18:20:53 +0000587
588 OverloadCandidateSet(const OverloadCandidateSet &);
589 OverloadCandidateSet &operator=(const OverloadCandidateSet &);
590
Douglas Gregor3f396022009-09-28 04:47:19 +0000591 public:
John McCall5769d612010-02-08 23:07:23 +0000592 OverloadCandidateSet(SourceLocation Loc) : Loc(Loc) {}
593
594 SourceLocation getLocation() const { return Loc; }
595
Douglas Gregor3f396022009-09-28 04:47:19 +0000596 /// \brief Determine when this overload candidate will be new to the
597 /// overload set.
598 bool isNewCandidate(Decl *F) {
599 return Functions.insert(F->getCanonicalDecl());
600 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000601
602 /// \brief Clear out all of the candidates.
Douglas Gregora9333192010-05-08 17:41:32 +0000603 void clear();
604
605 ~OverloadCandidateSet() { clear(); }
Douglas Gregor3f396022009-09-28 04:47:19 +0000606 };
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000607} // end namespace clang
608
609#endif // LLVM_CLANG_SEMA_OVERLOAD_H