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