Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [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" |
| 15 | #include "Lookup.h" |
| 16 | #include "SemaInit.h" |
| 17 | #include "clang/Basic/Diagnostic.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/CXXInheritance.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/ExprCXX.h" |
| 23 | #include "clang/AST/TypeOrdering.h" |
| 24 | #include "clang/Basic/PartialDiagnostic.h" |
| 25 | #include "llvm/ADT/SmallPtrSet.h" |
| 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include <algorithm> |
| 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | /// GetConversionCategory - Retrieve the implicit conversion |
| 32 | /// category corresponding to the given implicit conversion kind. |
| 33 | ImplicitConversionCategory |
| 34 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 35 | static const ImplicitConversionCategory |
| 36 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 37 | ICC_Identity, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Lvalue_Transformation, |
| 40 | ICC_Lvalue_Transformation, |
| 41 | ICC_Identity, |
| 42 | ICC_Qualification_Adjustment, |
| 43 | ICC_Promotion, |
| 44 | ICC_Promotion, |
| 45 | ICC_Promotion, |
| 46 | ICC_Conversion, |
| 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
| 50 | ICC_Conversion, |
| 51 | ICC_Conversion, |
| 52 | ICC_Conversion, |
| 53 | ICC_Conversion, |
| 54 | ICC_Conversion, |
| 55 | ICC_Conversion |
| 56 | }; |
| 57 | return Category[(int)Kind]; |
| 58 | } |
| 59 | |
| 60 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 61 | /// corresponding to the given implicit conversion kind. |
| 62 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 63 | static const ImplicitConversionRank |
| 64 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Exact_Match, |
| 70 | ICR_Exact_Match, |
| 71 | ICR_Promotion, |
| 72 | ICR_Promotion, |
| 73 | ICR_Promotion, |
| 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
| 79 | ICR_Conversion, |
| 80 | ICR_Conversion, |
| 81 | ICR_Conversion, |
| 82 | ICR_Conversion, |
| 83 | ICR_Conversion |
| 84 | }; |
| 85 | return Rank[(int)Kind]; |
| 86 | } |
| 87 | |
| 88 | /// GetImplicitConversionName - Return the name of this kind of |
| 89 | /// implicit conversion. |
| 90 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 91 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
| 92 | "No conversion", |
| 93 | "Lvalue-to-rvalue", |
| 94 | "Array-to-pointer", |
| 95 | "Function-to-pointer", |
| 96 | "Noreturn adjustment", |
| 97 | "Qualification", |
| 98 | "Integral promotion", |
| 99 | "Floating point promotion", |
| 100 | "Complex promotion", |
| 101 | "Integral conversion", |
| 102 | "Floating conversion", |
| 103 | "Complex conversion", |
| 104 | "Floating-integral conversion", |
| 105 | "Complex-real conversion", |
| 106 | "Pointer conversion", |
| 107 | "Pointer-to-member conversion", |
| 108 | "Boolean conversion", |
| 109 | "Compatible-types conversion", |
| 110 | "Derived-to-base conversion" |
| 111 | }; |
| 112 | return Name[Kind]; |
| 113 | } |
| 114 | |
| 115 | /// StandardConversionSequence - Set the standard conversion |
| 116 | /// sequence to the identity conversion. |
| 117 | void StandardConversionSequence::setAsIdentityConversion() { |
| 118 | First = ICK_Identity; |
| 119 | Second = ICK_Identity; |
| 120 | Third = ICK_Identity; |
| 121 | Deprecated = false; |
| 122 | ReferenceBinding = false; |
| 123 | DirectBinding = false; |
| 124 | RRefBinding = false; |
| 125 | CopyConstructor = 0; |
| 126 | } |
| 127 | |
| 128 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 129 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 130 | /// implicit conversions. |
| 131 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 132 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 133 | if (GetConversionRank(First) > Rank) |
| 134 | Rank = GetConversionRank(First); |
| 135 | if (GetConversionRank(Second) > Rank) |
| 136 | Rank = GetConversionRank(Second); |
| 137 | if (GetConversionRank(Third) > Rank) |
| 138 | Rank = GetConversionRank(Third); |
| 139 | return Rank; |
| 140 | } |
| 141 | |
| 142 | /// isPointerConversionToBool - Determines whether this conversion is |
| 143 | /// a conversion of a pointer or pointer-to-member to bool. This is |
| 144 | /// used as part of the ranking of standard conversion sequences |
| 145 | /// (C++ 13.3.3.2p4). |
| 146 | bool StandardConversionSequence::isPointerConversionToBool() const { |
| 147 | // Note that FromType has not necessarily been transformed by the |
| 148 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 149 | // check for their presence as well as checking whether FromType is |
| 150 | // a pointer. |
| 151 | if (getToType(1)->isBooleanType() && |
| 152 | (getFromType()->isPointerType() || getFromType()->isBlockPointerType() || |
| 153 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | /// isPointerConversionToVoidPointer - Determines whether this |
| 160 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 161 | /// used as part of the ranking of standard conversion sequences (C++ |
| 162 | /// 13.3.3.2p4). |
| 163 | bool |
| 164 | StandardConversionSequence:: |
| 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
| 166 | QualType FromType = getFromType(); |
| 167 | QualType ToType = getToType(1); |
| 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
| 175 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
| 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
| 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
| 185 | llvm::raw_ostream &OS = llvm::errs(); |
| 186 | bool PrintedSomething = false; |
| 187 | if (First != ICK_Identity) { |
| 188 | OS << GetImplicitConversionName(First); |
| 189 | PrintedSomething = true; |
| 190 | } |
| 191 | |
| 192 | if (Second != ICK_Identity) { |
| 193 | if (PrintedSomething) { |
| 194 | OS << " -> "; |
| 195 | } |
| 196 | OS << GetImplicitConversionName(Second); |
| 197 | |
| 198 | if (CopyConstructor) { |
| 199 | OS << " (by copy constructor)"; |
| 200 | } else if (DirectBinding) { |
| 201 | OS << " (direct reference binding)"; |
| 202 | } else if (ReferenceBinding) { |
| 203 | OS << " (reference binding)"; |
| 204 | } |
| 205 | PrintedSomething = true; |
| 206 | } |
| 207 | |
| 208 | if (Third != ICK_Identity) { |
| 209 | if (PrintedSomething) { |
| 210 | OS << " -> "; |
| 211 | } |
| 212 | OS << GetImplicitConversionName(Third); |
| 213 | PrintedSomething = true; |
| 214 | } |
| 215 | |
| 216 | if (!PrintedSomething) { |
| 217 | OS << "No conversions required"; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 222 | /// error. Useful for debugging overloading issues. |
| 223 | void UserDefinedConversionSequence::DebugPrint() const { |
| 224 | llvm::raw_ostream &OS = llvm::errs(); |
| 225 | if (Before.First || Before.Second || Before.Third) { |
| 226 | Before.DebugPrint(); |
| 227 | OS << " -> "; |
| 228 | } |
| 229 | OS << "'" << ConversionFunction->getNameAsString() << "'"; |
| 230 | if (After.First || After.Second || After.Third) { |
| 231 | OS << " -> "; |
| 232 | After.DebugPrint(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 237 | /// error. Useful for debugging overloading issues. |
| 238 | void ImplicitConversionSequence::DebugPrint() const { |
| 239 | llvm::raw_ostream &OS = llvm::errs(); |
| 240 | switch (ConversionKind) { |
| 241 | case StandardConversion: |
| 242 | OS << "Standard conversion: "; |
| 243 | Standard.DebugPrint(); |
| 244 | break; |
| 245 | case UserDefinedConversion: |
| 246 | OS << "User-defined conversion: "; |
| 247 | UserDefined.DebugPrint(); |
| 248 | break; |
| 249 | case EllipsisConversion: |
| 250 | OS << "Ellipsis conversion"; |
| 251 | break; |
| 252 | case AmbiguousConversion: |
| 253 | OS << "Ambiguous conversion"; |
| 254 | break; |
| 255 | case BadConversion: |
| 256 | OS << "Bad conversion"; |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | OS << "\n"; |
| 261 | } |
| 262 | |
| 263 | void AmbiguousConversionSequence::construct() { |
| 264 | new (&conversions()) ConversionSet(); |
| 265 | } |
| 266 | |
| 267 | void AmbiguousConversionSequence::destruct() { |
| 268 | conversions().~ConversionSet(); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 273 | FromTypePtr = O.FromTypePtr; |
| 274 | ToTypePtr = O.ToTypePtr; |
| 275 | new (&conversions()) ConversionSet(O.conversions()); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | // IsOverload - Determine whether the given New declaration is an |
| 280 | // overload of the declarations in Old. This routine returns false if |
| 281 | // New and Old cannot be overloaded, e.g., if New has the same |
| 282 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 283 | // declarations aren't functions (or function templates) at all. When |
| 284 | // it does return false, MatchedDecl will point to the decl that New |
| 285 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 286 | // top of the underlying declaration. |
| 287 | // |
| 288 | // Example: Given the following input: |
| 289 | // |
| 290 | // void f(int, float); // #1 |
| 291 | // void f(int, int); // #2 |
| 292 | // int f(int, int); // #3 |
| 293 | // |
| 294 | // When we process #1, there is no previous declaration of "f", |
| 295 | // so IsOverload will not be used. |
| 296 | // |
| 297 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 298 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 299 | // (since they have different signatures), so this routine returns |
| 300 | // false; MatchedDecl is unchanged. |
| 301 | // |
| 302 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 303 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 304 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 305 | // identical (return types of functions are not part of the |
| 306 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 307 | // point to the FunctionDecl for #2. |
| 308 | Sema::OverloadKind |
| 309 | Sema::CheckOverload(FunctionDecl *New, const LookupResult &Old, |
| 310 | NamedDecl *&Match) { |
| 311 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
| 312 | I != E; ++I) { |
| 313 | NamedDecl *OldD = (*I)->getUnderlyingDecl(); |
| 314 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
| 315 | if (!IsOverload(New, OldT->getTemplatedDecl())) { |
| 316 | Match = *I; |
| 317 | return Ovl_Match; |
| 318 | } |
| 319 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
| 320 | if (!IsOverload(New, OldF)) { |
| 321 | Match = *I; |
| 322 | return Ovl_Match; |
| 323 | } |
| 324 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 325 | // We can overload with these, which can show up when doing |
| 326 | // redeclaration checks for UsingDecls. |
| 327 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 328 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 329 | // Optimistically assume that an unresolved using decl will |
| 330 | // overload; if it doesn't, we'll have to diagnose during |
| 331 | // template instantiation. |
| 332 | } else { |
| 333 | // (C++ 13p1): |
| 334 | // Only function declarations can be overloaded; object and type |
| 335 | // declarations cannot be overloaded. |
| 336 | Match = *I; |
| 337 | return Ovl_NonFunction; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return Ovl_Overload; |
| 342 | } |
| 343 | |
| 344 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old) { |
| 345 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 346 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 347 | |
| 348 | // C++ [temp.fct]p2: |
| 349 | // A function template can be overloaded with other function templates |
| 350 | // and with normal (non-template) functions. |
| 351 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 352 | return true; |
| 353 | |
| 354 | // Is the function New an overload of the function Old? |
| 355 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 356 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 357 | |
| 358 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 359 | // determine whether they are overloads. If we find any mismatch |
| 360 | // in the signature, they are overloads. |
| 361 | |
| 362 | // If either of these functions is a K&R-style function (no |
| 363 | // prototype), then we consider them to have matching signatures. |
| 364 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 365 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 366 | return false; |
| 367 | |
| 368 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 369 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 370 | |
| 371 | // The signature of a function includes the types of its |
| 372 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 373 | // of the ellipsis; see C++ DR 357). |
| 374 | if (OldQType != NewQType && |
| 375 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 376 | OldType->isVariadic() != NewType->isVariadic() || |
| 377 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 378 | NewType->arg_type_begin()))) |
| 379 | return true; |
| 380 | |
| 381 | // C++ [temp.over.link]p4: |
| 382 | // The signature of a function template consists of its function |
| 383 | // signature, its return type and its template parameter list. The names |
| 384 | // of the template parameters are significant only for establishing the |
| 385 | // relationship between the template parameters and the rest of the |
| 386 | // signature. |
| 387 | // |
| 388 | // We check the return type and template parameter lists for function |
| 389 | // templates first; the remaining checks follow. |
| 390 | if (NewTemplate && |
| 391 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 392 | OldTemplate->getTemplateParameters(), |
| 393 | false, TPL_TemplateMatch) || |
| 394 | OldType->getResultType() != NewType->getResultType())) |
| 395 | return true; |
| 396 | |
| 397 | // If the function is a class member, its signature includes the |
| 398 | // cv-qualifiers (if any) on the function itself. |
| 399 | // |
| 400 | // As part of this, also check whether one of the member functions |
| 401 | // is static, in which case they are not overloads (C++ |
| 402 | // 13.1p2). While not part of the definition of the signature, |
| 403 | // this check is important to determine whether these functions |
| 404 | // can be overloaded. |
| 405 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 406 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 407 | if (OldMethod && NewMethod && |
| 408 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 409 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 410 | return true; |
| 411 | |
| 412 | // The signatures match; this is not an overload. |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 417 | /// from the given expression (Expr) to the given type (ToType). This |
| 418 | /// function returns an implicit conversion sequence that can be used |
| 419 | /// to perform the initialization. Given |
| 420 | /// |
| 421 | /// void f(float f); |
| 422 | /// void g(int i) { f(i); } |
| 423 | /// |
| 424 | /// this routine would produce an implicit conversion sequence to |
| 425 | /// describe the initialization of f from i, which will be a standard |
| 426 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 427 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 428 | // |
| 429 | /// Note that this routine only determines how the conversion can be |
| 430 | /// performed; it does not actually perform the conversion. As such, |
| 431 | /// it will not produce any diagnostics if no conversion is available, |
| 432 | /// but will instead return an implicit conversion sequence of kind |
| 433 | /// "BadConversion". |
| 434 | /// |
| 435 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 436 | /// not permitted. |
| 437 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 438 | /// permitted. |
| 439 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 440 | /// no matter its actual lvalueness. |
| 441 | /// If @p UserCast, the implicit conversion is being done for a user-specified |
| 442 | /// cast. |
| 443 | ImplicitConversionSequence |
| 444 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 445 | bool SuppressUserConversions, |
| 446 | bool AllowExplicit, bool ForceRValue, |
| 447 | bool InOverloadResolution, |
| 448 | bool UserCast) { |
| 449 | ImplicitConversionSequence ICS; |
| 450 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) { |
| 451 | ICS.setStandard(); |
| 452 | return ICS; |
| 453 | } |
| 454 | |
| 455 | if (!getLangOptions().CPlusPlus) { |
| 456 | ICS.setBad(); |
| 457 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
| 458 | return ICS; |
| 459 | } |
| 460 | |
| 461 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 462 | OverloadingResult UserDefResult |
| 463 | = IsUserDefinedConversion(From, ToType, ICS.UserDefined, Conversions, |
| 464 | !SuppressUserConversions, AllowExplicit, |
| 465 | ForceRValue, UserCast); |
| 466 | |
| 467 | if (UserDefResult == OR_Success) { |
| 468 | ICS.setUserDefined(); |
| 469 | // C++ [over.ics.user]p4: |
| 470 | // A conversion of an expression of class type to the same class |
| 471 | // type is given Exact Match rank, and a conversion of an |
| 472 | // expression of class type to a base class of that type is |
| 473 | // given Conversion rank, in spite of the fact that a copy |
| 474 | // constructor (i.e., a user-defined conversion function) is |
| 475 | // called for those cases. |
| 476 | if (CXXConstructorDecl *Constructor |
| 477 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 478 | QualType FromCanon |
| 479 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 480 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 481 | if (Constructor->isCopyConstructor() && |
| 482 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
| 483 | // Turn this into a "standard" conversion sequence, so that it |
| 484 | // gets ranked with standard conversion sequences. |
| 485 | ICS.setStandard(); |
| 486 | ICS.Standard.setAsIdentityConversion(); |
| 487 | ICS.Standard.setFromType(From->getType()); |
| 488 | ICS.Standard.setAllToTypes(ToType); |
| 489 | ICS.Standard.CopyConstructor = Constructor; |
| 490 | if (ToCanon != FromCanon) |
| 491 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // C++ [over.best.ics]p4: |
| 496 | // However, when considering the argument of a user-defined |
| 497 | // conversion function that is a candidate by 13.3.1.3 when |
| 498 | // invoked for the copying of the temporary in the second step |
| 499 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 500 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 501 | // ellipsis conversion sequences are allowed. |
| 502 | if (SuppressUserConversions && ICS.isUserDefined()) { |
| 503 | ICS.setBad(); |
| 504 | ICS.Bad.init(BadConversionSequence::suppressed_user, From, ToType); |
| 505 | } |
| 506 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
| 507 | ICS.setAmbiguous(); |
| 508 | ICS.Ambiguous.setFromType(From->getType()); |
| 509 | ICS.Ambiguous.setToType(ToType); |
| 510 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 511 | Cand != Conversions.end(); ++Cand) |
| 512 | if (Cand->Viable) |
| 513 | ICS.Ambiguous.addConversion(Cand->Function); |
| 514 | } else { |
| 515 | ICS.setBad(); |
| 516 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
| 517 | } |
| 518 | |
| 519 | return ICS; |
| 520 | } |
| 521 | |
| 522 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 523 | /// conversion that strips "noreturn" off the nested function type. |
| 524 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 525 | QualType ToType, QualType &ResultTy) { |
| 526 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 527 | return false; |
| 528 | |
| 529 | // Strip the noreturn off the type we're converting from; noreturn can |
| 530 | // safely be removed. |
| 531 | FromType = Context.getNoReturnType(FromType, false); |
| 532 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 533 | return false; |
| 534 | |
| 535 | ResultTy = FromType; |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | /// IsStandardConversion - Determines whether there is a standard |
| 540 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 541 | /// expression From to the type ToType. Standard conversion sequences |
| 542 | /// only consider non-class types; for conversions that involve class |
| 543 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 544 | /// contain the standard conversion sequence required to perform this |
| 545 | /// conversion and this routine will return true. Otherwise, this |
| 546 | /// routine will return false and the value of SCS is unspecified. |
| 547 | bool |
| 548 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
| 549 | bool InOverloadResolution, |
| 550 | StandardConversionSequence &SCS) { |
| 551 | QualType FromType = From->getType(); |
| 552 | |
| 553 | // Standard conversions (C++ [conv]) |
| 554 | SCS.setAsIdentityConversion(); |
| 555 | SCS.Deprecated = false; |
| 556 | SCS.IncompatibleObjC = false; |
| 557 | SCS.setFromType(FromType); |
| 558 | SCS.CopyConstructor = 0; |
| 559 | |
| 560 | // There are no standard conversions for class types in C++, so |
| 561 | // abort early. When overloading in C, however, we do permit |
| 562 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 563 | if (getLangOptions().CPlusPlus) |
| 564 | return false; |
| 565 | |
| 566 | // When we're overloading in C, we allow, as standard conversions, |
| 567 | } |
| 568 | |
| 569 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 570 | // array-to-pointer conversion, or function-to-pointer conversion |
| 571 | // (C++ 4p1). |
| 572 | |
| 573 | // Lvalue-to-rvalue conversion (C++ 4.1): |
| 574 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 575 | // converted to an rvalue. |
| 576 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
| 577 | if (argIsLvalue == Expr::LV_Valid && |
| 578 | !FromType->isFunctionType() && !FromType->isArrayType() && |
| 579 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
| 580 | SCS.First = ICK_Lvalue_To_Rvalue; |
| 581 | |
| 582 | // If T is a non-class type, the type of the rvalue is the |
| 583 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
| 584 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 585 | // just strip the qualifiers because they don't matter. |
| 586 | FromType = FromType.getUnqualifiedType(); |
| 587 | } else if (FromType->isArrayType()) { |
| 588 | // Array-to-pointer conversion (C++ 4.2) |
| 589 | SCS.First = ICK_Array_To_Pointer; |
| 590 | |
| 591 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 592 | // bound of T" can be converted to an rvalue of type "pointer to |
| 593 | // T" (C++ 4.2p1). |
| 594 | FromType = Context.getArrayDecayedType(FromType); |
| 595 | |
| 596 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 597 | // This conversion is deprecated. (C++ D.4). |
| 598 | SCS.Deprecated = true; |
| 599 | |
| 600 | // For the purpose of ranking in overload resolution |
| 601 | // (13.3.3.1.1), this conversion is considered an |
| 602 | // array-to-pointer conversion followed by a qualification |
| 603 | // conversion (4.4). (C++ 4.2p2) |
| 604 | SCS.Second = ICK_Identity; |
| 605 | SCS.Third = ICK_Qualification; |
| 606 | SCS.setAllToTypes(FromType); |
| 607 | return true; |
| 608 | } |
| 609 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 610 | // Function-to-pointer conversion (C++ 4.3). |
| 611 | SCS.First = ICK_Function_To_Pointer; |
| 612 | |
| 613 | // An lvalue of function type T can be converted to an rvalue of |
| 614 | // type "pointer to T." The result is a pointer to the |
| 615 | // function. (C++ 4.3p1). |
| 616 | FromType = Context.getPointerType(FromType); |
| 617 | } else if (FunctionDecl *Fn |
| 618 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
| 619 | // Address of overloaded function (C++ [over.over]). |
| 620 | SCS.First = ICK_Function_To_Pointer; |
| 621 | |
| 622 | // We were able to resolve the address of the overloaded function, |
| 623 | // so we can convert to the type of that function. |
| 624 | FromType = Fn->getType(); |
| 625 | if (ToType->isLValueReferenceType()) |
| 626 | FromType = Context.getLValueReferenceType(FromType); |
| 627 | else if (ToType->isRValueReferenceType()) |
| 628 | FromType = Context.getRValueReferenceType(FromType); |
| 629 | else if (ToType->isMemberPointerType()) { |
| 630 | // Resolve address only succeeds if both sides are member pointers, |
| 631 | // but it doesn't have to be the same class. See DR 247. |
| 632 | // Note that this means that the type of &Derived::fn can be |
| 633 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 634 | // base class, even if it was brought into the derived class via a |
| 635 | // using declaration. The standard isn't clear on this issue at all. |
| 636 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 637 | FromType = Context.getMemberPointerType(FromType, |
| 638 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 639 | } else |
| 640 | FromType = Context.getPointerType(FromType); |
| 641 | } else { |
| 642 | // We don't require any conversions for the first step. |
| 643 | SCS.First = ICK_Identity; |
| 644 | } |
| 645 | SCS.setToType(0, FromType); |
| 646 | |
| 647 | // The second conversion can be an integral promotion, floating |
| 648 | // point promotion, integral conversion, floating point conversion, |
| 649 | // floating-integral conversion, pointer conversion, |
| 650 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
| 651 | // For overloading in C, this can also be a "compatible-type" |
| 652 | // conversion. |
| 653 | bool IncompatibleObjC = false; |
| 654 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
| 655 | // The unqualified versions of the types are the same: there's no |
| 656 | // conversion to do. |
| 657 | SCS.Second = ICK_Identity; |
| 658 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
| 659 | // Integral promotion (C++ 4.5). |
| 660 | SCS.Second = ICK_Integral_Promotion; |
| 661 | FromType = ToType.getUnqualifiedType(); |
| 662 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 663 | // Floating point promotion (C++ 4.6). |
| 664 | SCS.Second = ICK_Floating_Promotion; |
| 665 | FromType = ToType.getUnqualifiedType(); |
| 666 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 667 | // Complex promotion (Clang extension) |
| 668 | SCS.Second = ICK_Complex_Promotion; |
| 669 | FromType = ToType.getUnqualifiedType(); |
| 670 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
| 671 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
| 672 | // Integral conversions (C++ 4.7). |
| 673 | SCS.Second = ICK_Integral_Conversion; |
| 674 | FromType = ToType.getUnqualifiedType(); |
| 675 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 676 | // Floating point conversions (C++ 4.8). |
| 677 | SCS.Second = ICK_Floating_Conversion; |
| 678 | FromType = ToType.getUnqualifiedType(); |
| 679 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 680 | // Complex conversions (C99 6.3.1.6) |
| 681 | SCS.Second = ICK_Complex_Conversion; |
| 682 | FromType = ToType.getUnqualifiedType(); |
| 683 | } else if ((FromType->isFloatingType() && |
| 684 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 685 | !ToType->isEnumeralType())) || |
| 686 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
| 687 | ToType->isFloatingType())) { |
| 688 | // Floating-integral conversions (C++ 4.9). |
| 689 | SCS.Second = ICK_Floating_Integral; |
| 690 | FromType = ToType.getUnqualifiedType(); |
| 691 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 692 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 693 | // Complex-real conversions (C99 6.3.1.7) |
| 694 | SCS.Second = ICK_Complex_Real; |
| 695 | FromType = ToType.getUnqualifiedType(); |
| 696 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 697 | FromType, IncompatibleObjC)) { |
| 698 | // Pointer conversions (C++ 4.10). |
| 699 | SCS.Second = ICK_Pointer_Conversion; |
| 700 | SCS.IncompatibleObjC = IncompatibleObjC; |
| 701 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 702 | InOverloadResolution, FromType)) { |
| 703 | // Pointer to member conversions (4.11). |
| 704 | SCS.Second = ICK_Pointer_Member; |
| 705 | } else if (ToType->isBooleanType() && |
| 706 | (FromType->isArithmeticType() || |
| 707 | FromType->isEnumeralType() || |
| 708 | FromType->isAnyPointerType() || |
| 709 | FromType->isBlockPointerType() || |
| 710 | FromType->isMemberPointerType() || |
| 711 | FromType->isNullPtrType())) { |
| 712 | // Boolean conversions (C++ 4.12). |
| 713 | SCS.Second = ICK_Boolean_Conversion; |
| 714 | FromType = Context.BoolTy; |
| 715 | } else if (!getLangOptions().CPlusPlus && |
| 716 | Context.typesAreCompatible(ToType, FromType)) { |
| 717 | // Compatible conversions (Clang extension for C function overloading) |
| 718 | SCS.Second = ICK_Compatible_Conversion; |
| 719 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 720 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 721 | SCS.Second = ICK_NoReturn_Adjustment; |
| 722 | } else { |
| 723 | // No second conversion required. |
| 724 | SCS.Second = ICK_Identity; |
| 725 | } |
| 726 | SCS.setToType(1, FromType); |
| 727 | |
| 728 | QualType CanonFrom; |
| 729 | QualType CanonTo; |
| 730 | // The third conversion can be a qualification conversion (C++ 4p1). |
| 731 | if (IsQualificationConversion(FromType, ToType)) { |
| 732 | SCS.Third = ICK_Qualification; |
| 733 | FromType = ToType; |
| 734 | CanonFrom = Context.getCanonicalType(FromType); |
| 735 | CanonTo = Context.getCanonicalType(ToType); |
| 736 | } else { |
| 737 | // No conversion required |
| 738 | SCS.Third = ICK_Identity; |
| 739 | |
| 740 | // C++ [over.best.ics]p6: |
| 741 | // [...] Any difference in top-level cv-qualification is |
| 742 | // subsumed by the initialization itself and does not constitute |
| 743 | // a conversion. [...] |
| 744 | CanonFrom = Context.getCanonicalType(FromType); |
| 745 | CanonTo = Context.getCanonicalType(ToType); |
| 746 | if (CanonFrom.getLocalUnqualifiedType() |
| 747 | == CanonTo.getLocalUnqualifiedType() && |
| 748 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
| 749 | FromType = ToType; |
| 750 | CanonFrom = CanonTo; |
| 751 | } |
| 752 | } |
| 753 | SCS.setToType(2, FromType); |
| 754 | |
| 755 | // If we have not converted the argument type to the parameter type, |
| 756 | // this is a bad conversion sequence. |
| 757 | if (CanonFrom != CanonTo) |
| 758 | return false; |
| 759 | |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 764 | /// expression From (whose potentially-adjusted type is FromType) to |
| 765 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 766 | /// sets PromotedType to the promoted type. |
| 767 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
| 768 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
| 769 | // All integers are built-in. |
| 770 | if (!To) { |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 775 | // unsigned short int can be converted to an rvalue of type int if |
| 776 | // int can represent all the values of the source type; otherwise, |
| 777 | // the source rvalue can be converted to an rvalue of type unsigned |
| 778 | // int (C++ 4.5p1). |
| 779 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 780 | !FromType->isEnumeralType()) { |
| 781 | if (// We can promote any signed, promotable integer type to an int |
| 782 | (FromType->isSignedIntegerType() || |
| 783 | // We can promote any unsigned integer type whose size is |
| 784 | // less than int to an int. |
| 785 | (!FromType->isSignedIntegerType() && |
| 786 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
| 787 | return To->getKind() == BuiltinType::Int; |
| 788 | } |
| 789 | |
| 790 | return To->getKind() == BuiltinType::UInt; |
| 791 | } |
| 792 | |
| 793 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 794 | // can be converted to an rvalue of the first of the following types |
| 795 | // that can represent all the values of its underlying type: int, |
| 796 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 797 | |
| 798 | // We pre-calculate the promotion type for enum types. |
| 799 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 800 | if (ToType->isIntegerType()) |
| 801 | return Context.hasSameUnqualifiedType(ToType, |
| 802 | FromEnumType->getDecl()->getPromotionType()); |
| 803 | |
| 804 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
| 805 | // Determine whether the type we're converting from is signed or |
| 806 | // unsigned. |
| 807 | bool FromIsSigned; |
| 808 | uint64_t FromSize = Context.getTypeSize(FromType); |
| 809 | |
| 810 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 811 | FromIsSigned = true; |
| 812 | |
| 813 | // The types we'll try to promote to, in the appropriate |
| 814 | // order. Try each of these types. |
| 815 | QualType PromoteTypes[6] = { |
| 816 | Context.IntTy, Context.UnsignedIntTy, |
| 817 | Context.LongTy, Context.UnsignedLongTy , |
| 818 | Context.LongLongTy, Context.UnsignedLongLongTy |
| 819 | }; |
| 820 | for (int Idx = 0; Idx < 6; ++Idx) { |
| 821 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 822 | if (FromSize < ToSize || |
| 823 | (FromSize == ToSize && |
| 824 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 825 | // We found the type that we can promote to. If this is the |
| 826 | // type we wanted, we have a promotion. Otherwise, no |
| 827 | // promotion. |
| 828 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 834 | // rvalue of type int if int can represent all the values of the |
| 835 | // bit-field; otherwise, it can be converted to unsigned int if |
| 836 | // unsigned int can represent all the values of the bit-field. If |
| 837 | // the bit-field is larger yet, no integral promotion applies to |
| 838 | // it. If the bit-field has an enumerated type, it is treated as any |
| 839 | // other value of that type for promotion purposes (C++ 4.5p3). |
| 840 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 841 | // conversion. |
| 842 | using llvm::APSInt; |
| 843 | if (From) |
| 844 | if (FieldDecl *MemberDecl = From->getBitField()) { |
| 845 | APSInt BitWidth; |
| 846 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 847 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 848 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 849 | ToSize = Context.getTypeSize(ToType); |
| 850 | |
| 851 | // Are we promoting to an int from a bitfield that fits in an int? |
| 852 | if (BitWidth < ToSize || |
| 853 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 854 | return To->getKind() == BuiltinType::Int; |
| 855 | } |
| 856 | |
| 857 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 858 | // that fits into an unsigned int? |
| 859 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 860 | return To->getKind() == BuiltinType::UInt; |
| 861 | } |
| 862 | |
| 863 | return false; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 868 | // with false becoming zero and true becoming one (C++ 4.5p4). |
| 869 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 877 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 878 | /// returns true and sets PromotedType to the promoted type. |
| 879 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
| 880 | /// An rvalue of type float can be converted to an rvalue of type |
| 881 | /// double. (C++ 4.6p1). |
| 882 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 883 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
| 884 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 885 | ToBuiltin->getKind() == BuiltinType::Double) |
| 886 | return true; |
| 887 | |
| 888 | // C99 6.3.1.5p1: |
| 889 | // When a float is promoted to double or long double, or a |
| 890 | // double is promoted to long double [...]. |
| 891 | if (!getLangOptions().CPlusPlus && |
| 892 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 893 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 894 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 895 | return true; |
| 896 | } |
| 897 | |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | /// \brief Determine if a conversion is a complex promotion. |
| 902 | /// |
| 903 | /// A complex promotion is defined as a complex -> complex conversion |
| 904 | /// where the conversion between the underlying real types is a |
| 905 | /// floating-point or integral promotion. |
| 906 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
| 907 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
| 908 | if (!FromComplex) |
| 909 | return false; |
| 910 | |
| 911 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
| 912 | if (!ToComplex) |
| 913 | return false; |
| 914 | |
| 915 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
| 916 | ToComplex->getElementType()) || |
| 917 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 918 | ToComplex->getElementType()); |
| 919 | } |
| 920 | |
| 921 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 922 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 923 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 924 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 925 | /// the right set of qualifiers on its pointee. |
| 926 | static QualType |
| 927 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
| 928 | QualType ToPointee, QualType ToType, |
| 929 | ASTContext &Context) { |
| 930 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 931 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| 932 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
| 933 | |
| 934 | // Exact qualifier match -> return the pointer type we're converting to. |
| 935 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
| 936 | // ToType is exactly what we need. Return it. |
| 937 | if (!ToType.isNull()) |
| 938 | return ToType; |
| 939 | |
| 940 | // Build a pointer to ToPointee. It has the right qualifiers |
| 941 | // already. |
| 942 | return Context.getPointerType(ToPointee); |
| 943 | } |
| 944 | |
| 945 | // Just build a canonical type that has the right qualifiers. |
| 946 | return Context.getPointerType( |
| 947 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 948 | Quals)); |
| 949 | } |
| 950 | |
| 951 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 952 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 953 | /// not have the right set of qualifiers. |
| 954 | static QualType |
| 955 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 956 | QualType ToType, |
| 957 | ASTContext &Context) { |
| 958 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 959 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 960 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 961 | |
| 962 | // Exact qualifier match -> return the pointer type we're converting to. |
| 963 | if (CanonToType.getLocalQualifiers() == Quals) |
| 964 | return ToType; |
| 965 | |
| 966 | // Just build a canonical type that has the right qualifiers. |
| 967 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 968 | } |
| 969 | |
| 970 | static bool isNullPointerConstantForConversion(Expr *Expr, |
| 971 | bool InOverloadResolution, |
| 972 | ASTContext &Context) { |
| 973 | // Handle value-dependent integral null pointer constants correctly. |
| 974 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 975 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 976 | Expr->getType()->isIntegralType()) |
| 977 | return !InOverloadResolution; |
| 978 | |
| 979 | return Expr->isNullPointerConstant(Context, |
| 980 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 981 | : Expr::NPC_ValueDependentIsNull); |
| 982 | } |
| 983 | |
| 984 | /// IsPointerConversion - Determines whether the conversion of the |
| 985 | /// expression From, which has the (possibly adjusted) type FromType, |
| 986 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 987 | /// 4.10). If so, returns true and places the converted type (that |
| 988 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 989 | /// ConvertedType. |
| 990 | /// |
| 991 | /// This routine also supports conversions to and from block pointers |
| 992 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 993 | /// pointers to interfaces. FIXME: Once we've determined the |
| 994 | /// appropriate overloading rules for Objective-C, we may want to |
| 995 | /// split the Objective-C checks into a different routine; however, |
| 996 | /// GCC seems to consider all of these conversions to be pointer |
| 997 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 998 | /// set if the conversion is an allowed Objective-C conversion that |
| 999 | /// should result in a warning. |
| 1000 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
| 1001 | bool InOverloadResolution, |
| 1002 | QualType& ConvertedType, |
| 1003 | bool &IncompatibleObjC) { |
| 1004 | IncompatibleObjC = false; |
| 1005 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1006 | return true; |
| 1007 | |
| 1008 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1009 | if (ToType->isObjCObjectPointerType() && |
| 1010 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| 1011 | ConvertedType = ToType; |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | // Blocks: Block pointers can be converted to void*. |
| 1016 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
| 1017 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
| 1018 | ConvertedType = ToType; |
| 1019 | return true; |
| 1020 | } |
| 1021 | // Blocks: A null pointer constant can be converted to a block |
| 1022 | // pointer type. |
| 1023 | if (ToType->isBlockPointerType() && |
| 1024 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| 1025 | ConvertedType = ToType; |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
| 1029 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1030 | // pointer constant. |
| 1031 | if (ToType->isNullPtrType() && |
| 1032 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| 1033 | ConvertedType = ToType; |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
| 1038 | if (!ToTypePtr) |
| 1039 | return false; |
| 1040 | |
| 1041 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
| 1042 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
| 1043 | ConvertedType = ToType; |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
| 1047 | // Beyond this point, both types need to be pointers |
| 1048 | // , including objective-c pointers. |
| 1049 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1050 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1051 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1052 | ToType, Context); |
| 1053 | return true; |
| 1054 | |
| 1055 | } |
| 1056 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
| 1057 | if (!FromTypePtr) |
| 1058 | return false; |
| 1059 | |
| 1060 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 1061 | |
| 1062 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1063 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1064 | // 4.10p2). |
| 1065 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
| 1066 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1067 | ToPointeeType, |
| 1068 | ToType, Context); |
| 1069 | return true; |
| 1070 | } |
| 1071 | |
| 1072 | // When we're overloading in C, we allow a special kind of pointer |
| 1073 | // conversion for compatible-but-not-identical pointee types. |
| 1074 | if (!getLangOptions().CPlusPlus && |
| 1075 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
| 1076 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1077 | ToPointeeType, |
| 1078 | ToType, Context); |
| 1079 | return true; |
| 1080 | } |
| 1081 | |
| 1082 | // C++ [conv.ptr]p3: |
| 1083 | // |
| 1084 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1085 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1086 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1087 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1088 | // necessitates this conversion is ill-formed. The result of the |
| 1089 | // conversion is a pointer to the base class sub-object of the |
| 1090 | // derived class object. The null pointer value is converted to |
| 1091 | // the null pointer value of the destination type. |
| 1092 | // |
| 1093 | // Note that we do not check for ambiguity or inaccessibility |
| 1094 | // here. That is handled by CheckPointerConversion. |
| 1095 | if (getLangOptions().CPlusPlus && |
| 1096 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1097 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
| 1098 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
| 1099 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 1100 | ToPointeeType, |
| 1101 | ToType, Context); |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | /// isObjCPointerConversion - Determines whether this is an |
| 1109 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1110 | /// with the same arguments and return values. |
| 1111 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
| 1112 | QualType& ConvertedType, |
| 1113 | bool &IncompatibleObjC) { |
| 1114 | if (!getLangOptions().ObjC1) |
| 1115 | return false; |
| 1116 | |
| 1117 | // First, we handle all conversions on ObjC object pointer types. |
| 1118 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
| 1119 | const ObjCObjectPointerType *FromObjCPtr = |
| 1120 | FromType->getAs<ObjCObjectPointerType>(); |
| 1121 | |
| 1122 | if (ToObjCPtr && FromObjCPtr) { |
| 1123 | // Objective C++: We're able to convert between "id" or "Class" and a |
| 1124 | // pointer to any interface (in both directions). |
| 1125 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
| 1126 | ConvertedType = ToType; |
| 1127 | return true; |
| 1128 | } |
| 1129 | // Conversions with Objective-C's id<...>. |
| 1130 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
| 1131 | ToObjCPtr->isObjCQualifiedIdType()) && |
| 1132 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
| 1133 | /*compare=*/false)) { |
| 1134 | ConvertedType = ToType; |
| 1135 | return true; |
| 1136 | } |
| 1137 | // Objective C++: We're able to convert from a pointer to an |
| 1138 | // interface to a pointer to a different interface. |
| 1139 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1140 | ConvertedType = ToType; |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1145 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1146 | // interfaces, which is permitted. However, we're going to |
| 1147 | // complain about it. |
| 1148 | IncompatibleObjC = true; |
| 1149 | ConvertedType = FromType; |
| 1150 | return true; |
| 1151 | } |
| 1152 | } |
| 1153 | // Beyond this point, both types need to be C pointers or block pointers. |
| 1154 | QualType ToPointeeType; |
| 1155 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
| 1156 | ToPointeeType = ToCPtr->getPointeeType(); |
| 1157 | else if (const BlockPointerType *ToBlockPtr = |
| 1158 | ToType->getAs<BlockPointerType>()) { |
| 1159 | // Objective C++: We're able to convert from a pointer to any object |
| 1160 | // to a block pointer type. |
| 1161 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1162 | ConvertedType = ToType; |
| 1163 | return true; |
| 1164 | } |
| 1165 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1166 | } |
| 1167 | else if (FromType->getAs<BlockPointerType>() && |
| 1168 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1169 | // Objective C++: We're able to convert from a block pointer type to a |
| 1170 | // pointer to any object. |
| 1171 | ConvertedType = ToType; |
| 1172 | return true; |
| 1173 | } |
| 1174 | else |
| 1175 | return false; |
| 1176 | |
| 1177 | QualType FromPointeeType; |
| 1178 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
| 1179 | FromPointeeType = FromCPtr->getPointeeType(); |
| 1180 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
| 1181 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1182 | else |
| 1183 | return false; |
| 1184 | |
| 1185 | // If we have pointers to pointers, recursively check whether this |
| 1186 | // is an Objective-C conversion. |
| 1187 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1188 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1189 | IncompatibleObjC)) { |
| 1190 | // We always complain about this conversion. |
| 1191 | IncompatibleObjC = true; |
| 1192 | ConvertedType = ToType; |
| 1193 | return true; |
| 1194 | } |
| 1195 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1196 | // as in I* to id. |
| 1197 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1198 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1199 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1200 | IncompatibleObjC)) { |
| 1201 | ConvertedType = ToType; |
| 1202 | return true; |
| 1203 | } |
| 1204 | |
| 1205 | // If we have pointers to functions or blocks, check whether the only |
| 1206 | // differences in the argument and result types are in Objective-C |
| 1207 | // pointer conversions. If so, we permit the conversion (but |
| 1208 | // complain about it). |
| 1209 | const FunctionProtoType *FromFunctionType |
| 1210 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 1211 | const FunctionProtoType *ToFunctionType |
| 1212 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 1213 | if (FromFunctionType && ToFunctionType) { |
| 1214 | // If the function types are exactly the same, this isn't an |
| 1215 | // Objective-C pointer conversion. |
| 1216 | if (Context.getCanonicalType(FromPointeeType) |
| 1217 | == Context.getCanonicalType(ToPointeeType)) |
| 1218 | return false; |
| 1219 | |
| 1220 | // Perform the quick checks that will tell us whether these |
| 1221 | // function types are obviously different. |
| 1222 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1223 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1224 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1225 | return false; |
| 1226 | |
| 1227 | bool HasObjCConversion = false; |
| 1228 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1229 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1230 | // Okay, the types match exactly. Nothing to do. |
| 1231 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1232 | ToFunctionType->getResultType(), |
| 1233 | ConvertedType, IncompatibleObjC)) { |
| 1234 | // Okay, we have an Objective-C pointer conversion. |
| 1235 | HasObjCConversion = true; |
| 1236 | } else { |
| 1237 | // Function types are too different. Abort. |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
| 1241 | // Check argument types. |
| 1242 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1243 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1244 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1245 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1246 | if (Context.getCanonicalType(FromArgType) |
| 1247 | == Context.getCanonicalType(ToArgType)) { |
| 1248 | // Okay, the types match exactly. Nothing to do. |
| 1249 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1250 | ConvertedType, IncompatibleObjC)) { |
| 1251 | // Okay, we have an Objective-C pointer conversion. |
| 1252 | HasObjCConversion = true; |
| 1253 | } else { |
| 1254 | // Argument types are too different. Abort. |
| 1255 | return false; |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | if (HasObjCConversion) { |
| 1260 | // We had an Objective-C conversion. Allow this pointer |
| 1261 | // conversion, but complain about it. |
| 1262 | ConvertedType = ToType; |
| 1263 | IncompatibleObjC = true; |
| 1264 | return true; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | return false; |
| 1269 | } |
| 1270 | |
| 1271 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1272 | /// expression From to the type ToType. This routine checks for |
| 1273 | /// ambiguous or inaccessible derived-to-base pointer |
| 1274 | /// conversions for which IsPointerConversion has already returned |
| 1275 | /// true. It returns true and produces a diagnostic if there was an |
| 1276 | /// error, or returns false otherwise. |
| 1277 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
| 1278 | CastExpr::CastKind &Kind, |
| 1279 | bool IgnoreBaseAccess) { |
| 1280 | QualType FromType = From->getType(); |
| 1281 | |
| 1282 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1283 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
| 1284 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1285 | ToPointeeType = ToPtrType->getPointeeType(); |
| 1286 | |
| 1287 | if (FromPointeeType->isRecordType() && |
| 1288 | ToPointeeType->isRecordType()) { |
| 1289 | // We must have a derived-to-base conversion. Check an |
| 1290 | // ambiguous or inaccessible conversion. |
| 1291 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1292 | From->getExprLoc(), |
| 1293 | From->getSourceRange(), |
| 1294 | IgnoreBaseAccess)) |
| 1295 | return true; |
| 1296 | |
| 1297 | // The conversion was successful. |
| 1298 | Kind = CastExpr::CK_DerivedToBase; |
| 1299 | } |
| 1300 | } |
| 1301 | if (const ObjCObjectPointerType *FromPtrType = |
| 1302 | FromType->getAs<ObjCObjectPointerType>()) |
| 1303 | if (const ObjCObjectPointerType *ToPtrType = |
| 1304 | ToType->getAs<ObjCObjectPointerType>()) { |
| 1305 | // Objective-C++ conversions are always okay. |
| 1306 | // FIXME: We should have a different class of conversions for the |
| 1307 | // Objective-C++ implicit conversions. |
| 1308 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
| 1309 | return false; |
| 1310 | |
| 1311 | } |
| 1312 | return false; |
| 1313 | } |
| 1314 | |
| 1315 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1316 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1317 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1318 | /// If so, returns true and places the converted type (that might differ from |
| 1319 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1320 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
| 1321 | QualType ToType, |
| 1322 | bool InOverloadResolution, |
| 1323 | QualType &ConvertedType) { |
| 1324 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
| 1325 | if (!ToTypePtr) |
| 1326 | return false; |
| 1327 | |
| 1328 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
| 1329 | if (From->isNullPointerConstant(Context, |
| 1330 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1331 | : Expr::NPC_ValueDependentIsNull)) { |
| 1332 | ConvertedType = ToType; |
| 1333 | return true; |
| 1334 | } |
| 1335 | |
| 1336 | // Otherwise, both types have to be member pointers. |
| 1337 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
| 1338 | if (!FromTypePtr) |
| 1339 | return false; |
| 1340 | |
| 1341 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1342 | // where D is derived from B (C++ 4.11p2). |
| 1343 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1344 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1345 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1346 | |
| 1347 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1348 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1349 | ToClass.getTypePtr()); |
| 1350 | return true; |
| 1351 | } |
| 1352 | |
| 1353 | return false; |
| 1354 | } |
| 1355 | |
| 1356 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1357 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1358 | /// virtual or inaccessible base-to-derived member pointer conversions |
| 1359 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1360 | /// true and produces a diagnostic if there was an error, or returns false |
| 1361 | /// otherwise. |
| 1362 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
| 1363 | CastExpr::CastKind &Kind, |
| 1364 | bool IgnoreBaseAccess) { |
| 1365 | QualType FromType = From->getType(); |
| 1366 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
| 1367 | if (!FromPtrType) { |
| 1368 | // This must be a null pointer to member pointer conversion |
| 1369 | assert(From->isNullPointerConstant(Context, |
| 1370 | Expr::NPC_ValueDependentIsNull) && |
| 1371 | "Expr must be null pointer constant!"); |
| 1372 | Kind = CastExpr::CK_NullToMemberPointer; |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
| 1376 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
| 1377 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1378 | "that is not a member pointer."); |
| 1379 | |
| 1380 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1381 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
| 1382 | |
| 1383 | // FIXME: What about dependent types? |
| 1384 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1385 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
| 1386 | |
| 1387 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/ true, |
| 1388 | /*DetectVirtual=*/true); |
| 1389 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1390 | assert(DerivationOkay && |
| 1391 | "Should not have been called if derivation isn't OK."); |
| 1392 | (void)DerivationOkay; |
| 1393 | |
| 1394 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1395 | getUnqualifiedType())) { |
| 1396 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1397 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1398 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1399 | return true; |
| 1400 | } |
| 1401 | |
| 1402 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
| 1403 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1404 | << FromClass << ToClass << QualType(VBase, 0) |
| 1405 | << From->getSourceRange(); |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
| 1409 | if (!IgnoreBaseAccess) |
| 1410 | CheckBaseClassAccess(From->getExprLoc(), /*BaseToDerived*/ true, |
| 1411 | FromClass, ToClass, Paths.front()); |
| 1412 | |
| 1413 | // Must be a base to derived member conversion. |
| 1414 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
| 1415 | return false; |
| 1416 | } |
| 1417 | |
| 1418 | /// IsQualificationConversion - Determines whether the conversion from |
| 1419 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1420 | /// (C++ 4.4). |
| 1421 | bool |
| 1422 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
| 1423 | FromType = Context.getCanonicalType(FromType); |
| 1424 | ToType = Context.getCanonicalType(ToType); |
| 1425 | |
| 1426 | // If FromType and ToType are the same type, this is not a |
| 1427 | // qualification conversion. |
| 1428 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
| 1429 | return false; |
| 1430 | |
| 1431 | // (C++ 4.4p4): |
| 1432 | // A conversion can add cv-qualifiers at levels other than the first |
| 1433 | // in multi-level pointers, subject to the following rules: [...] |
| 1434 | bool PreviousToQualsIncludeConst = true; |
| 1435 | bool UnwrappedAnyPointer = false; |
| 1436 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
| 1437 | // Within each iteration of the loop, we check the qualifiers to |
| 1438 | // determine if this still looks like a qualification |
| 1439 | // conversion. Then, if all is well, we unwrap one more level of |
| 1440 | // pointers or pointers-to-members and do it all again |
| 1441 | // until there are no more pointers or pointers-to-members left to |
| 1442 | // unwrap. |
| 1443 | UnwrappedAnyPointer = true; |
| 1444 | |
| 1445 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1446 | // 2,j, and similarly for volatile. |
| 1447 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
| 1448 | return false; |
| 1449 | |
| 1450 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1451 | // every cv for 0 < k < j. |
| 1452 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
| 1453 | && !PreviousToQualsIncludeConst) |
| 1454 | return false; |
| 1455 | |
| 1456 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1457 | // include const. |
| 1458 | PreviousToQualsIncludeConst |
| 1459 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
| 1460 | } |
| 1461 | |
| 1462 | // We are left with FromType and ToType being the pointee types |
| 1463 | // after unwrapping the original FromType and ToType the same number |
| 1464 | // of types. If we unwrapped any pointers, and if FromType and |
| 1465 | // ToType have the same unqualified type (since we checked |
| 1466 | // qualifiers above), then this is a qualification conversion. |
| 1467 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
| 1468 | } |
| 1469 | |
| 1470 | /// Determines whether there is a user-defined conversion sequence |
| 1471 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1472 | /// ToType. If such a conversion exists, User will contain the |
| 1473 | /// user-defined conversion sequence that performs such a conversion |
| 1474 | /// and this routine will return true. Otherwise, this routine returns |
| 1475 | /// false and User is unspecified. |
| 1476 | /// |
| 1477 | /// \param AllowConversionFunctions true if the conversion should |
| 1478 | /// consider conversion functions at all. If false, only constructors |
| 1479 | /// will be considered. |
| 1480 | /// |
| 1481 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1482 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1483 | /// functions (C++0x [class.conv.fct]p2). |
| 1484 | /// |
| 1485 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1486 | /// for overload resolution. |
| 1487 | /// \param UserCast true if looking for user defined conversion for a static |
| 1488 | /// cast. |
| 1489 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1490 | UserDefinedConversionSequence& User, |
| 1491 | OverloadCandidateSet& CandidateSet, |
| 1492 | bool AllowConversionFunctions, |
| 1493 | bool AllowExplicit, |
| 1494 | bool ForceRValue, |
| 1495 | bool UserCast) { |
| 1496 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
| 1497 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1498 | // We're not going to find any constructors. |
| 1499 | } else if (CXXRecordDecl *ToRecordDecl |
| 1500 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1501 | // C++ [over.match.ctor]p1: |
| 1502 | // When objects of class type are direct-initialized (8.5), or |
| 1503 | // copy-initialized from an expression of the same or a |
| 1504 | // derived class type (8.5), overload resolution selects the |
| 1505 | // constructor. [...] For copy-initialization, the candidate |
| 1506 | // functions are all the converting constructors (12.3.1) of |
| 1507 | // that class. The argument list is the expression-list within |
| 1508 | // the parentheses of the initializer. |
| 1509 | bool SuppressUserConversions = !UserCast; |
| 1510 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1511 | IsDerivedFrom(From->getType(), ToType)) { |
| 1512 | SuppressUserConversions = false; |
| 1513 | AllowConversionFunctions = false; |
| 1514 | } |
| 1515 | |
| 1516 | DeclarationName ConstructorName |
| 1517 | = Context.DeclarationNames.getCXXConstructorName( |
| 1518 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1519 | DeclContext::lookup_iterator Con, ConEnd; |
| 1520 | for (llvm::tie(Con, ConEnd) |
| 1521 | = ToRecordDecl->lookup(ConstructorName); |
| 1522 | Con != ConEnd; ++Con) { |
| 1523 | // Find the constructor (which may be a template). |
| 1524 | CXXConstructorDecl *Constructor = 0; |
| 1525 | FunctionTemplateDecl *ConstructorTmpl |
| 1526 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1527 | if (ConstructorTmpl) |
| 1528 | Constructor |
| 1529 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1530 | else |
| 1531 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 1532 | |
| 1533 | if (!Constructor->isInvalidDecl() && |
| 1534 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 1535 | if (ConstructorTmpl) |
| 1536 | AddTemplateOverloadCandidate(ConstructorTmpl, |
| 1537 | ConstructorTmpl->getAccess(), |
| 1538 | /*ExplicitArgs*/ 0, |
| 1539 | &From, 1, CandidateSet, |
| 1540 | SuppressUserConversions, ForceRValue); |
| 1541 | else |
| 1542 | // Allow one user-defined conversion when user specifies a |
| 1543 | // From->ToType conversion via an static cast (c-style, etc). |
| 1544 | AddOverloadCandidate(Constructor, Constructor->getAccess(), |
| 1545 | &From, 1, CandidateSet, |
| 1546 | SuppressUserConversions, ForceRValue); |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | if (!AllowConversionFunctions) { |
| 1553 | // Don't allow any conversion functions to enter the overload set. |
| 1554 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1555 | PDiag(0) |
| 1556 | << From->getSourceRange())) { |
| 1557 | // No conversion functions from incomplete types. |
| 1558 | } else if (const RecordType *FromRecordType |
| 1559 | = From->getType()->getAs<RecordType>()) { |
| 1560 | if (CXXRecordDecl *FromRecordDecl |
| 1561 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1562 | // Add all of the conversion functions as candidates. |
| 1563 | const UnresolvedSetImpl *Conversions |
| 1564 | = FromRecordDecl->getVisibleConversionFunctions(); |
| 1565 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 1566 | E = Conversions->end(); I != E; ++I) { |
| 1567 | NamedDecl *D = *I; |
| 1568 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1569 | if (isa<UsingShadowDecl>(D)) |
| 1570 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1571 | |
| 1572 | CXXConversionDecl *Conv; |
| 1573 | FunctionTemplateDecl *ConvTemplate; |
| 1574 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(*I))) |
| 1575 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1576 | else |
| 1577 | Conv = dyn_cast<CXXConversionDecl>(*I); |
| 1578 | |
| 1579 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1580 | if (ConvTemplate) |
| 1581 | AddTemplateConversionCandidate(ConvTemplate, I.getAccess(), |
| 1582 | ActingContext, From, ToType, |
| 1583 | CandidateSet); |
| 1584 | else |
| 1585 | AddConversionCandidate(Conv, I.getAccess(), ActingContext, |
| 1586 | From, ToType, CandidateSet); |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | OverloadCandidateSet::iterator Best; |
| 1593 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
| 1594 | case OR_Success: |
| 1595 | // Record the standard conversion we used and the conversion function. |
| 1596 | if (CXXConstructorDecl *Constructor |
| 1597 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1598 | // C++ [over.ics.user]p1: |
| 1599 | // If the user-defined conversion is specified by a |
| 1600 | // constructor (12.3.1), the initial standard conversion |
| 1601 | // sequence converts the source type to the type required by |
| 1602 | // the argument of the constructor. |
| 1603 | // |
| 1604 | QualType ThisType = Constructor->getThisType(Context); |
| 1605 | if (Best->Conversions[0].isEllipsis()) |
| 1606 | User.EllipsisConversion = true; |
| 1607 | else { |
| 1608 | User.Before = Best->Conversions[0].Standard; |
| 1609 | User.EllipsisConversion = false; |
| 1610 | } |
| 1611 | User.ConversionFunction = Constructor; |
| 1612 | User.After.setAsIdentityConversion(); |
| 1613 | User.After.setFromType( |
| 1614 | ThisType->getAs<PointerType>()->getPointeeType()); |
| 1615 | User.After.setAllToTypes(ToType); |
| 1616 | return OR_Success; |
| 1617 | } else if (CXXConversionDecl *Conversion |
| 1618 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1619 | // C++ [over.ics.user]p1: |
| 1620 | // |
| 1621 | // [...] If the user-defined conversion is specified by a |
| 1622 | // conversion function (12.3.2), the initial standard |
| 1623 | // conversion sequence converts the source type to the |
| 1624 | // implicit object parameter of the conversion function. |
| 1625 | User.Before = Best->Conversions[0].Standard; |
| 1626 | User.ConversionFunction = Conversion; |
| 1627 | User.EllipsisConversion = false; |
| 1628 | |
| 1629 | // C++ [over.ics.user]p2: |
| 1630 | // The second standard conversion sequence converts the |
| 1631 | // result of the user-defined conversion to the target type |
| 1632 | // for the sequence. Since an implicit conversion sequence |
| 1633 | // is an initialization, the special rules for |
| 1634 | // initialization by user-defined conversion apply when |
| 1635 | // selecting the best user-defined conversion for a |
| 1636 | // user-defined conversion sequence (see 13.3.3 and |
| 1637 | // 13.3.3.1). |
| 1638 | User.After = Best->FinalConversion; |
| 1639 | return OR_Success; |
| 1640 | } else { |
| 1641 | assert(false && "Not a constructor or conversion function?"); |
| 1642 | return OR_No_Viable_Function; |
| 1643 | } |
| 1644 | |
| 1645 | case OR_No_Viable_Function: |
| 1646 | return OR_No_Viable_Function; |
| 1647 | case OR_Deleted: |
| 1648 | // No conversion here! We're done. |
| 1649 | return OR_Deleted; |
| 1650 | |
| 1651 | case OR_Ambiguous: |
| 1652 | return OR_Ambiguous; |
| 1653 | } |
| 1654 | |
| 1655 | return OR_No_Viable_Function; |
| 1656 | } |
| 1657 | |
| 1658 | bool |
| 1659 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
| 1660 | ImplicitConversionSequence ICS; |
| 1661 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
| 1662 | OverloadingResult OvResult = |
| 1663 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1664 | CandidateSet, true, false, false); |
| 1665 | if (OvResult == OR_Ambiguous) |
| 1666 | Diag(From->getSourceRange().getBegin(), |
| 1667 | diag::err_typecheck_ambiguous_condition) |
| 1668 | << From->getType() << ToType << From->getSourceRange(); |
| 1669 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1670 | Diag(From->getSourceRange().getBegin(), |
| 1671 | diag::err_typecheck_nonviable_condition) |
| 1672 | << From->getType() << ToType << From->getSourceRange(); |
| 1673 | else |
| 1674 | return false; |
| 1675 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
| 1676 | return true; |
| 1677 | } |
| 1678 | |
| 1679 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1680 | /// conversion sequences to determine whether one is better than the |
| 1681 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
| 1682 | ImplicitConversionSequence::CompareKind |
| 1683 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1684 | const ImplicitConversionSequence& ICS2) |
| 1685 | { |
| 1686 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1687 | // conversion sequences (as defined in 13.3.3.1) |
| 1688 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1689 | // conversion sequence than a user-defined conversion sequence or |
| 1690 | // an ellipsis conversion sequence, and |
| 1691 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1692 | // conversion sequence than an ellipsis conversion sequence |
| 1693 | // (13.3.3.1.3). |
| 1694 | // |
| 1695 | // C++0x [over.best.ics]p10: |
| 1696 | // For the purpose of ranking implicit conversion sequences as |
| 1697 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1698 | // treated as a user-defined sequence that is indistinguishable |
| 1699 | // from any other user-defined conversion sequence. |
| 1700 | if (ICS1.getKind() < ICS2.getKind()) { |
| 1701 | if (!(ICS1.isUserDefined() && ICS2.isAmbiguous())) |
| 1702 | return ImplicitConversionSequence::Better; |
| 1703 | } else if (ICS2.getKind() < ICS1.getKind()) { |
| 1704 | if (!(ICS2.isUserDefined() && ICS1.isAmbiguous())) |
| 1705 | return ImplicitConversionSequence::Worse; |
| 1706 | } |
| 1707 | |
| 1708 | if (ICS1.isAmbiguous() || ICS2.isAmbiguous()) |
| 1709 | return ImplicitConversionSequence::Indistinguishable; |
| 1710 | |
| 1711 | // Two implicit conversion sequences of the same form are |
| 1712 | // indistinguishable conversion sequences unless one of the |
| 1713 | // following rules apply: (C++ 13.3.3.2p3): |
| 1714 | if (ICS1.isStandard()) |
| 1715 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
| 1716 | else if (ICS1.isUserDefined()) { |
| 1717 | // User-defined conversion sequence U1 is a better conversion |
| 1718 | // sequence than another user-defined conversion sequence U2 if |
| 1719 | // they contain the same user-defined conversion function or |
| 1720 | // constructor and if the second standard conversion sequence of |
| 1721 | // U1 is better than the second standard conversion sequence of |
| 1722 | // U2 (C++ 13.3.3.2p3). |
| 1723 | if (ICS1.UserDefined.ConversionFunction == |
| 1724 | ICS2.UserDefined.ConversionFunction) |
| 1725 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1726 | ICS2.UserDefined.After); |
| 1727 | } |
| 1728 | |
| 1729 | return ImplicitConversionSequence::Indistinguishable; |
| 1730 | } |
| 1731 | |
| 1732 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 1733 | // determine if one is a proper subset of the other. |
| 1734 | static ImplicitConversionSequence::CompareKind |
| 1735 | compareStandardConversionSubsets(ASTContext &Context, |
| 1736 | const StandardConversionSequence& SCS1, |
| 1737 | const StandardConversionSequence& SCS2) { |
| 1738 | ImplicitConversionSequence::CompareKind Result |
| 1739 | = ImplicitConversionSequence::Indistinguishable; |
| 1740 | |
| 1741 | if (SCS1.Second != SCS2.Second) { |
| 1742 | if (SCS1.Second == ICK_Identity) |
| 1743 | Result = ImplicitConversionSequence::Better; |
| 1744 | else if (SCS2.Second == ICK_Identity) |
| 1745 | Result = ImplicitConversionSequence::Worse; |
| 1746 | else |
| 1747 | return ImplicitConversionSequence::Indistinguishable; |
| 1748 | } else if (!Context.hasSameType(SCS1.getToType(1), SCS2.getToType(1))) |
| 1749 | return ImplicitConversionSequence::Indistinguishable; |
| 1750 | |
| 1751 | if (SCS1.Third == SCS2.Third) { |
| 1752 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 1753 | : ImplicitConversionSequence::Indistinguishable; |
| 1754 | } |
| 1755 | |
| 1756 | if (SCS1.Third == ICK_Identity) |
| 1757 | return Result == ImplicitConversionSequence::Worse |
| 1758 | ? ImplicitConversionSequence::Indistinguishable |
| 1759 | : ImplicitConversionSequence::Better; |
| 1760 | |
| 1761 | if (SCS2.Third == ICK_Identity) |
| 1762 | return Result == ImplicitConversionSequence::Better |
| 1763 | ? ImplicitConversionSequence::Indistinguishable |
| 1764 | : ImplicitConversionSequence::Worse; |
| 1765 | |
| 1766 | return ImplicitConversionSequence::Indistinguishable; |
| 1767 | } |
| 1768 | |
| 1769 | /// CompareStandardConversionSequences - Compare two standard |
| 1770 | /// conversion sequences to determine whether one is better than the |
| 1771 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
| 1772 | ImplicitConversionSequence::CompareKind |
| 1773 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1774 | const StandardConversionSequence& SCS2) |
| 1775 | { |
| 1776 | // Standard conversion sequence S1 is a better conversion sequence |
| 1777 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1778 | |
| 1779 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1780 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1781 | // excluding any Lvalue Transformation; the identity conversion |
| 1782 | // sequence is considered to be a subsequence of any |
| 1783 | // non-identity conversion sequence) or, if not that, |
| 1784 | if (ImplicitConversionSequence::CompareKind CK |
| 1785 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 1786 | return CK; |
| 1787 | |
| 1788 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1789 | // defined below), or, if not that, |
| 1790 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1791 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1792 | if (Rank1 < Rank2) |
| 1793 | return ImplicitConversionSequence::Better; |
| 1794 | else if (Rank2 < Rank1) |
| 1795 | return ImplicitConversionSequence::Worse; |
| 1796 | |
| 1797 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1798 | // are indistinguishable unless one of the following rules |
| 1799 | // applies: |
| 1800 | |
| 1801 | // A conversion that is not a conversion of a pointer, or |
| 1802 | // pointer to member, to bool is better than another conversion |
| 1803 | // that is such a conversion. |
| 1804 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1805 | return SCS2.isPointerConversionToBool() |
| 1806 | ? ImplicitConversionSequence::Better |
| 1807 | : ImplicitConversionSequence::Worse; |
| 1808 | |
| 1809 | // C++ [over.ics.rank]p4b2: |
| 1810 | // |
| 1811 | // If class B is derived directly or indirectly from class A, |
| 1812 | // conversion of B* to A* is better than conversion of B* to |
| 1813 | // void*, and conversion of A* to void* is better than conversion |
| 1814 | // of B* to void*. |
| 1815 | bool SCS1ConvertsToVoid |
| 1816 | = SCS1.isPointerConversionToVoidPointer(Context); |
| 1817 | bool SCS2ConvertsToVoid |
| 1818 | = SCS2.isPointerConversionToVoidPointer(Context); |
| 1819 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1820 | // Exactly one of the conversion sequences is a conversion to |
| 1821 | // a void pointer; it's the worse conversion. |
| 1822 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1823 | : ImplicitConversionSequence::Worse; |
| 1824 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1825 | // Neither conversion sequence converts to a void pointer; compare |
| 1826 | // their derived-to-base conversions. |
| 1827 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1828 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1829 | return DerivedCK; |
| 1830 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1831 | // Both conversion sequences are conversions to void |
| 1832 | // pointers. Compare the source types to determine if there's an |
| 1833 | // inheritance relationship in their sources. |
| 1834 | QualType FromType1 = SCS1.getFromType(); |
| 1835 | QualType FromType2 = SCS2.getFromType(); |
| 1836 | |
| 1837 | // Adjust the types we're converting from via the array-to-pointer |
| 1838 | // conversion, if we need to. |
| 1839 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1840 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1841 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1842 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1843 | |
| 1844 | QualType FromPointee1 |
| 1845 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1846 | QualType FromPointee2 |
| 1847 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1848 | |
| 1849 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1850 | return ImplicitConversionSequence::Better; |
| 1851 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1852 | return ImplicitConversionSequence::Worse; |
| 1853 | |
| 1854 | // Objective-C++: If one interface is more specific than the |
| 1855 | // other, it is the better one. |
| 1856 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1857 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1858 | if (FromIface1 && FromIface1) { |
| 1859 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1860 | return ImplicitConversionSequence::Better; |
| 1861 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1862 | return ImplicitConversionSequence::Worse; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1867 | // bullet 3). |
| 1868 | if (ImplicitConversionSequence::CompareKind QualCK |
| 1869 | = CompareQualificationConversions(SCS1, SCS2)) |
| 1870 | return QualCK; |
| 1871 | |
| 1872 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
| 1873 | // C++0x [over.ics.rank]p3b4: |
| 1874 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1875 | // implicit object parameter of a non-static member function declared |
| 1876 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1877 | // rvalue and S2 binds an lvalue reference. |
| 1878 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1879 | // or if the member function in this case has a ref qualifier. |
| 1880 | // (Of course, we don't have ref qualifiers yet.) |
| 1881 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1882 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1883 | : ImplicitConversionSequence::Worse; |
| 1884 | |
| 1885 | // C++ [over.ics.rank]p3b4: |
| 1886 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1887 | // which the references refer are the same type except for |
| 1888 | // top-level cv-qualifiers, and the type to which the reference |
| 1889 | // initialized by S2 refers is more cv-qualified than the type |
| 1890 | // to which the reference initialized by S1 refers. |
| 1891 | QualType T1 = SCS1.getToType(2); |
| 1892 | QualType T2 = SCS2.getToType(2); |
| 1893 | T1 = Context.getCanonicalType(T1); |
| 1894 | T2 = Context.getCanonicalType(T2); |
| 1895 | Qualifiers T1Quals, T2Quals; |
| 1896 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1897 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1898 | if (UnqualT1 == UnqualT2) { |
| 1899 | // If the type is an array type, promote the element qualifiers to the type |
| 1900 | // for comparison. |
| 1901 | if (isa<ArrayType>(T1) && T1Quals) |
| 1902 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1903 | if (isa<ArrayType>(T2) && T2Quals) |
| 1904 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1905 | if (T2.isMoreQualifiedThan(T1)) |
| 1906 | return ImplicitConversionSequence::Better; |
| 1907 | else if (T1.isMoreQualifiedThan(T2)) |
| 1908 | return ImplicitConversionSequence::Worse; |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | return ImplicitConversionSequence::Indistinguishable; |
| 1913 | } |
| 1914 | |
| 1915 | /// CompareQualificationConversions - Compares two standard conversion |
| 1916 | /// sequences to determine whether they can be ranked based on their |
| 1917 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1918 | ImplicitConversionSequence::CompareKind |
| 1919 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
| 1920 | const StandardConversionSequence& SCS2) { |
| 1921 | // C++ 13.3.3.2p3: |
| 1922 | // -- S1 and S2 differ only in their qualification conversion and |
| 1923 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1924 | // cv-qualification signature of type T1 is a proper subset of |
| 1925 | // the cv-qualification signature of type T2, and S1 is not the |
| 1926 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1927 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1928 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1929 | return ImplicitConversionSequence::Indistinguishable; |
| 1930 | |
| 1931 | // FIXME: the example in the standard doesn't use a qualification |
| 1932 | // conversion (!) |
| 1933 | QualType T1 = SCS1.getToType(2); |
| 1934 | QualType T2 = SCS2.getToType(2); |
| 1935 | T1 = Context.getCanonicalType(T1); |
| 1936 | T2 = Context.getCanonicalType(T2); |
| 1937 | Qualifiers T1Quals, T2Quals; |
| 1938 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1939 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1940 | |
| 1941 | // If the types are the same, we won't learn anything by unwrapped |
| 1942 | // them. |
| 1943 | if (UnqualT1 == UnqualT2) |
| 1944 | return ImplicitConversionSequence::Indistinguishable; |
| 1945 | |
| 1946 | // If the type is an array type, promote the element qualifiers to the type |
| 1947 | // for comparison. |
| 1948 | if (isa<ArrayType>(T1) && T1Quals) |
| 1949 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1950 | if (isa<ArrayType>(T2) && T2Quals) |
| 1951 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1952 | |
| 1953 | ImplicitConversionSequence::CompareKind Result |
| 1954 | = ImplicitConversionSequence::Indistinguishable; |
| 1955 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1956 | // Within each iteration of the loop, we check the qualifiers to |
| 1957 | // determine if this still looks like a qualification |
| 1958 | // conversion. Then, if all is well, we unwrap one more level of |
| 1959 | // pointers or pointers-to-members and do it all again |
| 1960 | // until there are no more pointers or pointers-to-members left |
| 1961 | // to unwrap. This essentially mimics what |
| 1962 | // IsQualificationConversion does, but here we're checking for a |
| 1963 | // strict subset of qualifiers. |
| 1964 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1965 | // The qualifiers are the same, so this doesn't tell us anything |
| 1966 | // about how the sequences rank. |
| 1967 | ; |
| 1968 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1969 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1970 | if (Result == ImplicitConversionSequence::Worse) |
| 1971 | // Neither has qualifiers that are a subset of the other's |
| 1972 | // qualifiers. |
| 1973 | return ImplicitConversionSequence::Indistinguishable; |
| 1974 | |
| 1975 | Result = ImplicitConversionSequence::Better; |
| 1976 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1977 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1978 | if (Result == ImplicitConversionSequence::Better) |
| 1979 | // Neither has qualifiers that are a subset of the other's |
| 1980 | // qualifiers. |
| 1981 | return ImplicitConversionSequence::Indistinguishable; |
| 1982 | |
| 1983 | Result = ImplicitConversionSequence::Worse; |
| 1984 | } else { |
| 1985 | // Qualifiers are disjoint. |
| 1986 | return ImplicitConversionSequence::Indistinguishable; |
| 1987 | } |
| 1988 | |
| 1989 | // If the types after this point are equivalent, we're done. |
| 1990 | if (Context.hasSameUnqualifiedType(T1, T2)) |
| 1991 | break; |
| 1992 | } |
| 1993 | |
| 1994 | // Check that the winning standard conversion sequence isn't using |
| 1995 | // the deprecated string literal array to pointer conversion. |
| 1996 | switch (Result) { |
| 1997 | case ImplicitConversionSequence::Better: |
| 1998 | if (SCS1.Deprecated) |
| 1999 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2000 | break; |
| 2001 | |
| 2002 | case ImplicitConversionSequence::Indistinguishable: |
| 2003 | break; |
| 2004 | |
| 2005 | case ImplicitConversionSequence::Worse: |
| 2006 | if (SCS2.Deprecated) |
| 2007 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2008 | break; |
| 2009 | } |
| 2010 | |
| 2011 | return Result; |
| 2012 | } |
| 2013 | |
| 2014 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2015 | /// sequences to determine whether they can be ranked based on their |
| 2016 | /// various kinds of derived-to-base conversions (C++ |
| 2017 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2018 | /// conversions between Objective-C interface types. |
| 2019 | ImplicitConversionSequence::CompareKind |
| 2020 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2021 | const StandardConversionSequence& SCS2) { |
| 2022 | QualType FromType1 = SCS1.getFromType(); |
| 2023 | QualType ToType1 = SCS1.getToType(1); |
| 2024 | QualType FromType2 = SCS2.getFromType(); |
| 2025 | QualType ToType2 = SCS2.getToType(1); |
| 2026 | |
| 2027 | // Adjust the types we're converting from via the array-to-pointer |
| 2028 | // conversion, if we need to. |
| 2029 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2030 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2031 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2032 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2033 | |
| 2034 | // Canonicalize all of the types. |
| 2035 | FromType1 = Context.getCanonicalType(FromType1); |
| 2036 | ToType1 = Context.getCanonicalType(ToType1); |
| 2037 | FromType2 = Context.getCanonicalType(FromType2); |
| 2038 | ToType2 = Context.getCanonicalType(ToType2); |
| 2039 | |
| 2040 | // C++ [over.ics.rank]p4b3: |
| 2041 | // |
| 2042 | // If class B is derived directly or indirectly from class A and |
| 2043 | // class C is derived directly or indirectly from B, |
| 2044 | // |
| 2045 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2046 | // interfaces. |
| 2047 | |
| 2048 | // Compare based on pointer conversions. |
| 2049 | if (SCS1.Second == ICK_Pointer_Conversion && |
| 2050 | SCS2.Second == ICK_Pointer_Conversion && |
| 2051 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2052 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2053 | ToType1->isPointerType() && ToType2->isPointerType()) { |
| 2054 | QualType FromPointee1 |
| 2055 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2056 | QualType ToPointee1 |
| 2057 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2058 | QualType FromPointee2 |
| 2059 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2060 | QualType ToPointee2 |
| 2061 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2062 | |
| 2063 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2064 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2065 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2066 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
| 2067 | |
| 2068 | // -- conversion of C* to B* is better than conversion of C* to A*, |
| 2069 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2070 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2071 | return ImplicitConversionSequence::Better; |
| 2072 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2073 | return ImplicitConversionSequence::Worse; |
| 2074 | |
| 2075 | if (ToIface1 && ToIface2) { |
| 2076 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2077 | return ImplicitConversionSequence::Better; |
| 2078 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2079 | return ImplicitConversionSequence::Worse; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2084 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2085 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2086 | return ImplicitConversionSequence::Better; |
| 2087 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2088 | return ImplicitConversionSequence::Worse; |
| 2089 | |
| 2090 | if (FromIface1 && FromIface2) { |
| 2091 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2092 | return ImplicitConversionSequence::Better; |
| 2093 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2094 | return ImplicitConversionSequence::Worse; |
| 2095 | } |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | // Compare based on reference bindings. |
| 2100 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 2101 | SCS1.Second == ICK_Derived_To_Base) { |
| 2102 | // -- binding of an expression of type C to a reference of type |
| 2103 | // B& is better than binding an expression of type C to a |
| 2104 | // reference of type A&, |
| 2105 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2106 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2107 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2108 | return ImplicitConversionSequence::Better; |
| 2109 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2110 | return ImplicitConversionSequence::Worse; |
| 2111 | } |
| 2112 | |
| 2113 | // -- binding of an expression of type B to a reference of type |
| 2114 | // A& is better than binding an expression of type C to a |
| 2115 | // reference of type A&, |
| 2116 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2117 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2118 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2119 | return ImplicitConversionSequence::Better; |
| 2120 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2121 | return ImplicitConversionSequence::Worse; |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | // Ranking of member-pointer types. |
| 2126 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2127 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2128 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2129 | const MemberPointerType * FromMemPointer1 = |
| 2130 | FromType1->getAs<MemberPointerType>(); |
| 2131 | const MemberPointerType * ToMemPointer1 = |
| 2132 | ToType1->getAs<MemberPointerType>(); |
| 2133 | const MemberPointerType * FromMemPointer2 = |
| 2134 | FromType2->getAs<MemberPointerType>(); |
| 2135 | const MemberPointerType * ToMemPointer2 = |
| 2136 | ToType2->getAs<MemberPointerType>(); |
| 2137 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2138 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2139 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2140 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2141 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2142 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2143 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2144 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
| 2145 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
| 2146 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2147 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2148 | return ImplicitConversionSequence::Worse; |
| 2149 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2150 | return ImplicitConversionSequence::Better; |
| 2151 | } |
| 2152 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2153 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2154 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2155 | return ImplicitConversionSequence::Better; |
| 2156 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2157 | return ImplicitConversionSequence::Worse; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 2162 | SCS1.Second == ICK_Derived_To_Base) { |
| 2163 | // -- conversion of C to B is better than conversion of C to A, |
| 2164 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2165 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2166 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2167 | return ImplicitConversionSequence::Better; |
| 2168 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2169 | return ImplicitConversionSequence::Worse; |
| 2170 | } |
| 2171 | |
| 2172 | // -- conversion of B to A is better than conversion of C to A. |
| 2173 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2174 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2175 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2176 | return ImplicitConversionSequence::Better; |
| 2177 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2178 | return ImplicitConversionSequence::Worse; |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | return ImplicitConversionSequence::Indistinguishable; |
| 2183 | } |
| 2184 | |
| 2185 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2186 | /// ToType from the expression From. Return the implicit conversion |
| 2187 | /// sequence required to pass this argument, which may be a bad |
| 2188 | /// conversion sequence (meaning that the argument cannot be passed to |
| 2189 | /// a parameter of this type). If @p SuppressUserConversions, then we |
| 2190 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2191 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
| 2192 | ImplicitConversionSequence |
| 2193 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
| 2194 | bool SuppressUserConversions, bool ForceRValue, |
| 2195 | bool InOverloadResolution) { |
| 2196 | if (ToType->isReferenceType()) { |
| 2197 | ImplicitConversionSequence ICS; |
| 2198 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
| 2199 | CheckReferenceInit(From, ToType, |
| 2200 | /*FIXME:*/From->getLocStart(), |
| 2201 | SuppressUserConversions, |
| 2202 | /*AllowExplicit=*/false, |
| 2203 | ForceRValue, |
| 2204 | &ICS); |
| 2205 | return ICS; |
| 2206 | } else { |
| 2207 | return TryImplicitConversion(From, ToType, |
| 2208 | SuppressUserConversions, |
| 2209 | /*AllowExplicit=*/false, |
| 2210 | ForceRValue, |
| 2211 | InOverloadResolution); |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2216 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2217 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2218 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2219 | /// differently in C++0x for this case. |
| 2220 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
| 2221 | AssignmentAction Action, bool Elidable) { |
| 2222 | if (!getLangOptions().CPlusPlus) { |
| 2223 | // In C, argument passing is the same as performing an assignment. |
| 2224 | QualType FromType = From->getType(); |
| 2225 | |
| 2226 | AssignConvertType ConvTy = |
| 2227 | CheckSingleAssignmentConstraints(ToType, From); |
| 2228 | if (ConvTy != Compatible && |
| 2229 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2230 | ConvTy = Compatible; |
| 2231 | |
| 2232 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 2233 | FromType, From, Action); |
| 2234 | } |
| 2235 | |
| 2236 | if (ToType->isReferenceType()) |
| 2237 | return CheckReferenceInit(From, ToType, |
| 2238 | /*FIXME:*/From->getLocStart(), |
| 2239 | /*SuppressUserConversions=*/false, |
| 2240 | /*AllowExplicit=*/false, |
| 2241 | /*ForceRValue=*/false); |
| 2242 | |
| 2243 | if (!PerformImplicitConversion(From, ToType, Action, |
| 2244 | /*AllowExplicit=*/false, Elidable)) |
| 2245 | return false; |
| 2246 | if (!DiagnoseMultipleUserDefinedConversion(From, ToType)) |
| 2247 | return Diag(From->getSourceRange().getBegin(), |
| 2248 | diag::err_typecheck_convert_incompatible) |
| 2249 | << ToType << From->getType() << Action << From->getSourceRange(); |
| 2250 | return true; |
| 2251 | } |
| 2252 | |
| 2253 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2254 | /// parameter of the given member function (@c Method) from the |
| 2255 | /// expression @p From. |
| 2256 | ImplicitConversionSequence |
| 2257 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
| 2258 | CXXMethodDecl *Method, |
| 2259 | CXXRecordDecl *ActingContext) { |
| 2260 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
| 2261 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2262 | // const volatile object. |
| 2263 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2264 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2265 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
| 2266 | |
| 2267 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2268 | // to exit early. |
| 2269 | ImplicitConversionSequence ICS; |
| 2270 | ICS.Standard.setAsIdentityConversion(); |
| 2271 | ICS.setBad(); |
| 2272 | |
| 2273 | // We need to have an object of class type. |
| 2274 | QualType FromType = OrigFromType; |
| 2275 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
| 2276 | FromType = PT->getPointeeType(); |
| 2277 | |
| 2278 | assert(FromType->isRecordType()); |
| 2279 | |
| 2280 | // The implicit object parameter is has the type "reference to cv X", |
| 2281 | // where X is the class of which the function is a member |
| 2282 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2283 | // conversion sequence for the argument, we are not allowed to |
| 2284 | // create temporaries or perform user-defined conversions |
| 2285 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2286 | // reference binding here, that allows class rvalues to bind to |
| 2287 | // non-constant references. |
| 2288 | |
| 2289 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2290 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2291 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 2292 | if (ImplicitParamType.getCVRQualifiers() |
| 2293 | != FromTypeCanon.getLocalCVRQualifiers() && |
| 2294 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
| 2295 | ICS.Bad.init(BadConversionSequence::bad_qualifiers, |
| 2296 | OrigFromType, ImplicitParamType); |
| 2297 | return ICS; |
| 2298 | } |
| 2299 | |
| 2300 | // Check that we have either the same type or a derived type. It |
| 2301 | // affects the conversion rank. |
| 2302 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 2303 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) |
| 2304 | ICS.Standard.Second = ICK_Identity; |
| 2305 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2306 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2307 | else { |
| 2308 | ICS.Bad.init(BadConversionSequence::unrelated_class, FromType, ImplicitParamType); |
| 2309 | return ICS; |
| 2310 | } |
| 2311 | |
| 2312 | // Success. Mark this as a reference binding. |
| 2313 | ICS.setStandard(); |
| 2314 | ICS.Standard.setFromType(FromType); |
| 2315 | ICS.Standard.setAllToTypes(ImplicitParamType); |
| 2316 | ICS.Standard.ReferenceBinding = true; |
| 2317 | ICS.Standard.DirectBinding = true; |
| 2318 | ICS.Standard.RRefBinding = false; |
| 2319 | return ICS; |
| 2320 | } |
| 2321 | |
| 2322 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2323 | /// the implicit object parameter for the given Method with the given |
| 2324 | /// expression. |
| 2325 | bool |
| 2326 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
| 2327 | QualType FromRecordType, DestType; |
| 2328 | QualType ImplicitParamRecordType = |
| 2329 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
| 2330 | |
| 2331 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
| 2332 | FromRecordType = PT->getPointeeType(); |
| 2333 | DestType = Method->getThisType(Context); |
| 2334 | } else { |
| 2335 | FromRecordType = From->getType(); |
| 2336 | DestType = ImplicitParamRecordType; |
| 2337 | } |
| 2338 | |
| 2339 | // Note that we always use the true parent context when performing |
| 2340 | // the actual argument initialization. |
| 2341 | ImplicitConversionSequence ICS |
| 2342 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2343 | Method->getParent()); |
| 2344 | if (ICS.isBad()) |
| 2345 | return Diag(From->getSourceRange().getBegin(), |
| 2346 | diag::err_implicit_object_parameter_init) |
| 2347 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
| 2348 | |
| 2349 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
| 2350 | CheckDerivedToBaseConversion(FromRecordType, |
| 2351 | ImplicitParamRecordType, |
| 2352 | From->getSourceRange().getBegin(), |
| 2353 | From->getSourceRange())) |
| 2354 | return true; |
| 2355 | |
| 2356 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 2357 | /*isLvalue=*/true); |
| 2358 | return false; |
| 2359 | } |
| 2360 | |
| 2361 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2362 | /// expression From to bool (C++0x [conv]p3). |
| 2363 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
| 2364 | return TryImplicitConversion(From, Context.BoolTy, |
| 2365 | // FIXME: Are these flags correct? |
| 2366 | /*SuppressUserConversions=*/false, |
| 2367 | /*AllowExplicit=*/true, |
| 2368 | /*ForceRValue=*/false, |
| 2369 | /*InOverloadResolution=*/false); |
| 2370 | } |
| 2371 | |
| 2372 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2373 | /// of the expression From to bool (C++0x [conv]p3). |
| 2374 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2375 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2376 | if (!ICS.isBad()) |
| 2377 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
| 2378 | |
| 2379 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
| 2380 | return Diag(From->getSourceRange().getBegin(), |
| 2381 | diag::err_typecheck_bool_condition) |
| 2382 | << From->getType() << From->getSourceRange(); |
| 2383 | return true; |
| 2384 | } |
| 2385 | |
| 2386 | /// AddOverloadCandidate - Adds the given function to the set of |
| 2387 | /// candidate functions, using the given function call arguments. If |
| 2388 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2389 | /// conversions via constructors or conversion operators. |
| 2390 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2391 | /// hacky way to implement the overloading rules for elidable copy |
| 2392 | /// initialization in C++0x (C++0x 12.8p15). |
| 2393 | /// |
| 2394 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2395 | /// based on an incomplete set of function arguments. This feature is used by |
| 2396 | /// code completion. |
| 2397 | void |
| 2398 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
| 2399 | AccessSpecifier Access, |
| 2400 | Expr **Args, unsigned NumArgs, |
| 2401 | OverloadCandidateSet& CandidateSet, |
| 2402 | bool SuppressUserConversions, |
| 2403 | bool ForceRValue, |
| 2404 | bool PartialOverloading) { |
| 2405 | const FunctionProtoType* Proto |
| 2406 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
| 2407 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
| 2408 | assert(!Function->getDescribedFunctionTemplate() && |
| 2409 | "Use AddTemplateOverloadCandidate for function templates"); |
| 2410 | |
| 2411 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2412 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2413 | // If we get here, it's because we're calling a member function |
| 2414 | // that is named without a member access expression (e.g., |
| 2415 | // "this->f") that was either written explicitly or created |
| 2416 | // implicitly. This can happen with a qualified call to a member |
| 2417 | // function, e.g., X::f(). We use an empty type for the implied |
| 2418 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2419 | // is irrelevant. |
| 2420 | AddMethodCandidate(Method, Access, Method->getParent(), |
| 2421 | QualType(), Args, NumArgs, CandidateSet, |
| 2422 | SuppressUserConversions, ForceRValue); |
| 2423 | return; |
| 2424 | } |
| 2425 | // We treat a constructor like a non-member function, since its object |
| 2426 | // argument doesn't participate in overload resolution. |
| 2427 | } |
| 2428 | |
| 2429 | if (!CandidateSet.isNewCandidate(Function)) |
| 2430 | return; |
| 2431 | |
| 2432 | // Overload resolution is always an unevaluated context. |
| 2433 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2434 | |
| 2435 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2436 | // C++ [class.copy]p3: |
| 2437 | // A member function template is never instantiated to perform the copy |
| 2438 | // of a class object to an object of its class type. |
| 2439 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2440 | if (NumArgs == 1 && |
| 2441 | Constructor->isCopyConstructorLikeSpecialization() && |
| 2442 | Context.hasSameUnqualifiedType(ClassType, Args[0]->getType())) |
| 2443 | return; |
| 2444 | } |
| 2445 | |
| 2446 | // Add this candidate |
| 2447 | CandidateSet.push_back(OverloadCandidate()); |
| 2448 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2449 | Candidate.Function = Function; |
| 2450 | Candidate.Access = Access; |
| 2451 | Candidate.Viable = true; |
| 2452 | Candidate.IsSurrogate = false; |
| 2453 | Candidate.IgnoreObjectArgument = false; |
| 2454 | |
| 2455 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2456 | |
| 2457 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2458 | // parameters is viable only if it has an ellipsis in its parameter |
| 2459 | // list (8.3.5). |
| 2460 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2461 | !Proto->isVariadic()) { |
| 2462 | Candidate.Viable = false; |
| 2463 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
| 2464 | return; |
| 2465 | } |
| 2466 | |
| 2467 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2468 | // is viable only if the (m+1)st parameter has a default argument |
| 2469 | // (8.3.6). For the purposes of overload resolution, the |
| 2470 | // parameter list is truncated on the right, so that there are |
| 2471 | // exactly m parameters. |
| 2472 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 2473 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
| 2474 | // Not enough arguments. |
| 2475 | Candidate.Viable = false; |
| 2476 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
| 2477 | return; |
| 2478 | } |
| 2479 | |
| 2480 | // Determine the implicit conversion sequences for each of the |
| 2481 | // arguments. |
| 2482 | Candidate.Conversions.resize(NumArgs); |
| 2483 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2484 | if (ArgIdx < NumArgsInProto) { |
| 2485 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2486 | // exist for each argument an implicit conversion sequence |
| 2487 | // (13.3.3.1) that converts that argument to the corresponding |
| 2488 | // parameter of F. |
| 2489 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2490 | Candidate.Conversions[ArgIdx] |
| 2491 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 2492 | SuppressUserConversions, ForceRValue, |
| 2493 | /*InOverloadResolution=*/true); |
| 2494 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2495 | Candidate.Viable = false; |
| 2496 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 2497 | break; |
| 2498 | } |
| 2499 | } else { |
| 2500 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2501 | // argument for which there is no corresponding parameter is |
| 2502 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2503 | Candidate.Conversions[ArgIdx].setEllipsis(); |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | /// \brief Add all of the function declarations in the given function set to |
| 2509 | /// the overload canddiate set. |
| 2510 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
| 2511 | Expr **Args, unsigned NumArgs, |
| 2512 | OverloadCandidateSet& CandidateSet, |
| 2513 | bool SuppressUserConversions) { |
| 2514 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
| 2515 | // FIXME: using declarations |
| 2516 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2517 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2518 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getAccess(), |
| 2519 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2520 | Args[0]->getType(), Args + 1, NumArgs - 1, |
| 2521 | CandidateSet, SuppressUserConversions); |
| 2522 | else |
| 2523 | AddOverloadCandidate(FD, AS_none, Args, NumArgs, CandidateSet, |
| 2524 | SuppressUserConversions); |
| 2525 | } else { |
| 2526 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2527 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2528 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2529 | AddMethodTemplateCandidate(FunTmpl, F.getAccess(), |
| 2530 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
| 2531 | /*FIXME: explicit args */ 0, |
| 2532 | Args[0]->getType(), Args + 1, NumArgs - 1, |
| 2533 | CandidateSet, |
| 2534 | SuppressUserConversions); |
| 2535 | else |
| 2536 | AddTemplateOverloadCandidate(FunTmpl, AS_none, |
| 2537 | /*FIXME: explicit args */ 0, |
| 2538 | Args, NumArgs, CandidateSet, |
| 2539 | SuppressUserConversions); |
| 2540 | } |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2545 | /// method) as a method candidate to the given overload set. |
| 2546 | void Sema::AddMethodCandidate(NamedDecl *Decl, |
| 2547 | AccessSpecifier Access, |
| 2548 | QualType ObjectType, |
| 2549 | Expr **Args, unsigned NumArgs, |
| 2550 | OverloadCandidateSet& CandidateSet, |
| 2551 | bool SuppressUserConversions, bool ForceRValue) { |
| 2552 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
| 2553 | |
| 2554 | if (isa<UsingShadowDecl>(Decl)) |
| 2555 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2556 | |
| 2557 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2558 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2559 | "Expected a member function template"); |
| 2560 | AddMethodTemplateCandidate(TD, Access, ActingContext, /*ExplicitArgs*/ 0, |
| 2561 | ObjectType, Args, NumArgs, |
| 2562 | CandidateSet, |
| 2563 | SuppressUserConversions, |
| 2564 | ForceRValue); |
| 2565 | } else { |
| 2566 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), Access, ActingContext, |
| 2567 | ObjectType, Args, NumArgs, |
| 2568 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2573 | /// of candidate functions, using the given function call arguments |
| 2574 | /// and the object argument (@c Object). For example, in a call |
| 2575 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2576 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2577 | /// allow user-defined conversions via constructors or conversion |
| 2578 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2579 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2580 | /// initialization in C++0x (C++0x 12.8p15). |
| 2581 | void |
| 2582 | Sema::AddMethodCandidate(CXXMethodDecl *Method, AccessSpecifier Access, |
| 2583 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 2584 | Expr **Args, unsigned NumArgs, |
| 2585 | OverloadCandidateSet& CandidateSet, |
| 2586 | bool SuppressUserConversions, bool ForceRValue) { |
| 2587 | const FunctionProtoType* Proto |
| 2588 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
| 2589 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
| 2590 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2591 | "Use AddOverloadCandidate for constructors"); |
| 2592 | |
| 2593 | if (!CandidateSet.isNewCandidate(Method)) |
| 2594 | return; |
| 2595 | |
| 2596 | // Overload resolution is always an unevaluated context. |
| 2597 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2598 | |
| 2599 | // Add this candidate |
| 2600 | CandidateSet.push_back(OverloadCandidate()); |
| 2601 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2602 | Candidate.Function = Method; |
| 2603 | Candidate.Access = Access; |
| 2604 | Candidate.IsSurrogate = false; |
| 2605 | Candidate.IgnoreObjectArgument = false; |
| 2606 | |
| 2607 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2608 | |
| 2609 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2610 | // parameters is viable only if it has an ellipsis in its parameter |
| 2611 | // list (8.3.5). |
| 2612 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2613 | Candidate.Viable = false; |
| 2614 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
| 2615 | return; |
| 2616 | } |
| 2617 | |
| 2618 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2619 | // is viable only if the (m+1)st parameter has a default argument |
| 2620 | // (8.3.6). For the purposes of overload resolution, the |
| 2621 | // parameter list is truncated on the right, so that there are |
| 2622 | // exactly m parameters. |
| 2623 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2624 | if (NumArgs < MinRequiredArgs) { |
| 2625 | // Not enough arguments. |
| 2626 | Candidate.Viable = false; |
| 2627 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
| 2628 | return; |
| 2629 | } |
| 2630 | |
| 2631 | Candidate.Viable = true; |
| 2632 | Candidate.Conversions.resize(NumArgs + 1); |
| 2633 | |
| 2634 | if (Method->isStatic() || ObjectType.isNull()) |
| 2635 | // The implicit object argument is ignored. |
| 2636 | Candidate.IgnoreObjectArgument = true; |
| 2637 | else { |
| 2638 | // Determine the implicit conversion sequence for the object |
| 2639 | // parameter. |
| 2640 | Candidate.Conversions[0] |
| 2641 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
| 2642 | if (Candidate.Conversions[0].isBad()) { |
| 2643 | Candidate.Viable = false; |
| 2644 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 2645 | return; |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | // Determine the implicit conversion sequences for each of the |
| 2650 | // arguments. |
| 2651 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2652 | if (ArgIdx < NumArgsInProto) { |
| 2653 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2654 | // exist for each argument an implicit conversion sequence |
| 2655 | // (13.3.3.1) that converts that argument to the corresponding |
| 2656 | // parameter of F. |
| 2657 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2658 | Candidate.Conversions[ArgIdx + 1] |
| 2659 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 2660 | SuppressUserConversions, ForceRValue, |
| 2661 | /*InOverloadResolution=*/true); |
| 2662 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
| 2663 | Candidate.Viable = false; |
| 2664 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 2665 | break; |
| 2666 | } |
| 2667 | } else { |
| 2668 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2669 | // argument for which there is no corresponding parameter is |
| 2670 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2671 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
| 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2677 | /// set, using template argument deduction to produce an appropriate member |
| 2678 | /// function template specialization. |
| 2679 | void |
| 2680 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2681 | AccessSpecifier Access, |
| 2682 | CXXRecordDecl *ActingContext, |
| 2683 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 2684 | QualType ObjectType, |
| 2685 | Expr **Args, unsigned NumArgs, |
| 2686 | OverloadCandidateSet& CandidateSet, |
| 2687 | bool SuppressUserConversions, |
| 2688 | bool ForceRValue) { |
| 2689 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2690 | return; |
| 2691 | |
| 2692 | // C++ [over.match.funcs]p7: |
| 2693 | // In each case where a candidate is a function template, candidate |
| 2694 | // function template specializations are generated using template argument |
| 2695 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
| 2696 | // candidate functions in the usual way.113) A given name can refer to one |
| 2697 | // or more function templates and also to a set of overloaded non-template |
| 2698 | // functions. In such a case, the candidate functions generated from each |
| 2699 | // function template are combined with the set of non-template candidate |
| 2700 | // functions. |
| 2701 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
| 2702 | FunctionDecl *Specialization = 0; |
| 2703 | if (TemplateDeductionResult Result |
| 2704 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
| 2705 | Args, NumArgs, Specialization, Info)) { |
| 2706 | // FIXME: Record what happened with template argument deduction, so |
| 2707 | // that we can give the user a beautiful diagnostic. |
| 2708 | (void)Result; |
| 2709 | return; |
| 2710 | } |
| 2711 | |
| 2712 | // Add the function template specialization produced by template argument |
| 2713 | // deduction as a candidate. |
| 2714 | assert(Specialization && "Missing member function template specialization?"); |
| 2715 | assert(isa<CXXMethodDecl>(Specialization) && |
| 2716 | "Specialization is not a member function?"); |
| 2717 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Access, |
| 2718 | ActingContext, ObjectType, Args, NumArgs, |
| 2719 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2720 | } |
| 2721 | |
| 2722 | /// \brief Add a C++ function template specialization as a candidate |
| 2723 | /// in the candidate set, using template argument deduction to produce |
| 2724 | /// an appropriate function template specialization. |
| 2725 | void |
| 2726 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2727 | AccessSpecifier Access, |
| 2728 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 2729 | Expr **Args, unsigned NumArgs, |
| 2730 | OverloadCandidateSet& CandidateSet, |
| 2731 | bool SuppressUserConversions, |
| 2732 | bool ForceRValue) { |
| 2733 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2734 | return; |
| 2735 | |
| 2736 | // C++ [over.match.funcs]p7: |
| 2737 | // In each case where a candidate is a function template, candidate |
| 2738 | // function template specializations are generated using template argument |
| 2739 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
| 2740 | // candidate functions in the usual way.113) A given name can refer to one |
| 2741 | // or more function templates and also to a set of overloaded non-template |
| 2742 | // functions. In such a case, the candidate functions generated from each |
| 2743 | // function template are combined with the set of non-template candidate |
| 2744 | // functions. |
| 2745 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
| 2746 | FunctionDecl *Specialization = 0; |
| 2747 | if (TemplateDeductionResult Result |
| 2748 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
| 2749 | Args, NumArgs, Specialization, Info)) { |
| 2750 | CandidateSet.push_back(OverloadCandidate()); |
| 2751 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 2752 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 2753 | Candidate.Access = Access; |
| 2754 | Candidate.Viable = false; |
| 2755 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 2756 | Candidate.IsSurrogate = false; |
| 2757 | Candidate.IgnoreObjectArgument = false; |
| 2758 | |
| 2759 | // TODO: record more information about failed template arguments |
| 2760 | Candidate.DeductionFailure.Result = Result; |
| 2761 | Candidate.DeductionFailure.TemplateParameter = Info.Param.getOpaqueValue(); |
| 2762 | return; |
| 2763 | } |
| 2764 | |
| 2765 | // Add the function template specialization produced by template argument |
| 2766 | // deduction as a candidate. |
| 2767 | assert(Specialization && "Missing function template specialization?"); |
| 2768 | AddOverloadCandidate(Specialization, Access, Args, NumArgs, CandidateSet, |
| 2769 | SuppressUserConversions, ForceRValue); |
| 2770 | } |
| 2771 | |
| 2772 | /// AddConversionCandidate - Add a C++ conversion function as a |
| 2773 | /// candidate in the candidate set (C++ [over.match.conv], |
| 2774 | /// C++ [over.match.copy]). From is the expression we're converting from, |
| 2775 | /// and ToType is the type that we're eventually trying to convert to |
| 2776 | /// (which may or may not be the same type as the type that the |
| 2777 | /// conversion function produces). |
| 2778 | void |
| 2779 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2780 | AccessSpecifier Access, |
| 2781 | CXXRecordDecl *ActingContext, |
| 2782 | Expr *From, QualType ToType, |
| 2783 | OverloadCandidateSet& CandidateSet) { |
| 2784 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2785 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2786 | |
| 2787 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2788 | return; |
| 2789 | |
| 2790 | // Overload resolution is always an unevaluated context. |
| 2791 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2792 | |
| 2793 | // Add this candidate |
| 2794 | CandidateSet.push_back(OverloadCandidate()); |
| 2795 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2796 | Candidate.Function = Conversion; |
| 2797 | Candidate.Access = Access; |
| 2798 | Candidate.IsSurrogate = false; |
| 2799 | Candidate.IgnoreObjectArgument = false; |
| 2800 | Candidate.FinalConversion.setAsIdentityConversion(); |
| 2801 | Candidate.FinalConversion.setFromType(Conversion->getConversionType()); |
| 2802 | Candidate.FinalConversion.setAllToTypes(ToType); |
| 2803 | |
| 2804 | // Determine the implicit conversion sequence for the implicit |
| 2805 | // object parameter. |
| 2806 | Candidate.Viable = true; |
| 2807 | Candidate.Conversions.resize(1); |
| 2808 | Candidate.Conversions[0] |
| 2809 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 2810 | ActingContext); |
| 2811 | // Conversion functions to a different type in the base class is visible in |
| 2812 | // the derived class. So, a derived to base conversion should not participate |
| 2813 | // in overload resolution. |
| 2814 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2815 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
| 2816 | if (Candidate.Conversions[0].isBad()) { |
| 2817 | Candidate.Viable = false; |
| 2818 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 2819 | return; |
| 2820 | } |
| 2821 | |
| 2822 | // We won't go through a user-define type conversion function to convert a |
| 2823 | // derived to base as such conversions are given Conversion Rank. They only |
| 2824 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2825 | QualType FromCanon |
| 2826 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2827 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2828 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2829 | Candidate.Viable = false; |
| 2830 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
| 2831 | return; |
| 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | // To determine what the conversion from the result of calling the |
| 2836 | // conversion function to the type we're eventually trying to |
| 2837 | // convert to (ToType), we need to synthesize a call to the |
| 2838 | // conversion function and attempt copy initialization from it. This |
| 2839 | // makes sure that we get the right semantics with respect to |
| 2840 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2841 | // call on the stack and we don't need its arguments to be |
| 2842 | // well-formed. |
| 2843 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
| 2844 | From->getLocStart()); |
| 2845 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
| 2846 | CastExpr::CK_FunctionToPointerDecay, |
| 2847 | &ConversionRef, false); |
| 2848 | |
| 2849 | // Note that it is safe to allocate CallExpr on the stack here because |
| 2850 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2851 | // allocator). |
| 2852 | CallExpr Call(Context, &ConversionFn, 0, 0, |
| 2853 | Conversion->getConversionType().getNonReferenceType(), |
| 2854 | From->getLocStart()); |
| 2855 | ImplicitConversionSequence ICS = |
| 2856 | TryCopyInitialization(&Call, ToType, |
| 2857 | /*SuppressUserConversions=*/true, |
| 2858 | /*ForceRValue=*/false, |
| 2859 | /*InOverloadResolution=*/false); |
| 2860 | |
| 2861 | switch (ICS.getKind()) { |
| 2862 | case ImplicitConversionSequence::StandardConversion: |
| 2863 | Candidate.FinalConversion = ICS.Standard; |
| 2864 | break; |
| 2865 | |
| 2866 | case ImplicitConversionSequence::BadConversion: |
| 2867 | Candidate.Viable = false; |
| 2868 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 2869 | break; |
| 2870 | |
| 2871 | default: |
| 2872 | assert(false && |
| 2873 | "Can only end up with a standard conversion sequence or failure"); |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | /// \brief Adds a conversion function template specialization |
| 2878 | /// candidate to the overload set, using template argument deduction |
| 2879 | /// to deduce the template arguments of the conversion function |
| 2880 | /// template from the type that we are converting to (C++ |
| 2881 | /// [temp.deduct.conv]). |
| 2882 | void |
| 2883 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2884 | AccessSpecifier Access, |
| 2885 | CXXRecordDecl *ActingDC, |
| 2886 | Expr *From, QualType ToType, |
| 2887 | OverloadCandidateSet &CandidateSet) { |
| 2888 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2889 | "Only conversion function templates permitted here"); |
| 2890 | |
| 2891 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2892 | return; |
| 2893 | |
| 2894 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
| 2895 | CXXConversionDecl *Specialization = 0; |
| 2896 | if (TemplateDeductionResult Result |
| 2897 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
| 2898 | Specialization, Info)) { |
| 2899 | // FIXME: Record what happened with template argument deduction, so |
| 2900 | // that we can give the user a beautiful diagnostic. |
| 2901 | (void)Result; |
| 2902 | return; |
| 2903 | } |
| 2904 | |
| 2905 | // Add the conversion function template specialization produced by |
| 2906 | // template argument deduction as a candidate. |
| 2907 | assert(Specialization && "Missing function template specialization?"); |
| 2908 | AddConversionCandidate(Specialization, Access, ActingDC, From, ToType, |
| 2909 | CandidateSet); |
| 2910 | } |
| 2911 | |
| 2912 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2913 | /// converts the given @c Object to a function pointer via the |
| 2914 | /// conversion function @c Conversion, and then attempts to call it |
| 2915 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2916 | /// the type of function that we'll eventually be calling. |
| 2917 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
| 2918 | AccessSpecifier Access, |
| 2919 | CXXRecordDecl *ActingContext, |
| 2920 | const FunctionProtoType *Proto, |
| 2921 | QualType ObjectType, |
| 2922 | Expr **Args, unsigned NumArgs, |
| 2923 | OverloadCandidateSet& CandidateSet) { |
| 2924 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2925 | return; |
| 2926 | |
| 2927 | // Overload resolution is always an unevaluated context. |
| 2928 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2929 | |
| 2930 | CandidateSet.push_back(OverloadCandidate()); |
| 2931 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2932 | Candidate.Function = 0; |
| 2933 | Candidate.Access = Access; |
| 2934 | Candidate.Surrogate = Conversion; |
| 2935 | Candidate.Viable = true; |
| 2936 | Candidate.IsSurrogate = true; |
| 2937 | Candidate.IgnoreObjectArgument = false; |
| 2938 | Candidate.Conversions.resize(NumArgs + 1); |
| 2939 | |
| 2940 | // Determine the implicit conversion sequence for the implicit |
| 2941 | // object parameter. |
| 2942 | ImplicitConversionSequence ObjectInit |
| 2943 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
| 2944 | if (ObjectInit.isBad()) { |
| 2945 | Candidate.Viable = false; |
| 2946 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 2947 | Candidate.Conversions[0] = ObjectInit; |
| 2948 | return; |
| 2949 | } |
| 2950 | |
| 2951 | // The first conversion is actually a user-defined conversion whose |
| 2952 | // first conversion is ObjectInit's standard conversion (which is |
| 2953 | // effectively a reference binding). Record it as such. |
| 2954 | Candidate.Conversions[0].setUserDefined(); |
| 2955 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2956 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
| 2957 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
| 2958 | Candidate.Conversions[0].UserDefined.After |
| 2959 | = Candidate.Conversions[0].UserDefined.Before; |
| 2960 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2961 | |
| 2962 | // Find the |
| 2963 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2964 | |
| 2965 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2966 | // parameters is viable only if it has an ellipsis in its parameter |
| 2967 | // list (8.3.5). |
| 2968 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2969 | Candidate.Viable = false; |
| 2970 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
| 2971 | return; |
| 2972 | } |
| 2973 | |
| 2974 | // Function types don't have any default arguments, so just check if |
| 2975 | // we have enough arguments. |
| 2976 | if (NumArgs < NumArgsInProto) { |
| 2977 | // Not enough arguments. |
| 2978 | Candidate.Viable = false; |
| 2979 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
| 2980 | return; |
| 2981 | } |
| 2982 | |
| 2983 | // Determine the implicit conversion sequences for each of the |
| 2984 | // arguments. |
| 2985 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2986 | if (ArgIdx < NumArgsInProto) { |
| 2987 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2988 | // exist for each argument an implicit conversion sequence |
| 2989 | // (13.3.3.1) that converts that argument to the corresponding |
| 2990 | // parameter of F. |
| 2991 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2992 | Candidate.Conversions[ArgIdx + 1] |
| 2993 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 2994 | /*SuppressUserConversions=*/false, |
| 2995 | /*ForceRValue=*/false, |
| 2996 | /*InOverloadResolution=*/false); |
| 2997 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
| 2998 | Candidate.Viable = false; |
| 2999 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 3000 | break; |
| 3001 | } |
| 3002 | } else { |
| 3003 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3004 | // argument for which there is no corresponding parameter is |
| 3005 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 3006 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
| 3007 | } |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 3012 | // operator overloading logic over to the scheme used by binary operators, which |
| 3013 | // works for template instantiation. |
| 3014 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
| 3015 | SourceLocation OpLoc, |
| 3016 | Expr **Args, unsigned NumArgs, |
| 3017 | OverloadCandidateSet& CandidateSet, |
| 3018 | SourceRange OpRange) { |
| 3019 | UnresolvedSet<16> Fns; |
| 3020 | |
| 3021 | QualType T1 = Args[0]->getType(); |
| 3022 | QualType T2; |
| 3023 | if (NumArgs > 1) |
| 3024 | T2 = Args[1]->getType(); |
| 3025 | |
| 3026 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3027 | if (S) |
| 3028 | LookupOverloadedOperatorName(Op, S, T1, T2, Fns); |
| 3029 | AddFunctionCandidates(Fns, Args, NumArgs, CandidateSet, false); |
| 3030 | AddArgumentDependentLookupCandidates(OpName, false, Args, NumArgs, 0, |
| 3031 | CandidateSet); |
| 3032 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 3033 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
| 3034 | } |
| 3035 | |
| 3036 | /// \brief Add overload candidates for overloaded operators that are |
| 3037 | /// member functions. |
| 3038 | /// |
| 3039 | /// Add the overloaded operator candidates that are member functions |
| 3040 | /// for the operator Op that was used in an operator expression such |
| 3041 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3042 | /// CandidateSet will store the added overload candidates. (C++ |
| 3043 | /// [over.match.oper]). |
| 3044 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3045 | SourceLocation OpLoc, |
| 3046 | Expr **Args, unsigned NumArgs, |
| 3047 | OverloadCandidateSet& CandidateSet, |
| 3048 | SourceRange OpRange) { |
| 3049 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3050 | |
| 3051 | // C++ [over.match.oper]p3: |
| 3052 | // For a unary operator @ with an operand of a type whose |
| 3053 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3054 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3055 | // a right operand of a type whose cv-unqualified version is T2, |
| 3056 | // three sets of candidate functions, designated member |
| 3057 | // candidates, non-member candidates and built-in candidates, are |
| 3058 | // constructed as follows: |
| 3059 | QualType T1 = Args[0]->getType(); |
| 3060 | QualType T2; |
| 3061 | if (NumArgs > 1) |
| 3062 | T2 = Args[1]->getType(); |
| 3063 | |
| 3064 | // -- If T1 is a class type, the set of member candidates is the |
| 3065 | // result of the qualified lookup of T1::operator@ |
| 3066 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3067 | // empty. |
| 3068 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
| 3069 | // Complete the type if it can be completed. Otherwise, we're done. |
| 3070 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
| 3071 | return; |
| 3072 | |
| 3073 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3074 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3075 | Operators.suppressDiagnostics(); |
| 3076 | |
| 3077 | for (LookupResult::iterator Oper = Operators.begin(), |
| 3078 | OperEnd = Operators.end(); |
| 3079 | Oper != OperEnd; |
| 3080 | ++Oper) |
| 3081 | AddMethodCandidate(*Oper, Oper.getAccess(), Args[0]->getType(), |
| 3082 | Args + 1, NumArgs - 1, CandidateSet, |
| 3083 | /* SuppressUserConversions = */ false); |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3088 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3089 | /// of the built-in candidate, respectively. Args and NumArgs are the |
| 3090 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3091 | /// should be true when this built-in candidate is an assignment |
| 3092 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3093 | /// (at the beginning of the argument list) that will be contextually |
| 3094 | /// converted to bool. |
| 3095 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
| 3096 | Expr **Args, unsigned NumArgs, |
| 3097 | OverloadCandidateSet& CandidateSet, |
| 3098 | bool IsAssignmentOperator, |
| 3099 | unsigned NumContextualBoolArguments) { |
| 3100 | // Overload resolution is always an unevaluated context. |
| 3101 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3102 | |
| 3103 | // Add this candidate |
| 3104 | CandidateSet.push_back(OverloadCandidate()); |
| 3105 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 3106 | Candidate.Function = 0; |
| 3107 | Candidate.Access = AS_none; |
| 3108 | Candidate.IsSurrogate = false; |
| 3109 | Candidate.IgnoreObjectArgument = false; |
| 3110 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3111 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3112 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3113 | |
| 3114 | // Determine the implicit conversion sequences for each of the |
| 3115 | // arguments. |
| 3116 | Candidate.Viable = true; |
| 3117 | Candidate.Conversions.resize(NumArgs); |
| 3118 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3119 | // C++ [over.match.oper]p4: |
| 3120 | // For the built-in assignment operators, conversions of the |
| 3121 | // left operand are restricted as follows: |
| 3122 | // -- no temporaries are introduced to hold the left operand, and |
| 3123 | // -- no user-defined conversions are applied to the left |
| 3124 | // operand to achieve a type match with the left-most |
| 3125 | // parameter of a built-in candidate. |
| 3126 | // |
| 3127 | // We block these conversions by turning off user-defined |
| 3128 | // conversions, since that is the only way that initialization of |
| 3129 | // a reference to a non-class type can occur from something that |
| 3130 | // is not of the same type. |
| 3131 | if (ArgIdx < NumContextualBoolArguments) { |
| 3132 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
| 3133 | "Contextual conversion to bool requires bool type"); |
| 3134 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3135 | } else { |
| 3136 | Candidate.Conversions[ArgIdx] |
| 3137 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
| 3138 | ArgIdx == 0 && IsAssignmentOperator, |
| 3139 | /*ForceRValue=*/false, |
| 3140 | /*InOverloadResolution=*/false); |
| 3141 | } |
| 3142 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3143 | Candidate.Viable = false; |
| 3144 | Candidate.FailureKind = ovl_fail_bad_conversion; |
| 3145 | break; |
| 3146 | } |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3151 | /// candidate operator functions for built-in operators (C++ |
| 3152 | /// [over.built]). The types are separated into pointer types and |
| 3153 | /// enumeration types. |
| 3154 | class BuiltinCandidateTypeSet { |
| 3155 | /// TypeSet - A set of types. |
| 3156 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
| 3157 | |
| 3158 | /// PointerTypes - The set of pointer types that will be used in the |
| 3159 | /// built-in candidates. |
| 3160 | TypeSet PointerTypes; |
| 3161 | |
| 3162 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3163 | /// used in the built-in candidates. |
| 3164 | TypeSet MemberPointerTypes; |
| 3165 | |
| 3166 | /// EnumerationTypes - The set of enumeration types that will be |
| 3167 | /// used in the built-in candidates. |
| 3168 | TypeSet EnumerationTypes; |
| 3169 | |
| 3170 | /// Sema - The semantic analysis instance where we are building the |
| 3171 | /// candidate type set. |
| 3172 | Sema &SemaRef; |
| 3173 | |
| 3174 | /// Context - The AST context in which we will build the type sets. |
| 3175 | ASTContext &Context; |
| 3176 | |
| 3177 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3178 | const Qualifiers &VisibleQuals); |
| 3179 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| 3180 | |
| 3181 | public: |
| 3182 | /// iterator - Iterates through the types that are part of the set. |
| 3183 | typedef TypeSet::iterator iterator; |
| 3184 | |
| 3185 | BuiltinCandidateTypeSet(Sema &SemaRef) |
| 3186 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
| 3187 | |
| 3188 | void AddTypesConvertedFrom(QualType Ty, |
| 3189 | SourceLocation Loc, |
| 3190 | bool AllowUserConversions, |
| 3191 | bool AllowExplicitConversions, |
| 3192 | const Qualifiers &VisibleTypeConversionsQuals); |
| 3193 | |
| 3194 | /// pointer_begin - First pointer type found; |
| 3195 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3196 | |
| 3197 | /// pointer_end - Past the last pointer type found; |
| 3198 | iterator pointer_end() { return PointerTypes.end(); } |
| 3199 | |
| 3200 | /// member_pointer_begin - First member pointer type found; |
| 3201 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3202 | |
| 3203 | /// member_pointer_end - Past the last member pointer type found; |
| 3204 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3205 | |
| 3206 | /// enumeration_begin - First enumeration type found; |
| 3207 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3208 | |
| 3209 | /// enumeration_end - Past the last enumeration type found; |
| 3210 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3211 | }; |
| 3212 | |
| 3213 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
| 3214 | /// the set of pointer types along with any more-qualified variants of |
| 3215 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3216 | /// will add "int const *", "int const volatile *", "int const |
| 3217 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3218 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3219 | /// false otherwise. |
| 3220 | /// |
| 3221 | /// FIXME: what to do about extended qualifiers? |
| 3222 | bool |
| 3223 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3224 | const Qualifiers &VisibleQuals) { |
| 3225 | |
| 3226 | // Insert this type. |
| 3227 | if (!PointerTypes.insert(Ty)) |
| 3228 | return false; |
| 3229 | |
| 3230 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3231 | assert(PointerTy && "type was not a pointer type!"); |
| 3232 | |
| 3233 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3234 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3235 | // (the qualifier would sink to the element type), and for another, the |
| 3236 | // only overload situation where it matters is subscript or pointer +- int, |
| 3237 | // and those shouldn't have qualifier variants anyway. |
| 3238 | if (PointeeTy->isArrayType()) |
| 3239 | return true; |
| 3240 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3241 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
| 3242 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
| 3243 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3244 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3245 | |
| 3246 | // Iterate through all strict supersets of BaseCVR. |
| 3247 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3248 | if ((CVR | BaseCVR) != CVR) continue; |
| 3249 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3250 | // in the types. |
| 3251 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3252 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
| 3253 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3254 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 3255 | } |
| 3256 | |
| 3257 | return true; |
| 3258 | } |
| 3259 | |
| 3260 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3261 | /// to the set of pointer types along with any more-qualified variants of |
| 3262 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3263 | /// will add "int const *", "int const volatile *", "int const |
| 3264 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3265 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3266 | /// false otherwise. |
| 3267 | /// |
| 3268 | /// FIXME: what to do about extended qualifiers? |
| 3269 | bool |
| 3270 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3271 | QualType Ty) { |
| 3272 | // Insert this type. |
| 3273 | if (!MemberPointerTypes.insert(Ty)) |
| 3274 | return false; |
| 3275 | |
| 3276 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3277 | assert(PointerTy && "type was not a member pointer type!"); |
| 3278 | |
| 3279 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3280 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3281 | // (the qualifier would sink to the element type), and for another, the |
| 3282 | // only overload situation where it matters is subscript or pointer +- int, |
| 3283 | // and those shouldn't have qualifier variants anyway. |
| 3284 | if (PointeeTy->isArrayType()) |
| 3285 | return true; |
| 3286 | const Type *ClassTy = PointerTy->getClass(); |
| 3287 | |
| 3288 | // Iterate through all strict supersets of the pointee type's CVR |
| 3289 | // qualifiers. |
| 3290 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3291 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3292 | if ((CVR | BaseCVR) != CVR) continue; |
| 3293 | |
| 3294 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3295 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
| 3296 | } |
| 3297 | |
| 3298 | return true; |
| 3299 | } |
| 3300 | |
| 3301 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3302 | /// Ty can be implicit converted to the given set of @p Types. We're |
| 3303 | /// primarily interested in pointer types and enumeration types. We also |
| 3304 | /// take member pointer types, for the conditional operator. |
| 3305 | /// AllowUserConversions is true if we should look at the conversion |
| 3306 | /// functions of a class type, and AllowExplicitConversions if we |
| 3307 | /// should also include the explicit conversion functions of a class |
| 3308 | /// type. |
| 3309 | void |
| 3310 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 3311 | SourceLocation Loc, |
| 3312 | bool AllowUserConversions, |
| 3313 | bool AllowExplicitConversions, |
| 3314 | const Qualifiers &VisibleQuals) { |
| 3315 | // Only deal with canonical types. |
| 3316 | Ty = Context.getCanonicalType(Ty); |
| 3317 | |
| 3318 | // Look through reference types; they aren't part of the type of an |
| 3319 | // expression for the purposes of conversions. |
| 3320 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
| 3321 | Ty = RefTy->getPointeeType(); |
| 3322 | |
| 3323 | // We don't care about qualifiers on the type. |
| 3324 | Ty = Ty.getLocalUnqualifiedType(); |
| 3325 | |
| 3326 | // If we're dealing with an array type, decay to the pointer. |
| 3327 | if (Ty->isArrayType()) |
| 3328 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3329 | |
| 3330 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
| 3331 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3332 | |
| 3333 | // Insert our type, and its more-qualified variants, into the set |
| 3334 | // of types. |
| 3335 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
| 3336 | return; |
| 3337 | } else if (Ty->isMemberPointerType()) { |
| 3338 | // Member pointers are far easier, since the pointee can't be converted. |
| 3339 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3340 | return; |
| 3341 | } else if (Ty->isEnumeralType()) { |
| 3342 | EnumerationTypes.insert(Ty); |
| 3343 | } else if (AllowUserConversions) { |
| 3344 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
| 3345 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
| 3346 | // No conversion functions in incomplete types. |
| 3347 | return; |
| 3348 | } |
| 3349 | |
| 3350 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 3351 | const UnresolvedSetImpl *Conversions |
| 3352 | = ClassDecl->getVisibleConversionFunctions(); |
| 3353 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3354 | E = Conversions->end(); I != E; ++I) { |
| 3355 | |
| 3356 | // Skip conversion function templates; they don't tell us anything |
| 3357 | // about which builtin types we can convert to. |
| 3358 | if (isa<FunctionTemplateDecl>(*I)) |
| 3359 | continue; |
| 3360 | |
| 3361 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); |
| 3362 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 3363 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 3364 | VisibleQuals); |
| 3365 | } |
| 3366 | } |
| 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3372 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3373 | /// given type to the candidate set. |
| 3374 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3375 | QualType T, |
| 3376 | Expr **Args, |
| 3377 | unsigned NumArgs, |
| 3378 | OverloadCandidateSet &CandidateSet) { |
| 3379 | QualType ParamTypes[2]; |
| 3380 | |
| 3381 | // T& operator=(T&, T) |
| 3382 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3383 | ParamTypes[1] = T; |
| 3384 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3385 | /*IsAssignmentOperator=*/true); |
| 3386 | |
| 3387 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3388 | // volatile T& operator=(volatile T&, T) |
| 3389 | ParamTypes[0] |
| 3390 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
| 3391 | ParamTypes[1] = T; |
| 3392 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3393 | /*IsAssignmentOperator=*/true); |
| 3394 | } |
| 3395 | } |
| 3396 | |
| 3397 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3398 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
| 3399 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3400 | Qualifiers VRQuals; |
| 3401 | const RecordType *TyRec; |
| 3402 | if (const MemberPointerType *RHSMPType = |
| 3403 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3404 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3405 | else |
| 3406 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3407 | if (!TyRec) { |
| 3408 | // Just to be safe, assume the worst case. |
| 3409 | VRQuals.addVolatile(); |
| 3410 | VRQuals.addRestrict(); |
| 3411 | return VRQuals; |
| 3412 | } |
| 3413 | |
| 3414 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 3415 | if (!ClassDecl->hasDefinition()) |
| 3416 | return VRQuals; |
| 3417 | |
| 3418 | const UnresolvedSetImpl *Conversions = |
| 3419 | ClassDecl->getVisibleConversionFunctions(); |
| 3420 | |
| 3421 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3422 | E = Conversions->end(); I != E; ++I) { |
| 3423 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*I)) { |
| 3424 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3425 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3426 | CanTy = ResTypeRef->getPointeeType(); |
| 3427 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3428 | // as see them. |
| 3429 | bool done = false; |
| 3430 | while (!done) { |
| 3431 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3432 | CanTy = ResTypePtr->getPointeeType(); |
| 3433 | else if (const MemberPointerType *ResTypeMPtr = |
| 3434 | CanTy->getAs<MemberPointerType>()) |
| 3435 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3436 | else |
| 3437 | done = true; |
| 3438 | if (CanTy.isVolatileQualified()) |
| 3439 | VRQuals.addVolatile(); |
| 3440 | if (CanTy.isRestrictQualified()) |
| 3441 | VRQuals.addRestrict(); |
| 3442 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3443 | return VRQuals; |
| 3444 | } |
| 3445 | } |
| 3446 | } |
| 3447 | return VRQuals; |
| 3448 | } |
| 3449 | |
| 3450 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3451 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3452 | /// on the operator @p Op and the arguments given. For example, if the |
| 3453 | /// operator is a binary '+', this routine might add "int |
| 3454 | /// operator+(int, int)" to cover integer addition. |
| 3455 | void |
| 3456 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 3457 | SourceLocation OpLoc, |
| 3458 | Expr **Args, unsigned NumArgs, |
| 3459 | OverloadCandidateSet& CandidateSet) { |
| 3460 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3461 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3462 | // that the first few of these types are the promoted integral |
| 3463 | // types; these types need to be first. |
| 3464 | // FIXME: What about complex? |
| 3465 | const unsigned FirstIntegralType = 0; |
| 3466 | const unsigned LastIntegralType = 13; |
| 3467 | const unsigned FirstPromotedIntegralType = 7, |
| 3468 | LastPromotedIntegralType = 13; |
| 3469 | const unsigned FirstPromotedArithmeticType = 7, |
| 3470 | LastPromotedArithmeticType = 16; |
| 3471 | const unsigned NumArithmeticTypes = 16; |
| 3472 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
| 3473 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3474 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
| 3475 | Context.SignedCharTy, Context.ShortTy, |
| 3476 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3477 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3478 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3479 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3480 | }; |
| 3481 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3482 | "Invalid first promoted integral type"); |
| 3483 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3484 | == Context.UnsignedLongLongTy && |
| 3485 | "Invalid last promoted integral type"); |
| 3486 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3487 | "Invalid first promoted arithmetic type"); |
| 3488 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3489 | == Context.LongDoubleTy && |
| 3490 | "Invalid last promoted arithmetic type"); |
| 3491 | |
| 3492 | // Find all of the types that the arguments can convert to, but only |
| 3493 | // if the operator we're looking at has built-in operator candidates |
| 3494 | // that make use of these types. |
| 3495 | Qualifiers VisibleTypeConversionsQuals; |
| 3496 | VisibleTypeConversionsQuals.addConst(); |
| 3497 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3498 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3499 | |
| 3500 | BuiltinCandidateTypeSet CandidateTypes(*this); |
| 3501 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3502 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
| 3503 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
| 3504 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
| 3505 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
| 3506 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
| 3507 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3508 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 3509 | OpLoc, |
| 3510 | true, |
| 3511 | (Op == OO_Exclaim || |
| 3512 | Op == OO_AmpAmp || |
| 3513 | Op == OO_PipePipe), |
| 3514 | VisibleTypeConversionsQuals); |
| 3515 | } |
| 3516 | |
| 3517 | bool isComparison = false; |
| 3518 | switch (Op) { |
| 3519 | case OO_None: |
| 3520 | case NUM_OVERLOADED_OPERATORS: |
| 3521 | assert(false && "Expected an overloaded operator"); |
| 3522 | break; |
| 3523 | |
| 3524 | case OO_Star: // '*' is either unary or binary |
| 3525 | if (NumArgs == 1) |
| 3526 | goto UnaryStar; |
| 3527 | else |
| 3528 | goto BinaryStar; |
| 3529 | break; |
| 3530 | |
| 3531 | case OO_Plus: // '+' is either unary or binary |
| 3532 | if (NumArgs == 1) |
| 3533 | goto UnaryPlus; |
| 3534 | else |
| 3535 | goto BinaryPlus; |
| 3536 | break; |
| 3537 | |
| 3538 | case OO_Minus: // '-' is either unary or binary |
| 3539 | if (NumArgs == 1) |
| 3540 | goto UnaryMinus; |
| 3541 | else |
| 3542 | goto BinaryMinus; |
| 3543 | break; |
| 3544 | |
| 3545 | case OO_Amp: // '&' is either unary or binary |
| 3546 | if (NumArgs == 1) |
| 3547 | goto UnaryAmp; |
| 3548 | else |
| 3549 | goto BinaryAmp; |
| 3550 | |
| 3551 | case OO_PlusPlus: |
| 3552 | case OO_MinusMinus: |
| 3553 | // C++ [over.built]p3: |
| 3554 | // |
| 3555 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3556 | // is either volatile or empty, there exist candidate operator |
| 3557 | // functions of the form |
| 3558 | // |
| 3559 | // VQ T& operator++(VQ T&); |
| 3560 | // T operator++(VQ T&, int); |
| 3561 | // |
| 3562 | // C++ [over.built]p4: |
| 3563 | // |
| 3564 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3565 | // than bool, and VQ is either volatile or empty, there exist |
| 3566 | // candidate operator functions of the form |
| 3567 | // |
| 3568 | // VQ T& operator--(VQ T&); |
| 3569 | // T operator--(VQ T&, int); |
| 3570 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 3571 | Arith < NumArithmeticTypes; ++Arith) { |
| 3572 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3573 | QualType ParamTypes[2] |
| 3574 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
| 3575 | |
| 3576 | // Non-volatile version. |
| 3577 | if (NumArgs == 1) |
| 3578 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3579 | else |
| 3580 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3581 | // heuristic to reduce number of builtin candidates in the set. |
| 3582 | // Add volatile version only if there are conversions to a volatile type. |
| 3583 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3584 | // Volatile version |
| 3585 | ParamTypes[0] |
| 3586 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3587 | if (NumArgs == 1) |
| 3588 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3589 | else |
| 3590 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | // C++ [over.built]p5: |
| 3595 | // |
| 3596 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3597 | // cv-unqualified object type, and VQ is either volatile or |
| 3598 | // empty, there exist candidate operator functions of the form |
| 3599 | // |
| 3600 | // T*VQ& operator++(T*VQ&); |
| 3601 | // T*VQ& operator--(T*VQ&); |
| 3602 | // T* operator++(T*VQ&, int); |
| 3603 | // T* operator--(T*VQ&, int); |
| 3604 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3605 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3606 | // Skip pointer types that aren't pointers to object types. |
| 3607 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
| 3608 | continue; |
| 3609 | |
| 3610 | QualType ParamTypes[2] = { |
| 3611 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
| 3612 | }; |
| 3613 | |
| 3614 | // Without volatile |
| 3615 | if (NumArgs == 1) |
| 3616 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3617 | else |
| 3618 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3619 | |
| 3620 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3621 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 3622 | // With volatile |
| 3623 | ParamTypes[0] |
| 3624 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
| 3625 | if (NumArgs == 1) |
| 3626 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3627 | else |
| 3628 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3629 | } |
| 3630 | } |
| 3631 | break; |
| 3632 | |
| 3633 | UnaryStar: |
| 3634 | // C++ [over.built]p6: |
| 3635 | // For every cv-qualified or cv-unqualified object type T, there |
| 3636 | // exist candidate operator functions of the form |
| 3637 | // |
| 3638 | // T& operator*(T*); |
| 3639 | // |
| 3640 | // C++ [over.built]p7: |
| 3641 | // For every function type T, there exist candidate operator |
| 3642 | // functions of the form |
| 3643 | // T& operator*(T*); |
| 3644 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3645 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3646 | QualType ParamTy = *Ptr; |
| 3647 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
| 3648 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
| 3649 | &ParamTy, Args, 1, CandidateSet); |
| 3650 | } |
| 3651 | break; |
| 3652 | |
| 3653 | UnaryPlus: |
| 3654 | // C++ [over.built]p8: |
| 3655 | // For every type T, there exist candidate operator functions of |
| 3656 | // the form |
| 3657 | // |
| 3658 | // T* operator+(T*); |
| 3659 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3660 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3661 | QualType ParamTy = *Ptr; |
| 3662 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3663 | } |
| 3664 | |
| 3665 | // Fall through |
| 3666 | |
| 3667 | UnaryMinus: |
| 3668 | // C++ [over.built]p9: |
| 3669 | // For every promoted arithmetic type T, there exist candidate |
| 3670 | // operator functions of the form |
| 3671 | // |
| 3672 | // T operator+(T); |
| 3673 | // T operator-(T); |
| 3674 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 3675 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3676 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3677 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3678 | } |
| 3679 | break; |
| 3680 | |
| 3681 | case OO_Tilde: |
| 3682 | // C++ [over.built]p10: |
| 3683 | // For every promoted integral type T, there exist candidate |
| 3684 | // operator functions of the form |
| 3685 | // |
| 3686 | // T operator~(T); |
| 3687 | for (unsigned Int = FirstPromotedIntegralType; |
| 3688 | Int < LastPromotedIntegralType; ++Int) { |
| 3689 | QualType IntTy = ArithmeticTypes[Int]; |
| 3690 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3691 | } |
| 3692 | break; |
| 3693 | |
| 3694 | case OO_New: |
| 3695 | case OO_Delete: |
| 3696 | case OO_Array_New: |
| 3697 | case OO_Array_Delete: |
| 3698 | case OO_Call: |
| 3699 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
| 3700 | break; |
| 3701 | |
| 3702 | case OO_Comma: |
| 3703 | UnaryAmp: |
| 3704 | case OO_Arrow: |
| 3705 | // C++ [over.match.oper]p3: |
| 3706 | // -- For the operator ',', the unary operator '&', or the |
| 3707 | // operator '->', the built-in candidates set is empty. |
| 3708 | break; |
| 3709 | |
| 3710 | case OO_EqualEqual: |
| 3711 | case OO_ExclaimEqual: |
| 3712 | // C++ [over.match.oper]p16: |
| 3713 | // For every pointer to member type T, there exist candidate operator |
| 3714 | // functions of the form |
| 3715 | // |
| 3716 | // bool operator==(T,T); |
| 3717 | // bool operator!=(T,T); |
| 3718 | for (BuiltinCandidateTypeSet::iterator |
| 3719 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3720 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3721 | MemPtr != MemPtrEnd; |
| 3722 | ++MemPtr) { |
| 3723 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3724 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3725 | } |
| 3726 | |
| 3727 | // Fall through |
| 3728 | |
| 3729 | case OO_Less: |
| 3730 | case OO_Greater: |
| 3731 | case OO_LessEqual: |
| 3732 | case OO_GreaterEqual: |
| 3733 | // C++ [over.built]p15: |
| 3734 | // |
| 3735 | // For every pointer or enumeration type T, there exist |
| 3736 | // candidate operator functions of the form |
| 3737 | // |
| 3738 | // bool operator<(T, T); |
| 3739 | // bool operator>(T, T); |
| 3740 | // bool operator<=(T, T); |
| 3741 | // bool operator>=(T, T); |
| 3742 | // bool operator==(T, T); |
| 3743 | // bool operator!=(T, T); |
| 3744 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3745 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3746 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3747 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3748 | } |
| 3749 | for (BuiltinCandidateTypeSet::iterator Enum |
| 3750 | = CandidateTypes.enumeration_begin(); |
| 3751 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3752 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3753 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3754 | } |
| 3755 | |
| 3756 | // Fall through. |
| 3757 | isComparison = true; |
| 3758 | |
| 3759 | BinaryPlus: |
| 3760 | BinaryMinus: |
| 3761 | if (!isComparison) { |
| 3762 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3763 | |
| 3764 | // C++ [over.built]p13: |
| 3765 | // |
| 3766 | // For every cv-qualified or cv-unqualified object type T |
| 3767 | // there exist candidate operator functions of the form |
| 3768 | // |
| 3769 | // T* operator+(T*, ptrdiff_t); |
| 3770 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3771 | // T* operator-(T*, ptrdiff_t); |
| 3772 | // T* operator+(ptrdiff_t, T*); |
| 3773 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3774 | // |
| 3775 | // C++ [over.built]p14: |
| 3776 | // |
| 3777 | // For every T, where T is a pointer to object type, there |
| 3778 | // exist candidate operator functions of the form |
| 3779 | // |
| 3780 | // ptrdiff_t operator-(T, T); |
| 3781 | for (BuiltinCandidateTypeSet::iterator Ptr |
| 3782 | = CandidateTypes.pointer_begin(); |
| 3783 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3784 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3785 | |
| 3786 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3787 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3788 | |
| 3789 | if (Op == OO_Plus) { |
| 3790 | // T* operator+(ptrdiff_t, T*); |
| 3791 | ParamTypes[0] = ParamTypes[1]; |
| 3792 | ParamTypes[1] = *Ptr; |
| 3793 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3794 | } else { |
| 3795 | // ptrdiff_t operator-(T, T); |
| 3796 | ParamTypes[1] = *Ptr; |
| 3797 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3798 | Args, 2, CandidateSet); |
| 3799 | } |
| 3800 | } |
| 3801 | } |
| 3802 | // Fall through |
| 3803 | |
| 3804 | case OO_Slash: |
| 3805 | BinaryStar: |
| 3806 | Conditional: |
| 3807 | // C++ [over.built]p12: |
| 3808 | // |
| 3809 | // For every pair of promoted arithmetic types L and R, there |
| 3810 | // exist candidate operator functions of the form |
| 3811 | // |
| 3812 | // LR operator*(L, R); |
| 3813 | // LR operator/(L, R); |
| 3814 | // LR operator+(L, R); |
| 3815 | // LR operator-(L, R); |
| 3816 | // bool operator<(L, R); |
| 3817 | // bool operator>(L, R); |
| 3818 | // bool operator<=(L, R); |
| 3819 | // bool operator>=(L, R); |
| 3820 | // bool operator==(L, R); |
| 3821 | // bool operator!=(L, R); |
| 3822 | // |
| 3823 | // where LR is the result of the usual arithmetic conversions |
| 3824 | // between types L and R. |
| 3825 | // |
| 3826 | // C++ [over.built]p24: |
| 3827 | // |
| 3828 | // For every pair of promoted arithmetic types L and R, there exist |
| 3829 | // candidate operator functions of the form |
| 3830 | // |
| 3831 | // LR operator?(bool, L, R); |
| 3832 | // |
| 3833 | // where LR is the result of the usual arithmetic conversions |
| 3834 | // between types L and R. |
| 3835 | // Our candidates ignore the first parameter. |
| 3836 | for (unsigned Left = FirstPromotedArithmeticType; |
| 3837 | Left < LastPromotedArithmeticType; ++Left) { |
| 3838 | for (unsigned Right = FirstPromotedArithmeticType; |
| 3839 | Right < LastPromotedArithmeticType; ++Right) { |
| 3840 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3841 | QualType Result |
| 3842 | = isComparison |
| 3843 | ? Context.BoolTy |
| 3844 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 3845 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3846 | } |
| 3847 | } |
| 3848 | break; |
| 3849 | |
| 3850 | case OO_Percent: |
| 3851 | BinaryAmp: |
| 3852 | case OO_Caret: |
| 3853 | case OO_Pipe: |
| 3854 | case OO_LessLess: |
| 3855 | case OO_GreaterGreater: |
| 3856 | // C++ [over.built]p17: |
| 3857 | // |
| 3858 | // For every pair of promoted integral types L and R, there |
| 3859 | // exist candidate operator functions of the form |
| 3860 | // |
| 3861 | // LR operator%(L, R); |
| 3862 | // LR operator&(L, R); |
| 3863 | // LR operator^(L, R); |
| 3864 | // LR operator|(L, R); |
| 3865 | // L operator<<(L, R); |
| 3866 | // L operator>>(L, R); |
| 3867 | // |
| 3868 | // where LR is the result of the usual arithmetic conversions |
| 3869 | // between types L and R. |
| 3870 | for (unsigned Left = FirstPromotedIntegralType; |
| 3871 | Left < LastPromotedIntegralType; ++Left) { |
| 3872 | for (unsigned Right = FirstPromotedIntegralType; |
| 3873 | Right < LastPromotedIntegralType; ++Right) { |
| 3874 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3875 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3876 | ? LandR[0] |
| 3877 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 3878 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3879 | } |
| 3880 | } |
| 3881 | break; |
| 3882 | |
| 3883 | case OO_Equal: |
| 3884 | // C++ [over.built]p20: |
| 3885 | // |
| 3886 | // For every pair (T, VQ), where T is an enumeration or |
| 3887 | // pointer to member type and VQ is either volatile or |
| 3888 | // empty, there exist candidate operator functions of the form |
| 3889 | // |
| 3890 | // VQ T& operator=(VQ T&, T); |
| 3891 | for (BuiltinCandidateTypeSet::iterator |
| 3892 | Enum = CandidateTypes.enumeration_begin(), |
| 3893 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3894 | Enum != EnumEnd; ++Enum) |
| 3895 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
| 3896 | CandidateSet); |
| 3897 | for (BuiltinCandidateTypeSet::iterator |
| 3898 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3899 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3900 | MemPtr != MemPtrEnd; ++MemPtr) |
| 3901 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
| 3902 | CandidateSet); |
| 3903 | // Fall through. |
| 3904 | |
| 3905 | case OO_PlusEqual: |
| 3906 | case OO_MinusEqual: |
| 3907 | // C++ [over.built]p19: |
| 3908 | // |
| 3909 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3910 | // volatile or empty, there exist candidate operator functions |
| 3911 | // of the form |
| 3912 | // |
| 3913 | // T*VQ& operator=(T*VQ&, T*); |
| 3914 | // |
| 3915 | // C++ [over.built]p21: |
| 3916 | // |
| 3917 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3918 | // cv-unqualified object type and VQ is either volatile or |
| 3919 | // empty, there exist candidate operator functions of the form |
| 3920 | // |
| 3921 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3922 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3923 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3924 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3925 | QualType ParamTypes[2]; |
| 3926 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3927 | |
| 3928 | // non-volatile version |
| 3929 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
| 3930 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3931 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3932 | |
| 3933 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3934 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 3935 | // volatile version |
| 3936 | ParamTypes[0] |
| 3937 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
| 3938 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3939 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3940 | } |
| 3941 | } |
| 3942 | // Fall through. |
| 3943 | |
| 3944 | case OO_StarEqual: |
| 3945 | case OO_SlashEqual: |
| 3946 | // C++ [over.built]p18: |
| 3947 | // |
| 3948 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3949 | // VQ is either volatile or empty, and R is a promoted |
| 3950 | // arithmetic type, there exist candidate operator functions of |
| 3951 | // the form |
| 3952 | // |
| 3953 | // VQ L& operator=(VQ L&, R); |
| 3954 | // VQ L& operator*=(VQ L&, R); |
| 3955 | // VQ L& operator/=(VQ L&, R); |
| 3956 | // VQ L& operator+=(VQ L&, R); |
| 3957 | // VQ L& operator-=(VQ L&, R); |
| 3958 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 3959 | for (unsigned Right = FirstPromotedArithmeticType; |
| 3960 | Right < LastPromotedArithmeticType; ++Right) { |
| 3961 | QualType ParamTypes[2]; |
| 3962 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3963 | |
| 3964 | // Add this built-in operator as a candidate (VQ is empty). |
| 3965 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
| 3966 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3967 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3968 | |
| 3969 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3970 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3971 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3972 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3973 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3974 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3975 | } |
| 3976 | } |
| 3977 | } |
| 3978 | break; |
| 3979 | |
| 3980 | case OO_PercentEqual: |
| 3981 | case OO_LessLessEqual: |
| 3982 | case OO_GreaterGreaterEqual: |
| 3983 | case OO_AmpEqual: |
| 3984 | case OO_CaretEqual: |
| 3985 | case OO_PipeEqual: |
| 3986 | // C++ [over.built]p22: |
| 3987 | // |
| 3988 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3989 | // is either volatile or empty, and R is a promoted integral |
| 3990 | // type, there exist candidate operator functions of the form |
| 3991 | // |
| 3992 | // VQ L& operator%=(VQ L&, R); |
| 3993 | // VQ L& operator<<=(VQ L&, R); |
| 3994 | // VQ L& operator>>=(VQ L&, R); |
| 3995 | // VQ L& operator&=(VQ L&, R); |
| 3996 | // VQ L& operator^=(VQ L&, R); |
| 3997 | // VQ L& operator|=(VQ L&, R); |
| 3998 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 3999 | for (unsigned Right = FirstPromotedIntegralType; |
| 4000 | Right < LastPromotedIntegralType; ++Right) { |
| 4001 | QualType ParamTypes[2]; |
| 4002 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4003 | |
| 4004 | // Add this built-in operator as a candidate (VQ is empty). |
| 4005 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
| 4006 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4007 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4008 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4009 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4010 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4011 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4012 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4013 | } |
| 4014 | } |
| 4015 | } |
| 4016 | break; |
| 4017 | |
| 4018 | case OO_Exclaim: { |
| 4019 | // C++ [over.operator]p23: |
| 4020 | // |
| 4021 | // There also exist candidate operator functions of the form |
| 4022 | // |
| 4023 | // bool operator!(bool); |
| 4024 | // bool operator&&(bool, bool); [BELOW] |
| 4025 | // bool operator||(bool, bool); [BELOW] |
| 4026 | QualType ParamTy = Context.BoolTy; |
| 4027 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4028 | /*IsAssignmentOperator=*/false, |
| 4029 | /*NumContextualBoolArguments=*/1); |
| 4030 | break; |
| 4031 | } |
| 4032 | |
| 4033 | case OO_AmpAmp: |
| 4034 | case OO_PipePipe: { |
| 4035 | // C++ [over.operator]p23: |
| 4036 | // |
| 4037 | // There also exist candidate operator functions of the form |
| 4038 | // |
| 4039 | // bool operator!(bool); [ABOVE] |
| 4040 | // bool operator&&(bool, bool); |
| 4041 | // bool operator||(bool, bool); |
| 4042 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
| 4043 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4044 | /*IsAssignmentOperator=*/false, |
| 4045 | /*NumContextualBoolArguments=*/2); |
| 4046 | break; |
| 4047 | } |
| 4048 | |
| 4049 | case OO_Subscript: |
| 4050 | // C++ [over.built]p13: |
| 4051 | // |
| 4052 | // For every cv-qualified or cv-unqualified object type T there |
| 4053 | // exist candidate operator functions of the form |
| 4054 | // |
| 4055 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4056 | // T& operator[](T*, ptrdiff_t); |
| 4057 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4058 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4059 | // T& operator[](ptrdiff_t, T*); |
| 4060 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4061 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4062 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4063 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
| 4064 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
| 4065 | |
| 4066 | // T& operator[](T*, ptrdiff_t) |
| 4067 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4068 | |
| 4069 | // T& operator[](ptrdiff_t, T*); |
| 4070 | ParamTypes[0] = ParamTypes[1]; |
| 4071 | ParamTypes[1] = *Ptr; |
| 4072 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4073 | } |
| 4074 | break; |
| 4075 | |
| 4076 | case OO_ArrowStar: |
| 4077 | // C++ [over.built]p11: |
| 4078 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4079 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4080 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4081 | // there exist candidate operator functions of the form |
| 4082 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4083 | // where CV12 is the union of CV1 and CV2. |
| 4084 | { |
| 4085 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4086 | CandidateTypes.pointer_begin(); |
| 4087 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4088 | QualType C1Ty = (*Ptr); |
| 4089 | QualType C1; |
| 4090 | QualifierCollector Q1; |
| 4091 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
| 4092 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
| 4093 | if (!isa<RecordType>(C1)) |
| 4094 | continue; |
| 4095 | // heuristic to reduce number of builtin candidates in the set. |
| 4096 | // Add volatile/restrict version only if there are conversions to a |
| 4097 | // volatile/restrict type. |
| 4098 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4099 | continue; |
| 4100 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4101 | continue; |
| 4102 | } |
| 4103 | for (BuiltinCandidateTypeSet::iterator |
| 4104 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4105 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4106 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4107 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4108 | QualType C2 = QualType(mptr->getClass(), 0); |
| 4109 | C2 = C2.getUnqualifiedType(); |
| 4110 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4111 | break; |
| 4112 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4113 | // build CV12 T& |
| 4114 | QualType T = mptr->getPointeeType(); |
| 4115 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4116 | T.isVolatileQualified()) |
| 4117 | continue; |
| 4118 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4119 | T.isRestrictQualified()) |
| 4120 | continue; |
| 4121 | T = Q1.apply(T); |
| 4122 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4123 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | break; |
| 4128 | |
| 4129 | case OO_Conditional: |
| 4130 | // Note that we don't consider the first argument, since it has been |
| 4131 | // contextually converted to bool long ago. The candidates below are |
| 4132 | // therefore added as binary. |
| 4133 | // |
| 4134 | // C++ [over.built]p24: |
| 4135 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4136 | // there exist candidate operator functions of the form |
| 4137 | // |
| 4138 | // T operator?(bool, T, T); |
| 4139 | // |
| 4140 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4141 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4142 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4143 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4144 | } |
| 4145 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4146 | CandidateTypes.member_pointer_begin(), |
| 4147 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4148 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4149 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4150 | } |
| 4151 | goto Conditional; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | /// \brief Add function candidates found via argument-dependent lookup |
| 4156 | /// to the set of overloading candidates. |
| 4157 | /// |
| 4158 | /// This routine performs argument-dependent name lookup based on the |
| 4159 | /// given function name (which may also be an operator name) and adds |
| 4160 | /// all of the overload candidates found by ADL to the overload |
| 4161 | /// candidate set (C++ [basic.lookup.argdep]). |
| 4162 | void |
| 4163 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 4164 | bool Operator, |
| 4165 | Expr **Args, unsigned NumArgs, |
| 4166 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4167 | OverloadCandidateSet& CandidateSet, |
| 4168 | bool PartialOverloading) { |
| 4169 | ADLResult Fns; |
| 4170 | |
| 4171 | // FIXME: This approach for uniquing ADL results (and removing |
| 4172 | // redundant candidates from the set) relies on pointer-equality, |
| 4173 | // which means we need to key off the canonical decl. However, |
| 4174 | // always going back to the canonical decl might not get us the |
| 4175 | // right set of default arguments. What default arguments are |
| 4176 | // we supposed to consider on ADL candidates, anyway? |
| 4177 | |
| 4178 | // FIXME: Pass in the explicit template arguments? |
| 4179 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
| 4180 | |
| 4181 | // Erase all of the candidates we already knew about. |
| 4182 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4183 | CandEnd = CandidateSet.end(); |
| 4184 | Cand != CandEnd; ++Cand) |
| 4185 | if (Cand->Function) { |
| 4186 | Fns.erase(Cand->Function); |
| 4187 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4188 | Fns.erase(FunTmpl); |
| 4189 | } |
| 4190 | |
| 4191 | // For each of the ADL candidates we found, add it to the overload |
| 4192 | // set. |
| 4193 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 4194 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 4195 | if (ExplicitTemplateArgs) |
| 4196 | continue; |
| 4197 | |
| 4198 | AddOverloadCandidate(FD, AS_none, Args, NumArgs, CandidateSet, |
| 4199 | false, false, PartialOverloading); |
| 4200 | } else |
| 4201 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
| 4202 | AS_none, ExplicitTemplateArgs, |
| 4203 | Args, NumArgs, CandidateSet); |
| 4204 | } |
| 4205 | } |
| 4206 | |
| 4207 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4208 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
| 4209 | bool |
| 4210 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
| 4211 | const OverloadCandidate& Cand2, |
| 4212 | SourceLocation Loc) { |
| 4213 | // Define viable functions to be better candidates than non-viable |
| 4214 | // functions. |
| 4215 | if (!Cand2.Viable) |
| 4216 | return Cand1.Viable; |
| 4217 | else if (!Cand1.Viable) |
| 4218 | return false; |
| 4219 | |
| 4220 | // C++ [over.match.best]p1: |
| 4221 | // |
| 4222 | // -- if F is a static member function, ICS1(F) is defined such |
| 4223 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4224 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4225 | // better nor worse than ICS1(F). |
| 4226 | unsigned StartArg = 0; |
| 4227 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4228 | StartArg = 1; |
| 4229 | |
| 4230 | // C++ [over.match.best]p1: |
| 4231 | // A viable function F1 is defined to be a better function than another |
| 4232 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
| 4233 | // conversion sequence than ICSi(F2), and then... |
| 4234 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4235 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4236 | bool HasBetterConversion = false; |
| 4237 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
| 4238 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4239 | Cand2.Conversions[ArgIdx])) { |
| 4240 | case ImplicitConversionSequence::Better: |
| 4241 | // Cand1 has a better conversion sequence. |
| 4242 | HasBetterConversion = true; |
| 4243 | break; |
| 4244 | |
| 4245 | case ImplicitConversionSequence::Worse: |
| 4246 | // Cand1 can't be better than Cand2. |
| 4247 | return false; |
| 4248 | |
| 4249 | case ImplicitConversionSequence::Indistinguishable: |
| 4250 | // Do nothing. |
| 4251 | break; |
| 4252 | } |
| 4253 | } |
| 4254 | |
| 4255 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
| 4256 | // ICSj(F2), or, if not that, |
| 4257 | if (HasBetterConversion) |
| 4258 | return true; |
| 4259 | |
| 4260 | // - F1 is a non-template function and F2 is a function template |
| 4261 | // specialization, or, if not that, |
| 4262 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4263 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4264 | return true; |
| 4265 | |
| 4266 | // -- F1 and F2 are function template specializations, and the function |
| 4267 | // template for F1 is more specialized than the template for F2 |
| 4268 | // according to the partial ordering rules described in 14.5.5.2, or, |
| 4269 | // if not that, |
| 4270 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4271 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4272 | if (FunctionTemplateDecl *BetterTemplate |
| 4273 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4274 | Cand2.Function->getPrimaryTemplate(), |
| 4275 | Loc, |
| 4276 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4277 | : TPOC_Call)) |
| 4278 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
| 4279 | |
| 4280 | // -- the context is an initialization by user-defined conversion |
| 4281 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4282 | // from the return type of F1 to the destination type (i.e., |
| 4283 | // the type of the entity being initialized) is a better |
| 4284 | // conversion sequence than the standard conversion sequence |
| 4285 | // from the return type of F2 to the destination type. |
| 4286 | if (Cand1.Function && Cand2.Function && |
| 4287 | isa<CXXConversionDecl>(Cand1.Function) && |
| 4288 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4289 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4290 | Cand2.FinalConversion)) { |
| 4291 | case ImplicitConversionSequence::Better: |
| 4292 | // Cand1 has a better conversion sequence. |
| 4293 | return true; |
| 4294 | |
| 4295 | case ImplicitConversionSequence::Worse: |
| 4296 | // Cand1 can't be better than Cand2. |
| 4297 | return false; |
| 4298 | |
| 4299 | case ImplicitConversionSequence::Indistinguishable: |
| 4300 | // Do nothing |
| 4301 | break; |
| 4302 | } |
| 4303 | } |
| 4304 | |
| 4305 | return false; |
| 4306 | } |
| 4307 | |
| 4308 | /// \brief Computes the best viable function (C++ 13.3.3) |
| 4309 | /// within an overload candidate set. |
| 4310 | /// |
| 4311 | /// \param CandidateSet the set of candidate functions. |
| 4312 | /// |
| 4313 | /// \param Loc the location of the function name (or operator symbol) for |
| 4314 | /// which overload resolution occurs. |
| 4315 | /// |
| 4316 | /// \param Best f overload resolution was successful or found a deleted |
| 4317 | /// function, Best points to the candidate function found. |
| 4318 | /// |
| 4319 | /// \returns The result of overload resolution. |
| 4320 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4321 | SourceLocation Loc, |
| 4322 | OverloadCandidateSet::iterator& Best) { |
| 4323 | // Find the best viable function. |
| 4324 | Best = CandidateSet.end(); |
| 4325 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4326 | Cand != CandidateSet.end(); ++Cand) { |
| 4327 | if (Cand->Viable) { |
| 4328 | if (Best == CandidateSet.end() || |
| 4329 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
| 4330 | Best = Cand; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | // If we didn't find any viable functions, abort. |
| 4335 | if (Best == CandidateSet.end()) |
| 4336 | return OR_No_Viable_Function; |
| 4337 | |
| 4338 | // Make sure that this function is better than every other viable |
| 4339 | // function. If not, we have an ambiguity. |
| 4340 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4341 | Cand != CandidateSet.end(); ++Cand) { |
| 4342 | if (Cand->Viable && |
| 4343 | Cand != Best && |
| 4344 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
| 4345 | Best = CandidateSet.end(); |
| 4346 | return OR_Ambiguous; |
| 4347 | } |
| 4348 | } |
| 4349 | |
| 4350 | // Best is the best viable function. |
| 4351 | if (Best->Function && |
| 4352 | (Best->Function->isDeleted() || |
| 4353 | Best->Function->getAttr<UnavailableAttr>())) |
| 4354 | return OR_Deleted; |
| 4355 | |
| 4356 | // C++ [basic.def.odr]p2: |
| 4357 | // An overloaded function is used if it is selected by overload resolution |
| 4358 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4359 | // covers calls to named functions (5.2.2), operator overloading |
| 4360 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4361 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4362 | if (Best->Function) |
| 4363 | MarkDeclarationReferenced(Loc, Best->Function); |
| 4364 | return OR_Success; |
| 4365 | } |
| 4366 | |
| 4367 | namespace { |
| 4368 | |
| 4369 | enum OverloadCandidateKind { |
| 4370 | oc_function, |
| 4371 | oc_method, |
| 4372 | oc_constructor, |
| 4373 | oc_function_template, |
| 4374 | oc_method_template, |
| 4375 | oc_constructor_template, |
| 4376 | oc_implicit_default_constructor, |
| 4377 | oc_implicit_copy_constructor, |
| 4378 | oc_implicit_copy_assignment |
| 4379 | }; |
| 4380 | |
| 4381 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4382 | FunctionDecl *Fn, |
| 4383 | std::string &Description) { |
| 4384 | bool isTemplate = false; |
| 4385 | |
| 4386 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4387 | isTemplate = true; |
| 4388 | Description = S.getTemplateArgumentBindingsText( |
| 4389 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4390 | } |
| 4391 | |
| 4392 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
| 4393 | if (!Ctor->isImplicit()) |
| 4394 | return isTemplate ? oc_constructor_template : oc_constructor; |
| 4395 | |
| 4396 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4397 | : oc_implicit_default_constructor; |
| 4398 | } |
| 4399 | |
| 4400 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4401 | // This actually gets spelled 'candidate function' for now, but |
| 4402 | // it doesn't hurt to split it out. |
| 4403 | if (!Meth->isImplicit()) |
| 4404 | return isTemplate ? oc_method_template : oc_method; |
| 4405 | |
| 4406 | assert(Meth->isCopyAssignment() |
| 4407 | && "implicit method is not copy assignment operator?"); |
| 4408 | return oc_implicit_copy_assignment; |
| 4409 | } |
| 4410 | |
| 4411 | return isTemplate ? oc_function_template : oc_function; |
| 4412 | } |
| 4413 | |
| 4414 | } // end anonymous namespace |
| 4415 | |
| 4416 | // Notes the location of an overload candidate. |
| 4417 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
| 4418 | std::string FnDesc; |
| 4419 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4420 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4421 | << (unsigned) K << FnDesc; |
| 4422 | } |
| 4423 | |
| 4424 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4425 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4426 | /// target types of the conversion. |
| 4427 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4428 | SourceLocation CaretLoc, |
| 4429 | const PartialDiagnostic &PDiag) { |
| 4430 | Diag(CaretLoc, PDiag) |
| 4431 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4432 | for (AmbiguousConversionSequence::const_iterator |
| 4433 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4434 | NoteOverloadCandidate(*I); |
| 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | namespace { |
| 4439 | |
| 4440 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4441 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4442 | assert(Conv.isBad()); |
| 4443 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4444 | FunctionDecl *Fn = Cand->Function; |
| 4445 | |
| 4446 | // There's a conversion slot for the object argument if this is a |
| 4447 | // non-constructor method. Note that 'I' corresponds the |
| 4448 | // conversion-slot index. |
| 4449 | bool isObjectArgument = false; |
| 4450 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
| 4451 | if (I == 0) |
| 4452 | isObjectArgument = true; |
| 4453 | else |
| 4454 | I--; |
| 4455 | } |
| 4456 | |
| 4457 | std::string FnDesc; |
| 4458 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4459 | |
| 4460 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 4461 | QualType FromTy = Conv.Bad.getFromType(); |
| 4462 | QualType ToTy = Conv.Bad.getToType(); |
| 4463 | |
| 4464 | if (FromTy == S.Context.OverloadTy) { |
| 4465 | assert(FromExpr); |
| 4466 | Expr *E = FromExpr->IgnoreParens(); |
| 4467 | if (isa<UnaryOperator>(E)) |
| 4468 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
| 4469 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
| 4470 | |
| 4471 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 4472 | << (unsigned) FnKind << FnDesc |
| 4473 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4474 | << ToTy << Name << I+1; |
| 4475 | return; |
| 4476 | } |
| 4477 | |
| 4478 | // Do some hand-waving analysis to see if the non-viability is due |
| 4479 | // to a qualifier mismatch. |
| 4480 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 4481 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 4482 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 4483 | CToTy = RT->getPointeeType(); |
| 4484 | else { |
| 4485 | // TODO: detect and diagnose the full richness of const mismatches. |
| 4486 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 4487 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 4488 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 4489 | } |
| 4490 | |
| 4491 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 4492 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 4493 | // It is dumb that we have to do this here. |
| 4494 | while (isa<ArrayType>(CFromTy)) |
| 4495 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4496 | while (isa<ArrayType>(CToTy)) |
| 4497 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4498 | |
| 4499 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 4500 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 4501 | |
| 4502 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 4503 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 4504 | << (unsigned) FnKind << FnDesc |
| 4505 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4506 | << FromTy |
| 4507 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 4508 | << (unsigned) isObjectArgument << I+1; |
| 4509 | return; |
| 4510 | } |
| 4511 | |
| 4512 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4513 | assert(CVR && "unexpected qualifiers mismatch"); |
| 4514 | |
| 4515 | if (isObjectArgument) { |
| 4516 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 4517 | << (unsigned) FnKind << FnDesc |
| 4518 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4519 | << FromTy << (CVR - 1); |
| 4520 | } else { |
| 4521 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 4522 | << (unsigned) FnKind << FnDesc |
| 4523 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4524 | << FromTy << (CVR - 1) << I+1; |
| 4525 | } |
| 4526 | return; |
| 4527 | } |
| 4528 | |
| 4529 | // Diagnose references or pointers to incomplete types differently, |
| 4530 | // since it's far from impossible that the incompleteness triggered |
| 4531 | // the failure. |
| 4532 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 4533 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 4534 | TempFromTy = PTy->getPointeeType(); |
| 4535 | if (TempFromTy->isIncompleteType()) { |
| 4536 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 4537 | << (unsigned) FnKind << FnDesc |
| 4538 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4539 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 4540 | return; |
| 4541 | } |
| 4542 | |
| 4543 | // TODO: specialize more based on the kind of mismatch |
| 4544 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 4545 | << (unsigned) FnKind << FnDesc |
| 4546 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4547 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 4548 | } |
| 4549 | |
| 4550 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 4551 | unsigned NumFormalArgs) { |
| 4552 | // TODO: treat calls to a missing default constructor as a special case |
| 4553 | |
| 4554 | FunctionDecl *Fn = Cand->Function; |
| 4555 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 4556 | |
| 4557 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 4558 | |
| 4559 | // at least / at most / exactly |
| 4560 | unsigned mode, modeCount; |
| 4561 | if (NumFormalArgs < MinParams) { |
| 4562 | assert(Cand->FailureKind == ovl_fail_too_few_arguments); |
| 4563 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 4564 | mode = 0; // "at least" |
| 4565 | else |
| 4566 | mode = 2; // "exactly" |
| 4567 | modeCount = MinParams; |
| 4568 | } else { |
| 4569 | assert(Cand->FailureKind == ovl_fail_too_many_arguments); |
| 4570 | if (MinParams != FnTy->getNumArgs()) |
| 4571 | mode = 1; // "at most" |
| 4572 | else |
| 4573 | mode = 2; // "exactly" |
| 4574 | modeCount = FnTy->getNumArgs(); |
| 4575 | } |
| 4576 | |
| 4577 | std::string Description; |
| 4578 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 4579 | |
| 4580 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 4581 | << (unsigned) FnKind << Description << mode << modeCount << NumFormalArgs; |
| 4582 | } |
| 4583 | |
| 4584 | /// Diagnose a failed template-argument deduction. |
| 4585 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 4586 | Expr **Args, unsigned NumArgs) { |
| 4587 | FunctionDecl *Fn = Cand->Function; // pattern |
| 4588 | |
| 4589 | TemplateParameter Param = TemplateParameter::getFromOpaqueValue( |
| 4590 | Cand->DeductionFailure.TemplateParameter); |
| 4591 | |
| 4592 | switch (Cand->DeductionFailure.Result) { |
| 4593 | case Sema::TDK_Success: |
| 4594 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 4595 | |
| 4596 | case Sema::TDK_Incomplete: { |
| 4597 | NamedDecl *ParamD; |
| 4598 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 4599 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 4600 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
| 4601 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 4602 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 4603 | << ParamD->getDeclName(); |
| 4604 | return; |
| 4605 | } |
| 4606 | |
| 4607 | // TODO: diagnose these individually, then kill off |
| 4608 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
| 4609 | case Sema::TDK_InstantiationDepth: |
| 4610 | case Sema::TDK_Inconsistent: |
| 4611 | case Sema::TDK_InconsistentQuals: |
| 4612 | case Sema::TDK_SubstitutionFailure: |
| 4613 | case Sema::TDK_NonDeducedMismatch: |
| 4614 | case Sema::TDK_TooManyArguments: |
| 4615 | case Sema::TDK_TooFewArguments: |
| 4616 | case Sema::TDK_InvalidExplicitArguments: |
| 4617 | case Sema::TDK_FailedOverloadResolution: |
| 4618 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 4619 | return; |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 4624 | /// already generated a primary error at the call site. |
| 4625 | /// |
| 4626 | /// It really does need to be a single diagnostic with its caret |
| 4627 | /// pointed at the candidate declaration. Yes, this creates some |
| 4628 | /// major challenges of technical writing. Yes, this makes pointing |
| 4629 | /// out problems with specific arguments quite awkward. It's still |
| 4630 | /// better than generating twenty screens of text for every failed |
| 4631 | /// overload. |
| 4632 | /// |
| 4633 | /// It would be great to be able to express per-candidate problems |
| 4634 | /// more richly for those diagnostic clients that cared, but we'd |
| 4635 | /// still have to be just as careful with the default diagnostics. |
| 4636 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 4637 | Expr **Args, unsigned NumArgs) { |
| 4638 | FunctionDecl *Fn = Cand->Function; |
| 4639 | |
| 4640 | // Note deleted candidates, but only if they're viable. |
| 4641 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
| 4642 | std::string FnDesc; |
| 4643 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4644 | |
| 4645 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
| 4646 | << FnKind << FnDesc << Fn->isDeleted(); |
| 4647 | return; |
| 4648 | } |
| 4649 | |
| 4650 | // We don't really have anything else to say about viable candidates. |
| 4651 | if (Cand->Viable) { |
| 4652 | S.NoteOverloadCandidate(Fn); |
| 4653 | return; |
| 4654 | } |
| 4655 | |
| 4656 | switch (Cand->FailureKind) { |
| 4657 | case ovl_fail_too_many_arguments: |
| 4658 | case ovl_fail_too_few_arguments: |
| 4659 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
| 4660 | |
| 4661 | case ovl_fail_bad_deduction: |
| 4662 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 4663 | |
| 4664 | case ovl_fail_trivial_conversion: |
| 4665 | case ovl_fail_bad_final_conversion: |
| 4666 | return S.NoteOverloadCandidate(Fn); |
| 4667 | |
| 4668 | case ovl_fail_bad_conversion: |
| 4669 | for (unsigned I = 0, N = Cand->Conversions.size(); I != N; ++I) |
| 4670 | if (Cand->Conversions[I].isBad()) |
| 4671 | return DiagnoseBadConversion(S, Cand, I); |
| 4672 | |
| 4673 | // FIXME: this currently happens when we're called from SemaInit |
| 4674 | // when user-conversion overload fails. Figure out how to handle |
| 4675 | // those conditions and diagnose them well. |
| 4676 | return S.NoteOverloadCandidate(Fn); |
| 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4681 | // Desugar the type of the surrogate down to a function type, |
| 4682 | // retaining as many typedefs as possible while still showing |
| 4683 | // the function type (and, therefore, its parameter types). |
| 4684 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4685 | bool isLValueReference = false; |
| 4686 | bool isRValueReference = false; |
| 4687 | bool isPointer = false; |
| 4688 | if (const LValueReferenceType *FnTypeRef = |
| 4689 | FnType->getAs<LValueReferenceType>()) { |
| 4690 | FnType = FnTypeRef->getPointeeType(); |
| 4691 | isLValueReference = true; |
| 4692 | } else if (const RValueReferenceType *FnTypeRef = |
| 4693 | FnType->getAs<RValueReferenceType>()) { |
| 4694 | FnType = FnTypeRef->getPointeeType(); |
| 4695 | isRValueReference = true; |
| 4696 | } |
| 4697 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 4698 | FnType = FnTypePtr->getPointeeType(); |
| 4699 | isPointer = true; |
| 4700 | } |
| 4701 | // Desugar down to a function type. |
| 4702 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 4703 | // Reconstruct the pointer/reference as appropriate. |
| 4704 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 4705 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 4706 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 4707 | |
| 4708 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 4709 | << FnType; |
| 4710 | } |
| 4711 | |
| 4712 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 4713 | const char *Opc, |
| 4714 | SourceLocation OpLoc, |
| 4715 | OverloadCandidate *Cand) { |
| 4716 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 4717 | std::string TypeStr("operator"); |
| 4718 | TypeStr += Opc; |
| 4719 | TypeStr += "("; |
| 4720 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4721 | if (Cand->Conversions.size() == 1) { |
| 4722 | TypeStr += ")"; |
| 4723 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 4724 | } else { |
| 4725 | TypeStr += ", "; |
| 4726 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4727 | TypeStr += ")"; |
| 4728 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 4729 | } |
| 4730 | } |
| 4731 | |
| 4732 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 4733 | OverloadCandidate *Cand) { |
| 4734 | unsigned NoOperands = Cand->Conversions.size(); |
| 4735 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 4736 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
| 4737 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 4738 | if (!ICS.isAmbiguous()) continue; |
| 4739 | |
| 4740 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
| 4741 | PDiag(diag::note_ambiguous_type_conversion)); |
| 4742 | } |
| 4743 | } |
| 4744 | |
| 4745 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 4746 | if (Cand->Function) |
| 4747 | return Cand->Function->getLocation(); |
| 4748 | if (Cand->IsSurrogate) |
| 4749 | return Cand->Surrogate->getLocation(); |
| 4750 | return SourceLocation(); |
| 4751 | } |
| 4752 | |
| 4753 | struct CompareOverloadCandidatesForDisplay { |
| 4754 | Sema &S; |
| 4755 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
| 4756 | |
| 4757 | bool operator()(const OverloadCandidate *L, |
| 4758 | const OverloadCandidate *R) { |
| 4759 | // Fast-path this check. |
| 4760 | if (L == R) return false; |
| 4761 | |
| 4762 | // Order first by viability. |
| 4763 | if (L->Viable) { |
| 4764 | if (!R->Viable) return true; |
| 4765 | |
| 4766 | // TODO: introduce a tri-valued comparison for overload |
| 4767 | // candidates. Would be more worthwhile if we had a sort |
| 4768 | // that could exploit it. |
| 4769 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 4770 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
| 4771 | } else if (R->Viable) |
| 4772 | return false; |
| 4773 | |
| 4774 | assert(L->Viable == R->Viable); |
| 4775 | |
| 4776 | // Criteria by which we can sort non-viable candidates: |
| 4777 | if (!L->Viable) { |
| 4778 | // 1. Arity mismatches come after other candidates. |
| 4779 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 4780 | L->FailureKind == ovl_fail_too_few_arguments) |
| 4781 | return false; |
| 4782 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 4783 | R->FailureKind == ovl_fail_too_few_arguments) |
| 4784 | return true; |
| 4785 | |
| 4786 | // 2. Bad conversions come first and are ordered by the number |
| 4787 | // of bad conversions and quality of good conversions. |
| 4788 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 4789 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 4790 | return true; |
| 4791 | |
| 4792 | // If there's any ordering between the defined conversions... |
| 4793 | // FIXME: this might not be transitive. |
| 4794 | assert(L->Conversions.size() == R->Conversions.size()); |
| 4795 | |
| 4796 | int leftBetter = 0; |
| 4797 | for (unsigned I = 0, E = L->Conversions.size(); I != E; ++I) { |
| 4798 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 4799 | R->Conversions[I])) { |
| 4800 | case ImplicitConversionSequence::Better: |
| 4801 | leftBetter++; |
| 4802 | break; |
| 4803 | |
| 4804 | case ImplicitConversionSequence::Worse: |
| 4805 | leftBetter--; |
| 4806 | break; |
| 4807 | |
| 4808 | case ImplicitConversionSequence::Indistinguishable: |
| 4809 | break; |
| 4810 | } |
| 4811 | } |
| 4812 | if (leftBetter > 0) return true; |
| 4813 | if (leftBetter < 0) return false; |
| 4814 | |
| 4815 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 4816 | return false; |
| 4817 | |
| 4818 | // TODO: others? |
| 4819 | } |
| 4820 | |
| 4821 | // Sort everything else by location. |
| 4822 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 4823 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 4824 | |
| 4825 | // Put candidates without locations (e.g. builtins) at the end. |
| 4826 | if (LLoc.isInvalid()) return false; |
| 4827 | if (RLoc.isInvalid()) return true; |
| 4828 | |
| 4829 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
| 4830 | } |
| 4831 | }; |
| 4832 | |
| 4833 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 4834 | /// computes up to the first |
| 4835 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 4836 | Expr **Args, unsigned NumArgs) { |
| 4837 | assert(!Cand->Viable); |
| 4838 | |
| 4839 | // Don't do anything on failures other than bad conversion. |
| 4840 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 4841 | |
| 4842 | // Skip forward to the first bad conversion. |
| 4843 | unsigned ConvIdx = 0; |
| 4844 | unsigned ConvCount = Cand->Conversions.size(); |
| 4845 | while (true) { |
| 4846 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 4847 | ConvIdx++; |
| 4848 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 4849 | break; |
| 4850 | } |
| 4851 | |
| 4852 | if (ConvIdx == ConvCount) |
| 4853 | return; |
| 4854 | |
| 4855 | // FIXME: these should probably be preserved from the overload |
| 4856 | // operation somehow. |
| 4857 | bool SuppressUserConversions = false; |
| 4858 | bool ForceRValue = false; |
| 4859 | |
| 4860 | const FunctionProtoType* Proto; |
| 4861 | unsigned ArgIdx = ConvIdx; |
| 4862 | |
| 4863 | if (Cand->IsSurrogate) { |
| 4864 | QualType ConvType |
| 4865 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 4866 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4867 | ConvType = ConvPtrType->getPointeeType(); |
| 4868 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 4869 | ArgIdx--; |
| 4870 | } else if (Cand->Function) { |
| 4871 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 4872 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 4873 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 4874 | ArgIdx--; |
| 4875 | } else { |
| 4876 | // Builtin binary operator with a bad first conversion. |
| 4877 | assert(ConvCount <= 3); |
| 4878 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 4879 | Cand->Conversions[ConvIdx] |
| 4880 | = S.TryCopyInitialization(Args[ConvIdx], |
| 4881 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 4882 | SuppressUserConversions, ForceRValue, |
| 4883 | /*InOverloadResolution*/ true); |
| 4884 | return; |
| 4885 | } |
| 4886 | |
| 4887 | // Fill in the rest of the conversions. |
| 4888 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4889 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 4890 | if (ArgIdx < NumArgsInProto) |
| 4891 | Cand->Conversions[ConvIdx] |
| 4892 | = S.TryCopyInitialization(Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 4893 | SuppressUserConversions, ForceRValue, |
| 4894 | /*InOverloadResolution=*/true); |
| 4895 | else |
| 4896 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 4897 | } |
| 4898 | } |
| 4899 | |
| 4900 | } // end anonymous namespace |
| 4901 | |
| 4902 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4903 | /// diagnostic messages containing the candidates in the candidate |
| 4904 | /// set. |
| 4905 | void |
| 4906 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
| 4907 | OverloadCandidateDisplayKind OCD, |
| 4908 | Expr **Args, unsigned NumArgs, |
| 4909 | const char *Opc, |
| 4910 | SourceLocation OpLoc) { |
| 4911 | // Sort the candidates by viability and position. Sorting directly would |
| 4912 | // be prohibitive, so we make a set of pointers and sort those. |
| 4913 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 4914 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 4915 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4916 | LastCand = CandidateSet.end(); |
| 4917 | Cand != LastCand; ++Cand) { |
| 4918 | if (Cand->Viable) |
| 4919 | Cands.push_back(Cand); |
| 4920 | else if (OCD == OCD_AllCandidates) { |
| 4921 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
| 4922 | Cands.push_back(Cand); |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | std::sort(Cands.begin(), Cands.end(), |
| 4927 | CompareOverloadCandidatesForDisplay(*this)); |
| 4928 | |
| 4929 | bool ReportedAmbiguousConversions = false; |
| 4930 | |
| 4931 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 4932 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 4933 | OverloadCandidate *Cand = *I; |
| 4934 | |
| 4935 | if (Cand->Function) |
| 4936 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
| 4937 | else if (Cand->IsSurrogate) |
| 4938 | NoteSurrogateCandidate(*this, Cand); |
| 4939 | |
| 4940 | // This a builtin candidate. We do not, in general, want to list |
| 4941 | // every possible builtin candidate. |
| 4942 | else if (Cand->Viable) { |
| 4943 | // Generally we only see ambiguities including viable builtin |
| 4944 | // operators if overload resolution got screwed up by an |
| 4945 | // ambiguous user-defined conversion. |
| 4946 | // |
| 4947 | // FIXME: It's quite possible for different conversions to see |
| 4948 | // different ambiguities, though. |
| 4949 | if (!ReportedAmbiguousConversions) { |
| 4950 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 4951 | ReportedAmbiguousConversions = true; |
| 4952 | } |
| 4953 | |
| 4954 | // If this is a viable builtin, print it. |
| 4955 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
| 4956 | } |
| 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, NamedDecl *D, |
| 4961 | AccessSpecifier AS) { |
| 4962 | if (isa<UnresolvedLookupExpr>(E)) |
| 4963 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D, AS); |
| 4964 | |
| 4965 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D, AS); |
| 4966 | } |
| 4967 | |
| 4968 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4969 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4970 | /// expression with overloaded function type and @p ToType is the type |
| 4971 | /// we're trying to resolve to. For example: |
| 4972 | /// |
| 4973 | /// @code |
| 4974 | /// int f(double); |
| 4975 | /// int f(int); |
| 4976 | /// |
| 4977 | /// int (*pfd)(double) = f; // selects f(double) |
| 4978 | /// @endcode |
| 4979 | /// |
| 4980 | /// This routine returns the resulting FunctionDecl if it could be |
| 4981 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4982 | /// routine will emit diagnostics if there is an error. |
| 4983 | FunctionDecl * |
| 4984 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
| 4985 | bool Complain) { |
| 4986 | QualType FunctionType = ToType; |
| 4987 | bool IsMember = false; |
| 4988 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
| 4989 | FunctionType = ToTypePtr->getPointeeType(); |
| 4990 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
| 4991 | FunctionType = ToTypeRef->getPointeeType(); |
| 4992 | else if (const MemberPointerType *MemTypePtr = |
| 4993 | ToType->getAs<MemberPointerType>()) { |
| 4994 | FunctionType = MemTypePtr->getPointeeType(); |
| 4995 | IsMember = true; |
| 4996 | } |
| 4997 | |
| 4998 | // We only look at pointers or references to functions. |
| 4999 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 5000 | if (!FunctionType->isFunctionType()) |
| 5001 | return 0; |
| 5002 | |
| 5003 | // Find the actual overloaded function declaration. |
| 5004 | if (From->getType() != Context.OverloadTy) |
| 5005 | return 0; |
| 5006 | |
| 5007 | // C++ [over.over]p1: |
| 5008 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5009 | // overloaded function name is ignored (5.1). ] |
| 5010 | // C++ [over.over]p1: |
| 5011 | // [...] The overloaded function name can be preceded by the & |
| 5012 | // operator. |
| 5013 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 5014 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 5015 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 5016 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 5017 | ExplicitTemplateArgs = &ETABuffer; |
| 5018 | } |
| 5019 | |
| 5020 | // Look through all of the overloaded functions, searching for one |
| 5021 | // whose type matches exactly. |
| 5022 | UnresolvedSet<4> Matches; // contains only FunctionDecls |
| 5023 | bool FoundNonTemplateFunction = false; |
| 5024 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5025 | E = OvlExpr->decls_end(); I != E; ++I) { |
| 5026 | // Look through any using declarations to find the underlying function. |
| 5027 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 5028 | |
| 5029 | // C++ [over.over]p3: |
| 5030 | // Non-member functions and static member functions match |
| 5031 | // targets of type "pointer-to-function" or "reference-to-function." |
| 5032 | // Nonstatic member functions match targets of |
| 5033 | // type "pointer-to-member-function." |
| 5034 | // Note that according to DR 247, the containing class does not matter. |
| 5035 | |
| 5036 | if (FunctionTemplateDecl *FunctionTemplate |
| 5037 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 5038 | if (CXXMethodDecl *Method |
| 5039 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 5040 | // Skip non-static function templates when converting to pointer, and |
| 5041 | // static when converting to member pointer. |
| 5042 | if (Method->isStatic() == IsMember) |
| 5043 | continue; |
| 5044 | } else if (IsMember) |
| 5045 | continue; |
| 5046 | |
| 5047 | // C++ [over.over]p2: |
| 5048 | // If the name is a function template, template argument deduction is |
| 5049 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5050 | // resulting template argument list is used to generate a single |
| 5051 | // function template specialization, which is added to the set of |
| 5052 | // overloaded functions considered. |
| 5053 | // FIXME: We don't really want to build the specialization here, do we? |
| 5054 | FunctionDecl *Specialization = 0; |
| 5055 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 5056 | if (TemplateDeductionResult Result |
| 5057 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
| 5058 | FunctionType, Specialization, Info)) { |
| 5059 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5060 | (void)Result; |
| 5061 | } else { |
| 5062 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 5063 | // a candidate? Find a testcase before changing the code. |
| 5064 | assert(FunctionType |
| 5065 | == Context.getCanonicalType(Specialization->getType())); |
| 5066 | Matches.addDecl(cast<FunctionDecl>(Specialization->getCanonicalDecl()), |
| 5067 | I.getAccess()); |
| 5068 | } |
| 5069 | |
| 5070 | continue; |
| 5071 | } |
| 5072 | |
| 5073 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5074 | // Skip non-static functions when converting to pointer, and static |
| 5075 | // when converting to member pointer. |
| 5076 | if (Method->isStatic() == IsMember) |
| 5077 | continue; |
| 5078 | |
| 5079 | // If we have explicit template arguments, skip non-templates. |
| 5080 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 5081 | continue; |
| 5082 | } else if (IsMember) |
| 5083 | continue; |
| 5084 | |
| 5085 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
| 5086 | QualType ResultTy; |
| 5087 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 5088 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 5089 | ResultTy)) { |
| 5090 | Matches.addDecl(cast<FunctionDecl>(FunDecl->getCanonicalDecl()), |
| 5091 | I.getAccess()); |
| 5092 | FoundNonTemplateFunction = true; |
| 5093 | } |
| 5094 | } |
| 5095 | } |
| 5096 | |
| 5097 | // If there were 0 or 1 matches, we're done. |
| 5098 | if (Matches.empty()) |
| 5099 | return 0; |
| 5100 | else if (Matches.size() == 1) { |
| 5101 | FunctionDecl *Result = cast<FunctionDecl>(*Matches.begin()); |
| 5102 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 5103 | if (Complain) |
| 5104 | CheckUnresolvedAccess(*this, OvlExpr, Result, Matches.begin().getAccess()); |
| 5105 | return Result; |
| 5106 | } |
| 5107 | |
| 5108 | // C++ [over.over]p4: |
| 5109 | // If more than one function is selected, [...] |
| 5110 | if (!FoundNonTemplateFunction) { |
| 5111 | // [...] and any given function template specialization F1 is |
| 5112 | // eliminated if the set contains a second function template |
| 5113 | // specialization whose function template is more specialized |
| 5114 | // than the function template of F1 according to the partial |
| 5115 | // ordering rules of 14.5.5.2. |
| 5116 | |
| 5117 | // The algorithm specified above is quadratic. We instead use a |
| 5118 | // two-pass algorithm (similar to the one used to identify the |
| 5119 | // best viable function in an overload set) that identifies the |
| 5120 | // best function template (if it exists). |
| 5121 | |
| 5122 | UnresolvedSetIterator Result = |
| 5123 | getMostSpecialized(Matches.begin(), Matches.end(), |
| 5124 | TPOC_Other, From->getLocStart(), |
| 5125 | PDiag(), |
| 5126 | PDiag(diag::err_addr_ovl_ambiguous) |
| 5127 | << Matches[0]->getDeclName(), |
| 5128 | PDiag(diag::note_ovl_candidate) |
| 5129 | << (unsigned) oc_function_template); |
| 5130 | assert(Result != Matches.end() && "no most-specialized template"); |
| 5131 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
| 5132 | if (Complain) |
| 5133 | CheckUnresolvedAccess(*this, OvlExpr, *Result, Result.getAccess()); |
| 5134 | return cast<FunctionDecl>(*Result); |
| 5135 | } |
| 5136 | |
| 5137 | // [...] any function template specializations in the set are |
| 5138 | // eliminated if the set also contains a non-template function, [...] |
| 5139 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 5140 | if (cast<FunctionDecl>(Matches[I].getDecl())->getPrimaryTemplate() == 0) |
| 5141 | ++I; |
| 5142 | else { |
| 5143 | Matches.erase(I); |
| 5144 | --N; |
| 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | // [...] After such eliminations, if any, there shall remain exactly one |
| 5149 | // selected function. |
| 5150 | if (Matches.size() == 1) { |
| 5151 | UnresolvedSetIterator Match = Matches.begin(); |
| 5152 | MarkDeclarationReferenced(From->getLocStart(), *Match); |
| 5153 | if (Complain) |
| 5154 | CheckUnresolvedAccess(*this, OvlExpr, *Match, Match.getAccess()); |
| 5155 | return cast<FunctionDecl>(*Match); |
| 5156 | } |
| 5157 | |
| 5158 | // FIXME: We should probably return the same thing that BestViableFunction |
| 5159 | // returns (even if we issue the diagnostics here). |
| 5160 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 5161 | << Matches[0]->getDeclName(); |
| 5162 | for (UnresolvedSetIterator I = Matches.begin(), |
| 5163 | E = Matches.end(); I != E; ++I) |
| 5164 | NoteOverloadCandidate(cast<FunctionDecl>(*I)); |
| 5165 | return 0; |
| 5166 | } |
| 5167 | |
| 5168 | /// \brief Given an expression that refers to an overloaded function, try to |
| 5169 | /// resolve that overloaded function expression down to a single function. |
| 5170 | /// |
| 5171 | /// This routine can only resolve template-ids that refer to a single function |
| 5172 | /// template, where that template-id refers to a single template whose template |
| 5173 | /// arguments are either provided by the template-id or have defaults, |
| 5174 | /// as described in C++0x [temp.arg.explicit]p3. |
| 5175 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 5176 | // C++ [over.over]p1: |
| 5177 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5178 | // overloaded function name is ignored (5.1). ] |
| 5179 | // C++ [over.over]p1: |
| 5180 | // [...] The overloaded function name can be preceded by the & |
| 5181 | // operator. |
| 5182 | |
| 5183 | if (From->getType() != Context.OverloadTy) |
| 5184 | return 0; |
| 5185 | |
| 5186 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 5187 | |
| 5188 | // If we didn't actually find any template-ids, we're done. |
| 5189 | if (!OvlExpr->hasExplicitTemplateArgs()) |
| 5190 | return 0; |
| 5191 | |
| 5192 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 5193 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
| 5194 | |
| 5195 | // Look through all of the overloaded functions, searching for one |
| 5196 | // whose type matches exactly. |
| 5197 | FunctionDecl *Matched = 0; |
| 5198 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5199 | E = OvlExpr->decls_end(); I != E; ++I) { |
| 5200 | // C++0x [temp.arg.explicit]p3: |
| 5201 | // [...] In contexts where deduction is done and fails, or in contexts |
| 5202 | // where deduction is not done, if a template argument list is |
| 5203 | // specified and it, along with any default template arguments, |
| 5204 | // identifies a single function template specialization, then the |
| 5205 | // template-id is an lvalue for the function template specialization. |
| 5206 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 5207 | |
| 5208 | // C++ [over.over]p2: |
| 5209 | // If the name is a function template, template argument deduction is |
| 5210 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5211 | // resulting template argument list is used to generate a single |
| 5212 | // function template specialization, which is added to the set of |
| 5213 | // overloaded functions considered. |
| 5214 | FunctionDecl *Specialization = 0; |
| 5215 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 5216 | if (TemplateDeductionResult Result |
| 5217 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 5218 | Specialization, Info)) { |
| 5219 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5220 | (void)Result; |
| 5221 | continue; |
| 5222 | } |
| 5223 | |
| 5224 | // Multiple matches; we can't resolve to a single declaration. |
| 5225 | if (Matched) |
| 5226 | return 0; |
| 5227 | |
| 5228 | Matched = Specialization; |
| 5229 | } |
| 5230 | |
| 5231 | return Matched; |
| 5232 | } |
| 5233 | |
| 5234 | /// \brief Add a single candidate to the overload set. |
| 5235 | static void AddOverloadedCallCandidate(Sema &S, |
| 5236 | NamedDecl *Callee, |
| 5237 | AccessSpecifier Access, |
| 5238 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 5239 | Expr **Args, unsigned NumArgs, |
| 5240 | OverloadCandidateSet &CandidateSet, |
| 5241 | bool PartialOverloading) { |
| 5242 | if (isa<UsingShadowDecl>(Callee)) |
| 5243 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5244 | |
| 5245 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
| 5246 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
| 5247 | S.AddOverloadCandidate(Func, Access, Args, NumArgs, CandidateSet, |
| 5248 | false, false, PartialOverloading); |
| 5249 | return; |
| 5250 | } |
| 5251 | |
| 5252 | if (FunctionTemplateDecl *FuncTemplate |
| 5253 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
| 5254 | S.AddTemplateOverloadCandidate(FuncTemplate, Access, ExplicitTemplateArgs, |
| 5255 | Args, NumArgs, CandidateSet); |
| 5256 | return; |
| 5257 | } |
| 5258 | |
| 5259 | assert(false && "unhandled case in overloaded call candidate"); |
| 5260 | |
| 5261 | // do nothing? |
| 5262 | } |
| 5263 | |
| 5264 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5265 | /// dependent lookup to the given overload set. |
| 5266 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
| 5267 | Expr **Args, unsigned NumArgs, |
| 5268 | OverloadCandidateSet &CandidateSet, |
| 5269 | bool PartialOverloading) { |
| 5270 | |
| 5271 | #ifndef NDEBUG |
| 5272 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5273 | // in C++0x [basic.lookup.argdep]p3: |
| 5274 | // |
| 5275 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5276 | // and let Y be the lookup set produced by argument dependent |
| 5277 | // lookup (defined as follows). If X contains |
| 5278 | // |
| 5279 | // -- a declaration of a class member, or |
| 5280 | // |
| 5281 | // -- a block-scope function declaration that is not a |
| 5282 | // using-declaration, or |
| 5283 | // |
| 5284 | // -- a declaration that is neither a function or a function |
| 5285 | // template |
| 5286 | // |
| 5287 | // then Y is empty. |
| 5288 | |
| 5289 | if (ULE->requiresADL()) { |
| 5290 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5291 | E = ULE->decls_end(); I != E; ++I) { |
| 5292 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5293 | assert(isa<UsingShadowDecl>(*I) || |
| 5294 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5295 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
| 5296 | } |
| 5297 | } |
| 5298 | #endif |
| 5299 | |
| 5300 | // It would be nice to avoid this copy. |
| 5301 | TemplateArgumentListInfo TABuffer; |
| 5302 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5303 | if (ULE->hasExplicitTemplateArgs()) { |
| 5304 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5305 | ExplicitTemplateArgs = &TABuffer; |
| 5306 | } |
| 5307 | |
| 5308 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5309 | E = ULE->decls_end(); I != E; ++I) |
| 5310 | AddOverloadedCallCandidate(*this, *I, I.getAccess(), ExplicitTemplateArgs, |
| 5311 | Args, NumArgs, CandidateSet, |
| 5312 | PartialOverloading); |
| 5313 | |
| 5314 | if (ULE->requiresADL()) |
| 5315 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 5316 | Args, NumArgs, |
| 5317 | ExplicitTemplateArgs, |
| 5318 | CandidateSet, |
| 5319 | PartialOverloading); |
| 5320 | } |
| 5321 | |
| 5322 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5323 | Expr **Args, unsigned NumArgs) { |
| 5324 | Fn->Destroy(SemaRef.Context); |
| 5325 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5326 | Args[Arg]->Destroy(SemaRef.Context); |
| 5327 | return SemaRef.ExprError(); |
| 5328 | } |
| 5329 | |
| 5330 | /// Attempts to recover from a call where no functions were found. |
| 5331 | /// |
| 5332 | /// Returns true if new candidates were found. |
| 5333 | static Sema::OwningExprResult |
| 5334 | BuildRecoveryCallExpr(Sema &SemaRef, Expr *Fn, |
| 5335 | UnresolvedLookupExpr *ULE, |
| 5336 | SourceLocation LParenLoc, |
| 5337 | Expr **Args, unsigned NumArgs, |
| 5338 | SourceLocation *CommaLocs, |
| 5339 | SourceLocation RParenLoc) { |
| 5340 | |
| 5341 | CXXScopeSpec SS; |
| 5342 | if (ULE->getQualifier()) { |
| 5343 | SS.setScopeRep(ULE->getQualifier()); |
| 5344 | SS.setRange(ULE->getQualifierRange()); |
| 5345 | } |
| 5346 | |
| 5347 | TemplateArgumentListInfo TABuffer; |
| 5348 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5349 | if (ULE->hasExplicitTemplateArgs()) { |
| 5350 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5351 | ExplicitTemplateArgs = &TABuffer; |
| 5352 | } |
| 5353 | |
| 5354 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5355 | Sema::LookupOrdinaryName); |
| 5356 | if (SemaRef.DiagnoseEmptyLookup(/*Scope=*/0, SS, R)) |
| 5357 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5358 | |
| 5359 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5360 | |
| 5361 | // Build an implicit member call if appropriate. Just drop the |
| 5362 | // casts and such from the call, we don't really care. |
| 5363 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5364 | if ((*R.begin())->isCXXClassMember()) |
| 5365 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5366 | else if (ExplicitTemplateArgs) |
| 5367 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5368 | else |
| 5369 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 5370 | |
| 5371 | if (NewFn.isInvalid()) |
| 5372 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5373 | |
| 5374 | Fn->Destroy(SemaRef.Context); |
| 5375 | |
| 5376 | // This shouldn't cause an infinite loop because we're giving it |
| 5377 | // an expression with non-empty lookup results, which should never |
| 5378 | // end up here. |
| 5379 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 5380 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 5381 | CommaLocs, RParenLoc); |
| 5382 | } |
| 5383 | |
| 5384 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
| 5385 | /// (which eventually refers to the declaration Func) and the call |
| 5386 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 5387 | /// to a specific function. If overload resolution succeeds, returns |
| 5388 | /// the function declaration produced by overload |
| 5389 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
| 5390 | /// arguments and Fn, and returns NULL. |
| 5391 | Sema::OwningExprResult |
| 5392 | Sema::BuildOverloadedCallExpr(Expr *Fn, UnresolvedLookupExpr *ULE, |
| 5393 | SourceLocation LParenLoc, |
| 5394 | Expr **Args, unsigned NumArgs, |
| 5395 | SourceLocation *CommaLocs, |
| 5396 | SourceLocation RParenLoc) { |
| 5397 | #ifndef NDEBUG |
| 5398 | if (ULE->requiresADL()) { |
| 5399 | // To do ADL, we must have found an unqualified name. |
| 5400 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 5401 | |
| 5402 | // We don't perform ADL for implicit declarations of builtins. |
| 5403 | // Verify that this was correctly set up. |
| 5404 | FunctionDecl *F; |
| 5405 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5406 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5407 | F->getBuiltinID() && F->isImplicit()) |
| 5408 | assert(0 && "performing ADL for builtin"); |
| 5409 | |
| 5410 | // We don't perform ADL in C. |
| 5411 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5412 | } |
| 5413 | #endif |
| 5414 | |
| 5415 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
| 5416 | |
| 5417 | // Add the functions denoted by the callee to the set of candidate |
| 5418 | // functions, including those from argument-dependent lookup. |
| 5419 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
| 5420 | |
| 5421 | // If we found nothing, try to recover. |
| 5422 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5423 | // bailout out if it fails. |
| 5424 | if (CandidateSet.empty()) |
| 5425 | return BuildRecoveryCallExpr(*this, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5426 | CommaLocs, RParenLoc); |
| 5427 | |
| 5428 | OverloadCandidateSet::iterator Best; |
| 5429 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
| 5430 | case OR_Success: { |
| 5431 | FunctionDecl *FDecl = Best->Function; |
| 5432 | CheckUnresolvedLookupAccess(ULE, FDecl, Best->getAccess()); |
| 5433 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 5434 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5435 | } |
| 5436 | |
| 5437 | case OR_No_Viable_Function: |
| 5438 | Diag(Fn->getSourceRange().getBegin(), |
| 5439 | diag::err_ovl_no_viable_function_in_call) |
| 5440 | << ULE->getName() << Fn->getSourceRange(); |
| 5441 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 5442 | break; |
| 5443 | |
| 5444 | case OR_Ambiguous: |
| 5445 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
| 5446 | << ULE->getName() << Fn->getSourceRange(); |
| 5447 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
| 5448 | break; |
| 5449 | |
| 5450 | case OR_Deleted: |
| 5451 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5452 | << Best->Function->isDeleted() |
| 5453 | << ULE->getName() |
| 5454 | << Fn->getSourceRange(); |
| 5455 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 5456 | break; |
| 5457 | } |
| 5458 | |
| 5459 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5460 | // return NULL. |
| 5461 | Fn->Destroy(Context); |
| 5462 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5463 | Args[Arg]->Destroy(Context); |
| 5464 | return ExprError(); |
| 5465 | } |
| 5466 | |
| 5467 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
| 5468 | return Functions.size() > 1 || |
| 5469 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5470 | } |
| 5471 | |
| 5472 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5473 | /// operator. |
| 5474 | /// |
| 5475 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5476 | /// |
| 5477 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5478 | /// operator. |
| 5479 | /// |
| 5480 | /// \param Functions The set of non-member functions that will be |
| 5481 | /// considered by overload resolution. The caller needs to build this |
| 5482 | /// set based on the context using, e.g., |
| 5483 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5484 | /// set should not contain any member functions; those will be added |
| 5485 | /// by CreateOverloadedUnaryOp(). |
| 5486 | /// |
| 5487 | /// \param input The input argument. |
| 5488 | Sema::OwningExprResult |
| 5489 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 5490 | const UnresolvedSetImpl &Fns, |
| 5491 | ExprArg input) { |
| 5492 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5493 | Expr *Input = (Expr *)input.get(); |
| 5494 | |
| 5495 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5496 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5497 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5498 | |
| 5499 | Expr *Args[2] = { Input, 0 }; |
| 5500 | unsigned NumArgs = 1; |
| 5501 | |
| 5502 | // For post-increment and post-decrement, add the implicit '0' as |
| 5503 | // the second argument, so that we know this is a post-increment or |
| 5504 | // post-decrement. |
| 5505 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5506 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
| 5507 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
| 5508 | SourceLocation()); |
| 5509 | NumArgs = 2; |
| 5510 | } |
| 5511 | |
| 5512 | if (Input->isTypeDependent()) { |
| 5513 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
| 5514 | UnresolvedLookupExpr *Fn |
| 5515 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
| 5516 | 0, SourceRange(), OpName, OpLoc, |
| 5517 | /*ADL*/ true, IsOverloaded(Fns)); |
| 5518 | Fn->addDecls(Fns.begin(), Fns.end()); |
| 5519 | |
| 5520 | input.release(); |
| 5521 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5522 | &Args[0], NumArgs, |
| 5523 | Context.DependentTy, |
| 5524 | OpLoc)); |
| 5525 | } |
| 5526 | |
| 5527 | // Build an empty overload set. |
| 5528 | OverloadCandidateSet CandidateSet(OpLoc); |
| 5529 | |
| 5530 | // Add the candidates from the given function set. |
| 5531 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
| 5532 | |
| 5533 | // Add operator candidates that are member functions. |
| 5534 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5535 | |
| 5536 | // Add candidates from ADL. |
| 5537 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 5538 | Args, NumArgs, |
| 5539 | /*ExplicitTemplateArgs*/ 0, |
| 5540 | CandidateSet); |
| 5541 | |
| 5542 | // Add builtin operator candidates. |
| 5543 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5544 | |
| 5545 | // Perform overload resolution. |
| 5546 | OverloadCandidateSet::iterator Best; |
| 5547 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
| 5548 | case OR_Success: { |
| 5549 | // We found a built-in operator or an overloaded operator. |
| 5550 | FunctionDecl *FnDecl = Best->Function; |
| 5551 | |
| 5552 | if (FnDecl) { |
| 5553 | // We matched an overloaded operator. Build a call to that |
| 5554 | // operator. |
| 5555 | |
| 5556 | // Convert the arguments. |
| 5557 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5558 | CheckMemberOperatorAccess(OpLoc, Args[0], Method, Best->getAccess()); |
| 5559 | |
| 5560 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 5561 | return ExprError(); |
| 5562 | } else { |
| 5563 | // Convert the arguments. |
| 5564 | OwningExprResult InputInit |
| 5565 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 5566 | FnDecl->getParamDecl(0)), |
| 5567 | SourceLocation(), |
| 5568 | move(input)); |
| 5569 | if (InputInit.isInvalid()) |
| 5570 | return ExprError(); |
| 5571 | |
| 5572 | input = move(InputInit); |
| 5573 | Input = (Expr *)input.get(); |
| 5574 | } |
| 5575 | |
| 5576 | // Determine the result type |
| 5577 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
| 5578 | |
| 5579 | // Build the actual expression node. |
| 5580 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5581 | SourceLocation()); |
| 5582 | UsualUnaryConversions(FnExpr); |
| 5583 | |
| 5584 | input.release(); |
| 5585 | Args[0] = Input; |
| 5586 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5587 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5588 | Args, NumArgs, ResultTy, OpLoc)); |
| 5589 | |
| 5590 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5591 | FnDecl)) |
| 5592 | return ExprError(); |
| 5593 | |
| 5594 | return MaybeBindToTemporary(TheCall.release()); |
| 5595 | } else { |
| 5596 | // We matched a built-in operator. Convert the arguments, then |
| 5597 | // break out so that we will build the appropriate built-in |
| 5598 | // operator node. |
| 5599 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 5600 | Best->Conversions[0], AA_Passing)) |
| 5601 | return ExprError(); |
| 5602 | |
| 5603 | break; |
| 5604 | } |
| 5605 | } |
| 5606 | |
| 5607 | case OR_No_Viable_Function: |
| 5608 | // No viable function; fall through to handling this as a |
| 5609 | // built-in operator, which will produce an error message for us. |
| 5610 | break; |
| 5611 | |
| 5612 | case OR_Ambiguous: |
| 5613 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5614 | << UnaryOperator::getOpcodeStr(Opc) |
| 5615 | << Input->getSourceRange(); |
| 5616 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
| 5617 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
| 5618 | return ExprError(); |
| 5619 | |
| 5620 | case OR_Deleted: |
| 5621 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5622 | << Best->Function->isDeleted() |
| 5623 | << UnaryOperator::getOpcodeStr(Opc) |
| 5624 | << Input->getSourceRange(); |
| 5625 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 5626 | return ExprError(); |
| 5627 | } |
| 5628 | |
| 5629 | // Either we found no viable overloaded operator or we matched a |
| 5630 | // built-in operator. In either case, fall through to trying to |
| 5631 | // build a built-in operation. |
| 5632 | input.release(); |
| 5633 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5634 | } |
| 5635 | |
| 5636 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5637 | /// operator. |
| 5638 | /// |
| 5639 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5640 | /// |
| 5641 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5642 | /// operator. |
| 5643 | /// |
| 5644 | /// \param Functions The set of non-member functions that will be |
| 5645 | /// considered by overload resolution. The caller needs to build this |
| 5646 | /// set based on the context using, e.g., |
| 5647 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5648 | /// set should not contain any member functions; those will be added |
| 5649 | /// by CreateOverloadedBinOp(). |
| 5650 | /// |
| 5651 | /// \param LHS Left-hand argument. |
| 5652 | /// \param RHS Right-hand argument. |
| 5653 | Sema::OwningExprResult |
| 5654 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
| 5655 | unsigned OpcIn, |
| 5656 | const UnresolvedSetImpl &Fns, |
| 5657 | Expr *LHS, Expr *RHS) { |
| 5658 | Expr *Args[2] = { LHS, RHS }; |
| 5659 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
| 5660 | |
| 5661 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5662 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5663 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5664 | |
| 5665 | // If either side is type-dependent, create an appropriate dependent |
| 5666 | // expression. |
| 5667 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5668 | if (Fns.empty()) { |
| 5669 | // If there are no functions to store, just build a dependent |
| 5670 | // BinaryOperator or CompoundAssignment. |
| 5671 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5672 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5673 | Context.DependentTy, OpLoc)); |
| 5674 | |
| 5675 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5676 | Context.DependentTy, |
| 5677 | Context.DependentTy, |
| 5678 | Context.DependentTy, |
| 5679 | OpLoc)); |
| 5680 | } |
| 5681 | |
| 5682 | // FIXME: save results of ADL from here? |
| 5683 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
| 5684 | UnresolvedLookupExpr *Fn |
| 5685 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
| 5686 | 0, SourceRange(), OpName, OpLoc, |
| 5687 | /*ADL*/ true, IsOverloaded(Fns)); |
| 5688 | |
| 5689 | Fn->addDecls(Fns.begin(), Fns.end()); |
| 5690 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5691 | Args, 2, |
| 5692 | Context.DependentTy, |
| 5693 | OpLoc)); |
| 5694 | } |
| 5695 | |
| 5696 | // If this is the .* operator, which is not overloadable, just |
| 5697 | // create a built-in binary operator. |
| 5698 | if (Opc == BinaryOperator::PtrMemD) |
| 5699 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 5700 | |
| 5701 | // If this is the assignment operator, we only perform overload resolution |
| 5702 | // if the left-hand side is a class or enumeration type. This is actually |
| 5703 | // a hack. The standard requires that we do overload resolution between the |
| 5704 | // various built-in candidates, but as DR507 points out, this can lead to |
| 5705 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 5706 | // Note that we go the traditional code path for compound assignment forms. |
| 5707 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
| 5708 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 5709 | |
| 5710 | // Build an empty overload set. |
| 5711 | OverloadCandidateSet CandidateSet(OpLoc); |
| 5712 | |
| 5713 | // Add the candidates from the given function set. |
| 5714 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
| 5715 | |
| 5716 | // Add operator candidates that are member functions. |
| 5717 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5718 | |
| 5719 | // Add candidates from ADL. |
| 5720 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 5721 | Args, 2, |
| 5722 | /*ExplicitTemplateArgs*/ 0, |
| 5723 | CandidateSet); |
| 5724 | |
| 5725 | // Add builtin operator candidates. |
| 5726 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5727 | |
| 5728 | // Perform overload resolution. |
| 5729 | OverloadCandidateSet::iterator Best; |
| 5730 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
| 5731 | case OR_Success: { |
| 5732 | // We found a built-in operator or an overloaded operator. |
| 5733 | FunctionDecl *FnDecl = Best->Function; |
| 5734 | |
| 5735 | if (FnDecl) { |
| 5736 | // We matched an overloaded operator. Build a call to that |
| 5737 | // operator. |
| 5738 | |
| 5739 | // Convert the arguments. |
| 5740 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5741 | // Best->Access is only meaningful for class members. |
| 5742 | CheckMemberOperatorAccess(OpLoc, Args[0], Method, Best->getAccess()); |
| 5743 | |
| 5744 | OwningExprResult Arg1 |
| 5745 | = PerformCopyInitialization( |
| 5746 | InitializedEntity::InitializeParameter( |
| 5747 | FnDecl->getParamDecl(0)), |
| 5748 | SourceLocation(), |
| 5749 | Owned(Args[1])); |
| 5750 | if (Arg1.isInvalid()) |
| 5751 | return ExprError(); |
| 5752 | |
| 5753 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
| 5754 | return ExprError(); |
| 5755 | |
| 5756 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
| 5757 | } else { |
| 5758 | // Convert the arguments. |
| 5759 | OwningExprResult Arg0 |
| 5760 | = PerformCopyInitialization( |
| 5761 | InitializedEntity::InitializeParameter( |
| 5762 | FnDecl->getParamDecl(0)), |
| 5763 | SourceLocation(), |
| 5764 | Owned(Args[0])); |
| 5765 | if (Arg0.isInvalid()) |
| 5766 | return ExprError(); |
| 5767 | |
| 5768 | OwningExprResult Arg1 |
| 5769 | = PerformCopyInitialization( |
| 5770 | InitializedEntity::InitializeParameter( |
| 5771 | FnDecl->getParamDecl(1)), |
| 5772 | SourceLocation(), |
| 5773 | Owned(Args[1])); |
| 5774 | if (Arg1.isInvalid()) |
| 5775 | return ExprError(); |
| 5776 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 5777 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
| 5778 | } |
| 5779 | |
| 5780 | // Determine the result type |
| 5781 | QualType ResultTy |
| 5782 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5783 | ResultTy = ResultTy.getNonReferenceType(); |
| 5784 | |
| 5785 | // Build the actual expression node. |
| 5786 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5787 | OpLoc); |
| 5788 | UsualUnaryConversions(FnExpr); |
| 5789 | |
| 5790 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5791 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5792 | Args, 2, ResultTy, |
| 5793 | OpLoc)); |
| 5794 | |
| 5795 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5796 | FnDecl)) |
| 5797 | return ExprError(); |
| 5798 | |
| 5799 | return MaybeBindToTemporary(TheCall.release()); |
| 5800 | } else { |
| 5801 | // We matched a built-in operator. Convert the arguments, then |
| 5802 | // break out so that we will build the appropriate built-in |
| 5803 | // operator node. |
| 5804 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 5805 | Best->Conversions[0], AA_Passing) || |
| 5806 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 5807 | Best->Conversions[1], AA_Passing)) |
| 5808 | return ExprError(); |
| 5809 | |
| 5810 | break; |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | case OR_No_Viable_Function: { |
| 5815 | // C++ [over.match.oper]p9: |
| 5816 | // If the operator is the operator , [...] and there are no |
| 5817 | // viable functions, then the operator is assumed to be the |
| 5818 | // built-in operator and interpreted according to clause 5. |
| 5819 | if (Opc == BinaryOperator::Comma) |
| 5820 | break; |
| 5821 | |
| 5822 | // For class as left operand for assignment or compound assigment operator |
| 5823 | // do not fall through to handling in built-in, but report that no overloaded |
| 5824 | // assignment operator found |
| 5825 | OwningExprResult Result = ExprError(); |
| 5826 | if (Args[0]->getType()->isRecordType() && |
| 5827 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
| 5828 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 5829 | << BinaryOperator::getOpcodeStr(Opc) |
| 5830 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5831 | } else { |
| 5832 | // No viable function; try to create a built-in operation, which will |
| 5833 | // produce an error. Then, show the non-viable candidates. |
| 5834 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 5835 | } |
| 5836 | assert(Result.isInvalid() && |
| 5837 | "C++ binary operator overloading is missing candidates!"); |
| 5838 | if (Result.isInvalid()) |
| 5839 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
| 5840 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
| 5841 | return move(Result); |
| 5842 | } |
| 5843 | |
| 5844 | case OR_Ambiguous: |
| 5845 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5846 | << BinaryOperator::getOpcodeStr(Opc) |
| 5847 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5848 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
| 5849 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
| 5850 | return ExprError(); |
| 5851 | |
| 5852 | case OR_Deleted: |
| 5853 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5854 | << Best->Function->isDeleted() |
| 5855 | << BinaryOperator::getOpcodeStr(Opc) |
| 5856 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5857 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
| 5858 | return ExprError(); |
| 5859 | } |
| 5860 | |
| 5861 | // We matched a built-in operator; build it. |
| 5862 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 5863 | } |
| 5864 | |
| 5865 | Action::OwningExprResult |
| 5866 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 5867 | SourceLocation RLoc, |
| 5868 | ExprArg Base, ExprArg Idx) { |
| 5869 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 5870 | static_cast<Expr*>(Idx.get()) }; |
| 5871 | DeclarationName OpName = |
| 5872 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 5873 | |
| 5874 | // If either side is type-dependent, create an appropriate dependent |
| 5875 | // expression. |
| 5876 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5877 | |
| 5878 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
| 5879 | UnresolvedLookupExpr *Fn |
| 5880 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
| 5881 | 0, SourceRange(), OpName, LLoc, |
| 5882 | /*ADL*/ true, /*Overloaded*/ false); |
| 5883 | // Can't add any actual overloads yet |
| 5884 | |
| 5885 | Base.release(); |
| 5886 | Idx.release(); |
| 5887 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5888 | Args, 2, |
| 5889 | Context.DependentTy, |
| 5890 | RLoc)); |
| 5891 | } |
| 5892 | |
| 5893 | // Build an empty overload set. |
| 5894 | OverloadCandidateSet CandidateSet(LLoc); |
| 5895 | |
| 5896 | // Subscript can only be overloaded as a member function. |
| 5897 | |
| 5898 | // Add operator candidates that are member functions. |
| 5899 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5900 | |
| 5901 | // Add builtin operator candidates. |
| 5902 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5903 | |
| 5904 | // Perform overload resolution. |
| 5905 | OverloadCandidateSet::iterator Best; |
| 5906 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5907 | case OR_Success: { |
| 5908 | // We found a built-in operator or an overloaded operator. |
| 5909 | FunctionDecl *FnDecl = Best->Function; |
| 5910 | |
| 5911 | if (FnDecl) { |
| 5912 | // We matched an overloaded operator. Build a call to that |
| 5913 | // operator. |
| 5914 | |
| 5915 | CheckMemberOperatorAccess(LLoc, Args[0], FnDecl, Best->getAccess()); |
| 5916 | |
| 5917 | // Convert the arguments. |
| 5918 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5919 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
| 5920 | return ExprError(); |
| 5921 | |
| 5922 | // Convert the arguments. |
| 5923 | OwningExprResult InputInit |
| 5924 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 5925 | FnDecl->getParamDecl(0)), |
| 5926 | SourceLocation(), |
| 5927 | Owned(Args[1])); |
| 5928 | if (InputInit.isInvalid()) |
| 5929 | return ExprError(); |
| 5930 | |
| 5931 | Args[1] = InputInit.takeAs<Expr>(); |
| 5932 | |
| 5933 | // Determine the result type |
| 5934 | QualType ResultTy |
| 5935 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5936 | ResultTy = ResultTy.getNonReferenceType(); |
| 5937 | |
| 5938 | // Build the actual expression node. |
| 5939 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5940 | LLoc); |
| 5941 | UsualUnaryConversions(FnExpr); |
| 5942 | |
| 5943 | Base.release(); |
| 5944 | Idx.release(); |
| 5945 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5946 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5947 | FnExpr, Args, 2, |
| 5948 | ResultTy, RLoc)); |
| 5949 | |
| 5950 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5951 | FnDecl)) |
| 5952 | return ExprError(); |
| 5953 | |
| 5954 | return MaybeBindToTemporary(TheCall.release()); |
| 5955 | } else { |
| 5956 | // We matched a built-in operator. Convert the arguments, then |
| 5957 | // break out so that we will build the appropriate built-in |
| 5958 | // operator node. |
| 5959 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 5960 | Best->Conversions[0], AA_Passing) || |
| 5961 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 5962 | Best->Conversions[1], AA_Passing)) |
| 5963 | return ExprError(); |
| 5964 | |
| 5965 | break; |
| 5966 | } |
| 5967 | } |
| 5968 | |
| 5969 | case OR_No_Viable_Function: { |
| 5970 | if (CandidateSet.empty()) |
| 5971 | Diag(LLoc, diag::err_ovl_no_oper) |
| 5972 | << Args[0]->getType() << /*subscript*/ 0 |
| 5973 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5974 | else |
| 5975 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 5976 | << Args[0]->getType() |
| 5977 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5978 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
| 5979 | "[]", LLoc); |
| 5980 | return ExprError(); |
| 5981 | } |
| 5982 | |
| 5983 | case OR_Ambiguous: |
| 5984 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5985 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5986 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
| 5987 | "[]", LLoc); |
| 5988 | return ExprError(); |
| 5989 | |
| 5990 | case OR_Deleted: |
| 5991 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5992 | << Best->Function->isDeleted() << "[]" |
| 5993 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5994 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
| 5995 | "[]", LLoc); |
| 5996 | return ExprError(); |
| 5997 | } |
| 5998 | |
| 5999 | // We matched a built-in operator; build it. |
| 6000 | Base.release(); |
| 6001 | Idx.release(); |
| 6002 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 6003 | Owned(Args[1]), RLoc); |
| 6004 | } |
| 6005 | |
| 6006 | /// BuildCallToMemberFunction - Build a call to a member |
| 6007 | /// function. MemExpr is the expression that refers to the member |
| 6008 | /// function (and includes the object parameter), Args/NumArgs are the |
| 6009 | /// arguments to the function call (not including the object |
| 6010 | /// parameter). The caller needs to validate that the member |
| 6011 | /// expression refers to a member function or an overloaded member |
| 6012 | /// function. |
| 6013 | Sema::OwningExprResult |
| 6014 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 6015 | SourceLocation LParenLoc, Expr **Args, |
| 6016 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 6017 | SourceLocation RParenLoc) { |
| 6018 | // Dig out the member expression. This holds both the object |
| 6019 | // argument and the member function we're referring to. |
| 6020 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 6021 | |
| 6022 | MemberExpr *MemExpr; |
| 6023 | CXXMethodDecl *Method = 0; |
| 6024 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 6025 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
| 6026 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 6027 | } else { |
| 6028 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
| 6029 | |
| 6030 | QualType ObjectType = UnresExpr->getBaseType(); |
| 6031 | |
| 6032 | // Add overload candidates |
| 6033 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
| 6034 | |
| 6035 | // FIXME: avoid copy. |
| 6036 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6037 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 6038 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6039 | TemplateArgs = &TemplateArgsBuffer; |
| 6040 | } |
| 6041 | |
| 6042 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 6043 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 6044 | |
| 6045 | NamedDecl *Func = *I; |
| 6046 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 6047 | if (isa<UsingShadowDecl>(Func)) |
| 6048 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 6049 | |
| 6050 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
| 6051 | // If explicit template arguments were provided, we can't call a |
| 6052 | // non-template member function. |
| 6053 | if (TemplateArgs) |
| 6054 | continue; |
| 6055 | |
| 6056 | AddMethodCandidate(Method, I.getAccess(), ActingDC, ObjectType, |
| 6057 | Args, NumArgs, |
| 6058 | CandidateSet, /*SuppressUserConversions=*/false); |
| 6059 | } else { |
| 6060 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
| 6061 | I.getAccess(), ActingDC, TemplateArgs, |
| 6062 | ObjectType, Args, NumArgs, |
| 6063 | CandidateSet, |
| 6064 | /*SuppressUsedConversions=*/false); |
| 6065 | } |
| 6066 | } |
| 6067 | |
| 6068 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 6069 | |
| 6070 | OverloadCandidateSet::iterator Best; |
| 6071 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
| 6072 | case OR_Success: |
| 6073 | Method = cast<CXXMethodDecl>(Best->Function); |
| 6074 | CheckUnresolvedMemberAccess(UnresExpr, Method, Best->getAccess()); |
| 6075 | break; |
| 6076 | |
| 6077 | case OR_No_Viable_Function: |
| 6078 | Diag(UnresExpr->getMemberLoc(), |
| 6079 | diag::err_ovl_no_viable_member_function_in_call) |
| 6080 | << DeclName << MemExprE->getSourceRange(); |
| 6081 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 6082 | // FIXME: Leaking incoming expressions! |
| 6083 | return ExprError(); |
| 6084 | |
| 6085 | case OR_Ambiguous: |
| 6086 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
| 6087 | << DeclName << MemExprE->getSourceRange(); |
| 6088 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 6089 | // FIXME: Leaking incoming expressions! |
| 6090 | return ExprError(); |
| 6091 | |
| 6092 | case OR_Deleted: |
| 6093 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
| 6094 | << Best->Function->isDeleted() |
| 6095 | << DeclName << MemExprE->getSourceRange(); |
| 6096 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 6097 | // FIXME: Leaking incoming expressions! |
| 6098 | return ExprError(); |
| 6099 | } |
| 6100 | |
| 6101 | MemExprE = FixOverloadedFunctionReference(MemExprE, Method); |
| 6102 | |
| 6103 | // If overload resolution picked a static member, build a |
| 6104 | // non-member call based on that function. |
| 6105 | if (Method->isStatic()) { |
| 6106 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 6107 | Args, NumArgs, RParenLoc); |
| 6108 | } |
| 6109 | |
| 6110 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
| 6111 | } |
| 6112 | |
| 6113 | assert(Method && "Member call to something that isn't a method?"); |
| 6114 | ExprOwningPtr<CXXMemberCallExpr> |
| 6115 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
| 6116 | NumArgs, |
| 6117 | Method->getResultType().getNonReferenceType(), |
| 6118 | RParenLoc)); |
| 6119 | |
| 6120 | // Check for a valid return type. |
| 6121 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6122 | TheCall.get(), Method)) |
| 6123 | return ExprError(); |
| 6124 | |
| 6125 | // Convert the object argument (for a non-static member function call). |
| 6126 | Expr *ObjectArg = MemExpr->getBase(); |
| 6127 | if (!Method->isStatic() && |
| 6128 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 6129 | return ExprError(); |
| 6130 | MemExpr->setBase(ObjectArg); |
| 6131 | |
| 6132 | // Convert the rest of the arguments |
| 6133 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
| 6134 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
| 6135 | RParenLoc)) |
| 6136 | return ExprError(); |
| 6137 | |
| 6138 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6139 | return ExprError(); |
| 6140 | |
| 6141 | return MaybeBindToTemporary(TheCall.release()); |
| 6142 | } |
| 6143 | |
| 6144 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 6145 | /// type (C++ [over.call.object]), which can end up invoking an |
| 6146 | /// overloaded function call operator (@c operator()) or performing a |
| 6147 | /// user-defined conversion on the object argument. |
| 6148 | Sema::ExprResult |
| 6149 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
| 6150 | SourceLocation LParenLoc, |
| 6151 | Expr **Args, unsigned NumArgs, |
| 6152 | SourceLocation *CommaLocs, |
| 6153 | SourceLocation RParenLoc) { |
| 6154 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
| 6155 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
| 6156 | |
| 6157 | // C++ [over.call.object]p1: |
| 6158 | // If the primary-expression E in the function call syntax |
| 6159 | // evaluates to a class object of type "cv T", then the set of |
| 6160 | // candidate functions includes at least the function call |
| 6161 | // operators of T. The function call operators of T are obtained by |
| 6162 | // ordinary lookup of the name operator() in the context of |
| 6163 | // (E).operator(). |
| 6164 | OverloadCandidateSet CandidateSet(LParenLoc); |
| 6165 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
| 6166 | |
| 6167 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 6168 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 6169 | << Object->getSourceRange())) |
| 6170 | return true; |
| 6171 | |
| 6172 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 6173 | LookupQualifiedName(R, Record->getDecl()); |
| 6174 | R.suppressDiagnostics(); |
| 6175 | |
| 6176 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 6177 | Oper != OperEnd; ++Oper) { |
| 6178 | AddMethodCandidate(*Oper, Oper.getAccess(), Object->getType(), |
| 6179 | Args, NumArgs, CandidateSet, |
| 6180 | /*SuppressUserConversions=*/ false); |
| 6181 | } |
| 6182 | |
| 6183 | // C++ [over.call.object]p2: |
| 6184 | // In addition, for each conversion function declared in T of the |
| 6185 | // form |
| 6186 | // |
| 6187 | // operator conversion-type-id () cv-qualifier; |
| 6188 | // |
| 6189 | // where cv-qualifier is the same cv-qualification as, or a |
| 6190 | // greater cv-qualification than, cv, and where conversion-type-id |
| 6191 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 6192 | // R", or the type "reference to pointer to function of |
| 6193 | // (P1,...,Pn) returning R", or the type "reference to function |
| 6194 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
| 6195 | // is also considered as a candidate function. Similarly, |
| 6196 | // surrogate call functions are added to the set of candidate |
| 6197 | // functions for each conversion function declared in an |
| 6198 | // accessible base class provided the function is not hidden |
| 6199 | // within T by another intervening declaration. |
| 6200 | const UnresolvedSetImpl *Conversions |
| 6201 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
| 6202 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 6203 | E = Conversions->end(); I != E; ++I) { |
| 6204 | NamedDecl *D = *I; |
| 6205 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6206 | if (isa<UsingShadowDecl>(D)) |
| 6207 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6208 | |
| 6209 | // Skip over templated conversion functions; they aren't |
| 6210 | // surrogates. |
| 6211 | if (isa<FunctionTemplateDecl>(D)) |
| 6212 | continue; |
| 6213 | |
| 6214 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 6215 | |
| 6216 | // Strip the reference type (if any) and then the pointer type (if |
| 6217 | // any) to get down to what might be a function type. |
| 6218 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 6219 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6220 | ConvType = ConvPtrType->getPointeeType(); |
| 6221 | |
| 6222 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 6223 | AddSurrogateCandidate(Conv, I.getAccess(), ActingContext, Proto, |
| 6224 | Object->getType(), Args, NumArgs, |
| 6225 | CandidateSet); |
| 6226 | } |
| 6227 | |
| 6228 | // Perform overload resolution. |
| 6229 | OverloadCandidateSet::iterator Best; |
| 6230 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
| 6231 | case OR_Success: |
| 6232 | // Overload resolution succeeded; we'll build the appropriate call |
| 6233 | // below. |
| 6234 | break; |
| 6235 | |
| 6236 | case OR_No_Viable_Function: |
| 6237 | if (CandidateSet.empty()) |
| 6238 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 6239 | << Object->getType() << /*call*/ 1 |
| 6240 | << Object->getSourceRange(); |
| 6241 | else |
| 6242 | Diag(Object->getSourceRange().getBegin(), |
| 6243 | diag::err_ovl_no_viable_object_call) |
| 6244 | << Object->getType() << Object->getSourceRange(); |
| 6245 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 6246 | break; |
| 6247 | |
| 6248 | case OR_Ambiguous: |
| 6249 | Diag(Object->getSourceRange().getBegin(), |
| 6250 | diag::err_ovl_ambiguous_object_call) |
| 6251 | << Object->getType() << Object->getSourceRange(); |
| 6252 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
| 6253 | break; |
| 6254 | |
| 6255 | case OR_Deleted: |
| 6256 | Diag(Object->getSourceRange().getBegin(), |
| 6257 | diag::err_ovl_deleted_object_call) |
| 6258 | << Best->Function->isDeleted() |
| 6259 | << Object->getType() << Object->getSourceRange(); |
| 6260 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
| 6261 | break; |
| 6262 | } |
| 6263 | |
| 6264 | if (Best == CandidateSet.end()) { |
| 6265 | // We had an error; delete all of the subexpressions and return |
| 6266 | // the error. |
| 6267 | Object->Destroy(Context); |
| 6268 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6269 | Args[ArgIdx]->Destroy(Context); |
| 6270 | return true; |
| 6271 | } |
| 6272 | |
| 6273 | if (Best->Function == 0) { |
| 6274 | // Since there is no function declaration, this is one of the |
| 6275 | // surrogate candidates. Dig out the conversion function. |
| 6276 | CXXConversionDecl *Conv |
| 6277 | = cast<CXXConversionDecl>( |
| 6278 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6279 | |
| 6280 | CheckMemberOperatorAccess(LParenLoc, Object, Conv, Best->getAccess()); |
| 6281 | |
| 6282 | // We selected one of the surrogate functions that converts the |
| 6283 | // object parameter to a function pointer. Perform the conversion |
| 6284 | // on the object argument, then let ActOnCallExpr finish the job. |
| 6285 | |
| 6286 | // Create an implicit member expr to refer to the conversion operator. |
| 6287 | // and then call it. |
| 6288 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Conv); |
| 6289 | |
| 6290 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
| 6291 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 6292 | CommaLocs, RParenLoc).release(); |
| 6293 | } |
| 6294 | |
| 6295 | CheckMemberOperatorAccess(LParenLoc, Object, |
| 6296 | Best->Function, Best->getAccess()); |
| 6297 | |
| 6298 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6299 | // that calls this method, using Object for the implicit object |
| 6300 | // parameter and passing along the remaining arguments. |
| 6301 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 6302 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
| 6303 | |
| 6304 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6305 | unsigned NumArgsToCheck = NumArgs; |
| 6306 | |
| 6307 | // Build the full argument list for the method call (the |
| 6308 | // implicit object parameter is placed at the beginning of the |
| 6309 | // list). |
| 6310 | Expr **MethodArgs; |
| 6311 | if (NumArgs < NumArgsInProto) { |
| 6312 | NumArgsToCheck = NumArgsInProto; |
| 6313 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6314 | } else { |
| 6315 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6316 | } |
| 6317 | MethodArgs[0] = Object; |
| 6318 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6319 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
| 6320 | |
| 6321 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6322 | SourceLocation()); |
| 6323 | UsualUnaryConversions(NewFn); |
| 6324 | |
| 6325 | // Once we've built TheCall, all of the expressions are properly |
| 6326 | // owned. |
| 6327 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6328 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6329 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
| 6330 | MethodArgs, NumArgs + 1, |
| 6331 | ResultTy, RParenLoc)); |
| 6332 | delete [] MethodArgs; |
| 6333 | |
| 6334 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6335 | Method)) |
| 6336 | return true; |
| 6337 | |
| 6338 | // We may have default arguments. If so, we need to allocate more |
| 6339 | // slots in the call for them. |
| 6340 | if (NumArgs < NumArgsInProto) |
| 6341 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
| 6342 | else if (NumArgs > NumArgsInProto) |
| 6343 | NumArgsToCheck = NumArgsInProto; |
| 6344 | |
| 6345 | bool IsError = false; |
| 6346 | |
| 6347 | // Initialize the implicit object parameter. |
| 6348 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
| 6349 | TheCall->setArg(0, Object); |
| 6350 | |
| 6351 | |
| 6352 | // Check the argument types. |
| 6353 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 6354 | Expr *Arg; |
| 6355 | if (i < NumArgs) { |
| 6356 | Arg = Args[i]; |
| 6357 | |
| 6358 | // Pass the argument. |
| 6359 | |
| 6360 | OwningExprResult InputInit |
| 6361 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6362 | Method->getParamDecl(i)), |
| 6363 | SourceLocation(), Owned(Arg)); |
| 6364 | |
| 6365 | IsError |= InputInit.isInvalid(); |
| 6366 | Arg = InputInit.takeAs<Expr>(); |
| 6367 | } else { |
| 6368 | OwningExprResult DefArg |
| 6369 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 6370 | if (DefArg.isInvalid()) { |
| 6371 | IsError = true; |
| 6372 | break; |
| 6373 | } |
| 6374 | |
| 6375 | Arg = DefArg.takeAs<Expr>(); |
| 6376 | } |
| 6377 | |
| 6378 | TheCall->setArg(i + 1, Arg); |
| 6379 | } |
| 6380 | |
| 6381 | // If this is a variadic call, handle args passed through "...". |
| 6382 | if (Proto->isVariadic()) { |
| 6383 | // Promote the arguments (C99 6.5.2.2p7). |
| 6384 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 6385 | Expr *Arg = Args[i]; |
| 6386 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
| 6387 | TheCall->setArg(i + 1, Arg); |
| 6388 | } |
| 6389 | } |
| 6390 | |
| 6391 | if (IsError) return true; |
| 6392 | |
| 6393 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6394 | return true; |
| 6395 | |
| 6396 | return MaybeBindToTemporary(TheCall.release()).release(); |
| 6397 | } |
| 6398 | |
| 6399 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
| 6400 | /// (if one exists), where @c Base is an expression of class type and |
| 6401 | /// @c Member is the name of the member we're trying to find. |
| 6402 | Sema::OwningExprResult |
| 6403 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 6404 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
| 6405 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
| 6406 | |
| 6407 | SourceLocation Loc = Base->getExprLoc(); |
| 6408 | |
| 6409 | // C++ [over.ref]p1: |
| 6410 | // |
| 6411 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 6412 | // for a class object x of type T if T::operator->() exists and if |
| 6413 | // the operator is selected as the best match function by the |
| 6414 | // overload resolution mechanism (13.3). |
| 6415 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 6416 | OverloadCandidateSet CandidateSet(Loc); |
| 6417 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
| 6418 | |
| 6419 | if (RequireCompleteType(Loc, Base->getType(), |
| 6420 | PDiag(diag::err_typecheck_incomplete_tag) |
| 6421 | << Base->getSourceRange())) |
| 6422 | return ExprError(); |
| 6423 | |
| 6424 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6425 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 6426 | R.suppressDiagnostics(); |
| 6427 | |
| 6428 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 6429 | Oper != OperEnd; ++Oper) { |
| 6430 | NamedDecl *D = *Oper; |
| 6431 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6432 | if (isa<UsingShadowDecl>(D)) |
| 6433 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6434 | |
| 6435 | AddMethodCandidate(cast<CXXMethodDecl>(D), Oper.getAccess(), ActingContext, |
| 6436 | Base->getType(), 0, 0, CandidateSet, |
| 6437 | /*SuppressUserConversions=*/false); |
| 6438 | } |
| 6439 | |
| 6440 | // Perform overload resolution. |
| 6441 | OverloadCandidateSet::iterator Best; |
| 6442 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
| 6443 | case OR_Success: |
| 6444 | // Overload resolution succeeded; we'll build the call below. |
| 6445 | break; |
| 6446 | |
| 6447 | case OR_No_Viable_Function: |
| 6448 | if (CandidateSet.empty()) |
| 6449 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
| 6450 | << Base->getType() << Base->getSourceRange(); |
| 6451 | else |
| 6452 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 6453 | << "operator->" << Base->getSourceRange(); |
| 6454 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
| 6455 | return ExprError(); |
| 6456 | |
| 6457 | case OR_Ambiguous: |
| 6458 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6459 | << "->" << Base->getSourceRange(); |
| 6460 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
| 6461 | return ExprError(); |
| 6462 | |
| 6463 | case OR_Deleted: |
| 6464 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6465 | << Best->Function->isDeleted() |
| 6466 | << "->" << Base->getSourceRange(); |
| 6467 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
| 6468 | return ExprError(); |
| 6469 | } |
| 6470 | |
| 6471 | // Convert the object parameter. |
| 6472 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
| 6473 | if (PerformObjectArgumentInitialization(Base, Method)) |
| 6474 | return ExprError(); |
| 6475 | |
| 6476 | // No concerns about early exits now. |
| 6477 | BaseIn.release(); |
| 6478 | |
| 6479 | // Build the operator call. |
| 6480 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6481 | SourceLocation()); |
| 6482 | UsualUnaryConversions(FnExpr); |
| 6483 | |
| 6484 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6485 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6486 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6487 | &Base, 1, ResultTy, OpLoc)); |
| 6488 | |
| 6489 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6490 | Method)) |
| 6491 | return ExprError(); |
| 6492 | return move(TheCall); |
| 6493 | } |
| 6494 | |
| 6495 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6496 | /// a C++ overloaded function (possibly with some parentheses and |
| 6497 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6498 | /// to the function declaration Fn, so patch up the expression E to |
| 6499 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 6500 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 6501 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 6502 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 6503 | if (SubExpr == PE->getSubExpr()) |
| 6504 | return PE->Retain(); |
| 6505 | |
| 6506 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6507 | } |
| 6508 | |
| 6509 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 6510 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
| 6511 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
| 6512 | SubExpr->getType()) && |
| 6513 | "Implicit cast type cannot be determined from overload"); |
| 6514 | if (SubExpr == ICE->getSubExpr()) |
| 6515 | return ICE->Retain(); |
| 6516 | |
| 6517 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6518 | ICE->getCastKind(), |
| 6519 | SubExpr, |
| 6520 | ICE->isLvalueCast()); |
| 6521 | } |
| 6522 | |
| 6523 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
| 6524 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
| 6525 | "Can only take the address of an overloaded function"); |
| 6526 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6527 | if (Method->isStatic()) { |
| 6528 | // Do nothing: static member functions aren't any different |
| 6529 | // from non-member functions. |
| 6530 | } else { |
| 6531 | // Fix the sub expression, which really has to be an |
| 6532 | // UnresolvedLookupExpr holding an overloaded member function |
| 6533 | // or template. |
| 6534 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6535 | if (SubExpr == UnOp->getSubExpr()) |
| 6536 | return UnOp->Retain(); |
| 6537 | |
| 6538 | assert(isa<DeclRefExpr>(SubExpr) |
| 6539 | && "fixed to something other than a decl ref"); |
| 6540 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6541 | && "fixed to a member ref with no nested name qualifier"); |
| 6542 | |
| 6543 | // We have taken the address of a pointer to member |
| 6544 | // function. Perform the computation here so that we get the |
| 6545 | // appropriate pointer to member type. |
| 6546 | QualType ClassType |
| 6547 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6548 | QualType MemPtrType |
| 6549 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6550 | |
| 6551 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6552 | MemPtrType, UnOp->getOperatorLoc()); |
| 6553 | } |
| 6554 | } |
| 6555 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6556 | if (SubExpr == UnOp->getSubExpr()) |
| 6557 | return UnOp->Retain(); |
| 6558 | |
| 6559 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6560 | Context.getPointerType(SubExpr->getType()), |
| 6561 | UnOp->getOperatorLoc()); |
| 6562 | } |
| 6563 | |
| 6564 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 6565 | // FIXME: avoid copy. |
| 6566 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6567 | if (ULE->hasExplicitTemplateArgs()) { |
| 6568 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6569 | TemplateArgs = &TemplateArgsBuffer; |
| 6570 | } |
| 6571 | |
| 6572 | return DeclRefExpr::Create(Context, |
| 6573 | ULE->getQualifier(), |
| 6574 | ULE->getQualifierRange(), |
| 6575 | Fn, |
| 6576 | ULE->getNameLoc(), |
| 6577 | Fn->getType(), |
| 6578 | TemplateArgs); |
| 6579 | } |
| 6580 | |
| 6581 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
| 6582 | // FIXME: avoid copy. |
| 6583 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6584 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6585 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6586 | TemplateArgs = &TemplateArgsBuffer; |
| 6587 | } |
| 6588 | |
| 6589 | Expr *Base; |
| 6590 | |
| 6591 | // If we're filling in |
| 6592 | if (MemExpr->isImplicitAccess()) { |
| 6593 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6594 | return DeclRefExpr::Create(Context, |
| 6595 | MemExpr->getQualifier(), |
| 6596 | MemExpr->getQualifierRange(), |
| 6597 | Fn, |
| 6598 | MemExpr->getMemberLoc(), |
| 6599 | Fn->getType(), |
| 6600 | TemplateArgs); |
| 6601 | } else { |
| 6602 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6603 | if (MemExpr->getQualifier()) |
| 6604 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6605 | Base = new (Context) CXXThisExpr(Loc, |
| 6606 | MemExpr->getBaseType(), |
| 6607 | /*isImplicit=*/true); |
| 6608 | } |
| 6609 | } else |
| 6610 | Base = MemExpr->getBase()->Retain(); |
| 6611 | |
| 6612 | return MemberExpr::Create(Context, Base, |
| 6613 | MemExpr->isArrow(), |
| 6614 | MemExpr->getQualifier(), |
| 6615 | MemExpr->getQualifierRange(), |
| 6616 | Fn, |
| 6617 | MemExpr->getMemberLoc(), |
| 6618 | TemplateArgs, |
| 6619 | Fn->getType()); |
| 6620 | } |
| 6621 | |
| 6622 | assert(false && "Invalid reference to overloaded function"); |
| 6623 | return E->Retain(); |
| 6624 | } |
| 6625 | |
| 6626 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
| 6627 | FunctionDecl *Fn) { |
| 6628 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Fn)); |
| 6629 | } |
| 6630 | |
| 6631 | } // end namespace clang |