Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- 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 Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 21 | #include "clang/AST/UnresolvedSet.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
| 24 | |
| 25 | namespace clang { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | class ASTContext; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 27 | class CXXConstructorDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 28 | class CXXConversionDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 29 | class FunctionDecl; |
| 30 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 31 | /// OverloadingResult - Capture the result of performing overload |
| 32 | /// resolution. |
| 33 | enum OverloadingResult { |
| 34 | OR_Success, ///< Overload resolution succeeded. |
| 35 | OR_No_Viable_Function, ///< No viable function found. |
| 36 | OR_Ambiguous, ///< Ambiguous candidates found. |
Sean Hunt | c39f697 | 2010-04-07 22:52:07 +0000 | [diff] [blame] | 37 | OR_Deleted ///< Succeeded, but refers to a deleted function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 40 | /// ImplicitConversionKind - The kind of implicit conversion used to |
| 41 | /// convert an argument to a parameter's type. The enumerator values |
| 42 | /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that |
| 43 | /// better conversion kinds have smaller values. |
| 44 | enum ImplicitConversionKind { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 45 | ICK_Identity = 0, ///< Identity conversion (no conversion) |
| 46 | ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1) |
| 47 | ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2) |
| 48 | ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 49 | ICK_NoReturn_Adjustment, ///< Removal of noreturn from a type (Clang) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 50 | ICK_Qualification, ///< Qualification conversions (C++ 4.4) |
| 51 | ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5) |
| 52 | ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 53 | ICK_Complex_Promotion, ///< Complex promotions (Clang extension) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 54 | ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7) |
| 55 | ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 56 | ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 57 | ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9) |
| 58 | ICK_Pointer_Conversion, ///< Pointer conversions (C++ 4.10) |
| 59 | ICK_Pointer_Member, ///< Pointer-to-member conversions (C++ 4.11) |
| 60 | ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12) |
| 61 | ICK_Compatible_Conversion, ///< Conversions between compatible types in C99 |
| 62 | ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics]) |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 63 | ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 64 | ICK_Num_Conversion_Kinds ///< The number of conversion kinds |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /// ImplicitConversionCategory - The category of an implicit |
| 68 | /// conversion kind. The enumerator values match with Table 9 of |
| 69 | /// (C++ 13.3.3.1.1) and are listed such that better conversion |
| 70 | /// categories have smaller values. |
| 71 | enum ImplicitConversionCategory { |
| 72 | ICC_Identity = 0, ///< Identity |
| 73 | ICC_Lvalue_Transformation, ///< Lvalue transformation |
| 74 | ICC_Qualification_Adjustment, ///< Qualification adjustment |
| 75 | ICC_Promotion, ///< Promotion |
| 76 | ICC_Conversion ///< Conversion |
| 77 | }; |
| 78 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 80 | GetConversionCategory(ImplicitConversionKind Kind); |
| 81 | |
| 82 | /// ImplicitConversionRank - The rank of an implicit conversion |
| 83 | /// kind. The enumerator values match with Table 9 of (C++ |
| 84 | /// 13.3.3.1.1) and are listed such that better conversion ranks |
| 85 | /// have smaller values. |
| 86 | enum ImplicitConversionRank { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 87 | ICR_Exact_Match = 0, ///< Exact Match |
| 88 | ICR_Promotion, ///< Promotion |
| 89 | ICR_Conversion, ///< Conversion |
| 90 | ICR_Complex_Real_Conversion ///< Complex <-> Real conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind); |
| 94 | |
| 95 | /// StandardConversionSequence - represents a standard conversion |
| 96 | /// sequence (C++ 13.3.3.1.1). A standard conversion sequence |
| 97 | /// contains between zero and three conversions. If a particular |
| 98 | /// conversion is not needed, it will be set to the identity conversion |
| 99 | /// (ICK_Identity). Note that the three conversions are |
| 100 | /// specified as separate members (rather than in an array) so that |
| 101 | /// we can keep the size of a standard conversion sequence to a |
| 102 | /// single word. |
| 103 | struct StandardConversionSequence { |
| 104 | /// First -- The first conversion can be an lvalue-to-rvalue |
| 105 | /// conversion, array-to-pointer conversion, or |
| 106 | /// function-to-pointer conversion. |
| 107 | ImplicitConversionKind First : 8; |
| 108 | |
| 109 | /// Second - The second conversion can be an integral promotion, |
| 110 | /// floating point promotion, integral conversion, floating point |
| 111 | /// conversion, floating-integral conversion, pointer conversion, |
| 112 | /// pointer-to-member conversion, or boolean conversion. |
| 113 | ImplicitConversionKind Second : 8; |
| 114 | |
| 115 | /// Third - The third conversion can be a qualification conversion. |
| 116 | ImplicitConversionKind Third : 8; |
| 117 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 118 | /// Deprecated - Whether this the deprecated conversion of a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | /// string literal to a pointer to non-const character data |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 120 | /// (C++ 4.2p2). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 121 | bool DeprecatedStringLiteralToCharPtr : 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 122 | |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 123 | /// IncompatibleObjC - Whether this is an Objective-C conversion |
| 124 | /// that we should warn about (if we actually use it). |
| 125 | bool IncompatibleObjC : 1; |
| 126 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | /// ReferenceBinding - True when this is a reference binding |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 128 | /// (C++ [over.ics.ref]). |
| 129 | bool ReferenceBinding : 1; |
| 130 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | /// DirectBinding - True when this is a reference binding that is a |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 132 | /// direct binding (C++ [dcl.init.ref]). |
| 133 | bool DirectBinding : 1; |
| 134 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 135 | /// RRefBinding - True when this is a reference binding of an rvalue |
| 136 | /// reference to an rvalue (C++0x [over.ics.rank]p3b4). |
| 137 | bool RRefBinding : 1; |
| 138 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 139 | /// FromType - The type that this conversion is converting |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 140 | /// from. This is an opaque pointer that can be translated into a |
| 141 | /// QualType. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | void *FromTypePtr; |
| 143 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 144 | /// ToType - The types that this conversion is converting to in |
| 145 | /// each step. This is an opaque pointer that can be translated |
| 146 | /// into a QualType. |
| 147 | void *ToTypePtrs[3]; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 149 | /// CopyConstructor - The copy constructor that is used to perform |
| 150 | /// this conversion, when the conversion is actually just the |
| 151 | /// initialization of an object via copy constructor. Such |
| 152 | /// conversions are either identity conversions or derived-to-base |
| 153 | /// conversions. |
| 154 | CXXConstructorDecl *CopyConstructor; |
| 155 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 156 | void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 157 | void setToType(unsigned Idx, QualType T) { |
| 158 | assert(Idx < 3 && "To type index is out of range"); |
| 159 | ToTypePtrs[Idx] = T.getAsOpaquePtr(); |
| 160 | } |
| 161 | void setAllToTypes(QualType T) { |
| 162 | ToTypePtrs[0] = T.getAsOpaquePtr(); |
| 163 | ToTypePtrs[1] = ToTypePtrs[0]; |
| 164 | ToTypePtrs[2] = ToTypePtrs[0]; |
| 165 | } |
| 166 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 167 | QualType getFromType() const { |
| 168 | return QualType::getFromOpaquePtr(FromTypePtr); |
| 169 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 170 | QualType getToType(unsigned Idx) const { |
| 171 | assert(Idx < 3 && "To type index is out of range"); |
| 172 | return QualType::getFromOpaquePtr(ToTypePtrs[Idx]); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | void setAsIdentityConversion(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 176 | ImplicitConversionRank getRank() const; |
| 177 | bool isPointerConversionToBool() const; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 178 | bool isPointerConversionToVoidPointer(ASTContext& Context) const; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 179 | void DebugPrint() const; |
| 180 | }; |
| 181 | |
| 182 | /// UserDefinedConversionSequence - Represents a user-defined |
| 183 | /// conversion sequence (C++ 13.3.3.1.2). |
| 184 | struct UserDefinedConversionSequence { |
| 185 | /// Before - Represents the standard conversion that occurs before |
| 186 | /// the actual user-defined conversion. (C++ 13.3.3.1.2p1): |
| 187 | /// |
| 188 | /// If the user-defined conversion is specified by a constructor |
| 189 | /// (12.3.1), the initial standard conversion sequence converts |
| 190 | /// the source type to the type required by the argument of the |
| 191 | /// constructor. If the user-defined conversion is specified by |
| 192 | /// a conversion function (12.3.2), the initial standard |
| 193 | /// conversion sequence converts the source type to the implicit |
| 194 | /// object parameter of the conversion function. |
| 195 | StandardConversionSequence Before; |
| 196 | |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 197 | /// EllipsisConversion - When this is true, it means user-defined |
| 198 | /// conversion sequence starts with a ... (elipsis) conversion, instead of |
| 199 | /// a standard conversion. In this case, 'Before' field must be ignored. |
| 200 | // FIXME. I much rather put this as the first field. But there seems to be |
| 201 | // a gcc code gen. bug which causes a crash in a test. Putting it here seems |
| 202 | // to work around the crash. |
| 203 | bool EllipsisConversion : 1; |
| 204 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 205 | /// After - Represents the standard conversion that occurs after |
| 206 | /// the actual user-defined conversion. |
| 207 | StandardConversionSequence After; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 209 | /// ConversionFunction - The function that will perform the |
| 210 | /// user-defined conversion. |
| 211 | FunctionDecl* ConversionFunction; |
| 212 | |
| 213 | void DebugPrint() const; |
| 214 | }; |
| 215 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 216 | /// Represents an ambiguous user-defined conversion sequence. |
| 217 | struct AmbiguousConversionSequence { |
| 218 | typedef llvm::SmallVector<FunctionDecl*, 4> ConversionSet; |
| 219 | |
| 220 | void *FromTypePtr; |
| 221 | void *ToTypePtr; |
| 222 | char Buffer[sizeof(ConversionSet)]; |
| 223 | |
| 224 | QualType getFromType() const { |
| 225 | return QualType::getFromOpaquePtr(FromTypePtr); |
| 226 | } |
| 227 | QualType getToType() const { |
| 228 | return QualType::getFromOpaquePtr(ToTypePtr); |
| 229 | } |
| 230 | void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } |
| 231 | void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); } |
| 232 | |
| 233 | ConversionSet &conversions() { |
| 234 | return *reinterpret_cast<ConversionSet*>(Buffer); |
| 235 | } |
| 236 | |
| 237 | const ConversionSet &conversions() const { |
| 238 | return *reinterpret_cast<const ConversionSet*>(Buffer); |
| 239 | } |
| 240 | |
| 241 | void addConversion(FunctionDecl *D) { |
| 242 | conversions().push_back(D); |
| 243 | } |
| 244 | |
| 245 | typedef ConversionSet::iterator iterator; |
| 246 | iterator begin() { return conversions().begin(); } |
| 247 | iterator end() { return conversions().end(); } |
| 248 | |
| 249 | typedef ConversionSet::const_iterator const_iterator; |
| 250 | const_iterator begin() const { return conversions().begin(); } |
| 251 | const_iterator end() const { return conversions().end(); } |
| 252 | |
| 253 | void construct(); |
| 254 | void destruct(); |
| 255 | void copyFrom(const AmbiguousConversionSequence &); |
| 256 | }; |
| 257 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 258 | /// BadConversionSequence - Records information about an invalid |
| 259 | /// conversion sequence. |
| 260 | struct BadConversionSequence { |
| 261 | enum FailureKind { |
| 262 | no_conversion, |
| 263 | unrelated_class, |
| 264 | suppressed_user, |
| 265 | bad_qualifiers |
| 266 | }; |
| 267 | |
| 268 | // This can be null, e.g. for implicit object arguments. |
| 269 | Expr *FromExpr; |
| 270 | |
| 271 | FailureKind Kind; |
| 272 | |
| 273 | private: |
| 274 | // The type we're converting from (an opaque QualType). |
| 275 | void *FromTy; |
| 276 | |
| 277 | // The type we're converting to (an opaque QualType). |
| 278 | void *ToTy; |
| 279 | |
| 280 | public: |
| 281 | void init(FailureKind K, Expr *From, QualType To) { |
| 282 | init(K, From->getType(), To); |
| 283 | FromExpr = From; |
| 284 | } |
| 285 | void init(FailureKind K, QualType From, QualType To) { |
| 286 | Kind = K; |
| 287 | FromExpr = 0; |
| 288 | setFromType(From); |
| 289 | setToType(To); |
| 290 | } |
| 291 | |
| 292 | QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); } |
| 293 | QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); } |
| 294 | |
| 295 | void setFromExpr(Expr *E) { |
| 296 | FromExpr = E; |
| 297 | setFromType(E->getType()); |
| 298 | } |
| 299 | void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); } |
| 300 | void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); } |
| 301 | }; |
| 302 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 303 | /// ImplicitConversionSequence - Represents an implicit conversion |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | /// sequence, which may be a standard conversion sequence |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 305 | /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2), |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 306 | /// or an ellipsis conversion sequence (C++ 13.3.3.1.3). |
| 307 | struct ImplicitConversionSequence { |
| 308 | /// Kind - The kind of implicit conversion sequence. BadConversion |
| 309 | /// specifies that there is no conversion from the source type to |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 310 | /// the target type. AmbiguousConversion represents the unique |
| 311 | /// ambiguous conversion (C++0x [over.best.ics]p10). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 312 | enum Kind { |
| 313 | StandardConversion = 0, |
| 314 | UserDefinedConversion, |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 315 | AmbiguousConversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 316 | EllipsisConversion, |
| 317 | BadConversion |
| 318 | }; |
| 319 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 320 | private: |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 321 | enum { |
| 322 | Uninitialized = BadConversion + 1 |
| 323 | }; |
| 324 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 325 | /// ConversionKind - The kind of implicit conversion sequence. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 326 | unsigned ConversionKind; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 327 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 328 | void setKind(Kind K) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 329 | destruct(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 330 | ConversionKind = K; |
| 331 | } |
| 332 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 333 | void destruct() { |
| 334 | if (ConversionKind == AmbiguousConversion) Ambiguous.destruct(); |
| 335 | } |
| 336 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 337 | public: |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 338 | union { |
| 339 | /// When ConversionKind == StandardConversion, provides the |
| 340 | /// details of the standard conversion sequence. |
| 341 | StandardConversionSequence Standard; |
| 342 | |
| 343 | /// When ConversionKind == UserDefinedConversion, provides the |
| 344 | /// details of the user-defined conversion sequence. |
| 345 | UserDefinedConversionSequence UserDefined; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 346 | |
| 347 | /// When ConversionKind == AmbiguousConversion, provides the |
| 348 | /// details of the ambiguous conversion. |
| 349 | AmbiguousConversionSequence Ambiguous; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 350 | |
| 351 | /// When ConversionKind == BadConversion, provides the details |
| 352 | /// of the bad conversion. |
| 353 | BadConversionSequence Bad; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 354 | }; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 355 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 356 | ImplicitConversionSequence() : ConversionKind(Uninitialized) {} |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 357 | ~ImplicitConversionSequence() { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 358 | destruct(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 359 | } |
| 360 | ImplicitConversionSequence(const ImplicitConversionSequence &Other) |
| 361 | : ConversionKind(Other.ConversionKind) |
| 362 | { |
| 363 | switch (ConversionKind) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 364 | case Uninitialized: break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 365 | case StandardConversion: Standard = Other.Standard; break; |
| 366 | case UserDefinedConversion: UserDefined = Other.UserDefined; break; |
| 367 | case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break; |
| 368 | case EllipsisConversion: break; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 369 | case BadConversion: Bad = Other.Bad; break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | ImplicitConversionSequence & |
| 374 | operator=(const ImplicitConversionSequence &Other) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 375 | destruct(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 376 | new (this) ImplicitConversionSequence(Other); |
| 377 | return *this; |
| 378 | } |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 379 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 380 | Kind getKind() const { |
| 381 | assert(isInitialized() && "querying uninitialized conversion"); |
| 382 | return Kind(ConversionKind); |
| 383 | } |
| 384 | bool isBad() const { return getKind() == BadConversion; } |
| 385 | bool isStandard() const { return getKind() == StandardConversion; } |
| 386 | bool isEllipsis() const { return getKind() == EllipsisConversion; } |
| 387 | bool isAmbiguous() const { return getKind() == AmbiguousConversion; } |
| 388 | bool isUserDefined() const { return getKind() == UserDefinedConversion; } |
| 389 | |
| 390 | /// Determines whether this conversion sequence has been |
| 391 | /// initialized. Most operations should never need to query |
| 392 | /// uninitialized conversions and should assert as above. |
| 393 | bool isInitialized() const { return ConversionKind != Uninitialized; } |
| 394 | |
| 395 | /// Sets this sequence as a bad conversion for an explicit argument. |
| 396 | void setBad(BadConversionSequence::FailureKind Failure, |
| 397 | Expr *FromExpr, QualType ToType) { |
| 398 | setKind(BadConversion); |
| 399 | Bad.init(Failure, FromExpr, ToType); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 400 | } |
| 401 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 402 | /// Sets this sequence as a bad conversion for an implicit argument. |
| 403 | void setBad(BadConversionSequence::FailureKind Failure, |
| 404 | QualType FromType, QualType ToType) { |
| 405 | setKind(BadConversion); |
| 406 | Bad.init(Failure, FromType, ToType); |
| 407 | } |
| 408 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 409 | void setStandard() { setKind(StandardConversion); } |
| 410 | void setEllipsis() { setKind(EllipsisConversion); } |
| 411 | void setUserDefined() { setKind(UserDefinedConversion); } |
| 412 | void setAmbiguous() { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 413 | if (ConversionKind == AmbiguousConversion) return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 414 | ConversionKind = AmbiguousConversion; |
| 415 | Ambiguous.construct(); |
| 416 | } |
| 417 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 418 | // The result of a comparison between implicit conversion |
| 419 | // sequences. Use Sema::CompareImplicitConversionSequences to |
| 420 | // actually perform the comparison. |
| 421 | enum CompareKind { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 422 | Better = -1, |
| 423 | Indistinguishable = 0, |
| 424 | Worse = 1 |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | void DebugPrint() const; |
| 428 | }; |
| 429 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 430 | enum OverloadFailureKind { |
| 431 | ovl_fail_too_many_arguments, |
| 432 | ovl_fail_too_few_arguments, |
| 433 | ovl_fail_bad_conversion, |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 434 | ovl_fail_bad_deduction, |
| 435 | |
| 436 | /// This conversion candidate was not considered because it |
| 437 | /// duplicates the work of a trivial or derived-to-base |
| 438 | /// conversion. |
| 439 | ovl_fail_trivial_conversion, |
| 440 | |
| 441 | /// This conversion candidate is not viable because its result |
| 442 | /// type is not implicitly convertible to the desired type. |
| 443 | ovl_fail_bad_final_conversion |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 446 | /// OverloadCandidate - A single candidate in an overload set (C++ 13.3). |
| 447 | struct OverloadCandidate { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 448 | /// Function - The actual function that this candidate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | /// represents. When NULL, this is a built-in candidate |
| 450 | /// (C++ [over.oper]) or a surrogate for a conversion to a |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 451 | /// function pointer or reference (C++ [over.call.object]). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 452 | FunctionDecl *Function; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 453 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 454 | /// FoundDecl - The original declaration that was looked up / |
| 455 | /// invented / otherwise found, together with its access. |
| 456 | /// Might be a UsingShadowDecl or a FunctionTemplateDecl. |
| 457 | DeclAccessPair FoundDecl; |
| 458 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 459 | // BuiltinTypes - Provides the return and parameter types of a |
| 460 | // built-in overload candidate. Only valid when Function is NULL. |
| 461 | struct { |
| 462 | QualType ResultTy; |
| 463 | QualType ParamTypes[3]; |
| 464 | } BuiltinTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 466 | /// Surrogate - The conversion function for which this candidate |
| 467 | /// is a surrogate, but only if IsSurrogate is true. |
| 468 | CXXConversionDecl *Surrogate; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 469 | |
| 470 | /// Conversions - The conversion sequences used to convert the |
| 471 | /// function arguments to the function parameters. |
| 472 | llvm::SmallVector<ImplicitConversionSequence, 4> Conversions; |
| 473 | |
| 474 | /// Viable - True to indicate that this overload candidate is viable. |
| 475 | bool Viable; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 477 | /// IsSurrogate - True to indicate that this candidate is a |
| 478 | /// surrogate for a conversion to a function pointer or reference |
| 479 | /// (C++ [over.call.object]). |
| 480 | bool IsSurrogate; |
| 481 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 482 | /// IgnoreObjectArgument - True to indicate that the first |
| 483 | /// argument's conversion, which for this function represents the |
| 484 | /// implicit object argument, should be ignored. This will be true |
| 485 | /// when the candidate is a static member function (where the |
| 486 | /// implicit object argument is just a placeholder) or a |
| 487 | /// non-static member function when the call doesn't have an |
| 488 | /// object argument. |
| 489 | bool IgnoreObjectArgument; |
| 490 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 491 | /// FailureKind - The reason why this candidate is not viable. |
| 492 | /// Actually an OverloadFailureKind. |
| 493 | unsigned char FailureKind; |
| 494 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 495 | /// A structure used to record information about a failed |
| 496 | /// template argument deduction. |
| 497 | struct DeductionFailureInfo { |
| 498 | // A Sema::TemplateDeductionResult. |
| 499 | unsigned Result; |
| 500 | |
| 501 | // A TemplateParameter. |
| 502 | void *TemplateParameter; |
| 503 | }; |
| 504 | |
| 505 | union { |
| 506 | DeductionFailureInfo DeductionFailure; |
| 507 | |
| 508 | /// FinalConversion - For a conversion function (where Function is |
| 509 | /// a CXXConversionDecl), the standard conversion that occurs |
| 510 | /// after the call to the overload candidate to convert the result |
| 511 | /// of calling the conversion function to the required type. |
| 512 | StandardConversionSequence FinalConversion; |
| 513 | }; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 514 | |
| 515 | /// hasAmbiguousConversion - Returns whether this overload |
| 516 | /// candidate requires an ambiguous conversion or not. |
| 517 | bool hasAmbiguousConversion() const { |
| 518 | for (llvm::SmallVectorImpl<ImplicitConversionSequence>::const_iterator |
| 519 | I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 520 | if (!I->isInitialized()) return false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 521 | if (I->isAmbiguous()) return true; |
| 522 | } |
| 523 | return false; |
| 524 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | /// OverloadCandidateSet - A set of overload candidates, used in C++ |
| 528 | /// overload resolution (C++ 13.3). |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 529 | class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 530 | typedef llvm::SmallVector<OverloadCandidate, 16> inherited; |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 531 | llvm::SmallPtrSet<Decl *, 16> Functions; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 532 | |
| 533 | SourceLocation Loc; |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 534 | public: |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 535 | OverloadCandidateSet(SourceLocation Loc) : Loc(Loc) {} |
| 536 | |
| 537 | SourceLocation getLocation() const { return Loc; } |
| 538 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 539 | /// \brief Determine when this overload candidate will be new to the |
| 540 | /// overload set. |
| 541 | bool isNewCandidate(Decl *F) { |
| 542 | return Functions.insert(F->getCanonicalDecl()); |
| 543 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 544 | |
| 545 | /// \brief Clear out all of the candidates. |
| 546 | void clear() { |
| 547 | inherited::clear(); |
| 548 | Functions.clear(); |
| 549 | } |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 550 | }; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 551 | } // end namespace clang |
| 552 | |
| 553 | #endif // LLVM_CLANG_SEMA_OVERLOAD_H |