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