blob: 096bbf56c9e5d25280a124458ff93f0ad2ee0208 [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 {
29 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)
36 ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7)
37 ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8)
38 ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9)
39 ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10)
40 ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11)
41 ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12)
Douglas Gregor15da57e2008-10-29 02:00:59 +000042 ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics][)
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000043 ICK_Num_Conversion_Kinds ///< The number of conversion kinds
44 };
45
46 /// ImplicitConversionCategory - The category of an implicit
47 /// conversion kind. The enumerator values match with Table 9 of
48 /// (C++ 13.3.3.1.1) and are listed such that better conversion
49 /// categories have smaller values.
50 enum ImplicitConversionCategory {
51 ICC_Identity = 0, ///< Identity
52 ICC_Lvalue_Transformation, ///< Lvalue transformation
53 ICC_Qualification_Adjustment, ///< Qualification adjustment
54 ICC_Promotion, ///< Promotion
55 ICC_Conversion ///< Conversion
56 };
57
58 ImplicitConversionCategory
59 GetConversionCategory(ImplicitConversionKind Kind);
60
61 /// ImplicitConversionRank - The rank of an implicit conversion
62 /// kind. The enumerator values match with Table 9 of (C++
63 /// 13.3.3.1.1) and are listed such that better conversion ranks
64 /// have smaller values.
65 enum ImplicitConversionRank {
66 ICR_Exact_Match = 0, ///< Exact Match
67 ICR_Promotion, ///< Promotion
68 ICR_Conversion ///< Conversion
69 };
70
71 ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
72
73 /// StandardConversionSequence - represents a standard conversion
74 /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
75 /// contains between zero and three conversions. If a particular
76 /// conversion is not needed, it will be set to the identity conversion
77 /// (ICK_Identity). Note that the three conversions are
78 /// specified as separate members (rather than in an array) so that
79 /// we can keep the size of a standard conversion sequence to a
80 /// single word.
81 struct StandardConversionSequence {
82 /// First -- The first conversion can be an lvalue-to-rvalue
83 /// conversion, array-to-pointer conversion, or
84 /// function-to-pointer conversion.
85 ImplicitConversionKind First : 8;
86
87 /// Second - The second conversion can be an integral promotion,
88 /// floating point promotion, integral conversion, floating point
89 /// conversion, floating-integral conversion, pointer conversion,
90 /// pointer-to-member conversion, or boolean conversion.
91 ImplicitConversionKind Second : 8;
92
93 /// Third - The third conversion can be a qualification conversion.
94 ImplicitConversionKind Third : 8;
95
Douglas Gregor15da57e2008-10-29 02:00:59 +000096 /// Deprecated - Whether this the deprecated conversion of a
97 /// string literal to a pointer to non-const character data
98 /// (C++ 4.2p2).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +000099 bool Deprecated : 1;
100
Douglas Gregor45920e82008-12-19 17:40:08 +0000101 /// IncompatibleObjC - Whether this is an Objective-C conversion
102 /// that we should warn about (if we actually use it).
103 bool IncompatibleObjC : 1;
104
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000105 /// ReferenceBinding - True when this is a reference binding
106 /// (C++ [over.ics.ref]).
107 bool ReferenceBinding : 1;
108
109 /// DirectBinding - True when this is a reference binding that is a
110 /// direct binding (C++ [dcl.init.ref]).
111 bool DirectBinding : 1;
112
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000113 /// FromType - The type that this conversion is converting
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000114 /// from. This is an opaque pointer that can be translated into a
115 /// QualType.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000116 void *FromTypePtr;
117
118 /// ToType - The type that this conversion is converting to. This
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000119 /// is an opaque pointer that can be translated into a QualType.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000120 void *ToTypePtr;
121
Douglas Gregor225c41e2008-11-03 19:09:14 +0000122 /// CopyConstructor - The copy constructor that is used to perform
123 /// this conversion, when the conversion is actually just the
124 /// initialization of an object via copy constructor. Such
125 /// conversions are either identity conversions or derived-to-base
126 /// conversions.
127 CXXConstructorDecl *CopyConstructor;
128
Douglas Gregor60d62c22008-10-31 16:23:19 +0000129 void setAsIdentityConversion();
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000130 ImplicitConversionRank getRank() const;
131 bool isPointerConversionToBool() const;
Douglas Gregorbc0805a2008-10-23 00:40:37 +0000132 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000133 void DebugPrint() const;
134 };
135
136 /// UserDefinedConversionSequence - Represents a user-defined
137 /// conversion sequence (C++ 13.3.3.1.2).
138 struct UserDefinedConversionSequence {
139 /// Before - Represents the standard conversion that occurs before
140 /// the actual user-defined conversion. (C++ 13.3.3.1.2p1):
141 ///
142 /// If the user-defined conversion is specified by a constructor
143 /// (12.3.1), the initial standard conversion sequence converts
144 /// the source type to the type required by the argument of the
145 /// constructor. If the user-defined conversion is specified by
146 /// a conversion function (12.3.2), the initial standard
147 /// conversion sequence converts the source type to the implicit
148 /// object parameter of the conversion function.
149 StandardConversionSequence Before;
150
151 /// After - Represents the standard conversion that occurs after
152 /// the actual user-defined conversion.
153 StandardConversionSequence After;
154
155 /// ConversionFunction - The function that will perform the
156 /// user-defined conversion.
157 FunctionDecl* ConversionFunction;
158
159 void DebugPrint() const;
160 };
161
162 /// ImplicitConversionSequence - Represents an implicit conversion
163 /// sequence, which may be a standard conversion sequence
164 // (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
165 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
166 struct ImplicitConversionSequence {
167 /// Kind - The kind of implicit conversion sequence. BadConversion
168 /// specifies that there is no conversion from the source type to
169 /// the target type. The enumerator values are ordered such that
170 /// better implicit conversions have smaller values.
171 enum Kind {
172 StandardConversion = 0,
173 UserDefinedConversion,
174 EllipsisConversion,
175 BadConversion
176 };
177
178 /// ConversionKind - The kind of implicit conversion sequence.
Douglas Gregorf70bdb92008-10-29 14:50:44 +0000179 Kind ConversionKind;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000180
181 union {
182 /// When ConversionKind == StandardConversion, provides the
183 /// details of the standard conversion sequence.
184 StandardConversionSequence Standard;
185
186 /// When ConversionKind == UserDefinedConversion, provides the
187 /// details of the user-defined conversion sequence.
188 UserDefinedConversionSequence UserDefined;
189 };
190
191 // The result of a comparison between implicit conversion
192 // sequences. Use Sema::CompareImplicitConversionSequences to
193 // actually perform the comparison.
194 enum CompareKind {
Douglas Gregor57373262008-10-22 14:17:15 +0000195 Better = -1,
196 Indistinguishable = 0,
197 Worse = 1
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000198 };
199
200 void DebugPrint() const;
201 };
202
203 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
204 struct OverloadCandidate {
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000205 /// Function - The actual function that this candidate
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000206 /// represents. When NULL, this is a built-in candidate
207 /// (C++ [over.oper]) or a surrogate for a conversion to a
208 /// function pointer or reference (C++ [over.call.object]).
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000209 FunctionDecl *Function;
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000210
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000211 // BuiltinTypes - Provides the return and parameter types of a
212 // built-in overload candidate. Only valid when Function is NULL.
213 struct {
214 QualType ResultTy;
215 QualType ParamTypes[3];
216 } BuiltinTypes;
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000217
218 /// Surrogate - The conversion function for which this candidate
219 /// is a surrogate, but only if IsSurrogate is true.
220 CXXConversionDecl *Surrogate;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000221
222 /// Conversions - The conversion sequences used to convert the
223 /// function arguments to the function parameters.
224 llvm::SmallVector<ImplicitConversionSequence, 4> Conversions;
225
226 /// Viable - True to indicate that this overload candidate is viable.
227 bool Viable;
Douglas Gregorf1991ea2008-11-07 22:36:19 +0000228
Douglas Gregor106c6eb2008-11-19 22:57:39 +0000229 /// IsSurrogate - True to indicate that this candidate is a
230 /// surrogate for a conversion to a function pointer or reference
231 /// (C++ [over.call.object]).
232 bool IsSurrogate;
233
Douglas Gregor88a35142008-12-22 05:46:06 +0000234 /// IgnoreObjectArgument - True to indicate that the first
235 /// argument's conversion, which for this function represents the
236 /// implicit object argument, should be ignored. This will be true
237 /// when the candidate is a static member function (where the
238 /// implicit object argument is just a placeholder) or a
239 /// non-static member function when the call doesn't have an
240 /// object argument.
241 bool IgnoreObjectArgument;
242
Douglas Gregorf1991ea2008-11-07 22:36:19 +0000243 /// FinalConversion - For a conversion function (where Function is
244 /// a CXXConversionDecl), the standard conversion that occurs
245 /// after the call to the overload candidate to convert the result
246 /// of calling the conversion function to the required type.
247 StandardConversionSequence FinalConversion;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000248 };
249
250 /// OverloadCandidateSet - A set of overload candidates, used in C++
251 /// overload resolution (C++ 13.3).
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000252 typedef llvm::SmallVector<OverloadCandidate, 16> OverloadCandidateSet;
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000253} // end namespace clang
254
255#endif // LLVM_CLANG_SEMA_OVERLOAD_H