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