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