blob: 8d261ddcbe8e6dbe144fe9b2eb8fadb3d716a14c [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 Gregor8e9bebd2008-10-21 16:13:35 +0000180 ImplicitConversionRank getRank() const;
181 bool isPointerConversionToBool() const;
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000182 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000183 void DebugPrint() const;
184 };
185
186 /// UserDefinedConversionSequence - Represents a user-defined
187 /// conversion sequence (C++ 13.3.3.1.2).
188 struct UserDefinedConversionSequence {
189 /// Before - Represents the standard conversion that occurs before
190 /// the actual user-defined conversion. (C++ 13.3.3.1.2p1):
191 ///
192 /// If the user-defined conversion is specified by a constructor
193 /// (12.3.1), the initial standard conversion sequence converts
194 /// the source type to the type required by the argument of the
195 /// constructor. If the user-defined conversion is specified by
196 /// a conversion function (12.3.2), the initial standard
197 /// conversion sequence converts the source type to the implicit
198 /// object parameter of the conversion function.
199 StandardConversionSequence Before;
200
Fariborz Jahanian966256a2009-11-06 00:23:08 +0000201 /// EllipsisConversion - When this is true, it means user-defined
202 /// conversion sequence starts with a ... (elipsis) conversion, instead of
203 /// a standard conversion. In this case, 'Before' field must be ignored.
204 // FIXME. I much rather put this as the first field. But there seems to be
205 // a gcc code gen. bug which causes a crash in a test. Putting it here seems
206 // to work around the crash.
207 bool EllipsisConversion : 1;
208
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000209 /// After - Represents the standard conversion that occurs after
210 /// the actual user-defined conversion.
211 StandardConversionSequence After;
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000213 /// ConversionFunction - The function that will perform the
214 /// user-defined conversion.
215 FunctionDecl* ConversionFunction;
216
217 void DebugPrint() const;
218 };
219
John McCall1d318332010-01-12 00:44:57 +0000220 /// Represents an ambiguous user-defined conversion sequence.
221 struct AmbiguousConversionSequence {
222 typedef llvm::SmallVector<FunctionDecl*, 4> ConversionSet;
223
224 void *FromTypePtr;
225 void *ToTypePtr;
226 char Buffer[sizeof(ConversionSet)];
227
228 QualType getFromType() const {
229 return QualType::getFromOpaquePtr(FromTypePtr);
230 }
231 QualType getToType() const {
232 return QualType::getFromOpaquePtr(ToTypePtr);
233 }
234 void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
235 void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
236
237 ConversionSet &conversions() {
238 return *reinterpret_cast<ConversionSet*>(Buffer);
239 }
240
241 const ConversionSet &conversions() const {
242 return *reinterpret_cast<const ConversionSet*>(Buffer);
243 }
244
245 void addConversion(FunctionDecl *D) {
246 conversions().push_back(D);
247 }
248
249 typedef ConversionSet::iterator iterator;
250 iterator begin() { return conversions().begin(); }
251 iterator end() { return conversions().end(); }
252
253 typedef ConversionSet::const_iterator const_iterator;
254 const_iterator begin() const { return conversions().begin(); }
255 const_iterator end() const { return conversions().end(); }
256
257 void construct();
258 void destruct();
259 void copyFrom(const AmbiguousConversionSequence &);
260 };
261
John McCalladbb8f82010-01-13 09:16:55 +0000262 /// BadConversionSequence - Records information about an invalid
263 /// conversion sequence.
264 struct BadConversionSequence {
265 enum FailureKind {
266 no_conversion,
267 unrelated_class,
268 suppressed_user,
269 bad_qualifiers
270 };
271
272 // This can be null, e.g. for implicit object arguments.
273 Expr *FromExpr;
274
275 FailureKind Kind;
276
277 private:
278 // The type we're converting from (an opaque QualType).
279 void *FromTy;
280
281 // The type we're converting to (an opaque QualType).
282 void *ToTy;
283
284 public:
285 void init(FailureKind K, Expr *From, QualType To) {
286 init(K, From->getType(), To);
287 FromExpr = From;
288 }
289 void init(FailureKind K, QualType From, QualType To) {
290 Kind = K;
291 FromExpr = 0;
292 setFromType(From);
293 setToType(To);
294 }
295
296 QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
297 QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
298
299 void setFromExpr(Expr *E) {
300 FromExpr = E;
301 setFromType(E->getType());
302 }
303 void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
304 void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
305 };
306
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000307 /// ImplicitConversionSequence - Represents an implicit conversion
Mike Stump1eb44332009-09-09 15:08:12 +0000308 /// sequence, which may be a standard conversion sequence
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000309 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000310 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
311 struct ImplicitConversionSequence {
312 /// Kind - The kind of implicit conversion sequence. BadConversion
313 /// specifies that there is no conversion from the source type to
John McCall1d318332010-01-12 00:44:57 +0000314 /// the target type. AmbiguousConversion represents the unique
315 /// ambiguous conversion (C++0x [over.best.ics]p10).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000316 enum Kind {
317 StandardConversion = 0,
318 UserDefinedConversion,
John McCall1d318332010-01-12 00:44:57 +0000319 AmbiguousConversion,
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000320 EllipsisConversion,
321 BadConversion
322 };
323
John McCall1d318332010-01-12 00:44:57 +0000324 private:
John McCallb1bdc622010-02-25 01:37:24 +0000325 enum {
326 Uninitialized = BadConversion + 1
327 };
328
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000329 /// ConversionKind - The kind of implicit conversion sequence.
John McCallb1bdc622010-02-25 01:37:24 +0000330 unsigned ConversionKind;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000331
John McCall1d318332010-01-12 00:44:57 +0000332 void setKind(Kind K) {
John McCallb1bdc622010-02-25 01:37:24 +0000333 destruct();
John McCall1d318332010-01-12 00:44:57 +0000334 ConversionKind = K;
335 }
336
John McCallb1bdc622010-02-25 01:37:24 +0000337 void destruct() {
338 if (ConversionKind == AmbiguousConversion) Ambiguous.destruct();
339 }
340
John McCall1d318332010-01-12 00:44:57 +0000341 public:
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000342 union {
343 /// When ConversionKind == StandardConversion, provides the
344 /// details of the standard conversion sequence.
345 StandardConversionSequence Standard;
346
347 /// When ConversionKind == UserDefinedConversion, provides the
348 /// details of the user-defined conversion sequence.
349 UserDefinedConversionSequence UserDefined;
John McCall1d318332010-01-12 00:44:57 +0000350
351 /// When ConversionKind == AmbiguousConversion, provides the
352 /// details of the ambiguous conversion.
353 AmbiguousConversionSequence Ambiguous;
John McCalladbb8f82010-01-13 09:16:55 +0000354
355 /// When ConversionKind == BadConversion, provides the details
356 /// of the bad conversion.
357 BadConversionSequence Bad;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000358 };
John McCall1d318332010-01-12 00:44:57 +0000359
John McCallb1bdc622010-02-25 01:37:24 +0000360 ImplicitConversionSequence() : ConversionKind(Uninitialized) {}
John McCall1d318332010-01-12 00:44:57 +0000361 ~ImplicitConversionSequence() {
John McCallb1bdc622010-02-25 01:37:24 +0000362 destruct();
John McCall1d318332010-01-12 00:44:57 +0000363 }
364 ImplicitConversionSequence(const ImplicitConversionSequence &Other)
365 : ConversionKind(Other.ConversionKind)
366 {
367 switch (ConversionKind) {
John McCallb1bdc622010-02-25 01:37:24 +0000368 case Uninitialized: break;
John McCall1d318332010-01-12 00:44:57 +0000369 case StandardConversion: Standard = Other.Standard; break;
370 case UserDefinedConversion: UserDefined = Other.UserDefined; break;
371 case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break;
372 case EllipsisConversion: break;
John McCalladbb8f82010-01-13 09:16:55 +0000373 case BadConversion: Bad = Other.Bad; break;
John McCall1d318332010-01-12 00:44:57 +0000374 }
375 }
376
377 ImplicitConversionSequence &
378 operator=(const ImplicitConversionSequence &Other) {
John McCallb1bdc622010-02-25 01:37:24 +0000379 destruct();
John McCall1d318332010-01-12 00:44:57 +0000380 new (this) ImplicitConversionSequence(Other);
381 return *this;
382 }
Fariborz Jahanianb1663d02009-09-23 00:58:07 +0000383
John McCallb1bdc622010-02-25 01:37:24 +0000384 Kind getKind() const {
385 assert(isInitialized() && "querying uninitialized conversion");
386 return Kind(ConversionKind);
387 }
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +0000388
389 /// \brief Return a ranking of the implicit conversion sequence
390 /// kind, where smaller ranks represent better conversion
391 /// sequences.
392 ///
393 /// In particular, this routine gives user-defined conversion
394 /// sequences and ambiguous conversion sequences the same rank,
395 /// per C++ [over.best.ics]p10.
396 unsigned getKindRank() const {
397 switch (getKind()) {
398 case StandardConversion:
399 return 0;
400
401 case UserDefinedConversion:
402 case AmbiguousConversion:
403 return 1;
404
405 case EllipsisConversion:
406 return 2;
407
408 case BadConversion:
409 return 3;
410 }
411
412 return 3;
413 }
414
John McCallb1bdc622010-02-25 01:37:24 +0000415 bool isBad() const { return getKind() == BadConversion; }
416 bool isStandard() const { return getKind() == StandardConversion; }
417 bool isEllipsis() const { return getKind() == EllipsisConversion; }
418 bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
419 bool isUserDefined() const { return getKind() == UserDefinedConversion; }
420
421 /// Determines whether this conversion sequence has been
422 /// initialized. Most operations should never need to query
423 /// uninitialized conversions and should assert as above.
424 bool isInitialized() const { return ConversionKind != Uninitialized; }
425
426 /// Sets this sequence as a bad conversion for an explicit argument.
427 void setBad(BadConversionSequence::FailureKind Failure,
428 Expr *FromExpr, QualType ToType) {
429 setKind(BadConversion);
430 Bad.init(Failure, FromExpr, ToType);
John McCall1d318332010-01-12 00:44:57 +0000431 }
432
John McCallb1bdc622010-02-25 01:37:24 +0000433 /// Sets this sequence as a bad conversion for an implicit argument.
434 void setBad(BadConversionSequence::FailureKind Failure,
435 QualType FromType, QualType ToType) {
436 setKind(BadConversion);
437 Bad.init(Failure, FromType, ToType);
438 }
439
John McCall1d318332010-01-12 00:44:57 +0000440 void setStandard() { setKind(StandardConversion); }
441 void setEllipsis() { setKind(EllipsisConversion); }
442 void setUserDefined() { setKind(UserDefinedConversion); }
443 void setAmbiguous() {
John McCallb1bdc622010-02-25 01:37:24 +0000444 if (ConversionKind == AmbiguousConversion) return;
John McCall1d318332010-01-12 00:44:57 +0000445 ConversionKind = AmbiguousConversion;
446 Ambiguous.construct();
447 }
448
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000449 // The result of a comparison between implicit conversion
450 // sequences. Use Sema::CompareImplicitConversionSequences to
451 // actually perform the comparison.
452 enum CompareKind {
Douglas Gregor57373262008-10-22 14:17:15 +0000453 Better = -1,
454 Indistinguishable = 0,
455 Worse = 1
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000456 };
457
458 void DebugPrint() const;
459 };
460
John McCalladbb8f82010-01-13 09:16:55 +0000461 enum OverloadFailureKind {
462 ovl_fail_too_many_arguments,
463 ovl_fail_too_few_arguments,
464 ovl_fail_bad_conversion,
John McCall717e8912010-01-23 05:17:32 +0000465 ovl_fail_bad_deduction,
466
467 /// This conversion candidate was not considered because it
468 /// duplicates the work of a trivial or derived-to-base
469 /// conversion.
470 ovl_fail_trivial_conversion,
471
472 /// This conversion candidate is not viable because its result
473 /// type is not implicitly convertible to the desired type.
Douglas Gregorc520c842010-04-12 23:42:09 +0000474 ovl_fail_bad_final_conversion,
475
476 /// This conversion function template specialization candidate is not
477 /// viable because the final conversion was not an exact match.
478 ovl_fail_final_conversion_not_exact
John McCalladbb8f82010-01-13 09:16:55 +0000479 };
480
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000481 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
482 struct OverloadCandidate {
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000483 /// Function - The actual function that this candidate
Mike Stump1eb44332009-09-09 15:08:12 +0000484 /// represents. When NULL, this is a built-in candidate
485 /// (C++ [over.oper]) or a surrogate for a conversion to a
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000486 /// function pointer or reference (C++ [over.call.object]).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000487 FunctionDecl *Function;
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000488
John McCall9aa472c2010-03-19 07:35:19 +0000489 /// FoundDecl - The original declaration that was looked up /
490 /// invented / otherwise found, together with its access.
491 /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
492 DeclAccessPair FoundDecl;
493
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000494 // BuiltinTypes - Provides the return and parameter types of a
495 // built-in overload candidate. Only valid when Function is NULL.
496 struct {
497 QualType ResultTy;
498 QualType ParamTypes[3];
499 } BuiltinTypes;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000501 /// Surrogate - The conversion function for which this candidate
502 /// is a surrogate, but only if IsSurrogate is true.
503 CXXConversionDecl *Surrogate;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000504
505 /// Conversions - The conversion sequences used to convert the
506 /// function arguments to the function parameters.
507 llvm::SmallVector<ImplicitConversionSequence, 4> Conversions;
508
509 /// Viable - True to indicate that this overload candidate is viable.
510 bool Viable;
Douglas Gregorf1991ea2008-11-07 22:36:19 +0000511
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000512 /// IsSurrogate - True to indicate that this candidate is a
513 /// surrogate for a conversion to a function pointer or reference
514 /// (C++ [over.call.object]).
515 bool IsSurrogate;
516
Douglas Gregor88a35142008-12-22 05:46:06 +0000517 /// IgnoreObjectArgument - True to indicate that the first
518 /// argument's conversion, which for this function represents the
519 /// implicit object argument, should be ignored. This will be true
520 /// when the candidate is a static member function (where the
521 /// implicit object argument is just a placeholder) or a
522 /// non-static member function when the call doesn't have an
523 /// object argument.
524 bool IgnoreObjectArgument;
525
John McCalladbb8f82010-01-13 09:16:55 +0000526 /// FailureKind - The reason why this candidate is not viable.
527 /// Actually an OverloadFailureKind.
528 unsigned char FailureKind;
529
John McCall342fec42010-02-01 18:53:26 +0000530 /// A structure used to record information about a failed
531 /// template argument deduction.
532 struct DeductionFailureInfo {
533 // A Sema::TemplateDeductionResult.
534 unsigned Result;
535
Douglas Gregora9333192010-05-08 17:41:32 +0000536 /// \brief Opaque pointer containing additional data about
537 /// this deduction failure.
538 void *Data;
539
540 /// \brief Retrieve the template parameter this deduction failure
541 /// refers to, if any.
542 TemplateParameter getTemplateParameter();
543
Douglas Gregorec20f462010-05-08 20:07:26 +0000544 /// \brief Retrieve the template argument list associated with this
545 /// deduction failure, if any.
546 TemplateArgumentList *getTemplateArgumentList();
547
Douglas Gregora9333192010-05-08 17:41:32 +0000548 /// \brief Return the first template argument this deduction failure
549 /// refers to, if any.
550 const TemplateArgument *getFirstArg();
551
552 /// \brief Return the second template argument this deduction failure
553 /// refers to, if any.
554 const TemplateArgument *getSecondArg();
555
556 /// \brief Free any memory associated with this deduction failure.
557 void Destroy();
John McCall342fec42010-02-01 18:53:26 +0000558 };
559
560 union {
561 DeductionFailureInfo DeductionFailure;
562
563 /// FinalConversion - For a conversion function (where Function is
564 /// a CXXConversionDecl), the standard conversion that occurs
565 /// after the call to the overload candidate to convert the result
566 /// of calling the conversion function to the required type.
567 StandardConversionSequence FinalConversion;
568 };
John McCall1d318332010-01-12 00:44:57 +0000569
570 /// hasAmbiguousConversion - Returns whether this overload
571 /// candidate requires an ambiguous conversion or not.
572 bool hasAmbiguousConversion() const {
573 for (llvm::SmallVectorImpl<ImplicitConversionSequence>::const_iterator
574 I = Conversions.begin(), E = Conversions.end(); I != E; ++I) {
John McCallb1bdc622010-02-25 01:37:24 +0000575 if (!I->isInitialized()) return false;
John McCall1d318332010-01-12 00:44:57 +0000576 if (I->isAmbiguous()) return true;
577 }
578 return false;
579 }
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000580 };
581
582 /// OverloadCandidateSet - A set of overload candidates, used in C++
583 /// overload resolution (C++ 13.3).
Douglas Gregor3f396022009-09-28 04:47:19 +0000584 class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> {
Douglas Gregor20093b42009-12-09 23:02:17 +0000585 typedef llvm::SmallVector<OverloadCandidate, 16> inherited;
Douglas Gregor3f396022009-09-28 04:47:19 +0000586 llvm::SmallPtrSet<Decl *, 16> Functions;
John McCall5769d612010-02-08 23:07:23 +0000587
588 SourceLocation Loc;
Douglas Gregor0ca4c582010-05-08 18:20:53 +0000589
590 OverloadCandidateSet(const OverloadCandidateSet &);
591 OverloadCandidateSet &operator=(const OverloadCandidateSet &);
592
Douglas Gregor3f396022009-09-28 04:47:19 +0000593 public:
John McCall5769d612010-02-08 23:07:23 +0000594 OverloadCandidateSet(SourceLocation Loc) : Loc(Loc) {}
595
596 SourceLocation getLocation() const { return Loc; }
597
Douglas Gregor3f396022009-09-28 04:47:19 +0000598 /// \brief Determine when this overload candidate will be new to the
599 /// overload set.
600 bool isNewCandidate(Decl *F) {
601 return Functions.insert(F->getCanonicalDecl());
602 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000603
604 /// \brief Clear out all of the candidates.
Douglas Gregora9333192010-05-08 17:41:32 +0000605 void clear();
606
607 ~OverloadCandidateSet() { clear(); }
Douglas Gregor3f396022009-09-28 04:47:19 +0000608 };
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000609} // end namespace clang
610
611#endif // LLVM_CLANG_SEMA_OVERLOAD_H