blob: 0d1f37aa4beb2461ed9070684e87fe157d38751e [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
18#include "llvm/ADT/SmallVector.h"
19
20namespace clang {
Douglas Gregor225c41e2008-11-03 19:09:14 +000021 class CXXConstructorDecl;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000022 class FunctionDecl;
23
24 /// ImplicitConversionKind - The kind of implicit conversion used to
25 /// convert an argument to a parameter's type. The enumerator values
26 /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that
27 /// better conversion kinds have smaller values.
28 enum ImplicitConversionKind {
Douglas Gregorf9201e02009-02-11 23:02:49 +000029 ICK_Identity = 0, ///< Identity conversion (no conversion)
30 ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1)
31 ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2)
32 ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3)
33 ICK_Qualification, ///< Qualification conversions (C++ 4.4)
34 ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5)
35 ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6)
Douglas Gregor5cdf8212009-02-12 00:15:05 +000036 ICK_Complex_Promotion, ///< Complex promotions (Clang extension)
Douglas Gregorf9201e02009-02-11 23:02:49 +000037 ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7)
38 ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8)
Douglas Gregor5cdf8212009-02-12 00:15:05 +000039 ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6)
Douglas Gregorf9201e02009-02-11 23:02:49 +000040 ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9)
Douglas Gregor5cdf8212009-02-12 00:15:05 +000041 ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7)
Douglas Gregorf9201e02009-02-11 23:02:49 +000042 ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10)
43 ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11)
44 ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12)
45 ICK_Compatible_Conversion, ///< Conversions between compatible types in C99
46 ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics])
47 ICK_Num_Conversion_Kinds ///< The number of conversion kinds
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000048 };
49
50 /// ImplicitConversionCategory - The category of an implicit
51 /// conversion kind. The enumerator values match with Table 9 of
52 /// (C++ 13.3.3.1.1) and are listed such that better conversion
53 /// categories have smaller values.
54 enum ImplicitConversionCategory {
55 ICC_Identity = 0, ///< Identity
56 ICC_Lvalue_Transformation, ///< Lvalue transformation
57 ICC_Qualification_Adjustment, ///< Qualification adjustment
58 ICC_Promotion, ///< Promotion
59 ICC_Conversion ///< Conversion
60 };
61
Mike Stump1eb44332009-09-09 15:08:12 +000062 ImplicitConversionCategory
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000063 GetConversionCategory(ImplicitConversionKind Kind);
64
65 /// ImplicitConversionRank - The rank of an implicit conversion
66 /// kind. The enumerator values match with Table 9 of (C++
67 /// 13.3.3.1.1) and are listed such that better conversion ranks
68 /// have smaller values.
69 enum ImplicitConversionRank {
70 ICR_Exact_Match = 0, ///< Exact Match
71 ICR_Promotion, ///< Promotion
72 ICR_Conversion ///< Conversion
73 };
74
75 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
76
77 /// StandardConversionSequence - represents a standard conversion
78 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
79 /// contains between zero and three conversions. If a particular
80 /// conversion is not needed, it will be set to the identity conversion
81 /// (ICK_Identity). Note that the three conversions are
82 /// specified as separate members (rather than in an array) so that
83 /// we can keep the size of a standard conversion sequence to a
84 /// single word.
85 struct StandardConversionSequence {
86 /// First -- The first conversion can be an lvalue-to-rvalue
87 /// conversion, array-to-pointer conversion, or
88 /// function-to-pointer conversion.
89 ImplicitConversionKind First : 8;
90
91 /// Second - The second conversion can be an integral promotion,
92 /// floating point promotion, integral conversion, floating point
93 /// conversion, floating-integral conversion, pointer conversion,
94 /// pointer-to-member conversion, or boolean conversion.
95 ImplicitConversionKind Second : 8;
96
97 /// Third - The third conversion can be a qualification conversion.
98 ImplicitConversionKind Third : 8;
99
Douglas Gregor15da57e2008-10-29 02:00:59 +0000100 /// Deprecated - Whether this the deprecated conversion of a
Mike Stump1eb44332009-09-09 15:08:12 +0000101 /// string literal to a pointer to non-const character data
Douglas Gregor15da57e2008-10-29 02:00:59 +0000102 /// (C++ 4.2p2).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000103 bool Deprecated : 1;
104
Douglas Gregor45920e82008-12-19 17:40:08 +0000105 /// IncompatibleObjC - Whether this is an Objective-C conversion
106 /// that we should warn about (if we actually use it).
107 bool IncompatibleObjC : 1;
108
Mike Stump1eb44332009-09-09 15:08:12 +0000109 /// ReferenceBinding - True when this is a reference binding
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000110 /// (C++ [over.ics.ref]).
111 bool ReferenceBinding : 1;
112
Mike Stump1eb44332009-09-09 15:08:12 +0000113 /// DirectBinding - True when this is a reference binding that is a
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000114 /// direct binding (C++ [dcl.init.ref]).
115 bool DirectBinding : 1;
116
Sebastian Redla9845802009-03-29 15:27:50 +0000117 /// RRefBinding - True when this is a reference binding of an rvalue
118 /// reference to an rvalue (C++0x [over.ics.rank]p3b4).
119 bool RRefBinding : 1;
120
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000121 /// FromType - The type that this conversion is converting
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000122 /// from. This is an opaque pointer that can be translated into a
123 /// QualType.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000124 void *FromTypePtr;
125
126 /// ToType - The type that this conversion is converting to. This
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000127 /// is an opaque pointer that can be translated into a QualType.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000128 void *ToTypePtr;
129
Douglas Gregor225c41e2008-11-03 19:09:14 +0000130 /// CopyConstructor - The copy constructor that is used to perform
131 /// this conversion, when the conversion is actually just the
132 /// initialization of an object via copy constructor. Such
133 /// conversions are either identity conversions or derived-to-base
134 /// conversions.
135 CXXConstructorDecl *CopyConstructor;
136
Mike Stump1eb44332009-09-09 15:08:12 +0000137 void setAsIdentityConversion();
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000138 ImplicitConversionRank getRank() const;
139 bool isPointerConversionToBool() const;
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000140 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000141 void DebugPrint() const;
142 };
143
144 /// UserDefinedConversionSequence - Represents a user-defined
145 /// conversion sequence (C++ 13.3.3.1.2).
146 struct UserDefinedConversionSequence {
147 /// Before - Represents the standard conversion that occurs before
148 /// the actual user-defined conversion. (C++ 13.3.3.1.2p1):
149 ///
150 /// If the user-defined conversion is specified by a constructor
151 /// (12.3.1), the initial standard conversion sequence converts
152 /// the source type to the type required by the argument of the
153 /// constructor. If the user-defined conversion is specified by
154 /// a conversion function (12.3.2), the initial standard
155 /// conversion sequence converts the source type to the implicit
156 /// object parameter of the conversion function.
157 StandardConversionSequence Before;
158
Fariborz Jahanian966256a2009-11-06 00:23:08 +0000159 /// EllipsisConversion - When this is true, it means user-defined
160 /// conversion sequence starts with a ... (elipsis) conversion, instead of
161 /// a standard conversion. In this case, 'Before' field must be ignored.
162 // FIXME. I much rather put this as the first field. But there seems to be
163 // a gcc code gen. bug which causes a crash in a test. Putting it here seems
164 // to work around the crash.
165 bool EllipsisConversion : 1;
166
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000167 /// After - Represents the standard conversion that occurs after
168 /// the actual user-defined conversion.
169 StandardConversionSequence After;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000171 /// ConversionFunction - The function that will perform the
172 /// user-defined conversion.
173 FunctionDecl* ConversionFunction;
174
175 void DebugPrint() const;
176 };
177
178 /// ImplicitConversionSequence - Represents an implicit conversion
Mike Stump1eb44332009-09-09 15:08:12 +0000179 /// sequence, which may be a standard conversion sequence
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000180 /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000181 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
182 struct ImplicitConversionSequence {
183 /// Kind - The kind of implicit conversion sequence. BadConversion
184 /// specifies that there is no conversion from the source type to
185 /// the target type. The enumerator values are ordered such that
186 /// better implicit conversions have smaller values.
187 enum Kind {
188 StandardConversion = 0,
189 UserDefinedConversion,
190 EllipsisConversion,
191 BadConversion
192 };
193
194 /// ConversionKind - The kind of implicit conversion sequence.
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000195 Kind ConversionKind;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000196
197 union {
198 /// When ConversionKind == StandardConversion, provides the
199 /// details of the standard conversion sequence.
200 StandardConversionSequence Standard;
201
202 /// When ConversionKind == UserDefinedConversion, provides the
203 /// details of the user-defined conversion sequence.
204 UserDefinedConversionSequence UserDefined;
205 };
Fariborz Jahanianb1663d02009-09-23 00:58:07 +0000206
207 /// When ConversionKind == BadConversion due to multiple conversion
208 /// functions, this will list those functions.
209 llvm::SmallVector<FunctionDecl*, 4> ConversionFunctionSet;
210
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000211 // The result of a comparison between implicit conversion
212 // sequences. Use Sema::CompareImplicitConversionSequences to
213 // actually perform the comparison.
214 enum CompareKind {
Douglas Gregor57373262008-10-22 14:17:15 +0000215 Better = -1,
216 Indistinguishable = 0,
217 Worse = 1
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000218 };
219
220 void DebugPrint() const;
221 };
222
223 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
224 struct OverloadCandidate {
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000225 /// Function - The actual function that this candidate
Mike Stump1eb44332009-09-09 15:08:12 +0000226 /// represents. When NULL, this is a built-in candidate
227 /// (C++ [over.oper]) or a surrogate for a conversion to a
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000228 /// function pointer or reference (C++ [over.call.object]).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000229 FunctionDecl *Function;
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000230
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000231 // BuiltinTypes - Provides the return and parameter types of a
232 // built-in overload candidate. Only valid when Function is NULL.
233 struct {
234 QualType ResultTy;
235 QualType ParamTypes[3];
236 } BuiltinTypes;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000238 /// Surrogate - The conversion function for which this candidate
239 /// is a surrogate, but only if IsSurrogate is true.
240 CXXConversionDecl *Surrogate;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000241
242 /// Conversions - The conversion sequences used to convert the
243 /// function arguments to the function parameters.
244 llvm::SmallVector<ImplicitConversionSequence, 4> Conversions;
245
246 /// Viable - True to indicate that this overload candidate is viable.
247 bool Viable;
Douglas Gregorf1991ea2008-11-07 22:36:19 +0000248
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000249 /// IsSurrogate - True to indicate that this candidate is a
250 /// surrogate for a conversion to a function pointer or reference
251 /// (C++ [over.call.object]).
252 bool IsSurrogate;
253
Douglas Gregor88a35142008-12-22 05:46:06 +0000254 /// IgnoreObjectArgument - True to indicate that the first
255 /// argument's conversion, which for this function represents the
256 /// implicit object argument, should be ignored. This will be true
257 /// when the candidate is a static member function (where the
258 /// implicit object argument is just a placeholder) or a
259 /// non-static member function when the call doesn't have an
260 /// object argument.
261 bool IgnoreObjectArgument;
262
Douglas Gregorf1991ea2008-11-07 22:36:19 +0000263 /// FinalConversion - For a conversion function (where Function is
264 /// a CXXConversionDecl), the standard conversion that occurs
265 /// after the call to the overload candidate to convert the result
266 /// of calling the conversion function to the required type.
267 StandardConversionSequence FinalConversion;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268 };
269
270 /// OverloadCandidateSet - A set of overload candidates, used in C++
271 /// overload resolution (C++ 13.3).
Douglas Gregor3f396022009-09-28 04:47:19 +0000272 class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> {
273 llvm::SmallPtrSet<Decl *, 16> Functions;
274
275 public:
276 /// \brief Determine when this overload candidate will be new to the
277 /// overload set.
278 bool isNewCandidate(Decl *F) {
279 return Functions.insert(F->getCanonicalDecl());
280 }
281 };
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000282} // end namespace clang
283
284#endif // LLVM_CLANG_SEMA_OVERLOAD_H