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