blob: 24b796b21143124abac8f5e5fbfe615f2b7ffb5e [file] [log] [blame]
Douglas Gregord2baafd2008-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 Gregora3b34bb2008-11-03 19:09:14 +000021 class CXXConstructorDecl;
Douglas Gregord2baafd2008-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 Gregor2aecd1f2008-10-29 02:00:59 +000042 ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics][)
Douglas Gregord2baafd2008-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 Gregor2aecd1f2008-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 Gregord2baafd2008-10-21 16:13:35 +000099 bool Deprecated : 1;
100
Douglas Gregor0e343382008-10-29 14:50:44 +0000101 /// ReferenceBinding - True when this is a reference binding
102 /// (C++ [over.ics.ref]).
103 bool ReferenceBinding : 1;
104
105 /// DirectBinding - True when this is a reference binding that is a
106 /// direct binding (C++ [dcl.init.ref]).
107 bool DirectBinding : 1;
108
Douglas Gregord2baafd2008-10-21 16:13:35 +0000109 /// FromType - The type that this conversion is converting
Douglas Gregor14046502008-10-23 00:40:37 +0000110 /// from. This is an opaque pointer that can be translated into a
111 /// QualType.
Douglas Gregord2baafd2008-10-21 16:13:35 +0000112 void *FromTypePtr;
113
114 /// ToType - The type that this conversion is converting to. This
Douglas Gregor14046502008-10-23 00:40:37 +0000115 /// is an opaque pointer that can be translated into a QualType.
Douglas Gregord2baafd2008-10-21 16:13:35 +0000116 void *ToTypePtr;
117
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000118 /// CopyConstructor - The copy constructor that is used to perform
119 /// this conversion, when the conversion is actually just the
120 /// initialization of an object via copy constructor. Such
121 /// conversions are either identity conversions or derived-to-base
122 /// conversions.
123 CXXConstructorDecl *CopyConstructor;
124
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000125 void setAsIdentityConversion();
Douglas Gregord2baafd2008-10-21 16:13:35 +0000126 ImplicitConversionRank getRank() const;
127 bool isPointerConversionToBool() const;
Douglas Gregor14046502008-10-23 00:40:37 +0000128 bool isPointerConversionToVoidPointer(ASTContext& Context) const;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000129 void DebugPrint() const;
130 };
131
132 /// UserDefinedConversionSequence - Represents a user-defined
133 /// conversion sequence (C++ 13.3.3.1.2).
134 struct UserDefinedConversionSequence {
135 /// Before - Represents the standard conversion that occurs before
136 /// the actual user-defined conversion. (C++ 13.3.3.1.2p1):
137 ///
138 /// If the user-defined conversion is specified by a constructor
139 /// (12.3.1), the initial standard conversion sequence converts
140 /// the source type to the type required by the argument of the
141 /// constructor. If the user-defined conversion is specified by
142 /// a conversion function (12.3.2), the initial standard
143 /// conversion sequence converts the source type to the implicit
144 /// object parameter of the conversion function.
145 StandardConversionSequence Before;
146
147 /// After - Represents the standard conversion that occurs after
148 /// the actual user-defined conversion.
149 StandardConversionSequence After;
150
151 /// ConversionFunction - The function that will perform the
152 /// user-defined conversion.
153 FunctionDecl* ConversionFunction;
154
155 void DebugPrint() const;
156 };
157
158 /// ImplicitConversionSequence - Represents an implicit conversion
159 /// sequence, which may be a standard conversion sequence
160 // (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
161 /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
162 struct ImplicitConversionSequence {
163 /// Kind - The kind of implicit conversion sequence. BadConversion
164 /// specifies that there is no conversion from the source type to
165 /// the target type. The enumerator values are ordered such that
166 /// better implicit conversions have smaller values.
167 enum Kind {
168 StandardConversion = 0,
169 UserDefinedConversion,
170 EllipsisConversion,
171 BadConversion
172 };
173
174 /// ConversionKind - The kind of implicit conversion sequence.
Douglas Gregor0e343382008-10-29 14:50:44 +0000175 Kind ConversionKind;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000176
177 union {
178 /// When ConversionKind == StandardConversion, provides the
179 /// details of the standard conversion sequence.
180 StandardConversionSequence Standard;
181
182 /// When ConversionKind == UserDefinedConversion, provides the
183 /// details of the user-defined conversion sequence.
184 UserDefinedConversionSequence UserDefined;
185 };
186
187 // The result of a comparison between implicit conversion
188 // sequences. Use Sema::CompareImplicitConversionSequences to
189 // actually perform the comparison.
190 enum CompareKind {
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000191 Better = -1,
192 Indistinguishable = 0,
193 Worse = 1
Douglas Gregord2baafd2008-10-21 16:13:35 +0000194 };
195
196 void DebugPrint() const;
197 };
198
199 /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
200 struct OverloadCandidate {
Douglas Gregor70d26122008-11-12 17:17:38 +0000201 /// Function - The actual function that this candidate
202 /// represents. When NULL, this is a built-in candidate.
Douglas Gregord2baafd2008-10-21 16:13:35 +0000203 FunctionDecl *Function;
Douglas Gregor70d26122008-11-12 17:17:38 +0000204
205 // BuiltinTypes - Provides the return and parameter types of a
206 // built-in overload candidate. Only valid when Function is NULL.
207 struct {
208 QualType ResultTy;
209 QualType ParamTypes[3];
210 } BuiltinTypes;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000211
212 /// Conversions - The conversion sequences used to convert the
213 /// function arguments to the function parameters.
214 llvm::SmallVector<ImplicitConversionSequence, 4> Conversions;
215
216 /// Viable - True to indicate that this overload candidate is viable.
217 bool Viable;
Douglas Gregor60714f92008-11-07 22:36:19 +0000218
219 /// FinalConversion - For a conversion function (where Function is
220 /// a CXXConversionDecl), the standard conversion that occurs
221 /// after the call to the overload candidate to convert the result
222 /// of calling the conversion function to the required type.
223 StandardConversionSequence FinalConversion;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000224 };
225
226 /// OverloadCandidateSet - A set of overload candidates, used in C++
227 /// overload resolution (C++ 13.3).
Douglas Gregor70d26122008-11-12 17:17:38 +0000228 typedef llvm::SmallVector<OverloadCandidate, 16> OverloadCandidateSet;
Douglas Gregord2baafd2008-10-21 16:13:35 +0000229} // end namespace clang
230
231#endif // LLVM_CLANG_SEMA_OVERLOAD_H