SemaOverload.cpp revision 27c8dc06f65d7abcf6a7e7f64a7960c9a150ca01
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 "SemaInherit.h" 16#include "clang/Basic/Diagnostic.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/Expr.h" 19#include "llvm/Support/Compiler.h" 20#include <algorithm> 21 22namespace clang { 23 24/// GetConversionCategory - Retrieve the implicit conversion 25/// category corresponding to the given implicit conversion kind. 26ImplicitConversionCategory 27GetConversionCategory(ImplicitConversionKind Kind) { 28 static const ImplicitConversionCategory 29 Category[(int)ICK_Num_Conversion_Kinds] = { 30 ICC_Identity, 31 ICC_Lvalue_Transformation, 32 ICC_Lvalue_Transformation, 33 ICC_Lvalue_Transformation, 34 ICC_Qualification_Adjustment, 35 ICC_Promotion, 36 ICC_Promotion, 37 ICC_Conversion, 38 ICC_Conversion, 39 ICC_Conversion, 40 ICC_Conversion, 41 ICC_Conversion, 42 ICC_Conversion 43 }; 44 return Category[(int)Kind]; 45} 46 47/// GetConversionRank - Retrieve the implicit conversion rank 48/// corresponding to the given implicit conversion kind. 49ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { 50 static const ImplicitConversionRank 51 Rank[(int)ICK_Num_Conversion_Kinds] = { 52 ICR_Exact_Match, 53 ICR_Exact_Match, 54 ICR_Exact_Match, 55 ICR_Exact_Match, 56 ICR_Exact_Match, 57 ICR_Promotion, 58 ICR_Promotion, 59 ICR_Conversion, 60 ICR_Conversion, 61 ICR_Conversion, 62 ICR_Conversion, 63 ICR_Conversion, 64 ICR_Conversion 65 }; 66 return Rank[(int)Kind]; 67} 68 69/// GetImplicitConversionName - Return the name of this kind of 70/// implicit conversion. 71const char* GetImplicitConversionName(ImplicitConversionKind Kind) { 72 static const char* Name[(int)ICK_Num_Conversion_Kinds] = { 73 "No conversion", 74 "Lvalue-to-rvalue", 75 "Array-to-pointer", 76 "Function-to-pointer", 77 "Qualification", 78 "Integral promotion", 79 "Floating point promotion", 80 "Integral conversion", 81 "Floating conversion", 82 "Floating-integral conversion", 83 "Pointer conversion", 84 "Pointer-to-member conversion", 85 "Boolean conversion" 86 }; 87 return Name[Kind]; 88} 89 90/// getRank - Retrieve the rank of this standard conversion sequence 91/// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the 92/// implicit conversions. 93ImplicitConversionRank StandardConversionSequence::getRank() const { 94 ImplicitConversionRank Rank = ICR_Exact_Match; 95 if (GetConversionRank(First) > Rank) 96 Rank = GetConversionRank(First); 97 if (GetConversionRank(Second) > Rank) 98 Rank = GetConversionRank(Second); 99 if (GetConversionRank(Third) > Rank) 100 Rank = GetConversionRank(Third); 101 return Rank; 102} 103 104/// isPointerConversionToBool - Determines whether this conversion is 105/// a conversion of a pointer or pointer-to-member to bool. This is 106/// used as part of the ranking of standard conversion sequences 107/// (C++ 13.3.3.2p4). 108bool StandardConversionSequence::isPointerConversionToBool() const 109{ 110 QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); 111 QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); 112 113 // Note that FromType has not necessarily been transformed by the 114 // array-to-pointer or function-to-pointer implicit conversions, so 115 // check for their presence as well as checking whether FromType is 116 // a pointer. 117 if (ToType->isBooleanType() && 118 (FromType->isPointerType() || 119 First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) 120 return true; 121 122 return false; 123} 124 125/// isPointerConversionToVoidPointer - Determines whether this 126/// conversion is a conversion of a pointer to a void pointer. This is 127/// used as part of the ranking of standard conversion sequences (C++ 128/// 13.3.3.2p4). 129bool 130StandardConversionSequence:: 131isPointerConversionToVoidPointer(ASTContext& Context) const 132{ 133 QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); 134 QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); 135 136 // Note that FromType has not necessarily been transformed by the 137 // array-to-pointer implicit conversion, so check for its presence 138 // and redo the conversion to get a pointer. 139 if (First == ICK_Array_To_Pointer) 140 FromType = Context.getArrayDecayedType(FromType); 141 142 if (Second == ICK_Pointer_Conversion) 143 if (const PointerType* ToPtrType = ToType->getAsPointerType()) 144 return ToPtrType->getPointeeType()->isVoidType(); 145 146 return false; 147} 148 149/// DebugPrint - Print this standard conversion sequence to standard 150/// error. Useful for debugging overloading issues. 151void StandardConversionSequence::DebugPrint() const { 152 bool PrintedSomething = false; 153 if (First != ICK_Identity) { 154 fprintf(stderr, "%s", GetImplicitConversionName(First)); 155 PrintedSomething = true; 156 } 157 158 if (Second != ICK_Identity) { 159 if (PrintedSomething) { 160 fprintf(stderr, " -> "); 161 } 162 fprintf(stderr, "%s", GetImplicitConversionName(Second)); 163 PrintedSomething = true; 164 } 165 166 if (Third != ICK_Identity) { 167 if (PrintedSomething) { 168 fprintf(stderr, " -> "); 169 } 170 fprintf(stderr, "%s", GetImplicitConversionName(Third)); 171 PrintedSomething = true; 172 } 173 174 if (!PrintedSomething) { 175 fprintf(stderr, "No conversions required"); 176 } 177} 178 179/// DebugPrint - Print this user-defined conversion sequence to standard 180/// error. Useful for debugging overloading issues. 181void UserDefinedConversionSequence::DebugPrint() const { 182 if (Before.First || Before.Second || Before.Third) { 183 Before.DebugPrint(); 184 fprintf(stderr, " -> "); 185 } 186 fprintf(stderr, "'%s'", ConversionFunction->getName()); 187 if (After.First || After.Second || After.Third) { 188 fprintf(stderr, " -> "); 189 After.DebugPrint(); 190 } 191} 192 193/// DebugPrint - Print this implicit conversion sequence to standard 194/// error. Useful for debugging overloading issues. 195void ImplicitConversionSequence::DebugPrint() const { 196 switch (ConversionKind) { 197 case StandardConversion: 198 fprintf(stderr, "Standard conversion: "); 199 Standard.DebugPrint(); 200 break; 201 case UserDefinedConversion: 202 fprintf(stderr, "User-defined conversion: "); 203 UserDefined.DebugPrint(); 204 break; 205 case EllipsisConversion: 206 fprintf(stderr, "Ellipsis conversion"); 207 break; 208 case BadConversion: 209 fprintf(stderr, "Bad conversion"); 210 break; 211 } 212 213 fprintf(stderr, "\n"); 214} 215 216// IsOverload - Determine whether the given New declaration is an 217// overload of the Old declaration. This routine returns false if New 218// and Old cannot be overloaded, e.g., if they are functions with the 219// same signature (C++ 1.3.10) or if the Old declaration isn't a 220// function (or overload set). When it does return false and Old is an 221// OverloadedFunctionDecl, MatchedDecl will be set to point to the 222// FunctionDecl that New cannot be overloaded with. 223// 224// Example: Given the following input: 225// 226// void f(int, float); // #1 227// void f(int, int); // #2 228// int f(int, int); // #3 229// 230// When we process #1, there is no previous declaration of "f", 231// so IsOverload will not be used. 232// 233// When we process #2, Old is a FunctionDecl for #1. By comparing the 234// parameter types, we see that #1 and #2 are overloaded (since they 235// have different signatures), so this routine returns false; 236// MatchedDecl is unchanged. 237// 238// When we process #3, Old is an OverloadedFunctionDecl containing #1 239// and #2. We compare the signatures of #3 to #1 (they're overloaded, 240// so we do nothing) and then #3 to #2. Since the signatures of #3 and 241// #2 are identical (return types of functions are not part of the 242// signature), IsOverload returns false and MatchedDecl will be set to 243// point to the FunctionDecl for #2. 244bool 245Sema::IsOverload(FunctionDecl *New, Decl* OldD, 246 OverloadedFunctionDecl::function_iterator& MatchedDecl) 247{ 248 if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { 249 // Is this new function an overload of every function in the 250 // overload set? 251 OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), 252 FuncEnd = Ovl->function_end(); 253 for (; Func != FuncEnd; ++Func) { 254 if (!IsOverload(New, *Func, MatchedDecl)) { 255 MatchedDecl = Func; 256 return false; 257 } 258 } 259 260 // This function overloads every function in the overload set. 261 return true; 262 } else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { 263 // Is the function New an overload of the function Old? 264 QualType OldQType = Context.getCanonicalType(Old->getType()); 265 QualType NewQType = Context.getCanonicalType(New->getType()); 266 267 // Compare the signatures (C++ 1.3.10) of the two functions to 268 // determine whether they are overloads. If we find any mismatch 269 // in the signature, they are overloads. 270 271 // If either of these functions is a K&R-style function (no 272 // prototype), then we consider them to have matching signatures. 273 if (isa<FunctionTypeNoProto>(OldQType.getTypePtr()) || 274 isa<FunctionTypeNoProto>(NewQType.getTypePtr())) 275 return false; 276 277 FunctionTypeProto* OldType = cast<FunctionTypeProto>(OldQType.getTypePtr()); 278 FunctionTypeProto* NewType = cast<FunctionTypeProto>(NewQType.getTypePtr()); 279 280 // The signature of a function includes the types of its 281 // parameters (C++ 1.3.10), which includes the presence or absence 282 // of the ellipsis; see C++ DR 357). 283 if (OldQType != NewQType && 284 (OldType->getNumArgs() != NewType->getNumArgs() || 285 OldType->isVariadic() != NewType->isVariadic() || 286 !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), 287 NewType->arg_type_begin()))) 288 return true; 289 290 // If the function is a class member, its signature includes the 291 // cv-qualifiers (if any) on the function itself. 292 // 293 // As part of this, also check whether one of the member functions 294 // is static, in which case they are not overloads (C++ 295 // 13.1p2). While not part of the definition of the signature, 296 // this check is important to determine whether these functions 297 // can be overloaded. 298 CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); 299 CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); 300 if (OldMethod && NewMethod && 301 !OldMethod->isStatic() && !NewMethod->isStatic() && 302 OldQType.getCVRQualifiers() != NewQType.getCVRQualifiers()) 303 return true; 304 305 // The signatures match; this is not an overload. 306 return false; 307 } else { 308 // (C++ 13p1): 309 // Only function declarations can be overloaded; object and type 310 // declarations cannot be overloaded. 311 return false; 312 } 313} 314 315/// TryImplicitConversion - Attempt to perform an implicit conversion 316/// from the given expression (Expr) to the given type (ToType). This 317/// function returns an implicit conversion sequence that can be used 318/// to perform the initialization. Given 319/// 320/// void f(float f); 321/// void g(int i) { f(i); } 322/// 323/// this routine would produce an implicit conversion sequence to 324/// describe the initialization of f from i, which will be a standard 325/// conversion sequence containing an lvalue-to-rvalue conversion (C++ 326/// 4.1) followed by a floating-integral conversion (C++ 4.9). 327// 328/// Note that this routine only determines how the conversion can be 329/// performed; it does not actually perform the conversion. As such, 330/// it will not produce any diagnostics if no conversion is available, 331/// but will instead return an implicit conversion sequence of kind 332/// "BadConversion". 333ImplicitConversionSequence 334Sema::TryImplicitConversion(Expr* From, QualType ToType) 335{ 336 ImplicitConversionSequence ICS; 337 338 QualType FromType = From->getType(); 339 340 // Standard conversions (C++ 4) 341 ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; 342 ICS.Standard.Deprecated = false; 343 ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); 344 345 // The first conversion can be an lvalue-to-rvalue conversion, 346 // array-to-pointer conversion, or function-to-pointer conversion 347 // (C++ 4p1). 348 349 // Lvalue-to-rvalue conversion (C++ 4.1): 350 // An lvalue (3.10) of a non-function, non-array type T can be 351 // converted to an rvalue. 352 Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); 353 if (argIsLvalue == Expr::LV_Valid && 354 !FromType->isFunctionType() && !FromType->isArrayType()) { 355 ICS.Standard.First = ICK_Lvalue_To_Rvalue; 356 357 // If T is a non-class type, the type of the rvalue is the 358 // cv-unqualified version of T. Otherwise, the type of the rvalue 359 // is T (C++ 4.1p1). 360 if (!FromType->isRecordType()) 361 FromType = FromType.getUnqualifiedType(); 362 } 363 // Array-to-pointer conversion (C++ 4.2) 364 else if (FromType->isArrayType()) { 365 ICS.Standard.First = ICK_Array_To_Pointer; 366 367 // An lvalue or rvalue of type "array of N T" or "array of unknown 368 // bound of T" can be converted to an rvalue of type "pointer to 369 // T" (C++ 4.2p1). 370 FromType = Context.getArrayDecayedType(FromType); 371 372 if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { 373 // This conversion is deprecated. (C++ D.4). 374 ICS.Standard.Deprecated = true; 375 376 // For the purpose of ranking in overload resolution 377 // (13.3.3.1.1), this conversion is considered an 378 // array-to-pointer conversion followed by a qualification 379 // conversion (4.4). (C++ 4.2p2) 380 ICS.Standard.Second = ICK_Identity; 381 ICS.Standard.Third = ICK_Qualification; 382 ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); 383 return ICS; 384 } 385 } 386 // Function-to-pointer conversion (C++ 4.3). 387 else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { 388 ICS.Standard.First = ICK_Function_To_Pointer; 389 390 // An lvalue of function type T can be converted to an rvalue of 391 // type "pointer to T." The result is a pointer to the 392 // function. (C++ 4.3p1). 393 FromType = Context.getPointerType(FromType); 394 395 // FIXME: Deal with overloaded functions here (C++ 4.3p2). 396 } 397 // We don't require any conversions for the first step. 398 else { 399 ICS.Standard.First = ICK_Identity; 400 } 401 402 // The second conversion can be an integral promotion, floating 403 // point promotion, integral conversion, floating point conversion, 404 // floating-integral conversion, pointer conversion, 405 // pointer-to-member conversion, or boolean conversion (C++ 4p1). 406 if (Context.getCanonicalType(FromType).getUnqualifiedType() == 407 Context.getCanonicalType(ToType).getUnqualifiedType()) { 408 // The unqualified versions of the types are the same: there's no 409 // conversion to do. 410 ICS.Standard.Second = ICK_Identity; 411 } 412 // Integral promotion (C++ 4.5). 413 else if (IsIntegralPromotion(From, FromType, ToType)) { 414 ICS.Standard.Second = ICK_Integral_Promotion; 415 FromType = ToType.getUnqualifiedType(); 416 } 417 // Floating point promotion (C++ 4.6). 418 else if (IsFloatingPointPromotion(FromType, ToType)) { 419 ICS.Standard.Second = ICK_Floating_Promotion; 420 FromType = ToType.getUnqualifiedType(); 421 } 422 // Integral conversions (C++ 4.7). 423 else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && 424 (ToType->isIntegralType() || ToType->isEnumeralType())) { 425 ICS.Standard.Second = ICK_Integral_Conversion; 426 FromType = ToType.getUnqualifiedType(); 427 } 428 // Floating point conversions (C++ 4.8). 429 else if (FromType->isFloatingType() && ToType->isFloatingType()) { 430 ICS.Standard.Second = ICK_Floating_Conversion; 431 FromType = ToType.getUnqualifiedType(); 432 } 433 // Floating-integral conversions (C++ 4.9). 434 else if ((FromType->isFloatingType() && 435 ToType->isIntegralType() && !ToType->isBooleanType()) || 436 ((FromType->isIntegralType() || FromType->isEnumeralType()) && 437 ToType->isFloatingType())) { 438 ICS.Standard.Second = ICK_Floating_Integral; 439 FromType = ToType.getUnqualifiedType(); 440 } 441 // Pointer conversions (C++ 4.10). 442 else if (IsPointerConversion(From, FromType, ToType, FromType)) 443 ICS.Standard.Second = ICK_Pointer_Conversion; 444 // FIXME: Pointer to member conversions (4.11). 445 // Boolean conversions (C++ 4.12). 446 // FIXME: pointer-to-member type 447 else if (ToType->isBooleanType() && 448 (FromType->isArithmeticType() || 449 FromType->isEnumeralType() || 450 FromType->isPointerType())) { 451 ICS.Standard.Second = ICK_Boolean_Conversion; 452 FromType = Context.BoolTy; 453 } else { 454 // No second conversion required. 455 ICS.Standard.Second = ICK_Identity; 456 } 457 458 QualType CanonFrom; 459 QualType CanonTo; 460 // The third conversion can be a qualification conversion (C++ 4p1). 461 if (IsQualificationConversion(FromType, ToType)) { 462 ICS.Standard.Third = ICK_Qualification; 463 FromType = ToType; 464 CanonFrom = Context.getCanonicalType(FromType); 465 CanonTo = Context.getCanonicalType(ToType); 466 } else { 467 // No conversion required 468 ICS.Standard.Third = ICK_Identity; 469 470 // C++ [dcl.init]p14 last bullet: 471 // Note: an expression of type "cv1 T" can initialize an object 472 // of type “cv2 T” independently of the cv-qualifiers cv1 and 473 // cv2. -- end note] 474 // 475 // FIXME: Where is the normative text? 476 CanonFrom = Context.getCanonicalType(FromType); 477 CanonTo = Context.getCanonicalType(ToType); 478 if (!FromType->isRecordType() && 479 CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && 480 CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { 481 FromType = ToType; 482 CanonFrom = CanonTo; 483 } 484 } 485 486 // If we have not converted the argument type to the parameter type, 487 // this is a bad conversion sequence. 488 if (CanonFrom != CanonTo) 489 ICS.ConversionKind = ImplicitConversionSequence::BadConversion; 490 491 ICS.Standard.ToTypePtr = FromType.getAsOpaquePtr(); 492 return ICS; 493} 494 495/// IsIntegralPromotion - Determines whether the conversion from the 496/// expression From (whose potentially-adjusted type is FromType) to 497/// ToType is an integral promotion (C++ 4.5). If so, returns true and 498/// sets PromotedType to the promoted type. 499bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) 500{ 501 const BuiltinType *To = ToType->getAsBuiltinType(); 502 503 // An rvalue of type char, signed char, unsigned char, short int, or 504 // unsigned short int can be converted to an rvalue of type int if 505 // int can represent all the values of the source type; otherwise, 506 // the source rvalue can be converted to an rvalue of type unsigned 507 // int (C++ 4.5p1). 508 if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && To) { 509 if (// We can promote any signed, promotable integer type to an int 510 (FromType->isSignedIntegerType() || 511 // We can promote any unsigned integer type whose size is 512 // less than int to an int. 513 (!FromType->isSignedIntegerType() && 514 Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) 515 return To->getKind() == BuiltinType::Int; 516 517 return To->getKind() == BuiltinType::UInt; 518 } 519 520 // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) 521 // can be converted to an rvalue of the first of the following types 522 // that can represent all the values of its underlying type: int, 523 // unsigned int, long, or unsigned long (C++ 4.5p2). 524 if ((FromType->isEnumeralType() || FromType->isWideCharType()) 525 && ToType->isIntegerType()) { 526 // Determine whether the type we're converting from is signed or 527 // unsigned. 528 bool FromIsSigned; 529 uint64_t FromSize = Context.getTypeSize(FromType); 530 if (const EnumType *FromEnumType = FromType->getAsEnumType()) { 531 QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); 532 FromIsSigned = UnderlyingType->isSignedIntegerType(); 533 } else { 534 // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. 535 FromIsSigned = true; 536 } 537 538 // The types we'll try to promote to, in the appropriate 539 // order. Try each of these types. 540 QualType PromoteTypes[4] = { 541 Context.IntTy, Context.UnsignedIntTy, 542 Context.LongTy, Context.UnsignedLongTy 543 }; 544 for (int Idx = 0; Idx < 0; ++Idx) { 545 uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); 546 if (FromSize < ToSize || 547 (FromSize == ToSize && 548 FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { 549 // We found the type that we can promote to. If this is the 550 // type we wanted, we have a promotion. Otherwise, no 551 // promotion. 552 return Context.getCanonicalType(FromType).getUnqualifiedType() 553 == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); 554 } 555 } 556 } 557 558 // An rvalue for an integral bit-field (9.6) can be converted to an 559 // rvalue of type int if int can represent all the values of the 560 // bit-field; otherwise, it can be converted to unsigned int if 561 // unsigned int can represent all the values of the bit-field. If 562 // the bit-field is larger yet, no integral promotion applies to 563 // it. If the bit-field has an enumerated type, it is treated as any 564 // other value of that type for promotion purposes (C++ 4.5p3). 565 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(From)) { 566 using llvm::APSInt; 567 FieldDecl *MemberDecl = MemRef->getMemberDecl(); 568 APSInt BitWidth; 569 if (MemberDecl->isBitField() && 570 FromType->isIntegralType() && !FromType->isEnumeralType() && 571 From->isIntegerConstantExpr(BitWidth, Context)) { 572 APSInt ToSize(Context.getTypeSize(ToType)); 573 574 // Are we promoting to an int from a bitfield that fits in an int? 575 if (BitWidth < ToSize || 576 (FromType->isSignedIntegerType() && BitWidth <= ToSize)) 577 return To->getKind() == BuiltinType::Int; 578 579 // Are we promoting to an unsigned int from an unsigned bitfield 580 // that fits into an unsigned int? 581 if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) 582 return To->getKind() == BuiltinType::UInt; 583 584 return false; 585 } 586 } 587 588 // An rvalue of type bool can be converted to an rvalue of type int, 589 // with false becoming zero and true becoming one (C++ 4.5p4). 590 if (FromType->isBooleanType() && To && To->getKind() == BuiltinType::Int) 591 return true; 592 593 return false; 594} 595 596/// IsFloatingPointPromotion - Determines whether the conversion from 597/// FromType to ToType is a floating point promotion (C++ 4.6). If so, 598/// returns true and sets PromotedType to the promoted type. 599bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) 600{ 601 /// An rvalue of type float can be converted to an rvalue of type 602 /// double. (C++ 4.6p1). 603 if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType()) 604 if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) 605 if (FromBuiltin->getKind() == BuiltinType::Float && 606 ToBuiltin->getKind() == BuiltinType::Double) 607 return true; 608 609 return false; 610} 611 612/// IsPointerConversion - Determines whether the conversion of the 613/// expression From, which has the (possibly adjusted) type FromType, 614/// can be converted to the type ToType via a pointer conversion (C++ 615/// 4.10). If so, returns true and places the converted type (that 616/// might differ from ToType in its cv-qualifiers at some level) into 617/// ConvertedType. 618bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, 619 QualType& ConvertedType) 620{ 621 const PointerType* ToTypePtr = ToType->getAsPointerType(); 622 if (!ToTypePtr) 623 return false; 624 625 // A null pointer constant can be converted to a pointer type (C++ 4.10p1). 626 if (From->isNullPointerConstant(Context)) { 627 ConvertedType = ToType; 628 return true; 629 } 630 631 // An rvalue of type "pointer to cv T," where T is an object type, 632 // can be converted to an rvalue of type "pointer to cv void" (C++ 633 // 4.10p2). 634 if (FromType->isPointerType() && 635 FromType->getAsPointerType()->getPointeeType()->isObjectType() && 636 ToTypePtr->getPointeeType()->isVoidType()) { 637 // We need to produce a pointer to cv void, where cv is the same 638 // set of cv-qualifiers as we had on the incoming pointee type. 639 QualType toPointee = ToTypePtr->getPointeeType(); 640 unsigned Quals = Context.getCanonicalType(FromType)->getAsPointerType() 641 ->getPointeeType().getCVRQualifiers(); 642 643 if (Context.getCanonicalType(ToTypePtr->getPointeeType()).getCVRQualifiers() 644 == Quals) { 645 // ToType is exactly the type we want. Use it. 646 ConvertedType = ToType; 647 } else { 648 // Build a new type with the right qualifiers. 649 ConvertedType 650 = Context.getPointerType(Context.VoidTy.getQualifiedType(Quals)); 651 } 652 return true; 653 } 654 655 // C++ [conv.ptr]p3: 656 // 657 // An rvalue of type "pointer to cv D," where D is a class type, 658 // can be converted to an rvalue of type "pointer to cv B," where 659 // B is a base class (clause 10) of D. If B is an inaccessible 660 // (clause 11) or ambiguous (10.2) base class of D, a program that 661 // necessitates this conversion is ill-formed. The result of the 662 // conversion is a pointer to the base class sub-object of the 663 // derived class object. The null pointer value is converted to 664 // the null pointer value of the destination type. 665 // 666 // Note that we do not check for ambiguity or inaccessibility 667 // here. That is handled by CheckPointerConversion. 668 if (const PointerType *FromPtrType = FromType->getAsPointerType()) 669 if (const PointerType *ToPtrType = ToType->getAsPointerType()) { 670 if (FromPtrType->getPointeeType()->isRecordType() && 671 ToPtrType->getPointeeType()->isRecordType() && 672 IsDerivedFrom(FromPtrType->getPointeeType(), 673 ToPtrType->getPointeeType())) { 674 // The conversion is okay. Now, we need to produce the type 675 // that results from this conversion, which will have the same 676 // qualifiers as the incoming type. 677 QualType CanonFromPointee 678 = Context.getCanonicalType(FromPtrType->getPointeeType()); 679 QualType ToPointee = ToPtrType->getPointeeType(); 680 QualType CanonToPointee = Context.getCanonicalType(ToPointee); 681 unsigned Quals = CanonFromPointee.getCVRQualifiers(); 682 683 if (CanonToPointee.getCVRQualifiers() == Quals) { 684 // ToType is exactly the type we want. Use it. 685 ConvertedType = ToType; 686 } else { 687 // Build a new type with the right qualifiers. 688 ConvertedType 689 = Context.getPointerType(CanonToPointee.getQualifiedType(Quals)); 690 } 691 return true; 692 } 693 } 694 695 return false; 696} 697 698/// CheckPointerConversion - Check the pointer conversion from the 699/// expression From to the type ToType. This routine checks for 700/// ambiguous (FIXME: or inaccessible) derived-to-base pointer 701/// conversions for which IsPointerConversion has already returned 702/// true. It returns true and produces a diagnostic if there was an 703/// error, or returns false otherwise. 704bool Sema::CheckPointerConversion(Expr *From, QualType ToType) { 705 QualType FromType = From->getType(); 706 707 if (const PointerType *FromPtrType = FromType->getAsPointerType()) 708 if (const PointerType *ToPtrType = ToType->getAsPointerType()) { 709 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false); 710 QualType FromPointeeType = FromPtrType->getPointeeType(), 711 ToPointeeType = ToPtrType->getPointeeType(); 712 if (FromPointeeType->isRecordType() && 713 ToPointeeType->isRecordType()) { 714 // We must have a derived-to-base conversion. Check an 715 // ambiguous or inaccessible conversion. 716 return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, 717 From->getExprLoc(), 718 From->getSourceRange()); 719 } 720 } 721 722 return false; 723} 724 725/// IsQualificationConversion - Determines whether the conversion from 726/// an rvalue of type FromType to ToType is a qualification conversion 727/// (C++ 4.4). 728bool 729Sema::IsQualificationConversion(QualType FromType, QualType ToType) 730{ 731 FromType = Context.getCanonicalType(FromType); 732 ToType = Context.getCanonicalType(ToType); 733 734 // If FromType and ToType are the same type, this is not a 735 // qualification conversion. 736 if (FromType == ToType) 737 return false; 738 739 // (C++ 4.4p4): 740 // A conversion can add cv-qualifiers at levels other than the first 741 // in multi-level pointers, subject to the following rules: [...] 742 bool PreviousToQualsIncludeConst = true; 743 bool UnwrappedAnyPointer = false; 744 while (UnwrapSimilarPointerTypes(FromType, ToType)) { 745 // Within each iteration of the loop, we check the qualifiers to 746 // determine if this still looks like a qualification 747 // conversion. Then, if all is well, we unwrap one more level of 748 // pointers or pointers-to-members and do it all again 749 // until there are no more pointers or pointers-to-members left to 750 // unwrap. 751 UnwrappedAnyPointer = true; 752 753 // -- for every j > 0, if const is in cv 1,j then const is in cv 754 // 2,j, and similarly for volatile. 755 if (!ToType.isAtLeastAsQualifiedAs(FromType)) 756 return false; 757 758 // -- if the cv 1,j and cv 2,j are different, then const is in 759 // every cv for 0 < k < j. 760 if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() 761 && !PreviousToQualsIncludeConst) 762 return false; 763 764 // Keep track of whether all prior cv-qualifiers in the "to" type 765 // include const. 766 PreviousToQualsIncludeConst 767 = PreviousToQualsIncludeConst && ToType.isConstQualified(); 768 } 769 770 // We are left with FromType and ToType being the pointee types 771 // after unwrapping the original FromType and ToType the same number 772 // of types. If we unwrapped any pointers, and if FromType and 773 // ToType have the same unqualified type (since we checked 774 // qualifiers above), then this is a qualification conversion. 775 return UnwrappedAnyPointer && 776 FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); 777} 778 779/// CompareImplicitConversionSequences - Compare two implicit 780/// conversion sequences to determine whether one is better than the 781/// other or if they are indistinguishable (C++ 13.3.3.2). 782ImplicitConversionSequence::CompareKind 783Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, 784 const ImplicitConversionSequence& ICS2) 785{ 786 // (C++ 13.3.3.2p2): When comparing the basic forms of implicit 787 // conversion sequences (as defined in 13.3.3.1) 788 // -- a standard conversion sequence (13.3.3.1.1) is a better 789 // conversion sequence than a user-defined conversion sequence or 790 // an ellipsis conversion sequence, and 791 // -- a user-defined conversion sequence (13.3.3.1.2) is a better 792 // conversion sequence than an ellipsis conversion sequence 793 // (13.3.3.1.3). 794 // 795 if (ICS1.ConversionKind < ICS2.ConversionKind) 796 return ImplicitConversionSequence::Better; 797 else if (ICS2.ConversionKind < ICS1.ConversionKind) 798 return ImplicitConversionSequence::Worse; 799 800 // Two implicit conversion sequences of the same form are 801 // indistinguishable conversion sequences unless one of the 802 // following rules apply: (C++ 13.3.3.2p3): 803 if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) 804 return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); 805 else if (ICS1.ConversionKind == 806 ImplicitConversionSequence::UserDefinedConversion) { 807 // User-defined conversion sequence U1 is a better conversion 808 // sequence than another user-defined conversion sequence U2 if 809 // they contain the same user-defined conversion function or 810 // constructor and if the second standard conversion sequence of 811 // U1 is better than the second standard conversion sequence of 812 // U2 (C++ 13.3.3.2p3). 813 if (ICS1.UserDefined.ConversionFunction == 814 ICS2.UserDefined.ConversionFunction) 815 return CompareStandardConversionSequences(ICS1.UserDefined.After, 816 ICS2.UserDefined.After); 817 } 818 819 return ImplicitConversionSequence::Indistinguishable; 820} 821 822/// CompareStandardConversionSequences - Compare two standard 823/// conversion sequences to determine whether one is better than the 824/// other or if they are indistinguishable (C++ 13.3.3.2p3). 825ImplicitConversionSequence::CompareKind 826Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, 827 const StandardConversionSequence& SCS2) 828{ 829 // Standard conversion sequence S1 is a better conversion sequence 830 // than standard conversion sequence S2 if (C++ 13.3.3.2p3): 831 832 // -- S1 is a proper subsequence of S2 (comparing the conversion 833 // sequences in the canonical form defined by 13.3.3.1.1, 834 // excluding any Lvalue Transformation; the identity conversion 835 // sequence is considered to be a subsequence of any 836 // non-identity conversion sequence) or, if not that, 837 if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) 838 // Neither is a proper subsequence of the other. Do nothing. 839 ; 840 else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || 841 (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || 842 (SCS1.Second == ICK_Identity && 843 SCS1.Third == ICK_Identity)) 844 // SCS1 is a proper subsequence of SCS2. 845 return ImplicitConversionSequence::Better; 846 else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || 847 (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || 848 (SCS2.Second == ICK_Identity && 849 SCS2.Third == ICK_Identity)) 850 // SCS2 is a proper subsequence of SCS1. 851 return ImplicitConversionSequence::Worse; 852 853 // -- the rank of S1 is better than the rank of S2 (by the rules 854 // defined below), or, if not that, 855 ImplicitConversionRank Rank1 = SCS1.getRank(); 856 ImplicitConversionRank Rank2 = SCS2.getRank(); 857 if (Rank1 < Rank2) 858 return ImplicitConversionSequence::Better; 859 else if (Rank2 < Rank1) 860 return ImplicitConversionSequence::Worse; 861 862 // (C++ 13.3.3.2p4): Two conversion sequences with the same rank 863 // are indistinguishable unless one of the following rules 864 // applies: 865 866 // A conversion that is not a conversion of a pointer, or 867 // pointer to member, to bool is better than another conversion 868 // that is such a conversion. 869 if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) 870 return SCS2.isPointerConversionToBool() 871 ? ImplicitConversionSequence::Better 872 : ImplicitConversionSequence::Worse; 873 874 // C++ [over.ics.rank]p4b2: 875 // 876 // If class B is derived directly or indirectly from class A, 877 // conversion of B* to A* is better than conversion of B* to void*, 878 // and (FIXME) conversion of A* to void* is better than conversion of B* 879 // to void*. 880 bool SCS1ConvertsToVoid 881 = SCS1.isPointerConversionToVoidPointer(Context); 882 bool SCS2ConvertsToVoid 883 = SCS2.isPointerConversionToVoidPointer(Context); 884 if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) 885 return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better 886 : ImplicitConversionSequence::Worse; 887 888 if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) 889 if (ImplicitConversionSequence::CompareKind DerivedCK 890 = CompareDerivedToBaseConversions(SCS1, SCS2)) 891 return DerivedCK; 892 893 // Compare based on qualification conversions (C++ 13.3.3.2p3, 894 // bullet 3). 895 if (ImplicitConversionSequence::CompareKind QualCK 896 = CompareQualificationConversions(SCS1, SCS2)) 897 return QualCK; 898 899 // FIXME: Handle comparison of reference bindings. 900 901 return ImplicitConversionSequence::Indistinguishable; 902} 903 904/// CompareQualificationConversions - Compares two standard conversion 905/// sequences to determine whether they can be ranked based on their 906/// qualification conversions (C++ 13.3.3.2p3 bullet 3). 907ImplicitConversionSequence::CompareKind 908Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, 909 const StandardConversionSequence& SCS2) 910{ 911 // C++ 13.3.3.2p3: 912 // -- S1 and S2 differ only in their qualification conversion and 913 // yield similar types T1 and T2 (C++ 4.4), respectively, and the 914 // cv-qualification signature of type T1 is a proper subset of 915 // the cv-qualification signature of type T2, and S1 is not the 916 // deprecated string literal array-to-pointer conversion (4.2). 917 if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || 918 SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) 919 return ImplicitConversionSequence::Indistinguishable; 920 921 // FIXME: the example in the standard doesn't use a qualification 922 // conversion (!) 923 QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); 924 QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); 925 T1 = Context.getCanonicalType(T1); 926 T2 = Context.getCanonicalType(T2); 927 928 // If the types are the same, we won't learn anything by unwrapped 929 // them. 930 if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) 931 return ImplicitConversionSequence::Indistinguishable; 932 933 ImplicitConversionSequence::CompareKind Result 934 = ImplicitConversionSequence::Indistinguishable; 935 while (UnwrapSimilarPointerTypes(T1, T2)) { 936 // Within each iteration of the loop, we check the qualifiers to 937 // determine if this still looks like a qualification 938 // conversion. Then, if all is well, we unwrap one more level of 939 // pointers or pointers-to-members and do it all again 940 // until there are no more pointers or pointers-to-members left 941 // to unwrap. This essentially mimics what 942 // IsQualificationConversion does, but here we're checking for a 943 // strict subset of qualifiers. 944 if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) 945 // The qualifiers are the same, so this doesn't tell us anything 946 // about how the sequences rank. 947 ; 948 else if (T2.isMoreQualifiedThan(T1)) { 949 // T1 has fewer qualifiers, so it could be the better sequence. 950 if (Result == ImplicitConversionSequence::Worse) 951 // Neither has qualifiers that are a subset of the other's 952 // qualifiers. 953 return ImplicitConversionSequence::Indistinguishable; 954 955 Result = ImplicitConversionSequence::Better; 956 } else if (T1.isMoreQualifiedThan(T2)) { 957 // T2 has fewer qualifiers, so it could be the better sequence. 958 if (Result == ImplicitConversionSequence::Better) 959 // Neither has qualifiers that are a subset of the other's 960 // qualifiers. 961 return ImplicitConversionSequence::Indistinguishable; 962 963 Result = ImplicitConversionSequence::Worse; 964 } else { 965 // Qualifiers are disjoint. 966 return ImplicitConversionSequence::Indistinguishable; 967 } 968 969 // If the types after this point are equivalent, we're done. 970 if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) 971 break; 972 } 973 974 // Check that the winning standard conversion sequence isn't using 975 // the deprecated string literal array to pointer conversion. 976 switch (Result) { 977 case ImplicitConversionSequence::Better: 978 if (SCS1.Deprecated) 979 Result = ImplicitConversionSequence::Indistinguishable; 980 break; 981 982 case ImplicitConversionSequence::Indistinguishable: 983 break; 984 985 case ImplicitConversionSequence::Worse: 986 if (SCS2.Deprecated) 987 Result = ImplicitConversionSequence::Indistinguishable; 988 break; 989 } 990 991 return Result; 992} 993 994/// CompareDerivedToBaseConversions - Compares two standard conversion 995/// sequences to determine whether they can be ranked based on their 996/// various kinds of derived-to-base conversions (C++ [over.ics.rank]p4b3). 997ImplicitConversionSequence::CompareKind 998Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, 999 const StandardConversionSequence& SCS2) { 1000 QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); 1001 QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); 1002 QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); 1003 QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); 1004 1005 // Adjust the types we're converting from via the array-to-pointer 1006 // conversion, if we need to. 1007 if (SCS1.First == ICK_Array_To_Pointer) 1008 FromType1 = Context.getArrayDecayedType(FromType1); 1009 if (SCS2.First == ICK_Array_To_Pointer) 1010 FromType2 = Context.getArrayDecayedType(FromType2); 1011 1012 // Canonicalize all of the types. 1013 FromType1 = Context.getCanonicalType(FromType1); 1014 ToType1 = Context.getCanonicalType(ToType1); 1015 FromType2 = Context.getCanonicalType(FromType2); 1016 ToType2 = Context.getCanonicalType(ToType2); 1017 1018 // C++ [over.ics.rank]p4b4: 1019 // 1020 // If class B is derived directly or indirectly from class A and 1021 // class C is derived directly or indirectly from B, 1022 // 1023 // FIXME: Verify that in this section we're talking about the 1024 // unqualified forms of C, B, and A. 1025 if (SCS1.Second == ICK_Pointer_Conversion && 1026 SCS2.Second == ICK_Pointer_Conversion) { 1027 // -- conversion of C* to B* is better than conversion of C* to A*, 1028 QualType FromPointee1 1029 = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); 1030 QualType ToPointee1 1031 = ToType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); 1032 QualType FromPointee2 1033 = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); 1034 QualType ToPointee2 1035 = ToType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); 1036 if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { 1037 if (IsDerivedFrom(ToPointee1, ToPointee2)) 1038 return ImplicitConversionSequence::Better; 1039 else if (IsDerivedFrom(ToPointee2, ToPointee1)) 1040 return ImplicitConversionSequence::Worse; 1041 } 1042 } 1043 1044 // FIXME: many more sub-bullets of C++ [over.ics.rank]p4b4 to 1045 // implement. 1046 return ImplicitConversionSequence::Indistinguishable; 1047} 1048 1049/// TryCopyInitialization - Try to copy-initialize a value of type 1050/// ToType from the expression From. Return the implicit conversion 1051/// sequence required to pass this argument, which may be a bad 1052/// conversion sequence (meaning that the argument cannot be passed to 1053/// a parameter of this type). This is user for argument passing, 1054ImplicitConversionSequence 1055Sema::TryCopyInitialization(Expr *From, QualType ToType) { 1056 if (!getLangOptions().CPlusPlus) { 1057 // In C, argument passing is the same as performing an assignment. 1058 AssignConvertType ConvTy = 1059 CheckSingleAssignmentConstraints(ToType, From); 1060 ImplicitConversionSequence ICS; 1061 if (getLangOptions().NoExtensions? ConvTy != Compatible 1062 : ConvTy == Incompatible) 1063 ICS.ConversionKind = ImplicitConversionSequence::BadConversion; 1064 else 1065 ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; 1066 return ICS; 1067 } else if (ToType->isReferenceType()) { 1068 ImplicitConversionSequence ICS; 1069 if (CheckReferenceInit(From, ToType, /*Complain=*/false)) 1070 ICS.ConversionKind = ImplicitConversionSequence::BadConversion; 1071 else 1072 ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; 1073 return ICS; 1074 } else { 1075 return TryImplicitConversion(From, ToType); 1076 } 1077} 1078 1079/// PerformArgumentPassing - Pass the argument Arg into a parameter of 1080/// type ToType. Returns true (and emits a diagnostic) if there was 1081/// an error, returns false if the initialization succeeded. 1082bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, 1083 const char* Flavor) { 1084 if (!getLangOptions().CPlusPlus) { 1085 // In C, argument passing is the same as performing an assignment. 1086 QualType FromType = From->getType(); 1087 AssignConvertType ConvTy = 1088 CheckSingleAssignmentConstraints(ToType, From); 1089 1090 return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, 1091 FromType, From, Flavor); 1092 } else if (ToType->isReferenceType()) { 1093 return CheckReferenceInit(From, ToType); 1094 } else { 1095 if (PerformImplicitConversion(From, ToType)) 1096 return Diag(From->getSourceRange().getBegin(), 1097 diag::err_typecheck_convert_incompatible, 1098 ToType.getAsString(), From->getType().getAsString(), 1099 Flavor, 1100 From->getSourceRange()); 1101 else 1102 return false; 1103 } 1104} 1105 1106/// AddOverloadCandidate - Adds the given function to the set of 1107/// candidate functions, using the given function call arguments. 1108void 1109Sema::AddOverloadCandidate(FunctionDecl *Function, 1110 Expr **Args, unsigned NumArgs, 1111 OverloadCandidateSet& CandidateSet) 1112{ 1113 const FunctionTypeProto* Proto 1114 = dyn_cast<FunctionTypeProto>(Function->getType()->getAsFunctionType()); 1115 assert(Proto && "Functions without a prototype cannot be overloaded"); 1116 1117 // Add this candidate 1118 CandidateSet.push_back(OverloadCandidate()); 1119 OverloadCandidate& Candidate = CandidateSet.back(); 1120 Candidate.Function = Function; 1121 1122 unsigned NumArgsInProto = Proto->getNumArgs(); 1123 1124 // (C++ 13.3.2p2): A candidate function having fewer than m 1125 // parameters is viable only if it has an ellipsis in its parameter 1126 // list (8.3.5). 1127 if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { 1128 Candidate.Viable = false; 1129 return; 1130 } 1131 1132 // (C++ 13.3.2p2): A candidate function having more than m parameters 1133 // is viable only if the (m+1)st parameter has a default argument 1134 // (8.3.6). For the purposes of overload resolution, the 1135 // parameter list is truncated on the right, so that there are 1136 // exactly m parameters. 1137 unsigned MinRequiredArgs = Function->getMinRequiredArguments(); 1138 if (NumArgs < MinRequiredArgs) { 1139 // Not enough arguments. 1140 Candidate.Viable = false; 1141 return; 1142 } 1143 1144 // Determine the implicit conversion sequences for each of the 1145 // arguments. 1146 Candidate.Viable = true; 1147 Candidate.Conversions.resize(NumArgs); 1148 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { 1149 if (ArgIdx < NumArgsInProto) { 1150 // (C++ 13.3.2p3): for F to be a viable function, there shall 1151 // exist for each argument an implicit conversion sequence 1152 // (13.3.3.1) that converts that argument to the corresponding 1153 // parameter of F. 1154 QualType ParamType = Proto->getArgType(ArgIdx); 1155 Candidate.Conversions[ArgIdx] 1156 = TryCopyInitialization(Args[ArgIdx], ParamType); 1157 if (Candidate.Conversions[ArgIdx].ConversionKind 1158 == ImplicitConversionSequence::BadConversion) 1159 Candidate.Viable = false; 1160 } else { 1161 // (C++ 13.3.2p2): For the purposes of overload resolution, any 1162 // argument for which there is no corresponding parameter is 1163 // considered to ""match the ellipsis" (C+ 13.3.3.1.3). 1164 Candidate.Conversions[ArgIdx].ConversionKind 1165 = ImplicitConversionSequence::EllipsisConversion; 1166 } 1167 } 1168} 1169 1170/// AddOverloadCandidates - Add all of the function overloads in Ovl 1171/// to the candidate set. 1172void 1173Sema::AddOverloadCandidates(OverloadedFunctionDecl *Ovl, 1174 Expr **Args, unsigned NumArgs, 1175 OverloadCandidateSet& CandidateSet) 1176{ 1177 for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(); 1178 Func != Ovl->function_end(); ++Func) 1179 AddOverloadCandidate(*Func, Args, NumArgs, CandidateSet); 1180} 1181 1182/// isBetterOverloadCandidate - Determines whether the first overload 1183/// candidate is a better candidate than the second (C++ 13.3.3p1). 1184bool 1185Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, 1186 const OverloadCandidate& Cand2) 1187{ 1188 // Define viable functions to be better candidates than non-viable 1189 // functions. 1190 if (!Cand2.Viable) 1191 return Cand1.Viable; 1192 else if (!Cand1.Viable) 1193 return false; 1194 1195 // FIXME: Deal with the implicit object parameter for static member 1196 // functions. (C++ 13.3.3p1). 1197 1198 // (C++ 13.3.3p1): a viable function F1 is defined to be a better 1199 // function than another viable function F2 if for all arguments i, 1200 // ICSi(F1) is not a worse conversion sequence than ICSi(F2), and 1201 // then... 1202 unsigned NumArgs = Cand1.Conversions.size(); 1203 assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); 1204 bool HasBetterConversion = false; 1205 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { 1206 switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], 1207 Cand2.Conversions[ArgIdx])) { 1208 case ImplicitConversionSequence::Better: 1209 // Cand1 has a better conversion sequence. 1210 HasBetterConversion = true; 1211 break; 1212 1213 case ImplicitConversionSequence::Worse: 1214 // Cand1 can't be better than Cand2. 1215 return false; 1216 1217 case ImplicitConversionSequence::Indistinguishable: 1218 // Do nothing. 1219 break; 1220 } 1221 } 1222 1223 if (HasBetterConversion) 1224 return true; 1225 1226 // FIXME: Several other bullets in (C++ 13.3.3p1) need to be implemented. 1227 1228 return false; 1229} 1230 1231/// BestViableFunction - Computes the best viable function (C++ 13.3.3) 1232/// within an overload candidate set. If overloading is successful, 1233/// the result will be OR_Success and Best will be set to point to the 1234/// best viable function within the candidate set. Otherwise, one of 1235/// several kinds of errors will be returned; see 1236/// Sema::OverloadingResult. 1237Sema::OverloadingResult 1238Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, 1239 OverloadCandidateSet::iterator& Best) 1240{ 1241 // Find the best viable function. 1242 Best = CandidateSet.end(); 1243 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); 1244 Cand != CandidateSet.end(); ++Cand) { 1245 if (Cand->Viable) { 1246 if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) 1247 Best = Cand; 1248 } 1249 } 1250 1251 // If we didn't find any viable functions, abort. 1252 if (Best == CandidateSet.end()) 1253 return OR_No_Viable_Function; 1254 1255 // Make sure that this function is better than every other viable 1256 // function. If not, we have an ambiguity. 1257 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); 1258 Cand != CandidateSet.end(); ++Cand) { 1259 if (Cand->Viable && 1260 Cand != Best && 1261 !isBetterOverloadCandidate(*Best, *Cand)) 1262 return OR_Ambiguous; 1263 } 1264 1265 // Best is the best viable function. 1266 return OR_Success; 1267} 1268 1269/// PrintOverloadCandidates - When overload resolution fails, prints 1270/// diagnostic messages containing the candidates in the candidate 1271/// set. If OnlyViable is true, only viable candidates will be printed. 1272void 1273Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, 1274 bool OnlyViable) 1275{ 1276 OverloadCandidateSet::iterator Cand = CandidateSet.begin(), 1277 LastCand = CandidateSet.end(); 1278 for (; Cand != LastCand; ++Cand) { 1279 if (Cand->Viable ||!OnlyViable) 1280 Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); 1281 } 1282} 1283 1284} // end namespace clang 1285