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" |
| 19 | #include "clang/AST/Type.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | |
| 23 | namespace clang { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame^] | 24 | class ASTContext; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 25 | class CXXConstructorDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame^] | 26 | class CXXConversionDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 27 | class FunctionDecl; |
| 28 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame^] | 29 | /// OverloadingResult - Capture the result of performing overload |
| 30 | /// resolution. |
| 31 | enum OverloadingResult { |
| 32 | OR_Success, ///< Overload resolution succeeded. |
| 33 | OR_No_Viable_Function, ///< No viable function found. |
| 34 | OR_Ambiguous, ///< Ambiguous candidates found. |
| 35 | OR_Deleted ///< Overload resoltuion refers to a deleted function. |
| 36 | }; |
| 37 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 38 | /// ImplicitConversionKind - The kind of implicit conversion used to |
| 39 | /// convert an argument to a parameter's type. The enumerator values |
| 40 | /// match with Table 9 of (C++ 13.3.3.1.1) and are listed such that |
| 41 | /// better conversion kinds have smaller values. |
| 42 | enum ImplicitConversionKind { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 43 | ICK_Identity = 0, ///< Identity conversion (no conversion) |
| 44 | ICK_Lvalue_To_Rvalue, ///< Lvalue-to-rvalue conversion (C++ 4.1) |
| 45 | ICK_Array_To_Pointer, ///< Array-to-pointer conversion (C++ 4.2) |
| 46 | ICK_Function_To_Pointer, ///< Function-to-pointer (C++ 4.3) |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 47 | ICK_NoReturn_Adjustment, ///< Removal of noreturn from a type (Clang) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 48 | ICK_Qualification, ///< Qualification conversions (C++ 4.4) |
| 49 | ICK_Integral_Promotion, ///< Integral promotions (C++ 4.5) |
| 50 | ICK_Floating_Promotion, ///< Floating point promotions (C++ 4.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 51 | ICK_Complex_Promotion, ///< Complex promotions (Clang extension) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 52 | ICK_Integral_Conversion, ///< Integral conversions (C++ 4.7) |
| 53 | ICK_Floating_Conversion, ///< Floating point conversions (C++ 4.8) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 54 | ICK_Complex_Conversion, ///< Complex conversions (C99 6.3.1.6) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 55 | ICK_Floating_Integral, ///< Floating-integral conversions (C++ 4.9) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 56 | ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 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]) |
| 62 | ICK_Num_Conversion_Kinds ///< The number of conversion kinds |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /// ImplicitConversionCategory - The category of an implicit |
| 66 | /// conversion kind. The enumerator values match with Table 9 of |
| 67 | /// (C++ 13.3.3.1.1) and are listed such that better conversion |
| 68 | /// categories have smaller values. |
| 69 | enum ImplicitConversionCategory { |
| 70 | ICC_Identity = 0, ///< Identity |
| 71 | ICC_Lvalue_Transformation, ///< Lvalue transformation |
| 72 | ICC_Qualification_Adjustment, ///< Qualification adjustment |
| 73 | ICC_Promotion, ///< Promotion |
| 74 | ICC_Conversion ///< Conversion |
| 75 | }; |
| 76 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 78 | GetConversionCategory(ImplicitConversionKind Kind); |
| 79 | |
| 80 | /// ImplicitConversionRank - The rank of an implicit conversion |
| 81 | /// kind. The enumerator values match with Table 9 of (C++ |
| 82 | /// 13.3.3.1.1) and are listed such that better conversion ranks |
| 83 | /// have smaller values. |
| 84 | enum ImplicitConversionRank { |
| 85 | ICR_Exact_Match = 0, ///< Exact Match |
| 86 | ICR_Promotion, ///< Promotion |
| 87 | ICR_Conversion ///< Conversion |
| 88 | }; |
| 89 | |
| 90 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind); |
| 91 | |
| 92 | /// StandardConversionSequence - represents a standard conversion |
| 93 | /// sequence (C++ 13.3.3.1.1). A standard conversion sequence |
| 94 | /// contains between zero and three conversions. If a particular |
| 95 | /// conversion is not needed, it will be set to the identity conversion |
| 96 | /// (ICK_Identity). Note that the three conversions are |
| 97 | /// specified as separate members (rather than in an array) so that |
| 98 | /// we can keep the size of a standard conversion sequence to a |
| 99 | /// single word. |
| 100 | struct StandardConversionSequence { |
| 101 | /// First -- The first conversion can be an lvalue-to-rvalue |
| 102 | /// conversion, array-to-pointer conversion, or |
| 103 | /// function-to-pointer conversion. |
| 104 | ImplicitConversionKind First : 8; |
| 105 | |
| 106 | /// Second - The second conversion can be an integral promotion, |
| 107 | /// floating point promotion, integral conversion, floating point |
| 108 | /// conversion, floating-integral conversion, pointer conversion, |
| 109 | /// pointer-to-member conversion, or boolean conversion. |
| 110 | ImplicitConversionKind Second : 8; |
| 111 | |
| 112 | /// Third - The third conversion can be a qualification conversion. |
| 113 | ImplicitConversionKind Third : 8; |
| 114 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 115 | /// Deprecated - Whether this the deprecated conversion of a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | /// string literal to a pointer to non-const character data |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 117 | /// (C++ 4.2p2). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 118 | bool Deprecated : 1; |
| 119 | |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 120 | /// IncompatibleObjC - Whether this is an Objective-C conversion |
| 121 | /// that we should warn about (if we actually use it). |
| 122 | bool IncompatibleObjC : 1; |
| 123 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | /// ReferenceBinding - True when this is a reference binding |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 125 | /// (C++ [over.ics.ref]). |
| 126 | bool ReferenceBinding : 1; |
| 127 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | /// DirectBinding - True when this is a reference binding that is a |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 129 | /// direct binding (C++ [dcl.init.ref]). |
| 130 | bool DirectBinding : 1; |
| 131 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 132 | /// RRefBinding - True when this is a reference binding of an rvalue |
| 133 | /// reference to an rvalue (C++0x [over.ics.rank]p3b4). |
| 134 | bool RRefBinding : 1; |
| 135 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 136 | /// FromType - The type that this conversion is converting |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 137 | /// from. This is an opaque pointer that can be translated into a |
| 138 | /// QualType. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 139 | void *FromTypePtr; |
| 140 | |
| 141 | /// ToType - The type that this conversion is converting to. This |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 142 | /// is an opaque pointer that can be translated into a QualType. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 143 | void *ToTypePtr; |
| 144 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 145 | /// CopyConstructor - The copy constructor that is used to perform |
| 146 | /// this conversion, when the conversion is actually just the |
| 147 | /// initialization of an object via copy constructor. Such |
| 148 | /// conversions are either identity conversions or derived-to-base |
| 149 | /// conversions. |
| 150 | CXXConstructorDecl *CopyConstructor; |
| 151 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | void setAsIdentityConversion(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | ImplicitConversionRank getRank() const; |
| 154 | bool isPointerConversionToBool() const; |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 155 | bool isPointerConversionToVoidPointer(ASTContext& Context) const; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 156 | void DebugPrint() const; |
| 157 | }; |
| 158 | |
| 159 | /// UserDefinedConversionSequence - Represents a user-defined |
| 160 | /// conversion sequence (C++ 13.3.3.1.2). |
| 161 | struct UserDefinedConversionSequence { |
| 162 | /// Before - Represents the standard conversion that occurs before |
| 163 | /// the actual user-defined conversion. (C++ 13.3.3.1.2p1): |
| 164 | /// |
| 165 | /// If the user-defined conversion is specified by a constructor |
| 166 | /// (12.3.1), the initial standard conversion sequence converts |
| 167 | /// the source type to the type required by the argument of the |
| 168 | /// constructor. If the user-defined conversion is specified by |
| 169 | /// a conversion function (12.3.2), the initial standard |
| 170 | /// conversion sequence converts the source type to the implicit |
| 171 | /// object parameter of the conversion function. |
| 172 | StandardConversionSequence Before; |
| 173 | |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 174 | /// EllipsisConversion - When this is true, it means user-defined |
| 175 | /// conversion sequence starts with a ... (elipsis) conversion, instead of |
| 176 | /// a standard conversion. In this case, 'Before' field must be ignored. |
| 177 | // FIXME. I much rather put this as the first field. But there seems to be |
| 178 | // a gcc code gen. bug which causes a crash in a test. Putting it here seems |
| 179 | // to work around the crash. |
| 180 | bool EllipsisConversion : 1; |
| 181 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// After - Represents the standard conversion that occurs after |
| 183 | /// the actual user-defined conversion. |
| 184 | StandardConversionSequence After; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | /// ConversionFunction - The function that will perform the |
| 187 | /// user-defined conversion. |
| 188 | FunctionDecl* ConversionFunction; |
| 189 | |
| 190 | void DebugPrint() const; |
| 191 | }; |
| 192 | |
| 193 | /// ImplicitConversionSequence - Represents an implicit conversion |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | /// sequence, which may be a standard conversion sequence |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 195 | /// (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] | 196 | /// or an ellipsis conversion sequence (C++ 13.3.3.1.3). |
| 197 | struct ImplicitConversionSequence { |
| 198 | /// Kind - The kind of implicit conversion sequence. BadConversion |
| 199 | /// specifies that there is no conversion from the source type to |
| 200 | /// the target type. The enumerator values are ordered such that |
| 201 | /// better implicit conversions have smaller values. |
| 202 | enum Kind { |
| 203 | StandardConversion = 0, |
| 204 | UserDefinedConversion, |
| 205 | EllipsisConversion, |
| 206 | BadConversion |
| 207 | }; |
| 208 | |
| 209 | /// ConversionKind - The kind of implicit conversion sequence. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 210 | Kind ConversionKind; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 211 | |
| 212 | union { |
| 213 | /// When ConversionKind == StandardConversion, provides the |
| 214 | /// details of the standard conversion sequence. |
| 215 | StandardConversionSequence Standard; |
| 216 | |
| 217 | /// When ConversionKind == UserDefinedConversion, provides the |
| 218 | /// details of the user-defined conversion sequence. |
| 219 | UserDefinedConversionSequence UserDefined; |
| 220 | }; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 221 | |
| 222 | /// When ConversionKind == BadConversion due to multiple conversion |
| 223 | /// functions, this will list those functions. |
| 224 | llvm::SmallVector<FunctionDecl*, 4> ConversionFunctionSet; |
| 225 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 226 | // The result of a comparison between implicit conversion |
| 227 | // sequences. Use Sema::CompareImplicitConversionSequences to |
| 228 | // actually perform the comparison. |
| 229 | enum CompareKind { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 230 | Better = -1, |
| 231 | Indistinguishable = 0, |
| 232 | Worse = 1 |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | void DebugPrint() const; |
| 236 | }; |
| 237 | |
| 238 | /// OverloadCandidate - A single candidate in an overload set (C++ 13.3). |
| 239 | struct OverloadCandidate { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 240 | /// Function - The actual function that this candidate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | /// represents. When NULL, this is a built-in candidate |
| 242 | /// (C++ [over.oper]) or a surrogate for a conversion to a |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 243 | /// function pointer or reference (C++ [over.call.object]). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 244 | FunctionDecl *Function; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 246 | // BuiltinTypes - Provides the return and parameter types of a |
| 247 | // built-in overload candidate. Only valid when Function is NULL. |
| 248 | struct { |
| 249 | QualType ResultTy; |
| 250 | QualType ParamTypes[3]; |
| 251 | } BuiltinTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 253 | /// Surrogate - The conversion function for which this candidate |
| 254 | /// is a surrogate, but only if IsSurrogate is true. |
| 255 | CXXConversionDecl *Surrogate; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 256 | |
| 257 | /// Conversions - The conversion sequences used to convert the |
| 258 | /// function arguments to the function parameters. |
| 259 | llvm::SmallVector<ImplicitConversionSequence, 4> Conversions; |
| 260 | |
| 261 | /// Viable - True to indicate that this overload candidate is viable. |
| 262 | bool Viable; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 263 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 264 | /// IsSurrogate - True to indicate that this candidate is a |
| 265 | /// surrogate for a conversion to a function pointer or reference |
| 266 | /// (C++ [over.call.object]). |
| 267 | bool IsSurrogate; |
| 268 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 269 | /// IgnoreObjectArgument - True to indicate that the first |
| 270 | /// argument's conversion, which for this function represents the |
| 271 | /// implicit object argument, should be ignored. This will be true |
| 272 | /// when the candidate is a static member function (where the |
| 273 | /// implicit object argument is just a placeholder) or a |
| 274 | /// non-static member function when the call doesn't have an |
| 275 | /// object argument. |
| 276 | bool IgnoreObjectArgument; |
| 277 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 278 | /// FinalConversion - For a conversion function (where Function is |
| 279 | /// a CXXConversionDecl), the standard conversion that occurs |
| 280 | /// after the call to the overload candidate to convert the result |
| 281 | /// of calling the conversion function to the required type. |
| 282 | StandardConversionSequence FinalConversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | /// OverloadCandidateSet - A set of overload candidates, used in C++ |
| 286 | /// overload resolution (C++ 13.3). |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 287 | class OverloadCandidateSet : public llvm::SmallVector<OverloadCandidate, 16> { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame^] | 288 | typedef llvm::SmallVector<OverloadCandidate, 16> inherited; |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 289 | llvm::SmallPtrSet<Decl *, 16> Functions; |
| 290 | |
| 291 | public: |
| 292 | /// \brief Determine when this overload candidate will be new to the |
| 293 | /// overload set. |
| 294 | bool isNewCandidate(Decl *F) { |
| 295 | return Functions.insert(F->getCanonicalDecl()); |
| 296 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame^] | 297 | |
| 298 | /// \brief Clear out all of the candidates. |
| 299 | void clear() { |
| 300 | inherited::clear(); |
| 301 | Functions.clear(); |
| 302 | } |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 303 | }; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 304 | } // end namespace clang |
| 305 | |
| 306 | #endif // LLVM_CLANG_SEMA_OVERLOAD_H |