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