Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - 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 provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include <algorithm> |
| 26 | |
| 27 | namespace clang { |
| 28 | |
| 29 | /// GetConversionCategory - Retrieve the implicit conversion |
| 30 | /// category corresponding to the given implicit conversion kind. |
| 31 | ImplicitConversionCategory |
| 32 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 33 | static const ImplicitConversionCategory |
| 34 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 35 | ICC_Identity, |
| 36 | ICC_Lvalue_Transformation, |
| 37 | ICC_Lvalue_Transformation, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Qualification_Adjustment, |
| 40 | ICC_Promotion, |
| 41 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 42 | ICC_Promotion, |
| 43 | ICC_Conversion, |
| 44 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 45 | ICC_Conversion, |
| 46 | ICC_Conversion, |
| 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 50 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 51 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 52 | ICC_Conversion |
| 53 | }; |
| 54 | return Category[(int)Kind]; |
| 55 | } |
| 56 | |
| 57 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 58 | /// corresponding to the given implicit conversion kind. |
| 59 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 60 | static const ImplicitConversionRank |
| 61 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 62 | ICR_Exact_Match, |
| 63 | ICR_Exact_Match, |
| 64 | ICR_Exact_Match, |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Promotion, |
| 68 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 69 | ICR_Promotion, |
| 70 | ICR_Conversion, |
| 71 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 72 | ICR_Conversion, |
| 73 | ICR_Conversion, |
| 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 77 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 78 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 79 | ICR_Conversion |
| 80 | }; |
| 81 | return Rank[(int)Kind]; |
| 82 | } |
| 83 | |
| 84 | /// GetImplicitConversionName - Return the name of this kind of |
| 85 | /// implicit conversion. |
| 86 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 87 | static const char* Name[(int)ICK_Num_Conversion_Kinds] = { |
| 88 | "No conversion", |
| 89 | "Lvalue-to-rvalue", |
| 90 | "Array-to-pointer", |
| 91 | "Function-to-pointer", |
| 92 | "Qualification", |
| 93 | "Integral promotion", |
| 94 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 95 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 96 | "Integral conversion", |
| 97 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 98 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 99 | "Floating-integral conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex-real conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Pointer conversion", |
| 102 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 103 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 104 | "Compatible-types conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | }; |
| 107 | return Name[Kind]; |
| 108 | } |
| 109 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 110 | /// StandardConversionSequence - Set the standard conversion |
| 111 | /// sequence to the identity conversion. |
| 112 | void StandardConversionSequence::setAsIdentityConversion() { |
| 113 | First = ICK_Identity; |
| 114 | Second = ICK_Identity; |
| 115 | Third = ICK_Identity; |
| 116 | Deprecated = false; |
| 117 | ReferenceBinding = false; |
| 118 | DirectBinding = false; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 119 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 120 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 123 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 124 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 125 | /// implicit conversions. |
| 126 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 127 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 128 | if (GetConversionRank(First) > Rank) |
| 129 | Rank = GetConversionRank(First); |
| 130 | if (GetConversionRank(Second) > Rank) |
| 131 | Rank = GetConversionRank(Second); |
| 132 | if (GetConversionRank(Third) > Rank) |
| 133 | Rank = GetConversionRank(Third); |
| 134 | return Rank; |
| 135 | } |
| 136 | |
| 137 | /// isPointerConversionToBool - Determines whether this conversion is |
| 138 | /// a conversion of a pointer or pointer-to-member to bool. This is |
| 139 | /// used as part of the ranking of standard conversion sequences |
| 140 | /// (C++ 13.3.3.2p4). |
| 141 | bool StandardConversionSequence::isPointerConversionToBool() const |
| 142 | { |
| 143 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 144 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 145 | |
| 146 | // Note that FromType has not necessarily been transformed by the |
| 147 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 148 | // check for their presence as well as checking whether FromType is |
| 149 | // a pointer. |
| 150 | if (ToType->isBooleanType() && |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 151 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 152 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 153 | return true; |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 158 | /// isPointerConversionToVoidPointer - Determines whether this |
| 159 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 160 | /// used as part of the ranking of standard conversion sequences (C++ |
| 161 | /// 13.3.3.2p4). |
| 162 | bool |
| 163 | StandardConversionSequence:: |
| 164 | isPointerConversionToVoidPointer(ASTContext& Context) const |
| 165 | { |
| 166 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 167 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
| 175 | if (Second == ICK_Pointer_Conversion) |
| 176 | if (const PointerType* ToPtrType = ToType->getAsPointerType()) |
| 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
| 185 | bool PrintedSomething = false; |
| 186 | if (First != ICK_Identity) { |
| 187 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 188 | PrintedSomething = true; |
| 189 | } |
| 190 | |
| 191 | if (Second != ICK_Identity) { |
| 192 | if (PrintedSomething) { |
| 193 | fprintf(stderr, " -> "); |
| 194 | } |
| 195 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 196 | |
| 197 | if (CopyConstructor) { |
| 198 | fprintf(stderr, " (by copy constructor)"); |
| 199 | } else if (DirectBinding) { |
| 200 | fprintf(stderr, " (direct reference binding)"); |
| 201 | } else if (ReferenceBinding) { |
| 202 | fprintf(stderr, " (reference binding)"); |
| 203 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | PrintedSomething = true; |
| 205 | } |
| 206 | |
| 207 | if (Third != ICK_Identity) { |
| 208 | if (PrintedSomething) { |
| 209 | fprintf(stderr, " -> "); |
| 210 | } |
| 211 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 212 | PrintedSomething = true; |
| 213 | } |
| 214 | |
| 215 | if (!PrintedSomething) { |
| 216 | fprintf(stderr, "No conversions required"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 221 | /// error. Useful for debugging overloading issues. |
| 222 | void UserDefinedConversionSequence::DebugPrint() const { |
| 223 | if (Before.First || Before.Second || Before.Third) { |
| 224 | Before.DebugPrint(); |
| 225 | fprintf(stderr, " -> "); |
| 226 | } |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 227 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | if (After.First || After.Second || After.Third) { |
| 229 | fprintf(stderr, " -> "); |
| 230 | After.DebugPrint(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 235 | /// error. Useful for debugging overloading issues. |
| 236 | void ImplicitConversionSequence::DebugPrint() const { |
| 237 | switch (ConversionKind) { |
| 238 | case StandardConversion: |
| 239 | fprintf(stderr, "Standard conversion: "); |
| 240 | Standard.DebugPrint(); |
| 241 | break; |
| 242 | case UserDefinedConversion: |
| 243 | fprintf(stderr, "User-defined conversion: "); |
| 244 | UserDefined.DebugPrint(); |
| 245 | break; |
| 246 | case EllipsisConversion: |
| 247 | fprintf(stderr, "Ellipsis conversion"); |
| 248 | break; |
| 249 | case BadConversion: |
| 250 | fprintf(stderr, "Bad conversion"); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | fprintf(stderr, "\n"); |
| 255 | } |
| 256 | |
| 257 | // IsOverload - Determine whether the given New declaration is an |
| 258 | // overload of the Old declaration. This routine returns false if New |
| 259 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 260 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 261 | // function (or overload set). When it does return false and Old is an |
| 262 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
| 263 | // FunctionDecl that New cannot be overloaded with. |
| 264 | // |
| 265 | // Example: Given the following input: |
| 266 | // |
| 267 | // void f(int, float); // #1 |
| 268 | // void f(int, int); // #2 |
| 269 | // int f(int, int); // #3 |
| 270 | // |
| 271 | // When we process #1, there is no previous declaration of "f", |
| 272 | // so IsOverload will not be used. |
| 273 | // |
| 274 | // When we process #2, Old is a FunctionDecl for #1. By comparing the |
| 275 | // parameter types, we see that #1 and #2 are overloaded (since they |
| 276 | // have different signatures), so this routine returns false; |
| 277 | // MatchedDecl is unchanged. |
| 278 | // |
| 279 | // When we process #3, Old is an OverloadedFunctionDecl containing #1 |
| 280 | // and #2. We compare the signatures of #3 to #1 (they're overloaded, |
| 281 | // so we do nothing) and then #3 to #2. Since the signatures of #3 and |
| 282 | // #2 are identical (return types of functions are not part of the |
| 283 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 284 | // point to the FunctionDecl for #2. |
| 285 | bool |
| 286 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 287 | OverloadedFunctionDecl::function_iterator& MatchedDecl) |
| 288 | { |
| 289 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 290 | // Is this new function an overload of every function in the |
| 291 | // overload set? |
| 292 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 293 | FuncEnd = Ovl->function_end(); |
| 294 | for (; Func != FuncEnd; ++Func) { |
| 295 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 296 | MatchedDecl = Func; |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // This function overloads every function in the overload set. |
| 302 | return true; |
| 303 | } else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
| 304 | // Is the function New an overload of the function Old? |
| 305 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 306 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 307 | |
| 308 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 309 | // determine whether they are overloads. If we find any mismatch |
| 310 | // in the signature, they are overloads. |
| 311 | |
| 312 | // If either of these functions is a K&R-style function (no |
| 313 | // prototype), then we consider them to have matching signatures. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 314 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 315 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 316 | return false; |
| 317 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 318 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType.getTypePtr()); |
| 319 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType.getTypePtr()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 320 | |
| 321 | // The signature of a function includes the types of its |
| 322 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 323 | // of the ellipsis; see C++ DR 357). |
| 324 | if (OldQType != NewQType && |
| 325 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 326 | OldType->isVariadic() != NewType->isVariadic() || |
| 327 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 328 | NewType->arg_type_begin()))) |
| 329 | return true; |
| 330 | |
| 331 | // If the function is a class member, its signature includes the |
| 332 | // cv-qualifiers (if any) on the function itself. |
| 333 | // |
| 334 | // As part of this, also check whether one of the member functions |
| 335 | // is static, in which case they are not overloads (C++ |
| 336 | // 13.1p2). While not part of the definition of the signature, |
| 337 | // this check is important to determine whether these functions |
| 338 | // can be overloaded. |
| 339 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 340 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 341 | if (OldMethod && NewMethod && |
| 342 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 343 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 344 | return true; |
| 345 | |
| 346 | // The signatures match; this is not an overload. |
| 347 | return false; |
| 348 | } else { |
| 349 | // (C++ 13p1): |
| 350 | // Only function declarations can be overloaded; object and type |
| 351 | // declarations cannot be overloaded. |
| 352 | return false; |
| 353 | } |
| 354 | } |
| 355 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 356 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 357 | /// from the given expression (Expr) to the given type (ToType). This |
| 358 | /// function returns an implicit conversion sequence that can be used |
| 359 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 360 | /// |
| 361 | /// void f(float f); |
| 362 | /// void g(int i) { f(i); } |
| 363 | /// |
| 364 | /// this routine would produce an implicit conversion sequence to |
| 365 | /// describe the initialization of f from i, which will be a standard |
| 366 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 367 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 368 | // |
| 369 | /// Note that this routine only determines how the conversion can be |
| 370 | /// performed; it does not actually perform the conversion. As such, |
| 371 | /// it will not produce any diagnostics if no conversion is available, |
| 372 | /// but will instead return an implicit conversion sequence of kind |
| 373 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 374 | /// |
| 375 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 376 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 377 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 378 | /// permitted. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 379 | ImplicitConversionSequence |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 380 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 381 | bool SuppressUserConversions, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 382 | bool AllowExplicit) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 383 | { |
| 384 | ImplicitConversionSequence ICS; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 385 | if (IsStandardConversion(From, ToType, ICS.Standard)) |
| 386 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 387 | else if (getLangOptions().CPlusPlus && |
| 388 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 389 | !SuppressUserConversions, AllowExplicit)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 390 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 391 | // C++ [over.ics.user]p4: |
| 392 | // A conversion of an expression of class type to the same class |
| 393 | // type is given Exact Match rank, and a conversion of an |
| 394 | // expression of class type to a base class of that type is |
| 395 | // given Conversion rank, in spite of the fact that a copy |
| 396 | // constructor (i.e., a user-defined conversion function) is |
| 397 | // called for those cases. |
| 398 | if (CXXConstructorDecl *Constructor |
| 399 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 400 | QualType FromCanon |
| 401 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 402 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 403 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 404 | // Turn this into a "standard" conversion sequence, so that it |
| 405 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 406 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 407 | ICS.Standard.setAsIdentityConversion(); |
| 408 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 409 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 410 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 411 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 412 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 413 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 414 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 415 | |
| 416 | // C++ [over.best.ics]p4: |
| 417 | // However, when considering the argument of a user-defined |
| 418 | // conversion function that is a candidate by 13.3.1.3 when |
| 419 | // invoked for the copying of the temporary in the second step |
| 420 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 421 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 422 | // ellipsis conversion sequences are allowed. |
| 423 | if (SuppressUserConversions && |
| 424 | ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) |
| 425 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 426 | } else |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 427 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 428 | |
| 429 | return ICS; |
| 430 | } |
| 431 | |
| 432 | /// IsStandardConversion - Determines whether there is a standard |
| 433 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 434 | /// expression From to the type ToType. Standard conversion sequences |
| 435 | /// only consider non-class types; for conversions that involve class |
| 436 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 437 | /// contain the standard conversion sequence required to perform this |
| 438 | /// conversion and this routine will return true. Otherwise, this |
| 439 | /// routine will return false and the value of SCS is unspecified. |
| 440 | bool |
| 441 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
| 442 | StandardConversionSequence &SCS) |
| 443 | { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 444 | QualType FromType = From->getType(); |
| 445 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 446 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 447 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 448 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 449 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 450 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 451 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 453 | // There are no standard conversions for class types in C++, so |
| 454 | // abort early. When overloading in C, however, we do permit |
| 455 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 456 | if (getLangOptions().CPlusPlus) |
| 457 | return false; |
| 458 | |
| 459 | // When we're overloading in C, we allow, as standard conversions, |
| 460 | } |
| 461 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 462 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 463 | // array-to-pointer conversion, or function-to-pointer conversion |
| 464 | // (C++ 4p1). |
| 465 | |
| 466 | // Lvalue-to-rvalue conversion (C++ 4.1): |
| 467 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 468 | // converted to an rvalue. |
| 469 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
| 470 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 471 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 472 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 473 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | |
| 475 | // If T is a non-class type, the type of the rvalue is the |
| 476 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 477 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 478 | // just strip the qualifiers because they don't matter. |
| 479 | |
| 480 | // FIXME: Doesn't see through to qualifiers behind a typedef! |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 481 | FromType = FromType.getUnqualifiedType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | } |
| 483 | // Array-to-pointer conversion (C++ 4.2) |
| 484 | else if (FromType->isArrayType()) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 485 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 486 | |
| 487 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 488 | // bound of T" can be converted to an rvalue of type "pointer to |
| 489 | // T" (C++ 4.2p1). |
| 490 | FromType = Context.getArrayDecayedType(FromType); |
| 491 | |
| 492 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 493 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 494 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 495 | |
| 496 | // For the purpose of ranking in overload resolution |
| 497 | // (13.3.3.1.1), this conversion is considered an |
| 498 | // array-to-pointer conversion followed by a qualification |
| 499 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 500 | SCS.Second = ICK_Identity; |
| 501 | SCS.Third = ICK_Qualification; |
| 502 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 503 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | // Function-to-pointer conversion (C++ 4.3). |
| 507 | else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 508 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 509 | |
| 510 | // An lvalue of function type T can be converted to an rvalue of |
| 511 | // type "pointer to T." The result is a pointer to the |
| 512 | // function. (C++ 4.3p1). |
| 513 | FromType = Context.getPointerType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 514 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 515 | // Address of overloaded function (C++ [over.over]). |
| 516 | else if (FunctionDecl *Fn |
| 517 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
| 518 | SCS.First = ICK_Function_To_Pointer; |
| 519 | |
| 520 | // We were able to resolve the address of the overloaded function, |
| 521 | // so we can convert to the type of that function. |
| 522 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 523 | if (ToType->isLValueReferenceType()) |
| 524 | FromType = Context.getLValueReferenceType(FromType); |
| 525 | else if (ToType->isRValueReferenceType()) |
| 526 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 527 | else if (ToType->isMemberPointerType()) { |
| 528 | // Resolve address only succeeds if both sides are member pointers, |
| 529 | // but it doesn't have to be the same class. See DR 247. |
| 530 | // Note that this means that the type of &Derived::fn can be |
| 531 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 532 | // base class, even if it was brought into the derived class via a |
| 533 | // using declaration. The standard isn't clear on this issue at all. |
| 534 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 535 | FromType = Context.getMemberPointerType(FromType, |
| 536 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 537 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 538 | FromType = Context.getPointerType(FromType); |
| 539 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 540 | // We don't require any conversions for the first step. |
| 541 | else { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 542 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // The second conversion can be an integral promotion, floating |
| 546 | // point promotion, integral conversion, floating point conversion, |
| 547 | // floating-integral conversion, pointer conversion, |
| 548 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 549 | // For overloading in C, this can also be a "compatible-type" |
| 550 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 551 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 552 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 553 | // The unqualified versions of the types are the same: there's no |
| 554 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 555 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 556 | } |
| 557 | // Integral promotion (C++ 4.5). |
| 558 | else if (IsIntegralPromotion(From, FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 559 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 560 | FromType = ToType.getUnqualifiedType(); |
| 561 | } |
| 562 | // Floating point promotion (C++ 4.6). |
| 563 | else if (IsFloatingPointPromotion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 564 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 565 | FromType = ToType.getUnqualifiedType(); |
| 566 | } |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 567 | // Complex promotion (Clang extension) |
| 568 | else if (IsComplexPromotion(FromType, ToType)) { |
| 569 | SCS.Second = ICK_Complex_Promotion; |
| 570 | FromType = ToType.getUnqualifiedType(); |
| 571 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 572 | // Integral conversions (C++ 4.7). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 573 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 574 | else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 575 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 576 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 577 | FromType = ToType.getUnqualifiedType(); |
| 578 | } |
| 579 | // Floating point conversions (C++ 4.8). |
| 580 | else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 581 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 582 | FromType = ToType.getUnqualifiedType(); |
| 583 | } |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 584 | // Complex conversions (C99 6.3.1.6) |
| 585 | else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 586 | SCS.Second = ICK_Complex_Conversion; |
| 587 | FromType = ToType.getUnqualifiedType(); |
| 588 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 589 | // Floating-integral conversions (C++ 4.9). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 590 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 591 | else if ((FromType->isFloatingType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 592 | ToType->isIntegralType() && !ToType->isBooleanType() && |
| 593 | !ToType->isEnumeralType()) || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 594 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
| 595 | ToType->isFloatingType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 596 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 597 | FromType = ToType.getUnqualifiedType(); |
| 598 | } |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 599 | // Complex-real conversions (C99 6.3.1.7) |
| 600 | else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 601 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 602 | SCS.Second = ICK_Complex_Real; |
| 603 | FromType = ToType.getUnqualifiedType(); |
| 604 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 605 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 606 | else if (IsPointerConversion(From, FromType, ToType, FromType, |
| 607 | IncompatibleObjC)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 608 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 609 | SCS.IncompatibleObjC = IncompatibleObjC; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 610 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 611 | // Pointer to member conversions (4.11). |
| 612 | else if (IsMemberPointerConversion(From, FromType, ToType, FromType)) { |
| 613 | SCS.Second = ICK_Pointer_Member; |
| 614 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 615 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 616 | else if (ToType->isBooleanType() && |
| 617 | (FromType->isArithmeticType() || |
| 618 | FromType->isEnumeralType() || |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 619 | FromType->isPointerType() || |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 620 | FromType->isBlockPointerType() || |
| 621 | FromType->isMemberPointerType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 622 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 623 | FromType = Context.BoolTy; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 624 | } |
| 625 | // Compatible conversions (Clang extension for C function overloading) |
| 626 | else if (!getLangOptions().CPlusPlus && |
| 627 | Context.typesAreCompatible(ToType, FromType)) { |
| 628 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 629 | } else { |
| 630 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 631 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 634 | QualType CanonFrom; |
| 635 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 636 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 637 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 638 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 639 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 640 | CanonFrom = Context.getCanonicalType(FromType); |
| 641 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 642 | } else { |
| 643 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 644 | SCS.Third = ICK_Identity; |
| 645 | |
| 646 | // C++ [over.best.ics]p6: |
| 647 | // [...] Any difference in top-level cv-qualification is |
| 648 | // subsumed by the initialization itself and does not constitute |
| 649 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 650 | CanonFrom = Context.getCanonicalType(FromType); |
| 651 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 652 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 653 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 654 | FromType = ToType; |
| 655 | CanonFrom = CanonTo; |
| 656 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // If we have not converted the argument type to the parameter type, |
| 660 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 661 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 662 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 664 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 665 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 669 | /// expression From (whose potentially-adjusted type is FromType) to |
| 670 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 671 | /// sets PromotedType to the promoted type. |
| 672 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) |
| 673 | { |
| 674 | const BuiltinType *To = ToType->getAsBuiltinType(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 675 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 676 | if (!To) { |
| 677 | return false; |
| 678 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 679 | |
| 680 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 681 | // unsigned short int can be converted to an rvalue of type int if |
| 682 | // int can represent all the values of the source type; otherwise, |
| 683 | // the source rvalue can be converted to an rvalue of type unsigned |
| 684 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 685 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 686 | if (// We can promote any signed, promotable integer type to an int |
| 687 | (FromType->isSignedIntegerType() || |
| 688 | // We can promote any unsigned integer type whose size is |
| 689 | // less than int to an int. |
| 690 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 691 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 692 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 695 | return To->getKind() == BuiltinType::UInt; |
| 696 | } |
| 697 | |
| 698 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 699 | // can be converted to an rvalue of the first of the following types |
| 700 | // that can represent all the values of its underlying type: int, |
| 701 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 702 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 703 | && ToType->isIntegerType()) { |
| 704 | // Determine whether the type we're converting from is signed or |
| 705 | // unsigned. |
| 706 | bool FromIsSigned; |
| 707 | uint64_t FromSize = Context.getTypeSize(FromType); |
| 708 | if (const EnumType *FromEnumType = FromType->getAsEnumType()) { |
| 709 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 710 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 711 | } else { |
| 712 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 713 | FromIsSigned = true; |
| 714 | } |
| 715 | |
| 716 | // The types we'll try to promote to, in the appropriate |
| 717 | // order. Try each of these types. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 718 | QualType PromoteTypes[6] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 719 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 720 | Context.LongTy, Context.UnsignedLongTy , |
| 721 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 722 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 723 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 724 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 725 | if (FromSize < ToSize || |
| 726 | (FromSize == ToSize && |
| 727 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 728 | // We found the type that we can promote to. If this is the |
| 729 | // type we wanted, we have a promotion. Otherwise, no |
| 730 | // promotion. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 731 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 732 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 738 | // rvalue of type int if int can represent all the values of the |
| 739 | // bit-field; otherwise, it can be converted to unsigned int if |
| 740 | // unsigned int can represent all the values of the bit-field. If |
| 741 | // the bit-field is larger yet, no integral promotion applies to |
| 742 | // it. If the bit-field has an enumerated type, it is treated as any |
| 743 | // other value of that type for promotion purposes (C++ 4.5p3). |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 744 | // FIXME: We should delay checking of bit-fields until we actually |
| 745 | // perform the conversion. |
| 746 | if (MemberExpr *MemRef = dyn_cast_or_null<MemberExpr>(From)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 747 | using llvm::APSInt; |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 748 | if (FieldDecl *MemberDecl = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) { |
| 749 | APSInt BitWidth; |
| 750 | if (MemberDecl->isBitField() && |
| 751 | FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 752 | From->isIntegerConstantExpr(BitWidth, Context)) { |
| 753 | APSInt ToSize(Context.getTypeSize(ToType)); |
| 754 | |
| 755 | // Are we promoting to an int from a bitfield that fits in an int? |
| 756 | if (BitWidth < ToSize || |
| 757 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 758 | return To->getKind() == BuiltinType::Int; |
| 759 | } |
| 760 | |
| 761 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 762 | // that fits into an unsigned int? |
| 763 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 764 | return To->getKind() == BuiltinType::UInt; |
| 765 | } |
| 766 | |
| 767 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 768 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 773 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 774 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 775 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 776 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 777 | |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 782 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 783 | /// returns true and sets PromotedType to the promoted type. |
| 784 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) |
| 785 | { |
| 786 | /// An rvalue of type float can be converted to an rvalue of type |
| 787 | /// double. (C++ 4.6p1). |
| 788 | if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType()) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 789 | if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 790 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 791 | ToBuiltin->getKind() == BuiltinType::Double) |
| 792 | return true; |
| 793 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 794 | // C99 6.3.1.5p1: |
| 795 | // When a float is promoted to double or long double, or a |
| 796 | // double is promoted to long double [...]. |
| 797 | if (!getLangOptions().CPlusPlus && |
| 798 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 799 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 800 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 801 | return true; |
| 802 | } |
| 803 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 804 | return false; |
| 805 | } |
| 806 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 807 | /// \brief Determine if a conversion is a complex promotion. |
| 808 | /// |
| 809 | /// A complex promotion is defined as a complex -> complex conversion |
| 810 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 811 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 812 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
| 813 | const ComplexType *FromComplex = FromType->getAsComplexType(); |
| 814 | if (!FromComplex) |
| 815 | return false; |
| 816 | |
| 817 | const ComplexType *ToComplex = ToType->getAsComplexType(); |
| 818 | if (!ToComplex) |
| 819 | return false; |
| 820 | |
| 821 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 822 | ToComplex->getElementType()) || |
| 823 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 824 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 827 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 828 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 829 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 830 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 831 | /// the right set of qualifiers on its pointee. |
| 832 | static QualType |
| 833 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
| 834 | QualType ToPointee, QualType ToType, |
| 835 | ASTContext &Context) { |
| 836 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 837 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| 838 | unsigned Quals = CanonFromPointee.getCVRQualifiers(); |
| 839 | |
| 840 | // Exact qualifier match -> return the pointer type we're converting to. |
| 841 | if (CanonToPointee.getCVRQualifiers() == Quals) { |
| 842 | // ToType is exactly what we need. Return it. |
| 843 | if (ToType.getTypePtr()) |
| 844 | return ToType; |
| 845 | |
| 846 | // Build a pointer to ToPointee. It has the right qualifiers |
| 847 | // already. |
| 848 | return Context.getPointerType(ToPointee); |
| 849 | } |
| 850 | |
| 851 | // Just build a canonical type that has the right qualifiers. |
| 852 | return Context.getPointerType(CanonToPointee.getQualifiedType(Quals)); |
| 853 | } |
| 854 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 855 | /// IsPointerConversion - Determines whether the conversion of the |
| 856 | /// expression From, which has the (possibly adjusted) type FromType, |
| 857 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 858 | /// 4.10). If so, returns true and places the converted type (that |
| 859 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 860 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 861 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 862 | /// This routine also supports conversions to and from block pointers |
| 863 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 864 | /// pointers to interfaces. FIXME: Once we've determined the |
| 865 | /// appropriate overloading rules for Objective-C, we may want to |
| 866 | /// split the Objective-C checks into a different routine; however, |
| 867 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 868 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 869 | /// set if the conversion is an allowed Objective-C conversion that |
| 870 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 871 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 872 | QualType& ConvertedType, |
| 873 | bool &IncompatibleObjC) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 874 | { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 875 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 876 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 877 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 879 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 880 | if (Context.isObjCObjectPointerType(ToType) && |
| 881 | From->isNullPointerConstant(Context)) { |
| 882 | ConvertedType = ToType; |
| 883 | return true; |
| 884 | } |
| 885 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 886 | // Blocks: Block pointers can be converted to void*. |
| 887 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
| 888 | ToType->getAsPointerType()->getPointeeType()->isVoidType()) { |
| 889 | ConvertedType = ToType; |
| 890 | return true; |
| 891 | } |
| 892 | // Blocks: A null pointer constant can be converted to a block |
| 893 | // pointer type. |
| 894 | if (ToType->isBlockPointerType() && From->isNullPointerConstant(Context)) { |
| 895 | ConvertedType = ToType; |
| 896 | return true; |
| 897 | } |
| 898 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 899 | const PointerType* ToTypePtr = ToType->getAsPointerType(); |
| 900 | if (!ToTypePtr) |
| 901 | return false; |
| 902 | |
| 903 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
| 904 | if (From->isNullPointerConstant(Context)) { |
| 905 | ConvertedType = ToType; |
| 906 | return true; |
| 907 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 909 | // Beyond this point, both types need to be pointers. |
| 910 | const PointerType *FromTypePtr = FromType->getAsPointerType(); |
| 911 | if (!FromTypePtr) |
| 912 | return false; |
| 913 | |
| 914 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 915 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 916 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 917 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 918 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 919 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 920 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 921 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 922 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 923 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 924 | return true; |
| 925 | } |
| 926 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 927 | // When we're overloading in C, we allow a special kind of pointer |
| 928 | // conversion for compatible-but-not-identical pointee types. |
| 929 | if (!getLangOptions().CPlusPlus && |
| 930 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
| 931 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 932 | ToPointeeType, |
| 933 | ToType, Context); |
| 934 | return true; |
| 935 | } |
| 936 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 937 | // C++ [conv.ptr]p3: |
| 938 | // |
| 939 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 940 | // can be converted to an rvalue of type "pointer to cv B," where |
| 941 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 942 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 943 | // necessitates this conversion is ill-formed. The result of the |
| 944 | // conversion is a pointer to the base class sub-object of the |
| 945 | // derived class object. The null pointer value is converted to |
| 946 | // the null pointer value of the destination type. |
| 947 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 948 | // Note that we do not check for ambiguity or inaccessibility |
| 949 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 950 | if (getLangOptions().CPlusPlus && |
| 951 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 952 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 953 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 954 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 955 | ToType, Context); |
| 956 | return true; |
| 957 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 959 | return false; |
| 960 | } |
| 961 | |
| 962 | /// isObjCPointerConversion - Determines whether this is an |
| 963 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 964 | /// with the same arguments and return values. |
| 965 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
| 966 | QualType& ConvertedType, |
| 967 | bool &IncompatibleObjC) { |
| 968 | if (!getLangOptions().ObjC1) |
| 969 | return false; |
| 970 | |
| 971 | // Conversions with Objective-C's id<...>. |
| 972 | if ((FromType->isObjCQualifiedIdType() || ToType->isObjCQualifiedIdType()) && |
| 973 | ObjCQualifiedIdTypesAreCompatible(ToType, FromType, /*compare=*/false)) { |
| 974 | ConvertedType = ToType; |
| 975 | return true; |
| 976 | } |
| 977 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 978 | // Beyond this point, both types need to be pointers or block pointers. |
| 979 | QualType ToPointeeType; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 980 | const PointerType* ToTypePtr = ToType->getAsPointerType(); |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 981 | if (ToTypePtr) |
| 982 | ToPointeeType = ToTypePtr->getPointeeType(); |
| 983 | else if (const BlockPointerType *ToBlockPtr = ToType->getAsBlockPointerType()) |
| 984 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 985 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 986 | return false; |
| 987 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 988 | QualType FromPointeeType; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 989 | const PointerType *FromTypePtr = FromType->getAsPointerType(); |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 990 | if (FromTypePtr) |
| 991 | FromPointeeType = FromTypePtr->getPointeeType(); |
| 992 | else if (const BlockPointerType *FromBlockPtr |
| 993 | = FromType->getAsBlockPointerType()) |
| 994 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 995 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 996 | return false; |
| 997 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 998 | // Objective C++: We're able to convert from a pointer to an |
| 999 | // interface to a pointer to a different interface. |
| 1000 | const ObjCInterfaceType* FromIface = FromPointeeType->getAsObjCInterfaceType(); |
| 1001 | const ObjCInterfaceType* ToIface = ToPointeeType->getAsObjCInterfaceType(); |
| 1002 | if (FromIface && ToIface && |
| 1003 | Context.canAssignObjCInterfaces(ToIface, FromIface)) { |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1004 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1005 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1006 | ToType, Context); |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1010 | if (FromIface && ToIface && |
| 1011 | Context.canAssignObjCInterfaces(FromIface, ToIface)) { |
| 1012 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1013 | // interfaces, which is permitted. However, we're going to |
| 1014 | // complain about it. |
| 1015 | IncompatibleObjC = true; |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1016 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1017 | ToPointeeType, |
| 1018 | ToType, Context); |
| 1019 | return true; |
| 1020 | } |
| 1021 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1022 | // Objective C++: We're able to convert between "id" and a pointer |
| 1023 | // to any interface (in both directions). |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 1024 | if ((FromIface && Context.isObjCIdStructType(ToPointeeType)) |
| 1025 | || (ToIface && Context.isObjCIdStructType(FromPointeeType))) { |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1026 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1027 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1028 | ToType, Context); |
| 1029 | return true; |
| 1030 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1032 | // Objective C++: Allow conversions between the Objective-C "id" and |
| 1033 | // "Class", in either direction. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 1034 | if ((Context.isObjCIdStructType(FromPointeeType) && |
| 1035 | Context.isObjCClassStructType(ToPointeeType)) || |
| 1036 | (Context.isObjCClassStructType(FromPointeeType) && |
| 1037 | Context.isObjCIdStructType(ToPointeeType))) { |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1038 | ConvertedType = ToType; |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1042 | // If we have pointers to pointers, recursively check whether this |
| 1043 | // is an Objective-C conversion. |
| 1044 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1045 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1046 | IncompatibleObjC)) { |
| 1047 | // We always complain about this conversion. |
| 1048 | IncompatibleObjC = true; |
| 1049 | ConvertedType = ToType; |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1053 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1054 | // differences in the argument and result types are in Objective-C |
| 1055 | // pointer conversions. If so, we permit the conversion (but |
| 1056 | // complain about it). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1057 | const FunctionProtoType *FromFunctionType |
| 1058 | = FromPointeeType->getAsFunctionProtoType(); |
| 1059 | const FunctionProtoType *ToFunctionType |
| 1060 | = ToPointeeType->getAsFunctionProtoType(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1061 | if (FromFunctionType && ToFunctionType) { |
| 1062 | // If the function types are exactly the same, this isn't an |
| 1063 | // Objective-C pointer conversion. |
| 1064 | if (Context.getCanonicalType(FromPointeeType) |
| 1065 | == Context.getCanonicalType(ToPointeeType)) |
| 1066 | return false; |
| 1067 | |
| 1068 | // Perform the quick checks that will tell us whether these |
| 1069 | // function types are obviously different. |
| 1070 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1071 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1072 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1073 | return false; |
| 1074 | |
| 1075 | bool HasObjCConversion = false; |
| 1076 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1077 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1078 | // Okay, the types match exactly. Nothing to do. |
| 1079 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1080 | ToFunctionType->getResultType(), |
| 1081 | ConvertedType, IncompatibleObjC)) { |
| 1082 | // Okay, we have an Objective-C pointer conversion. |
| 1083 | HasObjCConversion = true; |
| 1084 | } else { |
| 1085 | // Function types are too different. Abort. |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | // Check argument types. |
| 1090 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1091 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1092 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1093 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1094 | if (Context.getCanonicalType(FromArgType) |
| 1095 | == Context.getCanonicalType(ToArgType)) { |
| 1096 | // Okay, the types match exactly. Nothing to do. |
| 1097 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1098 | ConvertedType, IncompatibleObjC)) { |
| 1099 | // Okay, we have an Objective-C pointer conversion. |
| 1100 | HasObjCConversion = true; |
| 1101 | } else { |
| 1102 | // Argument types are too different. Abort. |
| 1103 | return false; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | if (HasObjCConversion) { |
| 1108 | // We had an Objective-C conversion. Allow this pointer |
| 1109 | // conversion, but complain about it. |
| 1110 | ConvertedType = ToType; |
| 1111 | IncompatibleObjC = true; |
| 1112 | return true; |
| 1113 | } |
| 1114 | } |
| 1115 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1116 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1119 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1120 | /// expression From to the type ToType. This routine checks for |
| 1121 | /// ambiguous (FIXME: or inaccessible) derived-to-base pointer |
| 1122 | /// conversions for which IsPointerConversion has already returned |
| 1123 | /// true. It returns true and produces a diagnostic if there was an |
| 1124 | /// error, or returns false otherwise. |
| 1125 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType) { |
| 1126 | QualType FromType = From->getType(); |
| 1127 | |
| 1128 | if (const PointerType *FromPtrType = FromType->getAsPointerType()) |
| 1129 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1130 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1131 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Objective-C++ conversions are always okay. |
| 1134 | // FIXME: We should have a different class of conversions for |
| 1135 | // the Objective-C++ implicit conversions. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 1136 | if (Context.isObjCIdStructType(FromPointeeType) || |
| 1137 | Context.isObjCIdStructType(ToPointeeType) || |
| 1138 | Context.isObjCClassStructType(FromPointeeType) || |
| 1139 | Context.isObjCClassStructType(ToPointeeType)) |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1140 | return false; |
| 1141 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1142 | if (FromPointeeType->isRecordType() && |
| 1143 | ToPointeeType->isRecordType()) { |
| 1144 | // We must have a derived-to-base conversion. Check an |
| 1145 | // ambiguous or inaccessible conversion. |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 1146 | return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1147 | From->getExprLoc(), |
| 1148 | From->getSourceRange()); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | return false; |
| 1153 | } |
| 1154 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1155 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1156 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1157 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1158 | /// If so, returns true and places the converted type (that might differ from |
| 1159 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1160 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
| 1161 | QualType ToType, QualType &ConvertedType) |
| 1162 | { |
| 1163 | const MemberPointerType *ToTypePtr = ToType->getAsMemberPointerType(); |
| 1164 | if (!ToTypePtr) |
| 1165 | return false; |
| 1166 | |
| 1167 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
| 1168 | if (From->isNullPointerConstant(Context)) { |
| 1169 | ConvertedType = ToType; |
| 1170 | return true; |
| 1171 | } |
| 1172 | |
| 1173 | // Otherwise, both types have to be member pointers. |
| 1174 | const MemberPointerType *FromTypePtr = FromType->getAsMemberPointerType(); |
| 1175 | if (!FromTypePtr) |
| 1176 | return false; |
| 1177 | |
| 1178 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1179 | // where D is derived from B (C++ 4.11p2). |
| 1180 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1181 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1182 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1183 | |
| 1184 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1185 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1186 | ToClass.getTypePtr()); |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
| 1193 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1194 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1195 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1196 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1197 | /// true and produces a diagnostic if there was an error, or returns false |
| 1198 | /// otherwise. |
| 1199 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType) { |
| 1200 | QualType FromType = From->getType(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1201 | const MemberPointerType *FromPtrType = FromType->getAsMemberPointerType(); |
| 1202 | if (!FromPtrType) |
| 1203 | return false; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1204 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1205 | const MemberPointerType *ToPtrType = ToType->getAsMemberPointerType(); |
| 1206 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1207 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1208 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1209 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1210 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1211 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1212 | // FIXME: What about dependent types? |
| 1213 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1214 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1215 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1216 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1217 | /*DetectVirtual=*/true); |
| 1218 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1219 | assert(DerivationOkay && |
| 1220 | "Should not have been called if derivation isn't OK."); |
| 1221 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1222 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1223 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1224 | getUnqualifiedType())) { |
| 1225 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1226 | Paths.clear(); |
| 1227 | Paths.setRecordingPaths(true); |
| 1228 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1229 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1230 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1231 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1232 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1233 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1234 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1235 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1236 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1238 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1239 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1240 | << FromClass << ToClass << QualType(VBase, 0) |
| 1241 | << From->getSourceRange(); |
| 1242 | return true; |
| 1243 | } |
| 1244 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1245 | return false; |
| 1246 | } |
| 1247 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1248 | /// IsQualificationConversion - Determines whether the conversion from |
| 1249 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1250 | /// (C++ 4.4). |
| 1251 | bool |
| 1252 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) |
| 1253 | { |
| 1254 | FromType = Context.getCanonicalType(FromType); |
| 1255 | ToType = Context.getCanonicalType(ToType); |
| 1256 | |
| 1257 | // If FromType and ToType are the same type, this is not a |
| 1258 | // qualification conversion. |
| 1259 | if (FromType == ToType) |
| 1260 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1262 | // (C++ 4.4p4): |
| 1263 | // A conversion can add cv-qualifiers at levels other than the first |
| 1264 | // in multi-level pointers, subject to the following rules: [...] |
| 1265 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1266 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1267 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1268 | // Within each iteration of the loop, we check the qualifiers to |
| 1269 | // determine if this still looks like a qualification |
| 1270 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1271 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1272 | // until there are no more pointers or pointers-to-members left to |
| 1273 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1274 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1275 | |
| 1276 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1277 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1278 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1279 | return false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1280 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1281 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1282 | // every cv for 0 < k < j. |
| 1283 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1284 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1285 | return false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1287 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1288 | // include const. |
| 1289 | PreviousToQualsIncludeConst |
| 1290 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1291 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1292 | |
| 1293 | // We are left with FromType and ToType being the pointee types |
| 1294 | // after unwrapping the original FromType and ToType the same number |
| 1295 | // of types. If we unwrapped any pointers, and if FromType and |
| 1296 | // ToType have the same unqualified type (since we checked |
| 1297 | // qualifiers above), then this is a qualification conversion. |
| 1298 | return UnwrappedAnyPointer && |
| 1299 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1300 | } |
| 1301 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1302 | /// Determines whether there is a user-defined conversion sequence |
| 1303 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1304 | /// ToType. If such a conversion exists, User will contain the |
| 1305 | /// user-defined conversion sequence that performs such a conversion |
| 1306 | /// and this routine will return true. Otherwise, this routine returns |
| 1307 | /// false and User is unspecified. |
| 1308 | /// |
| 1309 | /// \param AllowConversionFunctions true if the conversion should |
| 1310 | /// consider conversion functions at all. If false, only constructors |
| 1311 | /// will be considered. |
| 1312 | /// |
| 1313 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1314 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1315 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1316 | bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1317 | UserDefinedConversionSequence& User, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1318 | bool AllowConversionFunctions, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1319 | bool AllowExplicit) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1320 | { |
| 1321 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1322 | if (const RecordType *ToRecordType = ToType->getAsRecordType()) { |
| 1323 | if (CXXRecordDecl *ToRecordDecl |
| 1324 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1325 | // C++ [over.match.ctor]p1: |
| 1326 | // When objects of class type are direct-initialized (8.5), or |
| 1327 | // copy-initialized from an expression of the same or a |
| 1328 | // derived class type (8.5), overload resolution selects the |
| 1329 | // constructor. [...] For copy-initialization, the candidate |
| 1330 | // functions are all the converting constructors (12.3.1) of |
| 1331 | // that class. The argument list is the expression-list within |
| 1332 | // the parentheses of the initializer. |
| 1333 | DeclarationName ConstructorName |
| 1334 | = Context.DeclarationNames.getCXXConstructorName( |
| 1335 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1336 | DeclContext::lookup_iterator Con, ConEnd; |
| 1337 | for (llvm::tie(Con, ConEnd) = ToRecordDecl->lookup(ConstructorName); |
| 1338 | Con != ConEnd; ++Con) { |
| 1339 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 1340 | if (Constructor->isConvertingConstructor()) |
| 1341 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 1342 | /*SuppressUserConversions=*/true); |
| 1343 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1347 | if (!AllowConversionFunctions) { |
| 1348 | // Don't allow any conversion functions to enter the overload set. |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1349 | } else if (const RecordType *FromRecordType |
| 1350 | = From->getType()->getAsRecordType()) { |
| 1351 | if (CXXRecordDecl *FromRecordDecl |
| 1352 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1353 | // Add all of the conversion functions as candidates. |
| 1354 | // FIXME: Look for conversions in base classes! |
| 1355 | OverloadedFunctionDecl *Conversions |
| 1356 | = FromRecordDecl->getConversionFunctions(); |
| 1357 | for (OverloadedFunctionDecl::function_iterator Func |
| 1358 | = Conversions->function_begin(); |
| 1359 | Func != Conversions->function_end(); ++Func) { |
| 1360 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 1361 | if (AllowExplicit || !Conv->isExplicit()) |
| 1362 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1363 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1364 | } |
| 1365 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1366 | |
| 1367 | OverloadCandidateSet::iterator Best; |
| 1368 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1369 | case OR_Success: |
| 1370 | // Record the standard conversion we used and the conversion function. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1371 | if (CXXConstructorDecl *Constructor |
| 1372 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1373 | // C++ [over.ics.user]p1: |
| 1374 | // If the user-defined conversion is specified by a |
| 1375 | // constructor (12.3.1), the initial standard conversion |
| 1376 | // sequence converts the source type to the type required by |
| 1377 | // the argument of the constructor. |
| 1378 | // |
| 1379 | // FIXME: What about ellipsis conversions? |
| 1380 | QualType ThisType = Constructor->getThisType(Context); |
| 1381 | User.Before = Best->Conversions[0].Standard; |
| 1382 | User.ConversionFunction = Constructor; |
| 1383 | User.After.setAsIdentityConversion(); |
| 1384 | User.After.FromTypePtr |
| 1385 | = ThisType->getAsPointerType()->getPointeeType().getAsOpaquePtr(); |
| 1386 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
| 1387 | return true; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1388 | } else if (CXXConversionDecl *Conversion |
| 1389 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1390 | // C++ [over.ics.user]p1: |
| 1391 | // |
| 1392 | // [...] If the user-defined conversion is specified by a |
| 1393 | // conversion function (12.3.2), the initial standard |
| 1394 | // conversion sequence converts the source type to the |
| 1395 | // implicit object parameter of the conversion function. |
| 1396 | User.Before = Best->Conversions[0].Standard; |
| 1397 | User.ConversionFunction = Conversion; |
| 1398 | |
| 1399 | // C++ [over.ics.user]p2: |
| 1400 | // The second standard conversion sequence converts the |
| 1401 | // result of the user-defined conversion to the target type |
| 1402 | // for the sequence. Since an implicit conversion sequence |
| 1403 | // is an initialization, the special rules for |
| 1404 | // initialization by user-defined conversion apply when |
| 1405 | // selecting the best user-defined conversion for a |
| 1406 | // user-defined conversion sequence (see 13.3.3 and |
| 1407 | // 13.3.3.1). |
| 1408 | User.After = Best->FinalConversion; |
| 1409 | return true; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1410 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1411 | assert(false && "Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1412 | return false; |
| 1413 | } |
| 1414 | |
| 1415 | case OR_No_Viable_Function: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1416 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1417 | // No conversion here! We're done. |
| 1418 | return false; |
| 1419 | |
| 1420 | case OR_Ambiguous: |
| 1421 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 1422 | // ambiguous conversion sequences. |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1429 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1430 | /// conversion sequences to determine whether one is better than the |
| 1431 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
| 1432 | ImplicitConversionSequence::CompareKind |
| 1433 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1434 | const ImplicitConversionSequence& ICS2) |
| 1435 | { |
| 1436 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1437 | // conversion sequences (as defined in 13.3.3.1) |
| 1438 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1439 | // conversion sequence than a user-defined conversion sequence or |
| 1440 | // an ellipsis conversion sequence, and |
| 1441 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1442 | // conversion sequence than an ellipsis conversion sequence |
| 1443 | // (13.3.3.1.3). |
| 1444 | // |
| 1445 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1446 | return ImplicitConversionSequence::Better; |
| 1447 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1448 | return ImplicitConversionSequence::Worse; |
| 1449 | |
| 1450 | // Two implicit conversion sequences of the same form are |
| 1451 | // indistinguishable conversion sequences unless one of the |
| 1452 | // following rules apply: (C++ 13.3.3.2p3): |
| 1453 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1454 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
| 1455 | else if (ICS1.ConversionKind == |
| 1456 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1457 | // User-defined conversion sequence U1 is a better conversion |
| 1458 | // sequence than another user-defined conversion sequence U2 if |
| 1459 | // they contain the same user-defined conversion function or |
| 1460 | // constructor and if the second standard conversion sequence of |
| 1461 | // U1 is better than the second standard conversion sequence of |
| 1462 | // U2 (C++ 13.3.3.2p3). |
| 1463 | if (ICS1.UserDefined.ConversionFunction == |
| 1464 | ICS2.UserDefined.ConversionFunction) |
| 1465 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1466 | ICS2.UserDefined.After); |
| 1467 | } |
| 1468 | |
| 1469 | return ImplicitConversionSequence::Indistinguishable; |
| 1470 | } |
| 1471 | |
| 1472 | /// CompareStandardConversionSequences - Compare two standard |
| 1473 | /// conversion sequences to determine whether one is better than the |
| 1474 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
| 1475 | ImplicitConversionSequence::CompareKind |
| 1476 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1477 | const StandardConversionSequence& SCS2) |
| 1478 | { |
| 1479 | // Standard conversion sequence S1 is a better conversion sequence |
| 1480 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1481 | |
| 1482 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1483 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1484 | // excluding any Lvalue Transformation; the identity conversion |
| 1485 | // sequence is considered to be a subsequence of any |
| 1486 | // non-identity conversion sequence) or, if not that, |
| 1487 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1488 | // Neither is a proper subsequence of the other. Do nothing. |
| 1489 | ; |
| 1490 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1491 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
| 1492 | (SCS1.Second == ICK_Identity && |
| 1493 | SCS1.Third == ICK_Identity)) |
| 1494 | // SCS1 is a proper subsequence of SCS2. |
| 1495 | return ImplicitConversionSequence::Better; |
| 1496 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1497 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
| 1498 | (SCS2.Second == ICK_Identity && |
| 1499 | SCS2.Third == ICK_Identity)) |
| 1500 | // SCS2 is a proper subsequence of SCS1. |
| 1501 | return ImplicitConversionSequence::Worse; |
| 1502 | |
| 1503 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1504 | // defined below), or, if not that, |
| 1505 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1506 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1507 | if (Rank1 < Rank2) |
| 1508 | return ImplicitConversionSequence::Better; |
| 1509 | else if (Rank2 < Rank1) |
| 1510 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1512 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1513 | // are indistinguishable unless one of the following rules |
| 1514 | // applies: |
| 1515 | |
| 1516 | // A conversion that is not a conversion of a pointer, or |
| 1517 | // pointer to member, to bool is better than another conversion |
| 1518 | // that is such a conversion. |
| 1519 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1520 | return SCS2.isPointerConversionToBool() |
| 1521 | ? ImplicitConversionSequence::Better |
| 1522 | : ImplicitConversionSequence::Worse; |
| 1523 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1524 | // C++ [over.ics.rank]p4b2: |
| 1525 | // |
| 1526 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1527 | // conversion of B* to A* is better than conversion of B* to |
| 1528 | // void*, and conversion of A* to void* is better than conversion |
| 1529 | // of B* to void*. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1530 | bool SCS1ConvertsToVoid |
| 1531 | = SCS1.isPointerConversionToVoidPointer(Context); |
| 1532 | bool SCS2ConvertsToVoid |
| 1533 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1534 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1535 | // Exactly one of the conversion sequences is a conversion to |
| 1536 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1537 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1538 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1539 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1540 | // Neither conversion sequence converts to a void pointer; compare |
| 1541 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1542 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1543 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1544 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1545 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1546 | // Both conversion sequences are conversions to void |
| 1547 | // pointers. Compare the source types to determine if there's an |
| 1548 | // inheritance relationship in their sources. |
| 1549 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1550 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1551 | |
| 1552 | // Adjust the types we're converting from via the array-to-pointer |
| 1553 | // conversion, if we need to. |
| 1554 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1555 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1556 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1557 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1558 | |
| 1559 | QualType FromPointee1 |
| 1560 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1561 | QualType FromPointee2 |
| 1562 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1563 | |
| 1564 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1565 | return ImplicitConversionSequence::Better; |
| 1566 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1567 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Objective-C++: If one interface is more specific than the |
| 1570 | // other, it is the better one. |
| 1571 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1572 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1573 | if (FromIface1 && FromIface1) { |
| 1574 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1575 | return ImplicitConversionSequence::Better; |
| 1576 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1577 | return ImplicitConversionSequence::Worse; |
| 1578 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1579 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1580 | |
| 1581 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1582 | // bullet 3). |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1583 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1584 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1585 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1587 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1588 | // C++0x [over.ics.rank]p3b4: |
| 1589 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1590 | // implicit object parameter of a non-static member function declared |
| 1591 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1592 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1593 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1594 | // or if the member function in this case has a ref qualifier. |
| 1595 | // (Of course, we don't have ref qualifiers yet.) |
| 1596 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1597 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1598 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1599 | |
| 1600 | // C++ [over.ics.rank]p3b4: |
| 1601 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1602 | // which the references refer are the same type except for |
| 1603 | // top-level cv-qualifiers, and the type to which the reference |
| 1604 | // initialized by S2 refers is more cv-qualified than the type |
| 1605 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1606 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1607 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1608 | T1 = Context.getCanonicalType(T1); |
| 1609 | T2 = Context.getCanonicalType(T2); |
| 1610 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1611 | if (T2.isMoreQualifiedThan(T1)) |
| 1612 | return ImplicitConversionSequence::Better; |
| 1613 | else if (T1.isMoreQualifiedThan(T2)) |
| 1614 | return ImplicitConversionSequence::Worse; |
| 1615 | } |
| 1616 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1617 | |
| 1618 | return ImplicitConversionSequence::Indistinguishable; |
| 1619 | } |
| 1620 | |
| 1621 | /// CompareQualificationConversions - Compares two standard conversion |
| 1622 | /// sequences to determine whether they can be ranked based on their |
| 1623 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1624 | ImplicitConversionSequence::CompareKind |
| 1625 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
| 1626 | const StandardConversionSequence& SCS2) |
| 1627 | { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1628 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1629 | // -- S1 and S2 differ only in their qualification conversion and |
| 1630 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1631 | // cv-qualification signature of type T1 is a proper subset of |
| 1632 | // the cv-qualification signature of type T2, and S1 is not the |
| 1633 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1634 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1635 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1636 | return ImplicitConversionSequence::Indistinguishable; |
| 1637 | |
| 1638 | // FIXME: the example in the standard doesn't use a qualification |
| 1639 | // conversion (!) |
| 1640 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1641 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1642 | T1 = Context.getCanonicalType(T1); |
| 1643 | T2 = Context.getCanonicalType(T2); |
| 1644 | |
| 1645 | // If the types are the same, we won't learn anything by unwrapped |
| 1646 | // them. |
| 1647 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1648 | return ImplicitConversionSequence::Indistinguishable; |
| 1649 | |
| 1650 | ImplicitConversionSequence::CompareKind Result |
| 1651 | = ImplicitConversionSequence::Indistinguishable; |
| 1652 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1653 | // Within each iteration of the loop, we check the qualifiers to |
| 1654 | // determine if this still looks like a qualification |
| 1655 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1656 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1657 | // until there are no more pointers or pointers-to-members left |
| 1658 | // to unwrap. This essentially mimics what |
| 1659 | // IsQualificationConversion does, but here we're checking for a |
| 1660 | // strict subset of qualifiers. |
| 1661 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1662 | // The qualifiers are the same, so this doesn't tell us anything |
| 1663 | // about how the sequences rank. |
| 1664 | ; |
| 1665 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1666 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1667 | if (Result == ImplicitConversionSequence::Worse) |
| 1668 | // Neither has qualifiers that are a subset of the other's |
| 1669 | // qualifiers. |
| 1670 | return ImplicitConversionSequence::Indistinguishable; |
| 1671 | |
| 1672 | Result = ImplicitConversionSequence::Better; |
| 1673 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1674 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1675 | if (Result == ImplicitConversionSequence::Better) |
| 1676 | // Neither has qualifiers that are a subset of the other's |
| 1677 | // qualifiers. |
| 1678 | return ImplicitConversionSequence::Indistinguishable; |
| 1679 | |
| 1680 | Result = ImplicitConversionSequence::Worse; |
| 1681 | } else { |
| 1682 | // Qualifiers are disjoint. |
| 1683 | return ImplicitConversionSequence::Indistinguishable; |
| 1684 | } |
| 1685 | |
| 1686 | // If the types after this point are equivalent, we're done. |
| 1687 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1688 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1691 | // Check that the winning standard conversion sequence isn't using |
| 1692 | // the deprecated string literal array to pointer conversion. |
| 1693 | switch (Result) { |
| 1694 | case ImplicitConversionSequence::Better: |
| 1695 | if (SCS1.Deprecated) |
| 1696 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1697 | break; |
| 1698 | |
| 1699 | case ImplicitConversionSequence::Indistinguishable: |
| 1700 | break; |
| 1701 | |
| 1702 | case ImplicitConversionSequence::Worse: |
| 1703 | if (SCS2.Deprecated) |
| 1704 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1705 | break; |
| 1706 | } |
| 1707 | |
| 1708 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1711 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1712 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1713 | /// various kinds of derived-to-base conversions (C++ |
| 1714 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1715 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1716 | ImplicitConversionSequence::CompareKind |
| 1717 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1718 | const StandardConversionSequence& SCS2) { |
| 1719 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1720 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1721 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1722 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1723 | |
| 1724 | // Adjust the types we're converting from via the array-to-pointer |
| 1725 | // conversion, if we need to. |
| 1726 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1727 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1728 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1729 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1730 | |
| 1731 | // Canonicalize all of the types. |
| 1732 | FromType1 = Context.getCanonicalType(FromType1); |
| 1733 | ToType1 = Context.getCanonicalType(ToType1); |
| 1734 | FromType2 = Context.getCanonicalType(FromType2); |
| 1735 | ToType2 = Context.getCanonicalType(ToType2); |
| 1736 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1737 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1738 | // |
| 1739 | // If class B is derived directly or indirectly from class A and |
| 1740 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1741 | // |
| 1742 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1743 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Compare based on pointer conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1746 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1747 | SCS2.Second == ICK_Pointer_Conversion && |
| 1748 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1749 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1750 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1751 | QualType FromPointee1 |
| 1752 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1753 | QualType ToPointee1 |
| 1754 | = ToType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1755 | QualType FromPointee2 |
| 1756 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1757 | QualType ToPointee2 |
| 1758 | = ToType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1759 | |
| 1760 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1761 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1762 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAsObjCInterfaceType(); |
| 1763 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAsObjCInterfaceType(); |
| 1764 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1765 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1766 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1767 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1768 | return ImplicitConversionSequence::Better; |
| 1769 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1770 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1771 | |
| 1772 | if (ToIface1 && ToIface2) { |
| 1773 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1774 | return ImplicitConversionSequence::Better; |
| 1775 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1776 | return ImplicitConversionSequence::Worse; |
| 1777 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1778 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1779 | |
| 1780 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1781 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1782 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1783 | return ImplicitConversionSequence::Better; |
| 1784 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1785 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1786 | |
| 1787 | if (FromIface1 && FromIface2) { |
| 1788 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1789 | return ImplicitConversionSequence::Better; |
| 1790 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1791 | return ImplicitConversionSequence::Worse; |
| 1792 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1793 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1796 | // Compare based on reference bindings. |
| 1797 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1798 | SCS1.Second == ICK_Derived_To_Base) { |
| 1799 | // -- binding of an expression of type C to a reference of type |
| 1800 | // B& is better than binding an expression of type C to a |
| 1801 | // reference of type A&, |
| 1802 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1803 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1804 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1805 | return ImplicitConversionSequence::Better; |
| 1806 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1807 | return ImplicitConversionSequence::Worse; |
| 1808 | } |
| 1809 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1810 | // -- binding of an expression of type B to a reference of type |
| 1811 | // A& is better than binding an expression of type C to a |
| 1812 | // reference of type A&, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1813 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1814 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1815 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1816 | return ImplicitConversionSequence::Better; |
| 1817 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1818 | return ImplicitConversionSequence::Worse; |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | |
| 1823 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1824 | // A::* to C::*, |
| 1825 | |
| 1826 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1827 | // A::* to C::*, and |
| 1828 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1829 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1830 | SCS1.Second == ICK_Derived_To_Base) { |
| 1831 | // -- conversion of C to B is better than conversion of C to A, |
| 1832 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1833 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1834 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1835 | return ImplicitConversionSequence::Better; |
| 1836 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1837 | return ImplicitConversionSequence::Worse; |
| 1838 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1839 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1840 | // -- conversion of B to A is better than conversion of C to A. |
| 1841 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1842 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1843 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1844 | return ImplicitConversionSequence::Better; |
| 1845 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1846 | return ImplicitConversionSequence::Worse; |
| 1847 | } |
| 1848 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1849 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1850 | return ImplicitConversionSequence::Indistinguishable; |
| 1851 | } |
| 1852 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1853 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1854 | /// ToType from the expression From. Return the implicit conversion |
| 1855 | /// sequence required to pass this argument, which may be a bad |
| 1856 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1857 | /// a parameter of this type). If @p SuppressUserConversions, then we |
| 1858 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1859 | ImplicitConversionSequence |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1860 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
| 1861 | bool SuppressUserConversions) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1862 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1863 | ImplicitConversionSequence ICS; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1864 | CheckReferenceInit(From, ToType, &ICS, SuppressUserConversions); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1865 | return ICS; |
| 1866 | } else { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1867 | return TryImplicitConversion(From, ToType, SuppressUserConversions); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | /// PerformArgumentPassing - Pass the argument Arg into a parameter of |
| 1872 | /// type ToType. Returns true (and emits a diagnostic) if there was |
| 1873 | /// an error, returns false if the initialization succeeded. |
| 1874 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
| 1875 | const char* Flavor) { |
| 1876 | if (!getLangOptions().CPlusPlus) { |
| 1877 | // In C, argument passing is the same as performing an assignment. |
| 1878 | QualType FromType = From->getType(); |
| 1879 | AssignConvertType ConvTy = |
| 1880 | CheckSingleAssignmentConstraints(ToType, From); |
| 1881 | |
| 1882 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 1883 | FromType, From, Flavor); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1884 | } |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1885 | |
| 1886 | if (ToType->isReferenceType()) |
| 1887 | return CheckReferenceInit(From, ToType); |
| 1888 | |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1889 | if (!PerformImplicitConversion(From, ToType, Flavor)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1890 | return false; |
| 1891 | |
| 1892 | return Diag(From->getSourceRange().getBegin(), |
| 1893 | diag::err_typecheck_convert_incompatible) |
| 1894 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1897 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 1898 | /// parameter of the given member function (@c Method) from the |
| 1899 | /// expression @p From. |
| 1900 | ImplicitConversionSequence |
| 1901 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 1902 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
| 1903 | unsigned MethodQuals = Method->getTypeQualifiers(); |
| 1904 | QualType ImplicitParamType = ClassType.getQualifiedType(MethodQuals); |
| 1905 | |
| 1906 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 1907 | // to exit early. |
| 1908 | ImplicitConversionSequence ICS; |
| 1909 | ICS.Standard.setAsIdentityConversion(); |
| 1910 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1911 | |
| 1912 | // We need to have an object of class type. |
| 1913 | QualType FromType = From->getType(); |
| 1914 | if (!FromType->isRecordType()) |
| 1915 | return ICS; |
| 1916 | |
| 1917 | // The implicit object parmeter is has the type "reference to cv X", |
| 1918 | // where X is the class of which the function is a member |
| 1919 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 1920 | // conversion sequence for the argument, we are not allowed to |
| 1921 | // create temporaries or perform user-defined conversions |
| 1922 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 1923 | // reference binding here, that allows class rvalues to bind to |
| 1924 | // non-constant references. |
| 1925 | |
| 1926 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 1927 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 1928 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 1929 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 1930 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 1931 | return ICS; |
| 1932 | |
| 1933 | // Check that we have either the same type or a derived type. It |
| 1934 | // affects the conversion rank. |
| 1935 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 1936 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 1937 | ICS.Standard.Second = ICK_Identity; |
| 1938 | else if (IsDerivedFrom(FromType, ClassType)) |
| 1939 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1940 | else |
| 1941 | return ICS; |
| 1942 | |
| 1943 | // Success. Mark this as a reference binding. |
| 1944 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1945 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 1946 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 1947 | ICS.Standard.ReferenceBinding = true; |
| 1948 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 1949 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1950 | return ICS; |
| 1951 | } |
| 1952 | |
| 1953 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 1954 | /// the implicit object parameter for the given Method with the given |
| 1955 | /// expression. |
| 1956 | bool |
| 1957 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
| 1958 | QualType ImplicitParamType |
| 1959 | = Method->getThisType(Context)->getAsPointerType()->getPointeeType(); |
| 1960 | ImplicitConversionSequence ICS |
| 1961 | = TryObjectArgumentInitialization(From, Method); |
| 1962 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 1963 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1964 | diag::err_implicit_object_parameter_init) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1965 | << ImplicitParamType << From->getType() << From->getSourceRange(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
| 1968 | CheckDerivedToBaseConversion(From->getType(), ImplicitParamType, |
| 1969 | From->getSourceRange().getBegin(), |
| 1970 | From->getSourceRange())) |
| 1971 | return true; |
| 1972 | |
| 1973 | ImpCastExprToType(From, ImplicitParamType, /*isLvalue=*/true); |
| 1974 | return false; |
| 1975 | } |
| 1976 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1977 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 1978 | /// expression From to bool (C++0x [conv]p3). |
| 1979 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
| 1980 | return TryImplicitConversion(From, Context.BoolTy, false, true); |
| 1981 | } |
| 1982 | |
| 1983 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 1984 | /// of the expression From to bool (C++0x [conv]p3). |
| 1985 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 1986 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 1987 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 1988 | return false; |
| 1989 | |
| 1990 | return Diag(From->getSourceRange().getBegin(), |
| 1991 | diag::err_typecheck_bool_condition) |
| 1992 | << From->getType() << From->getSourceRange(); |
| 1993 | } |
| 1994 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1995 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1996 | /// candidate functions, using the given function call arguments. If |
| 1997 | /// @p SuppressUserConversions, then don't allow user-defined |
| 1998 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1999 | void |
| 2000 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
| 2001 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2002 | OverloadCandidateSet& CandidateSet, |
| 2003 | bool SuppressUserConversions) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2004 | { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2005 | const FunctionProtoType* Proto |
| 2006 | = dyn_cast<FunctionProtoType>(Function->getType()->getAsFunctionType()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2007 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2008 | assert(!isa<CXXConversionDecl>(Function) && |
| 2009 | "Use AddConversionCandidate for conversion functions"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2010 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2011 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2012 | // If we get here, it's because we're calling a member function |
| 2013 | // that is named without a member access expression (e.g., |
| 2014 | // "this->f") that was either written explicitly or created |
| 2015 | // implicitly. This can happen with a qualified call to a member |
| 2016 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2017 | // object argument (C++ [over.call.func]p3). |
| 2018 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
| 2019 | SuppressUserConversions); |
| 2020 | return; |
| 2021 | } |
| 2022 | |
| 2023 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2024 | // Add this candidate |
| 2025 | CandidateSet.push_back(OverloadCandidate()); |
| 2026 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2027 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2028 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2029 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2030 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2031 | |
| 2032 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2033 | |
| 2034 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2035 | // parameters is viable only if it has an ellipsis in its parameter |
| 2036 | // list (8.3.5). |
| 2037 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2038 | Candidate.Viable = false; |
| 2039 | return; |
| 2040 | } |
| 2041 | |
| 2042 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2043 | // is viable only if the (m+1)st parameter has a default argument |
| 2044 | // (8.3.6). For the purposes of overload resolution, the |
| 2045 | // parameter list is truncated on the right, so that there are |
| 2046 | // exactly m parameters. |
| 2047 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 2048 | if (NumArgs < MinRequiredArgs) { |
| 2049 | // Not enough arguments. |
| 2050 | Candidate.Viable = false; |
| 2051 | return; |
| 2052 | } |
| 2053 | |
| 2054 | // Determine the implicit conversion sequences for each of the |
| 2055 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2056 | Candidate.Conversions.resize(NumArgs); |
| 2057 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2058 | if (ArgIdx < NumArgsInProto) { |
| 2059 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2060 | // exist for each argument an implicit conversion sequence |
| 2061 | // (13.3.3.1) that converts that argument to the corresponding |
| 2062 | // parameter of F. |
| 2063 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2064 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2065 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 2066 | SuppressUserConversions); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2067 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2068 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2069 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2070 | break; |
| 2071 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2072 | } else { |
| 2073 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2074 | // argument for which there is no corresponding parameter is |
| 2075 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2076 | Candidate.Conversions[ArgIdx].ConversionKind |
| 2077 | = ImplicitConversionSequence::EllipsisConversion; |
| 2078 | } |
| 2079 | } |
| 2080 | } |
| 2081 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2082 | /// \brief Add all of the function declarations in the given function set to |
| 2083 | /// the overload canddiate set. |
| 2084 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2085 | Expr **Args, unsigned NumArgs, |
| 2086 | OverloadCandidateSet& CandidateSet, |
| 2087 | bool SuppressUserConversions) { |
| 2088 | for (FunctionSet::const_iterator F = Functions.begin(), |
| 2089 | FEnd = Functions.end(); |
| 2090 | F != FEnd; ++F) |
| 2091 | AddOverloadCandidate(*F, Args, NumArgs, CandidateSet, |
| 2092 | SuppressUserConversions); |
| 2093 | } |
| 2094 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2095 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2096 | /// of candidate functions, using the given function call arguments |
| 2097 | /// and the object argument (@c Object). For example, in a call |
| 2098 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2099 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2100 | /// allow user-defined conversions via constructors or conversion |
| 2101 | /// operators. |
| 2102 | void |
| 2103 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2104 | Expr **Args, unsigned NumArgs, |
| 2105 | OverloadCandidateSet& CandidateSet, |
| 2106 | bool SuppressUserConversions) |
| 2107 | { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2108 | const FunctionProtoType* Proto |
| 2109 | = dyn_cast<FunctionProtoType>(Method->getType()->getAsFunctionType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2110 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
| 2111 | assert(!isa<CXXConversionDecl>(Method) && |
| 2112 | "Use AddConversionCandidate for conversion functions"); |
| 2113 | |
| 2114 | // Add this candidate |
| 2115 | CandidateSet.push_back(OverloadCandidate()); |
| 2116 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2117 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2118 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2119 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2120 | |
| 2121 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2122 | |
| 2123 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2124 | // parameters is viable only if it has an ellipsis in its parameter |
| 2125 | // list (8.3.5). |
| 2126 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2127 | Candidate.Viable = false; |
| 2128 | return; |
| 2129 | } |
| 2130 | |
| 2131 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2132 | // is viable only if the (m+1)st parameter has a default argument |
| 2133 | // (8.3.6). For the purposes of overload resolution, the |
| 2134 | // parameter list is truncated on the right, so that there are |
| 2135 | // exactly m parameters. |
| 2136 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2137 | if (NumArgs < MinRequiredArgs) { |
| 2138 | // Not enough arguments. |
| 2139 | Candidate.Viable = false; |
| 2140 | return; |
| 2141 | } |
| 2142 | |
| 2143 | Candidate.Viable = true; |
| 2144 | Candidate.Conversions.resize(NumArgs + 1); |
| 2145 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2146 | if (Method->isStatic() || !Object) |
| 2147 | // The implicit object argument is ignored. |
| 2148 | Candidate.IgnoreObjectArgument = true; |
| 2149 | else { |
| 2150 | // Determine the implicit conversion sequence for the object |
| 2151 | // parameter. |
| 2152 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
| 2153 | if (Candidate.Conversions[0].ConversionKind |
| 2154 | == ImplicitConversionSequence::BadConversion) { |
| 2155 | Candidate.Viable = false; |
| 2156 | return; |
| 2157 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | // Determine the implicit conversion sequences for each of the |
| 2161 | // arguments. |
| 2162 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2163 | if (ArgIdx < NumArgsInProto) { |
| 2164 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2165 | // exist for each argument an implicit conversion sequence |
| 2166 | // (13.3.3.1) that converts that argument to the corresponding |
| 2167 | // parameter of F. |
| 2168 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2169 | Candidate.Conversions[ArgIdx + 1] |
| 2170 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 2171 | SuppressUserConversions); |
| 2172 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2173 | == ImplicitConversionSequence::BadConversion) { |
| 2174 | Candidate.Viable = false; |
| 2175 | break; |
| 2176 | } |
| 2177 | } else { |
| 2178 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2179 | // argument for which there is no corresponding parameter is |
| 2180 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2181 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2182 | = ImplicitConversionSequence::EllipsisConversion; |
| 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2187 | /// AddConversionCandidate - Add a C++ conversion function as a |
| 2188 | /// candidate in the candidate set (C++ [over.match.conv], |
| 2189 | /// C++ [over.match.copy]). From is the expression we're converting from, |
| 2190 | /// and ToType is the type that we're eventually trying to convert to |
| 2191 | /// (which may or may not be the same type as the type that the |
| 2192 | /// conversion function produces). |
| 2193 | void |
| 2194 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2195 | Expr *From, QualType ToType, |
| 2196 | OverloadCandidateSet& CandidateSet) { |
| 2197 | // Add this candidate |
| 2198 | CandidateSet.push_back(OverloadCandidate()); |
| 2199 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2200 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2201 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2202 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2203 | Candidate.FinalConversion.setAsIdentityConversion(); |
| 2204 | Candidate.FinalConversion.FromTypePtr |
| 2205 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2206 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2207 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2208 | // Determine the implicit conversion sequence for the implicit |
| 2209 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2210 | Candidate.Viable = true; |
| 2211 | Candidate.Conversions.resize(1); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2212 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2213 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2214 | if (Candidate.Conversions[0].ConversionKind |
| 2215 | == ImplicitConversionSequence::BadConversion) { |
| 2216 | Candidate.Viable = false; |
| 2217 | return; |
| 2218 | } |
| 2219 | |
| 2220 | // To determine what the conversion from the result of calling the |
| 2221 | // conversion function to the type we're eventually trying to |
| 2222 | // convert to (ToType), we need to synthesize a call to the |
| 2223 | // conversion function and attempt copy initialization from it. This |
| 2224 | // makes sure that we get the right semantics with respect to |
| 2225 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2226 | // call on the stack and we don't need its arguments to be |
| 2227 | // well-formed. |
| 2228 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
| 2229 | SourceLocation()); |
| 2230 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2231 | &ConversionRef, false); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2232 | |
| 2233 | // Note that it is safe to allocate CallExpr on the stack here because |
| 2234 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2235 | // allocator). |
| 2236 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2237 | Conversion->getConversionType().getNonReferenceType(), |
| 2238 | SourceLocation()); |
| 2239 | ImplicitConversionSequence ICS = TryCopyInitialization(&Call, ToType, true); |
| 2240 | switch (ICS.ConversionKind) { |
| 2241 | case ImplicitConversionSequence::StandardConversion: |
| 2242 | Candidate.FinalConversion = ICS.Standard; |
| 2243 | break; |
| 2244 | |
| 2245 | case ImplicitConversionSequence::BadConversion: |
| 2246 | Candidate.Viable = false; |
| 2247 | break; |
| 2248 | |
| 2249 | default: |
| 2250 | assert(false && |
| 2251 | "Can only end up with a standard conversion sequence or failure"); |
| 2252 | } |
| 2253 | } |
| 2254 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2255 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2256 | /// converts the given @c Object to a function pointer via the |
| 2257 | /// conversion function @c Conversion, and then attempts to call it |
| 2258 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2259 | /// the type of function that we'll eventually be calling. |
| 2260 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2261 | const FunctionProtoType *Proto, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2262 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2263 | OverloadCandidateSet& CandidateSet) { |
| 2264 | CandidateSet.push_back(OverloadCandidate()); |
| 2265 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2266 | Candidate.Function = 0; |
| 2267 | Candidate.Surrogate = Conversion; |
| 2268 | Candidate.Viable = true; |
| 2269 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2270 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2271 | Candidate.Conversions.resize(NumArgs + 1); |
| 2272 | |
| 2273 | // Determine the implicit conversion sequence for the implicit |
| 2274 | // object parameter. |
| 2275 | ImplicitConversionSequence ObjectInit |
| 2276 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2277 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2278 | Candidate.Viable = false; |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | // The first conversion is actually a user-defined conversion whose |
| 2283 | // first conversion is ObjectInit's standard conversion (which is |
| 2284 | // effectively a reference binding). Record it as such. |
| 2285 | Candidate.Conversions[0].ConversionKind |
| 2286 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2287 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2288 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
| 2289 | Candidate.Conversions[0].UserDefined.After |
| 2290 | = Candidate.Conversions[0].UserDefined.Before; |
| 2291 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2292 | |
| 2293 | // Find the |
| 2294 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2295 | |
| 2296 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2297 | // parameters is viable only if it has an ellipsis in its parameter |
| 2298 | // list (8.3.5). |
| 2299 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2300 | Candidate.Viable = false; |
| 2301 | return; |
| 2302 | } |
| 2303 | |
| 2304 | // Function types don't have any default arguments, so just check if |
| 2305 | // we have enough arguments. |
| 2306 | if (NumArgs < NumArgsInProto) { |
| 2307 | // Not enough arguments. |
| 2308 | Candidate.Viable = false; |
| 2309 | return; |
| 2310 | } |
| 2311 | |
| 2312 | // Determine the implicit conversion sequences for each of the |
| 2313 | // arguments. |
| 2314 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2315 | if (ArgIdx < NumArgsInProto) { |
| 2316 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2317 | // exist for each argument an implicit conversion sequence |
| 2318 | // (13.3.3.1) that converts that argument to the corresponding |
| 2319 | // parameter of F. |
| 2320 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2321 | Candidate.Conversions[ArgIdx + 1] |
| 2322 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 2323 | /*SuppressUserConversions=*/false); |
| 2324 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2325 | == ImplicitConversionSequence::BadConversion) { |
| 2326 | Candidate.Viable = false; |
| 2327 | break; |
| 2328 | } |
| 2329 | } else { |
| 2330 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2331 | // argument for which there is no corresponding parameter is |
| 2332 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2333 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2334 | = ImplicitConversionSequence::EllipsisConversion; |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2339 | // FIXME: This will eventually be removed, once we've migrated all of |
| 2340 | // the operator overloading logic over to the scheme used by binary |
| 2341 | // operators, which works for template instantiation. |
| 2342 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2343 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2344 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2345 | OverloadCandidateSet& CandidateSet, |
| 2346 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2347 | |
| 2348 | FunctionSet Functions; |
| 2349 | |
| 2350 | QualType T1 = Args[0]->getType(); |
| 2351 | QualType T2; |
| 2352 | if (NumArgs > 1) |
| 2353 | T2 = Args[1]->getType(); |
| 2354 | |
| 2355 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2356 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
| 2357 | ArgumentDependentLookup(OpName, Args, NumArgs, Functions); |
| 2358 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2359 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 2360 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
| 2361 | } |
| 2362 | |
| 2363 | /// \brief Add overload candidates for overloaded operators that are |
| 2364 | /// member functions. |
| 2365 | /// |
| 2366 | /// Add the overloaded operator candidates that are member functions |
| 2367 | /// for the operator Op that was used in an operator expression such |
| 2368 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2369 | /// CandidateSet will store the added overload candidates. (C++ |
| 2370 | /// [over.match.oper]). |
| 2371 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2372 | SourceLocation OpLoc, |
| 2373 | Expr **Args, unsigned NumArgs, |
| 2374 | OverloadCandidateSet& CandidateSet, |
| 2375 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2376 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2377 | |
| 2378 | // C++ [over.match.oper]p3: |
| 2379 | // For a unary operator @ with an operand of a type whose |
| 2380 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2381 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2382 | // a right operand of a type whose cv-unqualified version is T2, |
| 2383 | // three sets of candidate functions, designated member |
| 2384 | // candidates, non-member candidates and built-in candidates, are |
| 2385 | // constructed as follows: |
| 2386 | QualType T1 = Args[0]->getType(); |
| 2387 | QualType T2; |
| 2388 | if (NumArgs > 1) |
| 2389 | T2 = Args[1]->getType(); |
| 2390 | |
| 2391 | // -- If T1 is a class type, the set of member candidates is the |
| 2392 | // result of the qualified lookup of T1::operator@ |
| 2393 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2394 | // empty. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2395 | // FIXME: Lookup in base classes, too! |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2396 | if (const RecordType *T1Rec = T1->getAsRecordType()) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2397 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 2398 | for (llvm::tie(Oper, OperEnd) = T1Rec->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2399 | Oper != OperEnd; ++Oper) |
| 2400 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], |
| 2401 | Args+1, NumArgs - 1, CandidateSet, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2402 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2403 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2406 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2407 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2408 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2409 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2410 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2411 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2412 | /// (at the beginning of the argument list) that will be contextually |
| 2413 | /// converted to bool. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2414 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
| 2415 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2416 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2417 | bool IsAssignmentOperator, |
| 2418 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2419 | // Add this candidate |
| 2420 | CandidateSet.push_back(OverloadCandidate()); |
| 2421 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2422 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2423 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2424 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2425 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2426 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2427 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2428 | |
| 2429 | // Determine the implicit conversion sequences for each of the |
| 2430 | // arguments. |
| 2431 | Candidate.Viable = true; |
| 2432 | Candidate.Conversions.resize(NumArgs); |
| 2433 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2434 | // C++ [over.match.oper]p4: |
| 2435 | // For the built-in assignment operators, conversions of the |
| 2436 | // left operand are restricted as follows: |
| 2437 | // -- no temporaries are introduced to hold the left operand, and |
| 2438 | // -- no user-defined conversions are applied to the left |
| 2439 | // operand to achieve a type match with the left-most |
| 2440 | // parameter of a built-in candidate. |
| 2441 | // |
| 2442 | // We block these conversions by turning off user-defined |
| 2443 | // conversions, since that is the only way that initialization of |
| 2444 | // a reference to a non-class type can occur from something that |
| 2445 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2446 | if (ArgIdx < NumContextualBoolArguments) { |
| 2447 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
| 2448 | "Contextual conversion to bool requires bool type"); |
| 2449 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2450 | } else { |
| 2451 | Candidate.Conversions[ArgIdx] |
| 2452 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
| 2453 | ArgIdx == 0 && IsAssignmentOperator); |
| 2454 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2455 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2456 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2457 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2458 | break; |
| 2459 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2464 | /// candidate operator functions for built-in operators (C++ |
| 2465 | /// [over.built]). The types are separated into pointer types and |
| 2466 | /// enumeration types. |
| 2467 | class BuiltinCandidateTypeSet { |
| 2468 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2469 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2470 | |
| 2471 | /// PointerTypes - The set of pointer types that will be used in the |
| 2472 | /// built-in candidates. |
| 2473 | TypeSet PointerTypes; |
| 2474 | |
| 2475 | /// EnumerationTypes - The set of enumeration types that will be |
| 2476 | /// used in the built-in candidates. |
| 2477 | TypeSet EnumerationTypes; |
| 2478 | |
| 2479 | /// Context - The AST context in which we will build the type sets. |
| 2480 | ASTContext &Context; |
| 2481 | |
| 2482 | bool AddWithMoreQualifiedTypeVariants(QualType Ty); |
| 2483 | |
| 2484 | public: |
| 2485 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2486 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2487 | |
| 2488 | BuiltinCandidateTypeSet(ASTContext &Context) : Context(Context) { } |
| 2489 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2490 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions, |
| 2491 | bool AllowExplicitConversions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2492 | |
| 2493 | /// pointer_begin - First pointer type found; |
| 2494 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2495 | |
| 2496 | /// pointer_end - Last pointer type found; |
| 2497 | iterator pointer_end() { return PointerTypes.end(); } |
| 2498 | |
| 2499 | /// enumeration_begin - First enumeration type found; |
| 2500 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2501 | |
| 2502 | /// enumeration_end - Last enumeration type found; |
| 2503 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2504 | }; |
| 2505 | |
| 2506 | /// AddWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
| 2507 | /// the set of pointer types along with any more-qualified variants of |
| 2508 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2509 | /// will add "int const *", "int const volatile *", "int const |
| 2510 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2511 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2512 | /// false otherwise. |
| 2513 | bool BuiltinCandidateTypeSet::AddWithMoreQualifiedTypeVariants(QualType Ty) { |
| 2514 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2515 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2516 | return false; |
| 2517 | |
| 2518 | if (const PointerType *PointerTy = Ty->getAsPointerType()) { |
| 2519 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2520 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2521 | |
| 2522 | // FIXME: Do we have to add CVR qualifiers at *all* levels to deal |
| 2523 | // with all pointer conversions that don't cast away constness? |
| 2524 | if (!PointeeTy.isConstQualified()) |
| 2525 | AddWithMoreQualifiedTypeVariants |
| 2526 | (Context.getPointerType(PointeeTy.withConst())); |
| 2527 | if (!PointeeTy.isVolatileQualified()) |
| 2528 | AddWithMoreQualifiedTypeVariants |
| 2529 | (Context.getPointerType(PointeeTy.withVolatile())); |
| 2530 | if (!PointeeTy.isRestrictQualified()) |
| 2531 | AddWithMoreQualifiedTypeVariants |
| 2532 | (Context.getPointerType(PointeeTy.withRestrict())); |
| 2533 | } |
| 2534 | |
| 2535 | return true; |
| 2536 | } |
| 2537 | |
| 2538 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2539 | /// Ty can be implicit converted to the given set of @p Types. We're |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2540 | /// primarily interested in pointer types and enumeration types. |
| 2541 | /// AllowUserConversions is true if we should look at the conversion |
| 2542 | /// functions of a class type, and AllowExplicitConversions if we |
| 2543 | /// should also include the explicit conversion functions of a class |
| 2544 | /// type. |
| 2545 | void |
| 2546 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2547 | bool AllowUserConversions, |
| 2548 | bool AllowExplicitConversions) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2549 | // Only deal with canonical types. |
| 2550 | Ty = Context.getCanonicalType(Ty); |
| 2551 | |
| 2552 | // Look through reference types; they aren't part of the type of an |
| 2553 | // expression for the purposes of conversions. |
| 2554 | if (const ReferenceType *RefTy = Ty->getAsReferenceType()) |
| 2555 | Ty = RefTy->getPointeeType(); |
| 2556 | |
| 2557 | // We don't care about qualifiers on the type. |
| 2558 | Ty = Ty.getUnqualifiedType(); |
| 2559 | |
| 2560 | if (const PointerType *PointerTy = Ty->getAsPointerType()) { |
| 2561 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2562 | |
| 2563 | // Insert our type, and its more-qualified variants, into the set |
| 2564 | // of types. |
| 2565 | if (!AddWithMoreQualifiedTypeVariants(Ty)) |
| 2566 | return; |
| 2567 | |
| 2568 | // Add 'cv void*' to our set of types. |
| 2569 | if (!Ty->isVoidType()) { |
| 2570 | QualType QualVoid |
| 2571 | = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 2572 | AddWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
| 2573 | } |
| 2574 | |
| 2575 | // If this is a pointer to a class type, add pointers to its bases |
| 2576 | // (with the same level of cv-qualification as the original |
| 2577 | // derived class, of course). |
| 2578 | if (const RecordType *PointeeRec = PointeeTy->getAsRecordType()) { |
| 2579 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 2580 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2581 | Base != ClassDecl->bases_end(); ++Base) { |
| 2582 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
| 2583 | BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 2584 | |
| 2585 | // Add the pointer type, recursively, so that we get all of |
| 2586 | // the indirect base classes, too. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2587 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2591 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2592 | } else if (AllowUserConversions) { |
| 2593 | if (const RecordType *TyRec = Ty->getAsRecordType()) { |
| 2594 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 2595 | // FIXME: Visit conversion functions in the base classes, too. |
| 2596 | OverloadedFunctionDecl *Conversions |
| 2597 | = ClassDecl->getConversionFunctions(); |
| 2598 | for (OverloadedFunctionDecl::function_iterator Func |
| 2599 | = Conversions->function_begin(); |
| 2600 | Func != Conversions->function_end(); ++Func) { |
| 2601 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2602 | if (AllowExplicitConversions || !Conv->isExplicit()) |
| 2603 | AddTypesConvertedFrom(Conv->getConversionType(), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2609 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 2610 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 2611 | /// on the operator @p Op and the arguments given. For example, if the |
| 2612 | /// operator is a binary '+', this routine might add "int |
| 2613 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2614 | void |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2615 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 2616 | Expr **Args, unsigned NumArgs, |
| 2617 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2618 | // The set of "promoted arithmetic types", which are the arithmetic |
| 2619 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 2620 | // that the first few of these types are the promoted integral |
| 2621 | // types; these types need to be first. |
| 2622 | // FIXME: What about complex? |
| 2623 | const unsigned FirstIntegralType = 0; |
| 2624 | const unsigned LastIntegralType = 13; |
| 2625 | const unsigned FirstPromotedIntegralType = 7, |
| 2626 | LastPromotedIntegralType = 13; |
| 2627 | const unsigned FirstPromotedArithmeticType = 7, |
| 2628 | LastPromotedArithmeticType = 16; |
| 2629 | const unsigned NumArithmeticTypes = 16; |
| 2630 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
| 2631 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 2632 | Context.SignedCharTy, Context.ShortTy, |
| 2633 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 2634 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 2635 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 2636 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 2637 | }; |
| 2638 | |
| 2639 | // Find all of the types that the arguments can convert to, but only |
| 2640 | // if the operator we're looking at has built-in operator candidates |
| 2641 | // that make use of these types. |
| 2642 | BuiltinCandidateTypeSet CandidateTypes(Context); |
| 2643 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 2644 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2645 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2646 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2647 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
| 2648 | (Op == OO_Star && NumArgs == 1)) { |
| 2649 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2650 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 2651 | true, |
| 2652 | (Op == OO_Exclaim || |
| 2653 | Op == OO_AmpAmp || |
| 2654 | Op == OO_PipePipe)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | bool isComparison = false; |
| 2658 | switch (Op) { |
| 2659 | case OO_None: |
| 2660 | case NUM_OVERLOADED_OPERATORS: |
| 2661 | assert(false && "Expected an overloaded operator"); |
| 2662 | break; |
| 2663 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2664 | case OO_Star: // '*' is either unary or binary |
| 2665 | if (NumArgs == 1) |
| 2666 | goto UnaryStar; |
| 2667 | else |
| 2668 | goto BinaryStar; |
| 2669 | break; |
| 2670 | |
| 2671 | case OO_Plus: // '+' is either unary or binary |
| 2672 | if (NumArgs == 1) |
| 2673 | goto UnaryPlus; |
| 2674 | else |
| 2675 | goto BinaryPlus; |
| 2676 | break; |
| 2677 | |
| 2678 | case OO_Minus: // '-' is either unary or binary |
| 2679 | if (NumArgs == 1) |
| 2680 | goto UnaryMinus; |
| 2681 | else |
| 2682 | goto BinaryMinus; |
| 2683 | break; |
| 2684 | |
| 2685 | case OO_Amp: // '&' is either unary or binary |
| 2686 | if (NumArgs == 1) |
| 2687 | goto UnaryAmp; |
| 2688 | else |
| 2689 | goto BinaryAmp; |
| 2690 | |
| 2691 | case OO_PlusPlus: |
| 2692 | case OO_MinusMinus: |
| 2693 | // C++ [over.built]p3: |
| 2694 | // |
| 2695 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 2696 | // is either volatile or empty, there exist candidate operator |
| 2697 | // functions of the form |
| 2698 | // |
| 2699 | // VQ T& operator++(VQ T&); |
| 2700 | // T operator++(VQ T&, int); |
| 2701 | // |
| 2702 | // C++ [over.built]p4: |
| 2703 | // |
| 2704 | // For every pair (T, VQ), where T is an arithmetic type other |
| 2705 | // than bool, and VQ is either volatile or empty, there exist |
| 2706 | // candidate operator functions of the form |
| 2707 | // |
| 2708 | // VQ T& operator--(VQ T&); |
| 2709 | // T operator--(VQ T&, int); |
| 2710 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 2711 | Arith < NumArithmeticTypes; ++Arith) { |
| 2712 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 2713 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2714 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Non-volatile version. |
| 2717 | if (NumArgs == 1) |
| 2718 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2719 | else |
| 2720 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 2721 | |
| 2722 | // Volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2723 | ParamTypes[0] = Context.getLValueReferenceType(ArithTy.withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2724 | if (NumArgs == 1) |
| 2725 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2726 | else |
| 2727 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 2728 | } |
| 2729 | |
| 2730 | // C++ [over.built]p5: |
| 2731 | // |
| 2732 | // For every pair (T, VQ), where T is a cv-qualified or |
| 2733 | // cv-unqualified object type, and VQ is either volatile or |
| 2734 | // empty, there exist candidate operator functions of the form |
| 2735 | // |
| 2736 | // T*VQ& operator++(T*VQ&); |
| 2737 | // T*VQ& operator--(T*VQ&); |
| 2738 | // T* operator++(T*VQ&, int); |
| 2739 | // T* operator--(T*VQ&, int); |
| 2740 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2741 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2742 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2743 | if (!(*Ptr)->getAsPointerType()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2744 | continue; |
| 2745 | |
| 2746 | QualType ParamTypes[2] = { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2747 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2748 | }; |
| 2749 | |
| 2750 | // Without volatile |
| 2751 | if (NumArgs == 1) |
| 2752 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2753 | else |
| 2754 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2755 | |
| 2756 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 2757 | // With volatile |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2758 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2759 | if (NumArgs == 1) |
| 2760 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2761 | else |
| 2762 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2763 | } |
| 2764 | } |
| 2765 | break; |
| 2766 | |
| 2767 | UnaryStar: |
| 2768 | // C++ [over.built]p6: |
| 2769 | // For every cv-qualified or cv-unqualified object type T, there |
| 2770 | // exist candidate operator functions of the form |
| 2771 | // |
| 2772 | // T& operator*(T*); |
| 2773 | // |
| 2774 | // C++ [over.built]p7: |
| 2775 | // For every function type T, there exist candidate operator |
| 2776 | // functions of the form |
| 2777 | // T& operator*(T*); |
| 2778 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2779 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2780 | QualType ParamTy = *Ptr; |
| 2781 | QualType PointeeTy = ParamTy->getAsPointerType()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2782 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2783 | &ParamTy, Args, 1, CandidateSet); |
| 2784 | } |
| 2785 | break; |
| 2786 | |
| 2787 | UnaryPlus: |
| 2788 | // C++ [over.built]p8: |
| 2789 | // For every type T, there exist candidate operator functions of |
| 2790 | // the form |
| 2791 | // |
| 2792 | // T* operator+(T*); |
| 2793 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2794 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2795 | QualType ParamTy = *Ptr; |
| 2796 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 2797 | } |
| 2798 | |
| 2799 | // Fall through |
| 2800 | |
| 2801 | UnaryMinus: |
| 2802 | // C++ [over.built]p9: |
| 2803 | // For every promoted arithmetic type T, there exist candidate |
| 2804 | // operator functions of the form |
| 2805 | // |
| 2806 | // T operator+(T); |
| 2807 | // T operator-(T); |
| 2808 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 2809 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 2810 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 2811 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 2812 | } |
| 2813 | break; |
| 2814 | |
| 2815 | case OO_Tilde: |
| 2816 | // C++ [over.built]p10: |
| 2817 | // For every promoted integral type T, there exist candidate |
| 2818 | // operator functions of the form |
| 2819 | // |
| 2820 | // T operator~(T); |
| 2821 | for (unsigned Int = FirstPromotedIntegralType; |
| 2822 | Int < LastPromotedIntegralType; ++Int) { |
| 2823 | QualType IntTy = ArithmeticTypes[Int]; |
| 2824 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 2825 | } |
| 2826 | break; |
| 2827 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2828 | case OO_New: |
| 2829 | case OO_Delete: |
| 2830 | case OO_Array_New: |
| 2831 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2832 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2833 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2834 | break; |
| 2835 | |
| 2836 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2837 | UnaryAmp: |
| 2838 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2839 | // C++ [over.match.oper]p3: |
| 2840 | // -- For the operator ',', the unary operator '&', or the |
| 2841 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2842 | break; |
| 2843 | |
| 2844 | case OO_Less: |
| 2845 | case OO_Greater: |
| 2846 | case OO_LessEqual: |
| 2847 | case OO_GreaterEqual: |
| 2848 | case OO_EqualEqual: |
| 2849 | case OO_ExclaimEqual: |
| 2850 | // C++ [over.built]p15: |
| 2851 | // |
| 2852 | // For every pointer or enumeration type T, there exist |
| 2853 | // candidate operator functions of the form |
| 2854 | // |
| 2855 | // bool operator<(T, T); |
| 2856 | // bool operator>(T, T); |
| 2857 | // bool operator<=(T, T); |
| 2858 | // bool operator>=(T, T); |
| 2859 | // bool operator==(T, T); |
| 2860 | // bool operator!=(T, T); |
| 2861 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2862 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2863 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 2864 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 2865 | } |
| 2866 | for (BuiltinCandidateTypeSet::iterator Enum |
| 2867 | = CandidateTypes.enumeration_begin(); |
| 2868 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 2869 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 2870 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 2871 | } |
| 2872 | |
| 2873 | // Fall through. |
| 2874 | isComparison = true; |
| 2875 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2876 | BinaryPlus: |
| 2877 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2878 | if (!isComparison) { |
| 2879 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 2880 | |
| 2881 | // C++ [over.built]p13: |
| 2882 | // |
| 2883 | // For every cv-qualified or cv-unqualified object type T |
| 2884 | // there exist candidate operator functions of the form |
| 2885 | // |
| 2886 | // T* operator+(T*, ptrdiff_t); |
| 2887 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 2888 | // T* operator-(T*, ptrdiff_t); |
| 2889 | // T* operator+(ptrdiff_t, T*); |
| 2890 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 2891 | // |
| 2892 | // C++ [over.built]p14: |
| 2893 | // |
| 2894 | // For every T, where T is a pointer to object type, there |
| 2895 | // exist candidate operator functions of the form |
| 2896 | // |
| 2897 | // ptrdiff_t operator-(T, T); |
| 2898 | for (BuiltinCandidateTypeSet::iterator Ptr |
| 2899 | = CandidateTypes.pointer_begin(); |
| 2900 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2901 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 2902 | |
| 2903 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 2904 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2905 | |
| 2906 | if (Op == OO_Plus) { |
| 2907 | // T* operator+(ptrdiff_t, T*); |
| 2908 | ParamTypes[0] = ParamTypes[1]; |
| 2909 | ParamTypes[1] = *Ptr; |
| 2910 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2911 | } else { |
| 2912 | // ptrdiff_t operator-(T, T); |
| 2913 | ParamTypes[1] = *Ptr; |
| 2914 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 2915 | Args, 2, CandidateSet); |
| 2916 | } |
| 2917 | } |
| 2918 | } |
| 2919 | // Fall through |
| 2920 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2921 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2922 | BinaryStar: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2923 | // C++ [over.built]p12: |
| 2924 | // |
| 2925 | // For every pair of promoted arithmetic types L and R, there |
| 2926 | // exist candidate operator functions of the form |
| 2927 | // |
| 2928 | // LR operator*(L, R); |
| 2929 | // LR operator/(L, R); |
| 2930 | // LR operator+(L, R); |
| 2931 | // LR operator-(L, R); |
| 2932 | // bool operator<(L, R); |
| 2933 | // bool operator>(L, R); |
| 2934 | // bool operator<=(L, R); |
| 2935 | // bool operator>=(L, R); |
| 2936 | // bool operator==(L, R); |
| 2937 | // bool operator!=(L, R); |
| 2938 | // |
| 2939 | // where LR is the result of the usual arithmetic conversions |
| 2940 | // between types L and R. |
| 2941 | for (unsigned Left = FirstPromotedArithmeticType; |
| 2942 | Left < LastPromotedArithmeticType; ++Left) { |
| 2943 | for (unsigned Right = FirstPromotedArithmeticType; |
| 2944 | Right < LastPromotedArithmeticType; ++Right) { |
| 2945 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 2946 | QualType Result |
| 2947 | = isComparison? Context.BoolTy |
| 2948 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 2949 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 2950 | } |
| 2951 | } |
| 2952 | break; |
| 2953 | |
| 2954 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2955 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2956 | case OO_Caret: |
| 2957 | case OO_Pipe: |
| 2958 | case OO_LessLess: |
| 2959 | case OO_GreaterGreater: |
| 2960 | // C++ [over.built]p17: |
| 2961 | // |
| 2962 | // For every pair of promoted integral types L and R, there |
| 2963 | // exist candidate operator functions of the form |
| 2964 | // |
| 2965 | // LR operator%(L, R); |
| 2966 | // LR operator&(L, R); |
| 2967 | // LR operator^(L, R); |
| 2968 | // LR operator|(L, R); |
| 2969 | // L operator<<(L, R); |
| 2970 | // L operator>>(L, R); |
| 2971 | // |
| 2972 | // where LR is the result of the usual arithmetic conversions |
| 2973 | // between types L and R. |
| 2974 | for (unsigned Left = FirstPromotedIntegralType; |
| 2975 | Left < LastPromotedIntegralType; ++Left) { |
| 2976 | for (unsigned Right = FirstPromotedIntegralType; |
| 2977 | Right < LastPromotedIntegralType; ++Right) { |
| 2978 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 2979 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 2980 | ? LandR[0] |
| 2981 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 2982 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 2983 | } |
| 2984 | } |
| 2985 | break; |
| 2986 | |
| 2987 | case OO_Equal: |
| 2988 | // C++ [over.built]p20: |
| 2989 | // |
| 2990 | // For every pair (T, VQ), where T is an enumeration or |
| 2991 | // (FIXME:) pointer to member type and VQ is either volatile or |
| 2992 | // empty, there exist candidate operator functions of the form |
| 2993 | // |
| 2994 | // VQ T& operator=(VQ T&, T); |
| 2995 | for (BuiltinCandidateTypeSet::iterator Enum |
| 2996 | = CandidateTypes.enumeration_begin(); |
| 2997 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 2998 | QualType ParamTypes[2]; |
| 2999 | |
| 3000 | // T& operator=(T&, T) |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3001 | ParamTypes[0] = Context.getLValueReferenceType(*Enum); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3002 | ParamTypes[1] = *Enum; |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3003 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3004 | /*IsAssignmentOperator=*/false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3006 | if (!Context.getCanonicalType(*Enum).isVolatileQualified()) { |
| 3007 | // volatile T& operator=(volatile T&, T) |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3008 | ParamTypes[0] = Context.getLValueReferenceType((*Enum).withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3009 | ParamTypes[1] = *Enum; |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3010 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3011 | /*IsAssignmentOperator=*/false); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3012 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3013 | } |
| 3014 | // Fall through. |
| 3015 | |
| 3016 | case OO_PlusEqual: |
| 3017 | case OO_MinusEqual: |
| 3018 | // C++ [over.built]p19: |
| 3019 | // |
| 3020 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3021 | // volatile or empty, there exist candidate operator functions |
| 3022 | // of the form |
| 3023 | // |
| 3024 | // T*VQ& operator=(T*VQ&, T*); |
| 3025 | // |
| 3026 | // C++ [over.built]p21: |
| 3027 | // |
| 3028 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3029 | // cv-unqualified object type and VQ is either volatile or |
| 3030 | // empty, there exist candidate operator functions of the form |
| 3031 | // |
| 3032 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3033 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3034 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3035 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3036 | QualType ParamTypes[2]; |
| 3037 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3038 | |
| 3039 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3040 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3041 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3042 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3043 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3044 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3045 | // volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3046 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3047 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3048 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3049 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3050 | } |
| 3051 | // Fall through. |
| 3052 | |
| 3053 | case OO_StarEqual: |
| 3054 | case OO_SlashEqual: |
| 3055 | // C++ [over.built]p18: |
| 3056 | // |
| 3057 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3058 | // VQ is either volatile or empty, and R is a promoted |
| 3059 | // arithmetic type, there exist candidate operator functions of |
| 3060 | // the form |
| 3061 | // |
| 3062 | // VQ L& operator=(VQ L&, R); |
| 3063 | // VQ L& operator*=(VQ L&, R); |
| 3064 | // VQ L& operator/=(VQ L&, R); |
| 3065 | // VQ L& operator+=(VQ L&, R); |
| 3066 | // VQ L& operator-=(VQ L&, R); |
| 3067 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 3068 | for (unsigned Right = FirstPromotedArithmeticType; |
| 3069 | Right < LastPromotedArithmeticType; ++Right) { |
| 3070 | QualType ParamTypes[2]; |
| 3071 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3072 | |
| 3073 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3074 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3075 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3076 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3077 | |
| 3078 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3079 | ParamTypes[0] = ArithmeticTypes[Left].withVolatile(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3080 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3081 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3082 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3083 | } |
| 3084 | } |
| 3085 | break; |
| 3086 | |
| 3087 | case OO_PercentEqual: |
| 3088 | case OO_LessLessEqual: |
| 3089 | case OO_GreaterGreaterEqual: |
| 3090 | case OO_AmpEqual: |
| 3091 | case OO_CaretEqual: |
| 3092 | case OO_PipeEqual: |
| 3093 | // C++ [over.built]p22: |
| 3094 | // |
| 3095 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3096 | // is either volatile or empty, and R is a promoted integral |
| 3097 | // type, there exist candidate operator functions of the form |
| 3098 | // |
| 3099 | // VQ L& operator%=(VQ L&, R); |
| 3100 | // VQ L& operator<<=(VQ L&, R); |
| 3101 | // VQ L& operator>>=(VQ L&, R); |
| 3102 | // VQ L& operator&=(VQ L&, R); |
| 3103 | // VQ L& operator^=(VQ L&, R); |
| 3104 | // VQ L& operator|=(VQ L&, R); |
| 3105 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 3106 | for (unsigned Right = FirstPromotedIntegralType; |
| 3107 | Right < LastPromotedIntegralType; ++Right) { |
| 3108 | QualType ParamTypes[2]; |
| 3109 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3110 | |
| 3111 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3112 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3113 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3114 | |
| 3115 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3116 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3117 | ParamTypes[0].addVolatile(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3118 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3119 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3120 | } |
| 3121 | } |
| 3122 | break; |
| 3123 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3124 | case OO_Exclaim: { |
| 3125 | // C++ [over.operator]p23: |
| 3126 | // |
| 3127 | // There also exist candidate operator functions of the form |
| 3128 | // |
| 3129 | // bool operator!(bool); |
| 3130 | // bool operator&&(bool, bool); [BELOW] |
| 3131 | // bool operator||(bool, bool); [BELOW] |
| 3132 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3133 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3134 | /*IsAssignmentOperator=*/false, |
| 3135 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3136 | break; |
| 3137 | } |
| 3138 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3139 | case OO_AmpAmp: |
| 3140 | case OO_PipePipe: { |
| 3141 | // C++ [over.operator]p23: |
| 3142 | // |
| 3143 | // There also exist candidate operator functions of the form |
| 3144 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3145 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3146 | // bool operator&&(bool, bool); |
| 3147 | // bool operator||(bool, bool); |
| 3148 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3149 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3150 | /*IsAssignmentOperator=*/false, |
| 3151 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3152 | break; |
| 3153 | } |
| 3154 | |
| 3155 | case OO_Subscript: |
| 3156 | // C++ [over.built]p13: |
| 3157 | // |
| 3158 | // For every cv-qualified or cv-unqualified object type T there |
| 3159 | // exist candidate operator functions of the form |
| 3160 | // |
| 3161 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3162 | // T& operator[](T*, ptrdiff_t); |
| 3163 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3164 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3165 | // T& operator[](ptrdiff_t, T*); |
| 3166 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3167 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3168 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3169 | QualType PointeeType = (*Ptr)->getAsPointerType()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3170 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3171 | |
| 3172 | // T& operator[](T*, ptrdiff_t) |
| 3173 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3174 | |
| 3175 | // T& operator[](ptrdiff_t, T*); |
| 3176 | ParamTypes[0] = ParamTypes[1]; |
| 3177 | ParamTypes[1] = *Ptr; |
| 3178 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3179 | } |
| 3180 | break; |
| 3181 | |
| 3182 | case OO_ArrowStar: |
| 3183 | // FIXME: No support for pointer-to-members yet. |
| 3184 | break; |
| 3185 | } |
| 3186 | } |
| 3187 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3188 | /// \brief Add function candidates found via argument-dependent lookup |
| 3189 | /// to the set of overloading candidates. |
| 3190 | /// |
| 3191 | /// This routine performs argument-dependent name lookup based on the |
| 3192 | /// given function name (which may also be an operator name) and adds |
| 3193 | /// all of the overload candidates found by ADL to the overload |
| 3194 | /// candidate set (C++ [basic.lookup.argdep]). |
| 3195 | void |
| 3196 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3197 | Expr **Args, unsigned NumArgs, |
| 3198 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3199 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3201 | // Record all of the function candidates that we've already |
| 3202 | // added to the overload set, so that we don't add those same |
| 3203 | // candidates a second time. |
| 3204 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3205 | CandEnd = CandidateSet.end(); |
| 3206 | Cand != CandEnd; ++Cand) |
| 3207 | if (Cand->Function) |
| 3208 | Functions.insert(Cand->Function); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3209 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3210 | ArgumentDependentLookup(Name, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3211 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3212 | // Erase all of the candidates we already knew about. |
| 3213 | // FIXME: This is suboptimal. Is there a better way? |
| 3214 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3215 | CandEnd = CandidateSet.end(); |
| 3216 | Cand != CandEnd; ++Cand) |
| 3217 | if (Cand->Function) |
| 3218 | Functions.erase(Cand->Function); |
| 3219 | |
| 3220 | // For each of the ADL candidates we found, add it to the overload |
| 3221 | // set. |
| 3222 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3223 | FuncEnd = Functions.end(); |
| 3224 | Func != FuncEnd; ++Func) |
| 3225 | AddOverloadCandidate(*Func, Args, NumArgs, CandidateSet); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3228 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3229 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
| 3230 | bool |
| 3231 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
| 3232 | const OverloadCandidate& Cand2) |
| 3233 | { |
| 3234 | // Define viable functions to be better candidates than non-viable |
| 3235 | // functions. |
| 3236 | if (!Cand2.Viable) |
| 3237 | return Cand1.Viable; |
| 3238 | else if (!Cand1.Viable) |
| 3239 | return false; |
| 3240 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3241 | // C++ [over.match.best]p1: |
| 3242 | // |
| 3243 | // -- if F is a static member function, ICS1(F) is defined such |
| 3244 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3245 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3246 | // better nor worse than ICS1(F). |
| 3247 | unsigned StartArg = 0; |
| 3248 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3249 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3250 | |
| 3251 | // (C++ 13.3.3p1): a viable function F1 is defined to be a better |
| 3252 | // function than another viable function F2 if for all arguments i, |
| 3253 | // ICSi(F1) is not a worse conversion sequence than ICSi(F2), and |
| 3254 | // then... |
| 3255 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3256 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3257 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3258 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3259 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3260 | Cand2.Conversions[ArgIdx])) { |
| 3261 | case ImplicitConversionSequence::Better: |
| 3262 | // Cand1 has a better conversion sequence. |
| 3263 | HasBetterConversion = true; |
| 3264 | break; |
| 3265 | |
| 3266 | case ImplicitConversionSequence::Worse: |
| 3267 | // Cand1 can't be better than Cand2. |
| 3268 | return false; |
| 3269 | |
| 3270 | case ImplicitConversionSequence::Indistinguishable: |
| 3271 | // Do nothing. |
| 3272 | break; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | if (HasBetterConversion) |
| 3277 | return true; |
| 3278 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3279 | // FIXME: Several other bullets in (C++ 13.3.3p1) need to be |
| 3280 | // implemented, but they require template support. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3281 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3282 | // C++ [over.match.best]p1b4: |
| 3283 | // |
| 3284 | // -- the context is an initialization by user-defined conversion |
| 3285 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 3286 | // from the return type of F1 to the destination type (i.e., |
| 3287 | // the type of the entity being initialized) is a better |
| 3288 | // conversion sequence than the standard conversion sequence |
| 3289 | // from the return type of F2 to the destination type. |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3290 | if (Cand1.Function && Cand2.Function && |
| 3291 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3292 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 3293 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 3294 | Cand2.FinalConversion)) { |
| 3295 | case ImplicitConversionSequence::Better: |
| 3296 | // Cand1 has a better conversion sequence. |
| 3297 | return true; |
| 3298 | |
| 3299 | case ImplicitConversionSequence::Worse: |
| 3300 | // Cand1 can't be better than Cand2. |
| 3301 | return false; |
| 3302 | |
| 3303 | case ImplicitConversionSequence::Indistinguishable: |
| 3304 | // Do nothing |
| 3305 | break; |
| 3306 | } |
| 3307 | } |
| 3308 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3309 | return false; |
| 3310 | } |
| 3311 | |
| 3312 | /// BestViableFunction - Computes the best viable function (C++ 13.3.3) |
| 3313 | /// within an overload candidate set. If overloading is successful, |
| 3314 | /// the result will be OR_Success and Best will be set to point to the |
| 3315 | /// best viable function within the candidate set. Otherwise, one of |
| 3316 | /// several kinds of errors will be returned; see |
| 3317 | /// Sema::OverloadingResult. |
| 3318 | Sema::OverloadingResult |
| 3319 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 3320 | OverloadCandidateSet::iterator& Best) |
| 3321 | { |
| 3322 | // Find the best viable function. |
| 3323 | Best = CandidateSet.end(); |
| 3324 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3325 | Cand != CandidateSet.end(); ++Cand) { |
| 3326 | if (Cand->Viable) { |
| 3327 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 3328 | Best = Cand; |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | // If we didn't find any viable functions, abort. |
| 3333 | if (Best == CandidateSet.end()) |
| 3334 | return OR_No_Viable_Function; |
| 3335 | |
| 3336 | // Make sure that this function is better than every other viable |
| 3337 | // function. If not, we have an ambiguity. |
| 3338 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3339 | Cand != CandidateSet.end(); ++Cand) { |
| 3340 | if (Cand->Viable && |
| 3341 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3342 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 3343 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3344 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3345 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3349 | if (Best->Function && |
| 3350 | (Best->Function->isDeleted() || |
| 3351 | Best->Function->getAttr<UnavailableAttr>())) |
| 3352 | return OR_Deleted; |
| 3353 | |
| 3354 | // If Best refers to a function that is either deleted (C++0x) or |
| 3355 | // unavailable (Clang extension) report an error. |
| 3356 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3357 | return OR_Success; |
| 3358 | } |
| 3359 | |
| 3360 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 3361 | /// diagnostic messages containing the candidates in the candidate |
| 3362 | /// set. If OnlyViable is true, only viable candidates will be printed. |
| 3363 | void |
| 3364 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
| 3365 | bool OnlyViable) |
| 3366 | { |
| 3367 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3368 | LastCand = CandidateSet.end(); |
| 3369 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3370 | if (Cand->Viable || !OnlyViable) { |
| 3371 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3372 | if (Cand->Function->isDeleted() || |
| 3373 | Cand->Function->getAttr<UnavailableAttr>()) { |
| 3374 | // Deleted or "unavailable" function. |
| 3375 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 3376 | << Cand->Function->isDeleted(); |
| 3377 | } else { |
| 3378 | // Normal function |
| 3379 | // FIXME: Give a better reason! |
| 3380 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
| 3381 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3382 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3383 | // Desugar the type of the surrogate down to a function type, |
| 3384 | // retaining as many typedefs as possible while still showing |
| 3385 | // the function type (and, therefore, its parameter types). |
| 3386 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3387 | bool isLValueReference = false; |
| 3388 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3389 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3390 | if (const LValueReferenceType *FnTypeRef = |
| 3391 | FnType->getAsLValueReferenceType()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3392 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3393 | isLValueReference = true; |
| 3394 | } else if (const RValueReferenceType *FnTypeRef = |
| 3395 | FnType->getAsRValueReferenceType()) { |
| 3396 | FnType = FnTypeRef->getPointeeType(); |
| 3397 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3398 | } |
| 3399 | if (const PointerType *FnTypePtr = FnType->getAsPointerType()) { |
| 3400 | FnType = FnTypePtr->getPointeeType(); |
| 3401 | isPointer = true; |
| 3402 | } |
| 3403 | // Desugar down to a function type. |
| 3404 | FnType = QualType(FnType->getAsFunctionType(), 0); |
| 3405 | // Reconstruct the pointer/reference as appropriate. |
| 3406 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3407 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 3408 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3409 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3410 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3411 | << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3412 | } else { |
| 3413 | // FIXME: We need to get the identifier in here |
| 3414 | // FIXME: Do we want the error message to point at the |
| 3415 | // operator? (built-ins won't have a location) |
| 3416 | QualType FnType |
| 3417 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 3418 | Cand->BuiltinTypes.ParamTypes, |
| 3419 | Cand->Conversions.size(), |
| 3420 | false, 0); |
| 3421 | |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3422 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3423 | } |
| 3424 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3425 | } |
| 3426 | } |
| 3427 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3428 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 3429 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 3430 | /// expression with overloaded function type and @p ToType is the type |
| 3431 | /// we're trying to resolve to. For example: |
| 3432 | /// |
| 3433 | /// @code |
| 3434 | /// int f(double); |
| 3435 | /// int f(int); |
| 3436 | /// |
| 3437 | /// int (*pfd)(double) = f; // selects f(double) |
| 3438 | /// @endcode |
| 3439 | /// |
| 3440 | /// This routine returns the resulting FunctionDecl if it could be |
| 3441 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 3442 | /// routine will emit diagnostics if there is an error. |
| 3443 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3444 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3445 | bool Complain) { |
| 3446 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3447 | bool IsMember = false; |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 3448 | if (const PointerType *ToTypePtr = ToType->getAsPointerType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3449 | FunctionType = ToTypePtr->getPointeeType(); |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 3450 | else if (const ReferenceType *ToTypeRef = ToType->getAsReferenceType()) |
| 3451 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3452 | else if (const MemberPointerType *MemTypePtr = |
| 3453 | ToType->getAsMemberPointerType()) { |
| 3454 | FunctionType = MemTypePtr->getPointeeType(); |
| 3455 | IsMember = true; |
| 3456 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3457 | |
| 3458 | // We only look at pointers or references to functions. |
| 3459 | if (!FunctionType->isFunctionType()) |
| 3460 | return 0; |
| 3461 | |
| 3462 | // Find the actual overloaded function declaration. |
| 3463 | OverloadedFunctionDecl *Ovl = 0; |
| 3464 | |
| 3465 | // C++ [over.over]p1: |
| 3466 | // [...] [Note: any redundant set of parentheses surrounding the |
| 3467 | // overloaded function name is ignored (5.1). ] |
| 3468 | Expr *OvlExpr = From->IgnoreParens(); |
| 3469 | |
| 3470 | // C++ [over.over]p1: |
| 3471 | // [...] The overloaded function name can be preceded by the & |
| 3472 | // operator. |
| 3473 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 3474 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 3475 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 3476 | } |
| 3477 | |
| 3478 | // Try to dig out the overloaded function. |
| 3479 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) |
| 3480 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
| 3481 | |
| 3482 | // If there's no overloaded function declaration, we're done. |
| 3483 | if (!Ovl) |
| 3484 | return 0; |
| 3485 | |
| 3486 | // Look through all of the overloaded functions, searching for one |
| 3487 | // whose type matches exactly. |
| 3488 | // FIXME: When templates or using declarations come along, we'll actually |
| 3489 | // have to deal with duplicates, partial ordering, etc. For now, we |
| 3490 | // can just do a simple search. |
| 3491 | FunctionType = Context.getCanonicalType(FunctionType.getUnqualifiedType()); |
| 3492 | for (OverloadedFunctionDecl::function_iterator Fun = Ovl->function_begin(); |
| 3493 | Fun != Ovl->function_end(); ++Fun) { |
| 3494 | // C++ [over.over]p3: |
| 3495 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 3496 | // targets of type "pointer-to-function" or "reference-to-function." |
| 3497 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3498 | // type "pointer-to-member-function." |
| 3499 | // Note that according to DR 247, the containing class does not matter. |
| 3500 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 3501 | // Skip non-static functions when converting to pointer, and static |
| 3502 | // when converting to member pointer. |
| 3503 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3504 | continue; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3505 | } else if (IsMember) |
| 3506 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3507 | |
| 3508 | if (FunctionType == Context.getCanonicalType((*Fun)->getType())) |
| 3509 | return *Fun; |
| 3510 | } |
| 3511 | |
| 3512 | return 0; |
| 3513 | } |
| 3514 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3515 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3516 | /// (which eventually refers to the declaration Func) and the call |
| 3517 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 3518 | /// to a specific function. If overload resolution succeeds, returns |
| 3519 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3520 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3521 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3522 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3523 | DeclarationName UnqualifiedName, |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3524 | SourceLocation LParenLoc, |
| 3525 | Expr **Args, unsigned NumArgs, |
| 3526 | SourceLocation *CommaLocs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3527 | SourceLocation RParenLoc, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3528 | bool &ArgumentDependentLookup) { |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3529 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3530 | |
| 3531 | // Add the functions denoted by Callee to the set of candidate |
| 3532 | // functions. While we're doing so, track whether argument-dependent |
| 3533 | // lookup still applies, per: |
| 3534 | // |
| 3535 | // C++0x [basic.lookup.argdep]p3: |
| 3536 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 3537 | // and let Y be the lookup set produced by argument dependent |
| 3538 | // lookup (defined as follows). If X contains |
| 3539 | // |
| 3540 | // -- a declaration of a class member, or |
| 3541 | // |
| 3542 | // -- a block-scope function declaration that is not a |
| 3543 | // using-declaration, or |
| 3544 | // |
| 3545 | // -- a declaration that is neither a function or a function |
| 3546 | // template |
| 3547 | // |
| 3548 | // then Y is empty. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3549 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3550 | = dyn_cast_or_null<OverloadedFunctionDecl>(Callee)) { |
| 3551 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 3552 | FuncEnd = Ovl->function_end(); |
| 3553 | Func != FuncEnd; ++Func) { |
| 3554 | AddOverloadCandidate(*Func, Args, NumArgs, CandidateSet); |
| 3555 | |
| 3556 | if ((*Func)->getDeclContext()->isRecord() || |
| 3557 | (*Func)->getDeclContext()->isFunctionOrMethod()) |
| 3558 | ArgumentDependentLookup = false; |
| 3559 | } |
| 3560 | } else if (FunctionDecl *Func = dyn_cast_or_null<FunctionDecl>(Callee)) { |
| 3561 | AddOverloadCandidate(Func, Args, NumArgs, CandidateSet); |
| 3562 | |
| 3563 | if (Func->getDeclContext()->isRecord() || |
| 3564 | Func->getDeclContext()->isFunctionOrMethod()) |
| 3565 | ArgumentDependentLookup = false; |
| 3566 | } |
| 3567 | |
| 3568 | if (Callee) |
| 3569 | UnqualifiedName = Callee->getDeclName(); |
| 3570 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3571 | if (ArgumentDependentLookup) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3572 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3573 | CandidateSet); |
| 3574 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3575 | OverloadCandidateSet::iterator Best; |
| 3576 | switch (BestViableFunction(CandidateSet, Best)) { |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3577 | case OR_Success: |
| 3578 | return Best->Function; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3579 | |
| 3580 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3581 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3582 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3583 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3584 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 3585 | break; |
| 3586 | |
| 3587 | case OR_Ambiguous: |
| 3588 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3589 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3590 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3591 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3592 | |
| 3593 | case OR_Deleted: |
| 3594 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 3595 | << Best->Function->isDeleted() |
| 3596 | << UnqualifiedName |
| 3597 | << Fn->getSourceRange(); |
| 3598 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3599 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | // Overload resolution failed. Destroy all of the subexpressions and |
| 3603 | // return NULL. |
| 3604 | Fn->Destroy(Context); |
| 3605 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 3606 | Args[Arg]->Destroy(Context); |
| 3607 | return 0; |
| 3608 | } |
| 3609 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 3610 | /// \brief Create a unary operation that may resolve to an overloaded |
| 3611 | /// operator. |
| 3612 | /// |
| 3613 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 3614 | /// |
| 3615 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 3616 | /// operator. |
| 3617 | /// |
| 3618 | /// \param Functions The set of non-member functions that will be |
| 3619 | /// considered by overload resolution. The caller needs to build this |
| 3620 | /// set based on the context using, e.g., |
| 3621 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 3622 | /// set should not contain any member functions; those will be added |
| 3623 | /// by CreateOverloadedUnaryOp(). |
| 3624 | /// |
| 3625 | /// \param input The input argument. |
| 3626 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 3627 | unsigned OpcIn, |
| 3628 | FunctionSet &Functions, |
| 3629 | ExprArg input) { |
| 3630 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 3631 | Expr *Input = (Expr *)input.get(); |
| 3632 | |
| 3633 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 3634 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 3635 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3636 | |
| 3637 | Expr *Args[2] = { Input, 0 }; |
| 3638 | unsigned NumArgs = 1; |
| 3639 | |
| 3640 | // For post-increment and post-decrement, add the implicit '0' as |
| 3641 | // the second argument, so that we know this is a post-increment or |
| 3642 | // post-decrement. |
| 3643 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 3644 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
| 3645 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
| 3646 | SourceLocation()); |
| 3647 | NumArgs = 2; |
| 3648 | } |
| 3649 | |
| 3650 | if (Input->isTypeDependent()) { |
| 3651 | OverloadedFunctionDecl *Overloads |
| 3652 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
| 3653 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3654 | FuncEnd = Functions.end(); |
| 3655 | Func != FuncEnd; ++Func) |
| 3656 | Overloads->addOverload(*Func); |
| 3657 | |
| 3658 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 3659 | OpLoc, false, false); |
| 3660 | |
| 3661 | input.release(); |
| 3662 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 3663 | &Args[0], NumArgs, |
| 3664 | Context.DependentTy, |
| 3665 | OpLoc)); |
| 3666 | } |
| 3667 | |
| 3668 | // Build an empty overload set. |
| 3669 | OverloadCandidateSet CandidateSet; |
| 3670 | |
| 3671 | // Add the candidates from the given function set. |
| 3672 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 3673 | |
| 3674 | // Add operator candidates that are member functions. |
| 3675 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 3676 | |
| 3677 | // Add builtin operator candidates. |
| 3678 | AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet); |
| 3679 | |
| 3680 | // Perform overload resolution. |
| 3681 | OverloadCandidateSet::iterator Best; |
| 3682 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3683 | case OR_Success: { |
| 3684 | // We found a built-in operator or an overloaded operator. |
| 3685 | FunctionDecl *FnDecl = Best->Function; |
| 3686 | |
| 3687 | if (FnDecl) { |
| 3688 | // We matched an overloaded operator. Build a call to that |
| 3689 | // operator. |
| 3690 | |
| 3691 | // Convert the arguments. |
| 3692 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3693 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 3694 | return ExprError(); |
| 3695 | } else { |
| 3696 | // Convert the arguments. |
| 3697 | if (PerformCopyInitialization(Input, |
| 3698 | FnDecl->getParamDecl(0)->getType(), |
| 3699 | "passing")) |
| 3700 | return ExprError(); |
| 3701 | } |
| 3702 | |
| 3703 | // Determine the result type |
| 3704 | QualType ResultTy |
| 3705 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3706 | ResultTy = ResultTy.getNonReferenceType(); |
| 3707 | |
| 3708 | // Build the actual expression node. |
| 3709 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3710 | SourceLocation()); |
| 3711 | UsualUnaryConversions(FnExpr); |
| 3712 | |
| 3713 | input.release(); |
| 3714 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 3715 | &Input, 1, ResultTy, |
| 3716 | OpLoc)); |
| 3717 | } else { |
| 3718 | // We matched a built-in operator. Convert the arguments, then |
| 3719 | // break out so that we will build the appropriate built-in |
| 3720 | // operator node. |
| 3721 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 3722 | Best->Conversions[0], "passing")) |
| 3723 | return ExprError(); |
| 3724 | |
| 3725 | break; |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | case OR_No_Viable_Function: |
| 3730 | // No viable function; fall through to handling this as a |
| 3731 | // built-in operator, which will produce an error message for us. |
| 3732 | break; |
| 3733 | |
| 3734 | case OR_Ambiguous: |
| 3735 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 3736 | << UnaryOperator::getOpcodeStr(Opc) |
| 3737 | << Input->getSourceRange(); |
| 3738 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3739 | return ExprError(); |
| 3740 | |
| 3741 | case OR_Deleted: |
| 3742 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 3743 | << Best->Function->isDeleted() |
| 3744 | << UnaryOperator::getOpcodeStr(Opc) |
| 3745 | << Input->getSourceRange(); |
| 3746 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3747 | return ExprError(); |
| 3748 | } |
| 3749 | |
| 3750 | // Either we found no viable overloaded operator or we matched a |
| 3751 | // built-in operator. In either case, fall through to trying to |
| 3752 | // build a built-in operation. |
| 3753 | input.release(); |
| 3754 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 3755 | } |
| 3756 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3757 | /// \brief Create a binary operation that may resolve to an overloaded |
| 3758 | /// operator. |
| 3759 | /// |
| 3760 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 3761 | /// |
| 3762 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 3763 | /// operator. |
| 3764 | /// |
| 3765 | /// \param Functions The set of non-member functions that will be |
| 3766 | /// considered by overload resolution. The caller needs to build this |
| 3767 | /// set based on the context using, e.g., |
| 3768 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 3769 | /// set should not contain any member functions; those will be added |
| 3770 | /// by CreateOverloadedBinOp(). |
| 3771 | /// |
| 3772 | /// \param LHS Left-hand argument. |
| 3773 | /// \param RHS Right-hand argument. |
| 3774 | Sema::OwningExprResult |
| 3775 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
| 3776 | unsigned OpcIn, |
| 3777 | FunctionSet &Functions, |
| 3778 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3779 | Expr *Args[2] = { LHS, RHS }; |
| 3780 | |
| 3781 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 3782 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 3783 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3784 | |
| 3785 | // If either side is type-dependent, create an appropriate dependent |
| 3786 | // expression. |
| 3787 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) { |
| 3788 | // .* cannot be overloaded. |
| 3789 | if (Opc == BinaryOperator::PtrMemD) |
| 3790 | return Owned(new (Context) BinaryOperator(LHS, RHS, Opc, |
| 3791 | Context.DependentTy, OpLoc)); |
| 3792 | |
| 3793 | OverloadedFunctionDecl *Overloads |
| 3794 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
| 3795 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3796 | FuncEnd = Functions.end(); |
| 3797 | Func != FuncEnd; ++Func) |
| 3798 | Overloads->addOverload(*Func); |
| 3799 | |
| 3800 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 3801 | OpLoc, false, false); |
| 3802 | |
| 3803 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 3804 | Args, 2, |
| 3805 | Context.DependentTy, |
| 3806 | OpLoc)); |
| 3807 | } |
| 3808 | |
| 3809 | // If this is the .* operator, which is not overloadable, just |
| 3810 | // create a built-in binary operator. |
| 3811 | if (Opc == BinaryOperator::PtrMemD) |
| 3812 | return CreateBuiltinBinOp(OpLoc, Opc, LHS, RHS); |
| 3813 | |
| 3814 | // If this is one of the assignment operators, we only perform |
| 3815 | // overload resolution if the left-hand side is a class or |
| 3816 | // enumeration type (C++ [expr.ass]p3). |
| 3817 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
| 3818 | !LHS->getType()->isOverloadableType()) |
| 3819 | return CreateBuiltinBinOp(OpLoc, Opc, LHS, RHS); |
| 3820 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 3821 | // Build an empty overload set. |
| 3822 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3823 | |
| 3824 | // Add the candidates from the given function set. |
| 3825 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 3826 | |
| 3827 | // Add operator candidates that are member functions. |
| 3828 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 3829 | |
| 3830 | // Add builtin operator candidates. |
| 3831 | AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet); |
| 3832 | |
| 3833 | // Perform overload resolution. |
| 3834 | OverloadCandidateSet::iterator Best; |
| 3835 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3836 | case OR_Success: { |
| 3837 | // We found a built-in operator or an overloaded operator. |
| 3838 | FunctionDecl *FnDecl = Best->Function; |
| 3839 | |
| 3840 | if (FnDecl) { |
| 3841 | // We matched an overloaded operator. Build a call to that |
| 3842 | // operator. |
| 3843 | |
| 3844 | // Convert the arguments. |
| 3845 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3846 | if (PerformObjectArgumentInitialization(LHS, Method) || |
| 3847 | PerformCopyInitialization(RHS, FnDecl->getParamDecl(0)->getType(), |
| 3848 | "passing")) |
| 3849 | return ExprError(); |
| 3850 | } else { |
| 3851 | // Convert the arguments. |
| 3852 | if (PerformCopyInitialization(LHS, FnDecl->getParamDecl(0)->getType(), |
| 3853 | "passing") || |
| 3854 | PerformCopyInitialization(RHS, FnDecl->getParamDecl(1)->getType(), |
| 3855 | "passing")) |
| 3856 | return ExprError(); |
| 3857 | } |
| 3858 | |
| 3859 | // Determine the result type |
| 3860 | QualType ResultTy |
| 3861 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3862 | ResultTy = ResultTy.getNonReferenceType(); |
| 3863 | |
| 3864 | // Build the actual expression node. |
| 3865 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3866 | SourceLocation()); |
| 3867 | UsualUnaryConversions(FnExpr); |
| 3868 | |
| 3869 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 3870 | Args, 2, ResultTy, |
| 3871 | OpLoc)); |
| 3872 | } else { |
| 3873 | // We matched a built-in operator. Convert the arguments, then |
| 3874 | // break out so that we will build the appropriate built-in |
| 3875 | // operator node. |
| 3876 | if (PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], |
| 3877 | Best->Conversions[0], "passing") || |
| 3878 | PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], |
| 3879 | Best->Conversions[1], "passing")) |
| 3880 | return ExprError(); |
| 3881 | |
| 3882 | break; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | case OR_No_Viable_Function: |
| 3887 | // No viable function; fall through to handling this as a |
| 3888 | // built-in operator, which will produce an error message for us. |
| 3889 | break; |
| 3890 | |
| 3891 | case OR_Ambiguous: |
| 3892 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 3893 | << BinaryOperator::getOpcodeStr(Opc) |
| 3894 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3895 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3896 | return ExprError(); |
| 3897 | |
| 3898 | case OR_Deleted: |
| 3899 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 3900 | << Best->Function->isDeleted() |
| 3901 | << BinaryOperator::getOpcodeStr(Opc) |
| 3902 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3903 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3904 | return ExprError(); |
| 3905 | } |
| 3906 | |
| 3907 | // Either we found no viable overloaded operator or we matched a |
| 3908 | // built-in operator. In either case, try to build a built-in |
| 3909 | // operation. |
| 3910 | return CreateBuiltinBinOp(OpLoc, Opc, LHS, RHS); |
| 3911 | } |
| 3912 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3913 | /// BuildCallToMemberFunction - Build a call to a member |
| 3914 | /// function. MemExpr is the expression that refers to the member |
| 3915 | /// function (and includes the object parameter), Args/NumArgs are the |
| 3916 | /// arguments to the function call (not including the object |
| 3917 | /// parameter). The caller needs to validate that the member |
| 3918 | /// expression refers to a member function or an overloaded member |
| 3919 | /// function. |
| 3920 | Sema::ExprResult |
| 3921 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 3922 | SourceLocation LParenLoc, Expr **Args, |
| 3923 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 3924 | SourceLocation RParenLoc) { |
| 3925 | // Dig out the member expression. This holds both the object |
| 3926 | // argument and the member function we're referring to. |
| 3927 | MemberExpr *MemExpr = 0; |
| 3928 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 3929 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 3930 | else |
| 3931 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 3932 | assert(MemExpr && "Building member call without member expression"); |
| 3933 | |
| 3934 | // Extract the object argument. |
| 3935 | Expr *ObjectArg = MemExpr->getBase(); |
| 3936 | if (MemExpr->isArrow()) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3937 | ObjectArg = new (Context) UnaryOperator(ObjectArg, UnaryOperator::Deref, |
| 3938 | ObjectArg->getType()->getAsPointerType()->getPointeeType(), |
Douglas Gregor | 611a8c4 | 2009-02-19 00:52:42 +0000 | [diff] [blame] | 3939 | ObjectArg->getLocStart()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3940 | CXXMethodDecl *Method = 0; |
| 3941 | if (OverloadedFunctionDecl *Ovl |
| 3942 | = dyn_cast<OverloadedFunctionDecl>(MemExpr->getMemberDecl())) { |
| 3943 | // Add overload candidates |
| 3944 | OverloadCandidateSet CandidateSet; |
| 3945 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 3946 | FuncEnd = Ovl->function_end(); |
| 3947 | Func != FuncEnd; ++Func) { |
| 3948 | assert(isa<CXXMethodDecl>(*Func) && "Function is not a method"); |
| 3949 | Method = cast<CXXMethodDecl>(*Func); |
| 3950 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
| 3951 | /*SuppressUserConversions=*/false); |
| 3952 | } |
| 3953 | |
| 3954 | OverloadCandidateSet::iterator Best; |
| 3955 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3956 | case OR_Success: |
| 3957 | Method = cast<CXXMethodDecl>(Best->Function); |
| 3958 | break; |
| 3959 | |
| 3960 | case OR_No_Viable_Function: |
| 3961 | Diag(MemExpr->getSourceRange().getBegin(), |
| 3962 | diag::err_ovl_no_viable_member_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3963 | << Ovl->getDeclName() << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3964 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 3965 | // FIXME: Leaking incoming expressions! |
| 3966 | return true; |
| 3967 | |
| 3968 | case OR_Ambiguous: |
| 3969 | Diag(MemExpr->getSourceRange().getBegin(), |
| 3970 | diag::err_ovl_ambiguous_member_call) |
| 3971 | << Ovl->getDeclName() << MemExprE->getSourceRange(); |
| 3972 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 3973 | // FIXME: Leaking incoming expressions! |
| 3974 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3975 | |
| 3976 | case OR_Deleted: |
| 3977 | Diag(MemExpr->getSourceRange().getBegin(), |
| 3978 | diag::err_ovl_deleted_member_call) |
| 3979 | << Best->Function->isDeleted() |
| 3980 | << Ovl->getDeclName() << MemExprE->getSourceRange(); |
| 3981 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 3982 | // FIXME: Leaking incoming expressions! |
| 3983 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | FixOverloadedFunctionReference(MemExpr, Method); |
| 3987 | } else { |
| 3988 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 3989 | } |
| 3990 | |
| 3991 | assert(Method && "Member call to something that isn't a method?"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3992 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3993 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
| 3994 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3995 | Method->getResultType().getNonReferenceType(), |
| 3996 | RParenLoc)); |
| 3997 | |
| 3998 | // Convert the object argument (for a non-static member function call). |
| 3999 | if (!Method->isStatic() && |
| 4000 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 4001 | return true; |
| 4002 | MemExpr->setBase(ObjectArg); |
| 4003 | |
| 4004 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4005 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4006 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
| 4007 | RParenLoc)) |
| 4008 | return true; |
| 4009 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4010 | return CheckFunctionCall(Method, TheCall.take()).release(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4013 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 4014 | /// type (C++ [over.call.object]), which can end up invoking an |
| 4015 | /// overloaded function call operator (@c operator()) or performing a |
| 4016 | /// user-defined conversion on the object argument. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4017 | Sema::ExprResult |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 4018 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
| 4019 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4020 | Expr **Args, unsigned NumArgs, |
| 4021 | SourceLocation *CommaLocs, |
| 4022 | SourceLocation RParenLoc) { |
| 4023 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
| 4024 | const RecordType *Record = Object->getType()->getAsRecordType(); |
| 4025 | |
| 4026 | // C++ [over.call.object]p1: |
| 4027 | // If the primary-expression E in the function call syntax |
| 4028 | // evaluates to a class object of type “cv T”, then the set of |
| 4029 | // candidate functions includes at least the function call |
| 4030 | // operators of T. The function call operators of T are obtained by |
| 4031 | // ordinary lookup of the name operator() in the context of |
| 4032 | // (E).operator(). |
| 4033 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4034 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4035 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 4036 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4037 | Oper != OperEnd; ++Oper) |
| 4038 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
| 4039 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4040 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4041 | // C++ [over.call.object]p2: |
| 4042 | // In addition, for each conversion function declared in T of the |
| 4043 | // form |
| 4044 | // |
| 4045 | // operator conversion-type-id () cv-qualifier; |
| 4046 | // |
| 4047 | // where cv-qualifier is the same cv-qualification as, or a |
| 4048 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 4049 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 4050 | // R", or the type "reference to pointer to function of |
| 4051 | // (P1,...,Pn) returning R", or the type "reference to function |
| 4052 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4053 | // is also considered as a candidate function. Similarly, |
| 4054 | // surrogate call functions are added to the set of candidate |
| 4055 | // functions for each conversion function declared in an |
| 4056 | // accessible base class provided the function is not hidden |
| 4057 | // within T by another intervening declaration. |
| 4058 | // |
| 4059 | // FIXME: Look in base classes for more conversion operators! |
| 4060 | OverloadedFunctionDecl *Conversions |
| 4061 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4062 | for (OverloadedFunctionDecl::function_iterator |
| 4063 | Func = Conversions->function_begin(), |
| 4064 | FuncEnd = Conversions->function_end(); |
| 4065 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4066 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 4067 | |
| 4068 | // Strip the reference type (if any) and then the pointer type (if |
| 4069 | // any) to get down to what might be a function type. |
| 4070 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 4071 | if (const PointerType *ConvPtrType = ConvType->getAsPointerType()) |
| 4072 | ConvType = ConvPtrType->getPointeeType(); |
| 4073 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4074 | if (const FunctionProtoType *Proto = ConvType->getAsFunctionProtoType()) |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4075 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 4076 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4077 | |
| 4078 | // Perform overload resolution. |
| 4079 | OverloadCandidateSet::iterator Best; |
| 4080 | switch (BestViableFunction(CandidateSet, Best)) { |
| 4081 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4082 | // Overload resolution succeeded; we'll build the appropriate call |
| 4083 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4084 | break; |
| 4085 | |
| 4086 | case OR_No_Viable_Function: |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4087 | Diag(Object->getSourceRange().getBegin(), |
| 4088 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4089 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4090 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4091 | break; |
| 4092 | |
| 4093 | case OR_Ambiguous: |
| 4094 | Diag(Object->getSourceRange().getBegin(), |
| 4095 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4096 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4097 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4098 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4099 | |
| 4100 | case OR_Deleted: |
| 4101 | Diag(Object->getSourceRange().getBegin(), |
| 4102 | diag::err_ovl_deleted_object_call) |
| 4103 | << Best->Function->isDeleted() |
| 4104 | << Object->getType() << Object->getSourceRange(); |
| 4105 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4106 | break; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4109 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4110 | // We had an error; delete all of the subexpressions and return |
| 4111 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4112 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4113 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4114 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4115 | return true; |
| 4116 | } |
| 4117 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4118 | if (Best->Function == 0) { |
| 4119 | // Since there is no function declaration, this is one of the |
| 4120 | // surrogate candidates. Dig out the conversion function. |
| 4121 | CXXConversionDecl *Conv |
| 4122 | = cast<CXXConversionDecl>( |
| 4123 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 4124 | |
| 4125 | // We selected one of the surrogate functions that converts the |
| 4126 | // object parameter to a function pointer. Perform the conversion |
| 4127 | // on the object argument, then let ActOnCallExpr finish the job. |
| 4128 | // FIXME: Represent the user-defined conversion in the AST! |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4129 | ImpCastExprToType(Object, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4130 | Conv->getConversionType().getNonReferenceType(), |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4131 | Conv->getConversionType()->isLValueReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4132 | return ActOnCallExpr(S, ExprArg(*this, Object), LParenLoc, |
| 4133 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 4134 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 4138 | // that calls this method, using Object for the implicit object |
| 4139 | // parameter and passing along the remaining arguments. |
| 4140 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4141 | const FunctionProtoType *Proto = Method->getType()->getAsFunctionProtoType(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4142 | |
| 4143 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4144 | unsigned NumArgsToCheck = NumArgs; |
| 4145 | |
| 4146 | // Build the full argument list for the method call (the |
| 4147 | // implicit object parameter is placed at the beginning of the |
| 4148 | // list). |
| 4149 | Expr **MethodArgs; |
| 4150 | if (NumArgs < NumArgsInProto) { |
| 4151 | NumArgsToCheck = NumArgsInProto; |
| 4152 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 4153 | } else { |
| 4154 | MethodArgs = new Expr*[NumArgs + 1]; |
| 4155 | } |
| 4156 | MethodArgs[0] = Object; |
| 4157 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4158 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
| 4159 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4160 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
| 4161 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4162 | UsualUnaryConversions(NewFn); |
| 4163 | |
| 4164 | // Once we've built TheCall, all of the expressions are properly |
| 4165 | // owned. |
| 4166 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4167 | ExprOwningPtr<CXXOperatorCallExpr> |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4168 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
| 4169 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4170 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4171 | delete [] MethodArgs; |
| 4172 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4173 | // We may have default arguments. If so, we need to allocate more |
| 4174 | // slots in the call for them. |
| 4175 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4176 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4177 | else if (NumArgs > NumArgsInProto) |
| 4178 | NumArgsToCheck = NumArgsInProto; |
| 4179 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4180 | // Initialize the implicit object parameter. |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4181 | if (PerformObjectArgumentInitialization(Object, Method)) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4182 | return true; |
| 4183 | TheCall->setArg(0, Object); |
| 4184 | |
| 4185 | // Check the argument types. |
| 4186 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4187 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4188 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4189 | Arg = Args[i]; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4190 | |
| 4191 | // Pass the argument. |
| 4192 | QualType ProtoArgType = Proto->getArgType(i); |
| 4193 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 4194 | return true; |
| 4195 | } else { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4196 | Arg = new (Context) CXXDefaultArgExpr(Method->getParamDecl(i)); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4197 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4198 | |
| 4199 | TheCall->setArg(i + 1, Arg); |
| 4200 | } |
| 4201 | |
| 4202 | // If this is a variadic call, handle args passed through "...". |
| 4203 | if (Proto->isVariadic()) { |
| 4204 | // Promote the arguments (C99 6.5.2.2p7). |
| 4205 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 4206 | Expr *Arg = Args[i]; |
Anders Carlsson | 906fed0 | 2009-01-13 05:48:52 +0000 | [diff] [blame] | 4207 | |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 4208 | DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4209 | TheCall->setArg(i + 1, Arg); |
| 4210 | } |
| 4211 | } |
| 4212 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4213 | return CheckFunctionCall(Method, TheCall.take()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4216 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
| 4217 | /// (if one exists), where @c Base is an expression of class type and |
| 4218 | /// @c Member is the name of the member we're trying to find. |
| 4219 | Action::ExprResult |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4220 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4221 | SourceLocation MemberLoc, |
| 4222 | IdentifierInfo &Member) { |
| 4223 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
| 4224 | |
| 4225 | // C++ [over.ref]p1: |
| 4226 | // |
| 4227 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 4228 | // for a class object x of type T if T::operator->() exists and if |
| 4229 | // the operator is selected as the best match function by the |
| 4230 | // overload resolution mechanism (13.3). |
| 4231 | // FIXME: look in base classes. |
| 4232 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 4233 | OverloadCandidateSet CandidateSet; |
| 4234 | const RecordType *BaseRecord = Base->getType()->getAsRecordType(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4235 | |
| 4236 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 4237 | for (llvm::tie(Oper, OperEnd) = BaseRecord->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4238 | Oper != OperEnd; ++Oper) |
| 4239 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4240 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4241 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4242 | ExprOwningPtr<Expr> BasePtr(this, Base); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4243 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4244 | // Perform overload resolution. |
| 4245 | OverloadCandidateSet::iterator Best; |
| 4246 | switch (BestViableFunction(CandidateSet, Best)) { |
| 4247 | case OR_Success: |
| 4248 | // Overload resolution succeeded; we'll build the call below. |
| 4249 | break; |
| 4250 | |
| 4251 | case OR_No_Viable_Function: |
| 4252 | if (CandidateSet.empty()) |
| 4253 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4254 | << BasePtr->getType() << BasePtr->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4255 | else |
| 4256 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4257 | << "operator->" << BasePtr->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4258 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4259 | return true; |
| 4260 | |
| 4261 | case OR_Ambiguous: |
| 4262 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4263 | << "operator->" << BasePtr->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4264 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4265 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4266 | |
| 4267 | case OR_Deleted: |
| 4268 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4269 | << Best->Function->isDeleted() |
| 4270 | << "operator->" << BasePtr->getSourceRange(); |
| 4271 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4272 | return true; |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | // Convert the object parameter. |
| 4276 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4277 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4278 | return true; |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4279 | |
| 4280 | // No concerns about early exits now. |
| 4281 | BasePtr.take(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4282 | |
| 4283 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4284 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 4285 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4286 | UsualUnaryConversions(FnExpr); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4287 | Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4288 | Method->getResultType().getNonReferenceType(), |
| 4289 | OpLoc); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4290 | return ActOnMemberReferenceExpr(S, ExprArg(*this, Base), OpLoc, tok::arrow, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4291 | MemberLoc, Member, DeclPtrTy()).release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4294 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 4295 | /// a C++ overloaded function (possibly with some parentheses and |
| 4296 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 4297 | /// to the function declaration Fn, so patch up the expression E to |
| 4298 | /// refer (possibly indirectly) to Fn. |
| 4299 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 4300 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 4301 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 4302 | E->setType(PE->getSubExpr()->getType()); |
| 4303 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
| 4304 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
| 4305 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4306 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4307 | if (Method->isStatic()) { |
| 4308 | // Do nothing: static member functions aren't any different |
| 4309 | // from non-member functions. |
| 4310 | } |
| 4311 | else if (QualifiedDeclRefExpr *DRE |
| 4312 | = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) { |
| 4313 | // We have taken the address of a pointer to member |
| 4314 | // function. Perform the computation here so that we get the |
| 4315 | // appropriate pointer to member type. |
| 4316 | DRE->setDecl(Fn); |
| 4317 | DRE->setType(Fn->getType()); |
| 4318 | QualType ClassType |
| 4319 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 4320 | E->setType(Context.getMemberPointerType(Fn->getType(), |
| 4321 | ClassType.getTypePtr())); |
| 4322 | return; |
| 4323 | } |
| 4324 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4325 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4326 | E->setType(Context.getPointerType(UnOp->getSubExpr()->getType())); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4327 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
| 4328 | assert(isa<OverloadedFunctionDecl>(DR->getDecl()) && |
| 4329 | "Expected overloaded function"); |
| 4330 | DR->setDecl(Fn); |
| 4331 | E->setType(Fn->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4332 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 4333 | MemExpr->setMemberDecl(Fn); |
| 4334 | E->setType(Fn->getType()); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4335 | } else { |
| 4336 | assert(false && "Invalid reference to overloaded function"); |
| 4337 | } |
| 4338 | } |
| 4339 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4340 | } // end namespace clang |