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) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 57 | ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 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]) |
| 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 { |
| 86 | ICR_Exact_Match = 0, ///< Exact Match |
| 87 | ICR_Promotion, ///< Promotion |
| 88 | ICR_Conversion ///< Conversion |
| 89 | }; |
| 90 | |
| 91 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind); |
| 92 | |
| 93 | /// StandardConversionSequence - represents a standard conversion |
| 94 | /// sequence (C++ 13.3.3.1.1). A standard conversion sequence |
| 95 | /// contains between zero and three conversions. If a particular |
| 96 | /// conversion is not needed, it will be set to the identity conversion |
| 97 | /// (ICK_Identity). Note that the three conversions are |
| 98 | /// specified as separate members (rather than in an array) so that |
| 99 | /// we can keep the size of a standard conversion sequence to a |
| 100 | /// single word. |
| 101 | struct StandardConversionSequence { |
| 102 | /// First -- The first conversion can be an lvalue-to-rvalue |
| 103 | /// conversion, array-to-pointer conversion, or |
| 104 | /// function-to-pointer conversion. |
| 105 | ImplicitConversionKind First : 8; |
| 106 | |
| 107 | /// Second - The second conversion can be an integral promotion, |
| 108 | /// floating point promotion, integral conversion, floating point |
| 109 | /// conversion, floating-integral conversion, pointer conversion, |
| 110 | /// pointer-to-member conversion, or boolean conversion. |
| 111 | ImplicitConversionKind Second : 8; |
| 112 | |
| 113 | /// Third - The third conversion can be a qualification conversion. |
| 114 | ImplicitConversionKind Third : 8; |
| 115 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 116 | /// Deprecated - Whether this the deprecated conversion of a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | /// string literal to a pointer to non-const character data |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 118 | /// (C++ 4.2p2). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 119 | bool Deprecated : 1; |
| 120 | |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 121 | /// IncompatibleObjC - Whether this is an Objective-C conversion |
| 122 | /// that we should warn about (if we actually use it). |
| 123 | bool IncompatibleObjC : 1; |
| 124 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | /// ReferenceBinding - True when this is a reference binding |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 126 | /// (C++ [over.ics.ref]). |
| 127 | bool ReferenceBinding : 1; |
| 128 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | /// DirectBinding - True when this is a reference binding that is a |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 130 | /// direct binding (C++ [dcl.init.ref]). |
| 131 | bool DirectBinding : 1; |
| 132 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 133 | /// RRefBinding - True when this is a reference binding of an rvalue |
| 134 | /// reference to an rvalue (C++0x [over.ics.rank]p3b4). |
| 135 | bool RRefBinding : 1; |
| 136 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 137 | /// FromType - The type that this conversion is converting |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 138 | /// from. This is an opaque pointer that can be translated into a |
| 139 | /// QualType. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 140 | void *FromTypePtr; |
| 141 | |
| 142 | /// ToType - The type that this conversion is converting to. This |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 143 | /// is an opaque pointer that can be translated into a QualType. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 144 | void *ToTypePtr; |
| 145 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 146 | /// CopyConstructor - The copy constructor that is used to perform |
| 147 | /// this conversion, when the conversion is actually just the |
| 148 | /// initialization of an object via copy constructor. Such |
| 149 | /// conversions are either identity conversions or derived-to-base |
| 150 | /// conversions. |
| 151 | CXXConstructorDecl *CopyConstructor; |
| 152 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 153 | void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } |
| 154 | void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); } |
| 155 | QualType getFromType() const { |
| 156 | return QualType::getFromOpaquePtr(FromTypePtr); |
| 157 | } |
| 158 | QualType getToType() const { |
| 159 | return QualType::getFromOpaquePtr(ToTypePtr); |
| 160 | } |
| 161 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | void setAsIdentityConversion(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 163 | ImplicitConversionRank getRank() const; |
| 164 | bool isPointerConversionToBool() const; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 165 | bool isPointerConversionToVoidPointer(ASTContext& Context) const; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 166 | void DebugPrint() const; |
| 167 | }; |
| 168 | |
| 169 | /// UserDefinedConversionSequence - Represents a user-defined |
| 170 | /// conversion sequence (C++ 13.3.3.1.2). |
| 171 | struct UserDefinedConversionSequence { |
| 172 | /// Before - Represents the standard conversion that occurs before |
| 173 | /// the actual user-defined conversion. (C++ 13.3.3.1.2p1): |
| 174 | /// |
| 175 | /// If the user-defined conversion is specified by a constructor |
| 176 | /// (12.3.1), the initial standard conversion sequence converts |
| 177 | /// the source type to the type required by the argument of the |
| 178 | /// constructor. If the user-defined conversion is specified by |
| 179 | /// a conversion function (12.3.2), the initial standard |
| 180 | /// conversion sequence converts the source type to the implicit |
| 181 | /// object parameter of the conversion function. |
| 182 | StandardConversionSequence Before; |
| 183 | |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 184 | /// EllipsisConversion - When this is true, it means user-defined |
| 185 | /// conversion sequence starts with a ... (elipsis) conversion, instead of |
| 186 | /// a standard conversion. In this case, 'Before' field must be ignored. |
| 187 | // FIXME. I much rather put this as the first field. But there seems to be |
| 188 | // a gcc code gen. bug which causes a crash in a test. Putting it here seems |
| 189 | // to work around the crash. |
| 190 | bool EllipsisConversion : 1; |
| 191 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 192 | /// After - Represents the standard conversion that occurs after |
| 193 | /// the actual user-defined conversion. |
| 194 | StandardConversionSequence After; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 196 | /// ConversionFunction - The function that will perform the |
| 197 | /// user-defined conversion. |
| 198 | FunctionDecl* ConversionFunction; |
| 199 | |
| 200 | void DebugPrint() const; |
| 201 | }; |
| 202 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 203 | /// Represents an ambiguous user-defined conversion sequence. |
| 204 | struct AmbiguousConversionSequence { |
| 205 | typedef llvm::SmallVector<FunctionDecl*, 4> ConversionSet; |
| 206 | |
| 207 | void *FromTypePtr; |
| 208 | void *ToTypePtr; |
| 209 | char Buffer[sizeof(ConversionSet)]; |
| 210 | |
| 211 | QualType getFromType() const { |
| 212 | return QualType::getFromOpaquePtr(FromTypePtr); |
| 213 | } |
| 214 | QualType getToType() const { |
| 215 | return QualType::getFromOpaquePtr(ToTypePtr); |
| 216 | } |
| 217 | void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); } |
| 218 | void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); } |
| 219 | |
| 220 | ConversionSet &conversions() { |
| 221 | return *reinterpret_cast<ConversionSet*>(Buffer); |
| 222 | } |
| 223 | |
| 224 | const ConversionSet &conversions() const { |
| 225 | return *reinterpret_cast<const ConversionSet*>(Buffer); |
| 226 | } |
| 227 | |
| 228 | void addConversion(FunctionDecl *D) { |
| 229 | conversions().push_back(D); |
| 230 | } |
| 231 | |
| 232 | typedef ConversionSet::iterator iterator; |
| 233 | iterator begin() { return conversions().begin(); } |
| 234 | iterator end() { return conversions().end(); } |
| 235 | |
| 236 | typedef ConversionSet::const_iterator const_iterator; |
| 237 | const_iterator begin() const { return conversions().begin(); } |
| 238 | const_iterator end() const { return conversions().end(); } |
| 239 | |
| 240 | void construct(); |
| 241 | void destruct(); |
| 242 | void copyFrom(const AmbiguousConversionSequence &); |
| 243 | }; |
| 244 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 245 | /// BadConversionSequence - Records information about an invalid |
| 246 | /// conversion sequence. |
| 247 | struct BadConversionSequence { |
| 248 | enum FailureKind { |
| 249 | no_conversion, |
| 250 | unrelated_class, |
| 251 | suppressed_user, |
| 252 | bad_qualifiers |
| 253 | }; |
| 254 | |
| 255 | // This can be null, e.g. for implicit object arguments. |
| 256 | Expr *FromExpr; |
| 257 | |
| 258 | FailureKind Kind; |
| 259 | |
| 260 | private: |
| 261 | // The type we're converting from (an opaque QualType). |
| 262 | void *FromTy; |
| 263 | |
| 264 | // The type we're converting to (an opaque QualType). |
| 265 | void *ToTy; |
| 266 | |
| 267 | public: |
| 268 | void init(FailureKind K, Expr *From, QualType To) { |
| 269 | init(K, From->getType(), To); |
| 270 | FromExpr = From; |
| 271 | } |
| 272 | void init(FailureKind K, QualType From, QualType To) { |
| 273 | Kind = K; |
| 274 | FromExpr = 0; |
| 275 | setFromType(From); |
| 276 | setToType(To); |
| 277 | } |
| 278 | |
| 279 | QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); } |
| 280 | QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); } |
| 281 | |
| 282 | void setFromExpr(Expr *E) { |
| 283 | FromExpr = E; |
| 284 | setFromType(E->getType()); |
| 285 | } |
| 286 | void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); } |
| 287 | void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); } |
| 288 | }; |
| 289 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 290 | /// ImplicitConversionSequence - Represents an implicit conversion |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | /// sequence, which may be a standard conversion sequence |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 292 | /// (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] | 293 | /// or an ellipsis conversion sequence (C++ 13.3.3.1.3). |
| 294 | struct ImplicitConversionSequence { |
| 295 | /// Kind - The kind of implicit conversion sequence. BadConversion |
| 296 | /// specifies that there is no conversion from the source type to |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 297 | /// the target type. AmbiguousConversion represents the unique |
| 298 | /// ambiguous conversion (C++0x [over.best.ics]p10). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 299 | enum Kind { |
| 300 | StandardConversion = 0, |
| 301 | UserDefinedConversion, |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 302 | AmbiguousConversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 303 | EllipsisConversion, |
| 304 | BadConversion |
| 305 | }; |
| 306 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 307 | private: |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 308 | /// ConversionKind - The kind of implicit conversion sequence. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 309 | Kind ConversionKind; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 310 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 311 | void setKind(Kind K) { |
| 312 | if (isAmbiguous()) Ambiguous.destruct(); |
| 313 | ConversionKind = K; |
| 314 | } |
| 315 | |
| 316 | public: |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 317 | union { |
| 318 | /// When ConversionKind == StandardConversion, provides the |
| 319 | /// details of the standard conversion sequence. |
| 320 | StandardConversionSequence Standard; |
| 321 | |
| 322 | /// When ConversionKind == UserDefinedConversion, provides the |
| 323 | /// details of the user-defined conversion sequence. |
| 324 | UserDefinedConversionSequence UserDefined; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 325 | |
| 326 | /// When ConversionKind == AmbiguousConversion, provides the |
| 327 | /// details of the ambiguous conversion. |
| 328 | AmbiguousConversionSequence Ambiguous; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 329 | |
| 330 | /// When ConversionKind == BadConversion, provides the details |
| 331 | /// of the bad conversion. |
| 332 | BadConversionSequence Bad; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 333 | }; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 334 | |
| 335 | ImplicitConversionSequence() : ConversionKind(BadConversion) {} |
| 336 | ~ImplicitConversionSequence() { |
| 337 | if (isAmbiguous()) Ambiguous.destruct(); |
| 338 | } |
| 339 | ImplicitConversionSequence(const ImplicitConversionSequence &Other) |
| 340 | : ConversionKind(Other.ConversionKind) |
| 341 | { |
| 342 | switch (ConversionKind) { |
| 343 | case StandardConversion: Standard = Other.Standard; break; |
| 344 | case UserDefinedConversion: UserDefined = Other.UserDefined; break; |
| 345 | case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break; |
| 346 | case EllipsisConversion: break; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 347 | case BadConversion: Bad = Other.Bad; break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | ImplicitConversionSequence & |
| 352 | operator=(const ImplicitConversionSequence &Other) { |
| 353 | if (isAmbiguous()) Ambiguous.destruct(); |
| 354 | new (this) ImplicitConversionSequence(Other); |
| 355 | return *this; |
| 356 | } |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 357 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 358 | Kind getKind() const { return ConversionKind; } |
| 359 | bool isBad() const { return ConversionKind == BadConversion; } |
| 360 | bool isStandard() const { return ConversionKind == StandardConversion; } |
| 361 | bool isEllipsis() const { return ConversionKind == EllipsisConversion; } |
| 362 | bool isAmbiguous() const { return ConversionKind == AmbiguousConversion; } |
| 363 | bool isUserDefined() const { |
| 364 | return ConversionKind == UserDefinedConversion; |
| 365 | } |
| 366 | |
| 367 | void setBad() { setKind(BadConversion); } |
| 368 | void setStandard() { setKind(StandardConversion); } |
| 369 | void setEllipsis() { setKind(EllipsisConversion); } |
| 370 | void setUserDefined() { setKind(UserDefinedConversion); } |
| 371 | void setAmbiguous() { |
| 372 | if (isAmbiguous()) return; |
| 373 | ConversionKind = AmbiguousConversion; |
| 374 | Ambiguous.construct(); |
| 375 | } |
| 376 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 377 | // The result of a comparison between implicit conversion |
| 378 | // sequences. Use Sema::CompareImplicitConversionSequences to |
| 379 | // actually perform the comparison. |
| 380 | enum CompareKind { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 381 | Better = -1, |
| 382 | Indistinguishable = 0, |
| 383 | Worse = 1 |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | void DebugPrint() const; |
| 387 | }; |
| 388 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 389 | enum OverloadFailureKind { |
| 390 | ovl_fail_too_many_arguments, |
| 391 | ovl_fail_too_few_arguments, |
| 392 | ovl_fail_bad_conversion, |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame^] | 393 | ovl_fail_bad_deduction, |
| 394 | |
| 395 | /// This conversion candidate was not considered because it |
| 396 | /// duplicates the work of a trivial or derived-to-base |
| 397 | /// conversion. |
| 398 | ovl_fail_trivial_conversion, |
| 399 | |
| 400 | /// This conversion candidate is not viable because its result |
| 401 | /// type is not implicitly convertible to the desired type. |
| 402 | ovl_fail_bad_final_conversion |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 403 | }; |
| 404 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 405 | /// OverloadCandidate - A single candidate in an overload set (C++ 13.3). |
| 406 | struct OverloadCandidate { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 407 | /// Function - The actual function that this candidate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | /// represents. When NULL, this is a built-in candidate |
| 409 | /// (C++ [over.oper]) or a surrogate for a conversion to a |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 410 | /// function pointer or reference (C++ [over.call.object]). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 411 | FunctionDecl *Function; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 413 | // BuiltinTypes - Provides the return and parameter types of a |
| 414 | // built-in overload candidate. Only valid when Function is NULL. |
| 415 | struct { |
| 416 | QualType ResultTy; |
| 417 | QualType ParamTypes[3]; |
| 418 | } BuiltinTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 420 | /// Surrogate - The conversion function for which this candidate |
| 421 | /// is a surrogate, but only if IsSurrogate is true. |
| 422 | CXXConversionDecl *Surrogate; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 423 | |
| 424 | /// Conversions - The conversion sequences used to convert the |
| 425 | /// function arguments to the function parameters. |
| 426 | llvm::SmallVector<ImplicitConversionSequence, 4> Conversions; |
| 427 | |
| 428 | /// Viable - True to indicate that this overload candidate is viable. |
| 429 | bool Viable; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 431 | /// IsSurrogate - True to indicate that this candidate is a |
| 432 | /// surrogate for a conversion to a function pointer or reference |
| 433 | /// (C++ [over.call.object]). |
| 434 | bool IsSurrogate; |
| 435 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 436 | /// IgnoreObjectArgument - True to indicate that the first |
| 437 | /// argument's conversion, which for this function represents the |
| 438 | /// implicit object argument, should be ignored. This will be true |
| 439 | /// when the candidate is a static member function (where the |
| 440 | /// implicit object argument is just a placeholder) or a |
| 441 | /// non-static member function when the call doesn't have an |
| 442 | /// object argument. |
| 443 | bool IgnoreObjectArgument; |
| 444 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 445 | /// FailureKind - The reason why this candidate is not viable. |
| 446 | /// Actually an OverloadFailureKind. |
| 447 | unsigned char FailureKind; |
| 448 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 449 | /// FinalConversion - For a conversion function (where Function is |
| 450 | /// a CXXConversionDecl), the standard conversion that occurs |
| 451 | /// after the call to the overload candidate to convert the result |
| 452 | /// of calling the conversion function to the required type. |
| 453 | StandardConversionSequence FinalConversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 454 | |
| 455 | /// hasAmbiguousConversion - Returns whether this overload |
| 456 | /// candidate requires an ambiguous conversion or not. |
| 457 | bool hasAmbiguousConversion() const { |
| 458 | for (llvm::SmallVectorImpl<ImplicitConversionSequence>::const_iterator |
| 459 | I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
| 460 | if (I->isAmbiguous()) return true; |
| 461 | } |
| 462 | return false; |
| 463 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 464 | }; |
| 465 | |
| 466 | /// OverloadCandidateSet - A set of overload candidates, used in C++ |
| 467 | /// overload resolution (C++ 13.3). |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 468 | class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 469 | typedef llvm::SmallVector<OverloadCandidate, 16> inherited; |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 470 | llvm::SmallPtrSet<Decl *, 16> Functions; |
| 471 | |
| 472 | public: |
| 473 | /// \brief Determine when this overload candidate will be new to the |
| 474 | /// overload set. |
| 475 | bool isNewCandidate(Decl *F) { |
| 476 | return Functions.insert(F->getCanonicalDecl()); |
| 477 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 478 | |
| 479 | /// \brief Clear out all of the candidates. |
| 480 | void clear() { |
| 481 | inherited::clear(); |
| 482 | Functions.clear(); |
| 483 | } |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 484 | }; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 485 | } // end namespace clang |
| 486 | |
| 487 | #endif // LLVM_CLANG_SEMA_OVERLOAD_H |