blob: eb4fc6581796a1571163d9d019fa555c25cdc99e [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])
Douglas Gregorfb4a5432010-05-18 22:42:18 +000065 ICK_Vector_Conversion, ///< Vector conversions
66 ICK_Vector_Splat, ///< A vector splat from an arithmetic type
Chandler Carruth23a370f2010-02-25 07:20:54 +000067 ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7)
Douglas Gregorf9201e02009-02-11 23:02:49 +000068 ICK_Num_Conversion_Kinds ///< The number of conversion kinds
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000069 };
70
71 /// ImplicitConversionCategory - The category of an implicit
72 /// conversion kind. The enumerator values match with Table 9 of
73 /// (C++ 13.3.3.1.1) and are listed such that better conversion
74 /// categories have smaller values.
75 enum ImplicitConversionCategory {
76 ICC_Identity = 0, ///< Identity
77 ICC_Lvalue_Transformation, ///< Lvalue transformation
78 ICC_Qualification_Adjustment, ///< Qualification adjustment
79 ICC_Promotion, ///< Promotion
80 ICC_Conversion ///< Conversion
81 };
82
Mike Stump1eb44332009-09-09 15:08:12 +000083 ImplicitConversionCategory
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000084 GetConversionCategory(ImplicitConversionKind Kind);
85
86 /// ImplicitConversionRank - The rank of an implicit conversion
87 /// kind. The enumerator values match with Table 9 of (C++
88 /// 13.3.3.1.1) and are listed such that better conversion ranks
89 /// have smaller values.
90 enum ImplicitConversionRank {
Chandler Carruth23a370f2010-02-25 07:20:54 +000091 ICR_Exact_Match = 0, ///< Exact Match
92 ICR_Promotion, ///< Promotion
93 ICR_Conversion, ///< Conversion
94 ICR_Complex_Real_Conversion ///< Complex <-> Real conversion
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000095 };
96
97 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
98
99 /// StandardConversionSequence - represents a standard conversion
100 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
101 /// contains between zero and three conversions. If a particular
102 /// conversion is not needed, it will be set to the identity conversion
103 /// (ICK_Identity). Note that the three conversions are
104 /// specified as separate members (rather than in an array) so that
105 /// we can keep the size of a standard conversion sequence to a
106 /// single word.
107 struct StandardConversionSequence {
108 /// First -- The first conversion can be an lvalue-to-rvalue
109 /// conversion, array-to-pointer conversion, or
110 /// function-to-pointer conversion.
111 ImplicitConversionKind First : 8;
112
113 /// Second - The second conversion can be an integral promotion,
114 /// floating point promotion, integral conversion, floating point
115 /// conversion, floating-integral conversion, pointer conversion,
116 /// pointer-to-member conversion, or boolean conversion.
117 ImplicitConversionKind Second : 8;
118
119 /// Third - The third conversion can be a qualification conversion.
120 ImplicitConversionKind Third : 8;
121
Douglas Gregor15da57e2008-10-29 02:00:59 +0000122 /// Deprecated - Whether this the deprecated conversion of a
Mike Stump1eb44332009-09-09 15:08:12 +0000123 /// string literal to a pointer to non-const character data
Douglas Gregor15da57e2008-10-29 02:00:59 +0000124 /// (C++ 4.2p2).
Douglas Gregora9bff302010-02-28 18:30:25 +0000125 bool DeprecatedStringLiteralToCharPtr : 1;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000126
Douglas Gregor45920e82008-12-19 17:40:08 +0000127 /// IncompatibleObjC - Whether this is an Objective-C conversion
128 /// that we should warn about (if we actually use it).
129 bool IncompatibleObjC : 1;
130
Mike Stump1eb44332009-09-09 15:08:12 +0000131 /// ReferenceBinding - True when this is a reference binding
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000132 /// (C++ [over.ics.ref]).
133 bool ReferenceBinding : 1;
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135 /// DirectBinding - True when this is a reference binding that is a
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000136 /// direct binding (C++ [dcl.init.ref]).
137 bool DirectBinding : 1;
138
Sebastian Redla9845802009-03-29 15:27:50 +0000139 /// RRefBinding - True when this is a reference binding of an rvalue
140 /// reference to an rvalue (C++0x [over.ics.rank]p3b4).
141 bool RRefBinding : 1;
142
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000143 /// FromType - The type that this conversion is converting
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000144 /// from. This is an opaque pointer that can be translated into a
145 /// QualType.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000146 void *FromTypePtr;
147
Douglas Gregorad323a82010-01-27 03:51:04 +0000148 /// ToType - The types that this conversion is converting to in
149 /// each step. This is an opaque pointer that can be translated
150 /// into a QualType.
151 void *ToTypePtrs[3];
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000152
Douglas Gregor225c41e2008-11-03 19:09:14 +0000153 /// CopyConstructor - The copy constructor that is used to perform
154 /// this conversion, when the conversion is actually just the
155 /// initialization of an object via copy constructor. Such
156 /// conversions are either identity conversions or derived-to-base
157 /// conversions.
158 CXXConstructorDecl *CopyConstructor;
159
John McCall1d318332010-01-12 00:44:57 +0000160 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
Douglas Gregorad323a82010-01-27 03:51:04 +0000161 void setToType(unsigned Idx, QualType T) {
162 assert(Idx < 3 && "To type index is out of range");
163 ToTypePtrs[Idx] = T.getAsOpaquePtr();
164 }
165 void setAllToTypes(QualType T) {
166 ToTypePtrs[0] = T.getAsOpaquePtr();
167 ToTypePtrs[1] = ToTypePtrs[0];
168 ToTypePtrs[2] = ToTypePtrs[0];
169 }
170
John McCall1d318332010-01-12 00:44:57 +0000171 QualType getFromType() const {
172 return QualType::getFromOpaquePtr(FromTypePtr);
173 }
Douglas Gregorad323a82010-01-27 03:51:04 +0000174 QualType getToType(unsigned Idx) const {
175 assert(Idx < 3 && "To type index is out of range");
176 return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
John McCall1d318332010-01-12 00:44:57 +0000177 }
178
Mike Stump1eb44332009-09-09 15:08:12 +0000179 void setAsIdentityConversion();
Douglas Gregorae65f4b2010-05-23 22:10:15 +0000180
181 bool isIdentityConversion() const {
182 return First == ICK_Identity && Second == ICK_Identity &&
183 Third == ICK_Identity;
184 }
185
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000186 ImplicitConversionRank getRank() const;
187 bool isPointerConversionToBool() const;
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000188 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000189 void DebugPrint() const;
190 };
191
192 /// UserDefinedConversionSequence - Represents a user-defined
193 /// conversion sequence (C++ 13.3.3.1.2).
194 struct UserDefinedConversionSequence {
195 /// Before - Represents the standard conversion that occurs before
196 /// the actual user-defined conversion. (C++ 13.3.3.1.2p1):
197 ///
198 /// If the user-defined conversion is specified by a constructor
199 /// (12.3.1), the initial standard conversion sequence converts
200 /// the source type to the type required by the argument of the
201 /// constructor. If the user-defined conversion is specified by
202 /// a conversion function (12.3.2), the initial standard
203 /// conversion sequence converts the source type to the implicit
204 /// object parameter of the conversion function.
205 StandardConversionSequence Before;
206
Fariborz Jahanian966256a2009-11-06 00:23:08 +0000207 /// EllipsisConversion - When this is true, it means user-defined
208 /// conversion sequence starts with a ... (elipsis) conversion, instead of
209 /// a standard conversion. In this case, 'Before' field must be ignored.
210 // FIXME. I much rather put this as the first field. But there seems to be
211 // a gcc code gen. bug which causes a crash in a test. Putting it here seems
212 // to work around the crash.
213 bool EllipsisConversion : 1;
214
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000215 /// After - Represents the standard conversion that occurs after
216 /// the actual user-defined conversion.
217 StandardConversionSequence After;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000219 /// ConversionFunction - The function that will perform the
220 /// user-defined conversion.
221 FunctionDecl* ConversionFunction;
222
223 void DebugPrint() const;
224 };
225
John McCall1d318332010-01-12 00:44:57 +0000226 /// Represents an ambiguous user-defined conversion sequence.
227 struct AmbiguousConversionSequence {
228 typedef llvm::SmallVector<FunctionDecl*, 4> ConversionSet;
229
230 void *FromTypePtr;
231 void *ToTypePtr;
232 char Buffer[sizeof(ConversionSet)];
233
234 QualType getFromType() const {
235 return QualType::getFromOpaquePtr(FromTypePtr);
236 }
237 QualType getToType() const {
238 return QualType::getFromOpaquePtr(ToTypePtr);
239 }
240 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
241 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
242
243 ConversionSet &conversions() {
244 return *reinterpret_cast<ConversionSet*>(Buffer);
245 }
246
247 const ConversionSet &conversions() const {
248 return *reinterpret_cast<const ConversionSet*>(Buffer);
249 }
250
251 void addConversion(FunctionDecl *D) {
252 conversions().push_back(D);
253 }
254
255 typedef ConversionSet::iterator iterator;
256 iterator begin() { return conversions().begin(); }
257 iterator end() { return conversions().end(); }
258
259 typedef ConversionSet::const_iterator const_iterator;
260 const_iterator begin() const { return conversions().begin(); }
261 const_iterator end() const { return conversions().end(); }
262
263 void construct();
264 void destruct();
265 void copyFrom(const AmbiguousConversionSequence &);
266 };
267
John McCalladbb8f82010-01-13 09:16:55 +0000268 /// BadConversionSequence - Records information about an invalid
269 /// conversion sequence.
270 struct BadConversionSequence {
271 enum FailureKind {
272 no_conversion,
273 unrelated_class,
274 suppressed_user,
275 bad_qualifiers
276 };
277
278 // This can be null, e.g. for implicit object arguments.
279 Expr *FromExpr;
280
281 FailureKind Kind;
282
283 private:
284 // The type we're converting from (an opaque QualType).
285 void *FromTy;
286
287 // The type we're converting to (an opaque QualType).
288 void *ToTy;
289
290 public:
291 void init(FailureKind K, Expr *From, QualType To) {
292 init(K, From->getType(), To);
293 FromExpr = From;
294 }
295 void init(FailureKind K, QualType From, QualType To) {
296 Kind = K;
297 FromExpr = 0;
298 setFromType(From);
299 setToType(To);
300 }
301
302 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
303 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
304
305 void setFromExpr(Expr *E) {
306 FromExpr = E;
307 setFromType(E->getType());
308 }
309 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
310 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
311 };
312
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000313 /// ImplicitConversionSequence - Represents an implicit conversion
Mike Stump1eb44332009-09-09 15:08:12 +0000314 /// sequence, which may be a standard conversion sequence
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000315 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000316 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
317 struct ImplicitConversionSequence {
318 /// Kind - The kind of implicit conversion sequence. BadConversion
319 /// specifies that there is no conversion from the source type to
John McCall1d318332010-01-12 00:44:57 +0000320 /// the target type. AmbiguousConversion represents the unique
321 /// ambiguous conversion (C++0x [over.best.ics]p10).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000322 enum Kind {
323 StandardConversion = 0,
324 UserDefinedConversion,
John McCall1d318332010-01-12 00:44:57 +0000325 AmbiguousConversion,
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000326 EllipsisConversion,
327 BadConversion
328 };
329
John McCall1d318332010-01-12 00:44:57 +0000330 private:
John McCallb1bdc622010-02-25 01:37:24 +0000331 enum {
332 Uninitialized = BadConversion + 1
333 };
334
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000335 /// ConversionKind - The kind of implicit conversion sequence.
John McCallb1bdc622010-02-25 01:37:24 +0000336 unsigned ConversionKind;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000337
John McCall1d318332010-01-12 00:44:57 +0000338 void setKind(Kind K) {
John McCallb1bdc622010-02-25 01:37:24 +0000339 destruct();
John McCall1d318332010-01-12 00:44:57 +0000340 ConversionKind = K;
341 }
342
John McCallb1bdc622010-02-25 01:37:24 +0000343 void destruct() {
344 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
345 }
346
John McCall1d318332010-01-12 00:44:57 +0000347 public:
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000348 union {
349 /// When ConversionKind == StandardConversion, provides the
350 /// details of the standard conversion sequence.
351 StandardConversionSequence Standard;
352
353 /// When ConversionKind == UserDefinedConversion, provides the
354 /// details of the user-defined conversion sequence.
355 UserDefinedConversionSequence UserDefined;
John McCall1d318332010-01-12 00:44:57 +0000356
357 /// When ConversionKind == AmbiguousConversion, provides the
358 /// details of the ambiguous conversion.
359 AmbiguousConversionSequence Ambiguous;
John McCalladbb8f82010-01-13 09:16:55 +0000360
361 /// When ConversionKind == BadConversion, provides the details
362 /// of the bad conversion.
363 BadConversionSequence Bad;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000364 };
John McCall1d318332010-01-12 00:44:57 +0000365
John McCallb1bdc622010-02-25 01:37:24 +0000366 ImplicitConversionSequence() : ConversionKind(Uninitialized) {}
John McCall1d318332010-01-12 00:44:57 +0000367 ~ImplicitConversionSequence() {
John McCallb1bdc622010-02-25 01:37:24 +0000368 destruct();
John McCall1d318332010-01-12 00:44:57 +0000369 }
370 ImplicitConversionSequence(const ImplicitConversionSequence &Other)
371 : ConversionKind(Other.ConversionKind)
372 {
373 switch (ConversionKind) {
John McCallb1bdc622010-02-25 01:37:24 +0000374 case Uninitialized: break;
John McCall1d318332010-01-12 00:44:57 +0000375 case StandardConversion: Standard = Other.Standard; break;
376 case UserDefinedConversion: UserDefined = Other.UserDefined; break;
377 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
378 case EllipsisConversion: break;
John McCalladbb8f82010-01-13 09:16:55 +0000379 case BadConversion: Bad = Other.Bad; break;
John McCall1d318332010-01-12 00:44:57 +0000380 }
381 }
382
383 ImplicitConversionSequence &
384 operator=(const ImplicitConversionSequence &Other) {
John McCallb1bdc622010-02-25 01:37:24 +0000385 destruct();
John McCall1d318332010-01-12 00:44:57 +0000386 new (this) ImplicitConversionSequence(Other);
387 return *this;
388 }
Fariborz Jahanianb1663d02009-09-23 00:58:07 +0000389
John McCallb1bdc622010-02-25 01:37:24 +0000390 Kind getKind() const {
391 assert(isInitialized() && "querying uninitialized conversion");
392 return Kind(ConversionKind);
393 }
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +0000394
395 /// \brief Return a ranking of the implicit conversion sequence
396 /// kind, where smaller ranks represent better conversion
397 /// sequences.
398 ///
399 /// In particular, this routine gives user-defined conversion
400 /// sequences and ambiguous conversion sequences the same rank,
401 /// per C++ [over.best.ics]p10.
402 unsigned getKindRank() const {
403 switch (getKind()) {
404 case StandardConversion:
405 return 0;
406
407 case UserDefinedConversion:
408 case AmbiguousConversion:
409 return 1;
410
411 case EllipsisConversion:
412 return 2;
413
414 case BadConversion:
415 return 3;
416 }
417
418 return 3;
419 }
420
John McCallb1bdc622010-02-25 01:37:24 +0000421 bool isBad() const { return getKind() == BadConversion; }
422 bool isStandard() const { return getKind() == StandardConversion; }
423 bool isEllipsis() const { return getKind() == EllipsisConversion; }
424 bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
425 bool isUserDefined() const { return getKind() == UserDefinedConversion; }
426
427 /// Determines whether this conversion sequence has been
428 /// initialized. Most operations should never need to query
429 /// uninitialized conversions and should assert as above.
430 bool isInitialized() const { return ConversionKind != Uninitialized; }
431
432 /// Sets this sequence as a bad conversion for an explicit argument.
433 void setBad(BadConversionSequence::FailureKind Failure,
434 Expr *FromExpr, QualType ToType) {
435 setKind(BadConversion);
436 Bad.init(Failure, FromExpr, ToType);
John McCall1d318332010-01-12 00:44:57 +0000437 }
438
John McCallb1bdc622010-02-25 01:37:24 +0000439 /// Sets this sequence as a bad conversion for an implicit argument.
440 void setBad(BadConversionSequence::FailureKind Failure,
441 QualType FromType, QualType ToType) {
442 setKind(BadConversion);
443 Bad.init(Failure, FromType, ToType);
444 }
445
John McCall1d318332010-01-12 00:44:57 +0000446 void setStandard() { setKind(StandardConversion); }
447 void setEllipsis() { setKind(EllipsisConversion); }
448 void setUserDefined() { setKind(UserDefinedConversion); }
449 void setAmbiguous() {
John McCallb1bdc622010-02-25 01:37:24 +0000450 if (ConversionKind == AmbiguousConversion) return;
John McCall1d318332010-01-12 00:44:57 +0000451 ConversionKind = AmbiguousConversion;
452 Ambiguous.construct();
453 }
454
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000455 // The result of a comparison between implicit conversion
456 // sequences. Use Sema::CompareImplicitConversionSequences to
457 // actually perform the comparison.
458 enum CompareKind {
Douglas Gregor57373262008-10-22 14:17:15 +0000459 Better = -1,
460 Indistinguishable = 0,
461 Worse = 1
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000462 };
463
464 void DebugPrint() const;
465 };
466
John McCalladbb8f82010-01-13 09:16:55 +0000467 enum OverloadFailureKind {
468 ovl_fail_too_many_arguments,
469 ovl_fail_too_few_arguments,
470 ovl_fail_bad_conversion,
John McCall717e8912010-01-23 05:17:32 +0000471 ovl_fail_bad_deduction,
472
473 /// This conversion candidate was not considered because it
474 /// duplicates the work of a trivial or derived-to-base
475 /// conversion.
476 ovl_fail_trivial_conversion,
477
478 /// This conversion candidate is not viable because its result
479 /// type is not implicitly convertible to the desired type.
Douglas Gregorc520c842010-04-12 23:42:09 +0000480 ovl_fail_bad_final_conversion,
481
482 /// This conversion function template specialization candidate is not
483 /// viable because the final conversion was not an exact match.
484 ovl_fail_final_conversion_not_exact
John McCalladbb8f82010-01-13 09:16:55 +0000485 };
486
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000487 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
488 struct OverloadCandidate {
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000489 /// Function - The actual function that this candidate
Mike Stump1eb44332009-09-09 15:08:12 +0000490 /// represents. When NULL, this is a built-in candidate
491 /// (C++ [over.oper]) or a surrogate for a conversion to a
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000492 /// function pointer or reference (C++ [over.call.object]).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000493 FunctionDecl *Function;
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000494
John McCall9aa472c2010-03-19 07:35:19 +0000495 /// FoundDecl - The original declaration that was looked up /
496 /// invented / otherwise found, together with its access.
497 /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
498 DeclAccessPair FoundDecl;
499
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000500 // BuiltinTypes - Provides the return and parameter types of a
501 // built-in overload candidate. Only valid when Function is NULL.
502 struct {
503 QualType ResultTy;
504 QualType ParamTypes[3];
505 } BuiltinTypes;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000507 /// Surrogate - The conversion function for which this candidate
508 /// is a surrogate, but only if IsSurrogate is true.
509 CXXConversionDecl *Surrogate;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000510
511 /// Conversions - The conversion sequences used to convert the
512 /// function arguments to the function parameters.
513 llvm::SmallVector<ImplicitConversionSequence, 4> Conversions;
514
515 /// Viable - True to indicate that this overload candidate is viable.
516 bool Viable;
Douglas Gregorf1991ea2008-11-07 22:36:19 +0000517
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000518 /// IsSurrogate - True to indicate that this candidate is a
519 /// surrogate for a conversion to a function pointer or reference
520 /// (C++ [over.call.object]).
521 bool IsSurrogate;
522
Douglas Gregor88a35142008-12-22 05:46:06 +0000523 /// IgnoreObjectArgument - True to indicate that the first
524 /// argument's conversion, which for this function represents the
525 /// implicit object argument, should be ignored. This will be true
526 /// when the candidate is a static member function (where the
527 /// implicit object argument is just a placeholder) or a
528 /// non-static member function when the call doesn't have an
529 /// object argument.
530 bool IgnoreObjectArgument;
531
John McCalladbb8f82010-01-13 09:16:55 +0000532 /// FailureKind - The reason why this candidate is not viable.
533 /// Actually an OverloadFailureKind.
534 unsigned char FailureKind;
535
John McCall342fec42010-02-01 18:53:26 +0000536 /// A structure used to record information about a failed
537 /// template argument deduction.
538 struct DeductionFailureInfo {
539 // A Sema::TemplateDeductionResult.
540 unsigned Result;
541
Douglas Gregora9333192010-05-08 17:41:32 +0000542 /// \brief Opaque pointer containing additional data about
543 /// this deduction failure.
544 void *Data;
545
546 /// \brief Retrieve the template parameter this deduction failure
547 /// refers to, if any.
548 TemplateParameter getTemplateParameter();
549
Douglas Gregorec20f462010-05-08 20:07:26 +0000550 /// \brief Retrieve the template argument list associated with this
551 /// deduction failure, if any.
552 TemplateArgumentList *getTemplateArgumentList();
553
Douglas Gregora9333192010-05-08 17:41:32 +0000554 /// \brief Return the first template argument this deduction failure
555 /// refers to, if any.
556 const TemplateArgument *getFirstArg();
557
558 /// \brief Return the second template argument this deduction failure
559 /// refers to, if any.
560 const TemplateArgument *getSecondArg();
561
562 /// \brief Free any memory associated with this deduction failure.
563 void Destroy();
John McCall342fec42010-02-01 18:53:26 +0000564 };
565
566 union {
567 DeductionFailureInfo DeductionFailure;
568
569 /// FinalConversion - For a conversion function (where Function is
570 /// a CXXConversionDecl), the standard conversion that occurs
571 /// after the call to the overload candidate to convert the result
572 /// of calling the conversion function to the required type.
573 StandardConversionSequence FinalConversion;
574 };
John McCall1d318332010-01-12 00:44:57 +0000575
576 /// hasAmbiguousConversion - Returns whether this overload
577 /// candidate requires an ambiguous conversion or not.
578 bool hasAmbiguousConversion() const {
579 for (llvm::SmallVectorImpl<ImplicitConversionSequence>::const_iterator
580 I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
John McCallb1bdc622010-02-25 01:37:24 +0000581 if (!I->isInitialized()) return false;
John McCall1d318332010-01-12 00:44:57 +0000582 if (I->isAmbiguous()) return true;
583 }
584 return false;
585 }
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000586 };
587
588 /// OverloadCandidateSet - A set of overload candidates, used in C++
589 /// overload resolution (C++ 13.3).
Douglas Gregor3f396022009-09-28 04:47:19 +0000590 class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> {
Douglas Gregor20093b42009-12-09 23:02:17 +0000591 typedef llvm::SmallVector<OverloadCandidate, 16> inherited;
Douglas Gregor3f396022009-09-28 04:47:19 +0000592 llvm::SmallPtrSet<Decl *, 16> Functions;
John McCall5769d612010-02-08 23:07:23 +0000593
594 SourceLocation Loc;
Douglas Gregor0ca4c582010-05-08 18:20:53 +0000595
596 OverloadCandidateSet(const OverloadCandidateSet &);
597 OverloadCandidateSet &operator=(const OverloadCandidateSet &);
598
Douglas Gregor3f396022009-09-28 04:47:19 +0000599 public:
John McCall5769d612010-02-08 23:07:23 +0000600 OverloadCandidateSet(SourceLocation Loc) : Loc(Loc) {}
601
602 SourceLocation getLocation() const { return Loc; }
603
Douglas Gregor3f396022009-09-28 04:47:19 +0000604 /// \brief Determine when this overload candidate will be new to the
605 /// overload set.
606 bool isNewCandidate(Decl *F) {
607 return Functions.insert(F->getCanonicalDecl());
608 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000609
610 /// \brief Clear out all of the candidates.
Douglas Gregora9333192010-05-08 17:41:32 +0000611 void clear();
612
613 ~OverloadCandidateSet() { clear(); }
Douglas Gregor3f396022009-09-28 04:47:19 +0000614 };
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000615} // end namespace clang
616
617#endif // LLVM_CLANG_SEMA_OVERLOAD_H