Douglas Gregor | d2baafd | 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 | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | d2baafd | 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 | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 42 | ICC_Conversion, |
Douglas Gregor | d2baafd | 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 | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 65 | ICR_Conversion, |
Douglas Gregor | d2baafd | 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 | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 87 | "Boolean conversion", |
| 88 | "Derived-to-base conversion" |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 89 | }; |
| 90 | return Name[Kind]; |
| 91 | } |
| 92 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 93 | /// StandardConversionSequence - Set the standard conversion |
| 94 | /// sequence to the identity conversion. |
| 95 | void StandardConversionSequence::setAsIdentityConversion() { |
| 96 | First = ICK_Identity; |
| 97 | Second = ICK_Identity; |
| 98 | Third = ICK_Identity; |
| 99 | Deprecated = false; |
| 100 | ReferenceBinding = false; |
| 101 | DirectBinding = false; |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 102 | CopyConstructor = 0; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 106 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 107 | /// implicit conversions. |
| 108 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 109 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 110 | if (GetConversionRank(First) > Rank) |
| 111 | Rank = GetConversionRank(First); |
| 112 | if (GetConversionRank(Second) > Rank) |
| 113 | Rank = GetConversionRank(Second); |
| 114 | if (GetConversionRank(Third) > Rank) |
| 115 | Rank = GetConversionRank(Third); |
| 116 | return Rank; |
| 117 | } |
| 118 | |
| 119 | /// isPointerConversionToBool - Determines whether this conversion is |
| 120 | /// a conversion of a pointer or pointer-to-member to bool. This is |
| 121 | /// used as part of the ranking of standard conversion sequences |
| 122 | /// (C++ 13.3.3.2p4). |
| 123 | bool StandardConversionSequence::isPointerConversionToBool() const |
| 124 | { |
| 125 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 126 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 127 | |
| 128 | // Note that FromType has not necessarily been transformed by the |
| 129 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 130 | // check for their presence as well as checking whether FromType is |
| 131 | // a pointer. |
| 132 | if (ToType->isBooleanType() && |
| 133 | (FromType->isPointerType() || |
| 134 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 135 | return true; |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 140 | /// isPointerConversionToVoidPointer - Determines whether this |
| 141 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 142 | /// used as part of the ranking of standard conversion sequences (C++ |
| 143 | /// 13.3.3.2p4). |
| 144 | bool |
| 145 | StandardConversionSequence:: |
| 146 | isPointerConversionToVoidPointer(ASTContext& Context) const |
| 147 | { |
| 148 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 149 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 150 | |
| 151 | // Note that FromType has not necessarily been transformed by the |
| 152 | // array-to-pointer implicit conversion, so check for its presence |
| 153 | // and redo the conversion to get a pointer. |
| 154 | if (First == ICK_Array_To_Pointer) |
| 155 | FromType = Context.getArrayDecayedType(FromType); |
| 156 | |
| 157 | if (Second == ICK_Pointer_Conversion) |
| 158 | if (const PointerType* ToPtrType = ToType->getAsPointerType()) |
| 159 | return ToPtrType->getPointeeType()->isVoidType(); |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 164 | /// DebugPrint - Print this standard conversion sequence to standard |
| 165 | /// error. Useful for debugging overloading issues. |
| 166 | void StandardConversionSequence::DebugPrint() const { |
| 167 | bool PrintedSomething = false; |
| 168 | if (First != ICK_Identity) { |
| 169 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 170 | PrintedSomething = true; |
| 171 | } |
| 172 | |
| 173 | if (Second != ICK_Identity) { |
| 174 | if (PrintedSomething) { |
| 175 | fprintf(stderr, " -> "); |
| 176 | } |
| 177 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 178 | |
| 179 | if (CopyConstructor) { |
| 180 | fprintf(stderr, " (by copy constructor)"); |
| 181 | } else if (DirectBinding) { |
| 182 | fprintf(stderr, " (direct reference binding)"); |
| 183 | } else if (ReferenceBinding) { |
| 184 | fprintf(stderr, " (reference binding)"); |
| 185 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | PrintedSomething = true; |
| 187 | } |
| 188 | |
| 189 | if (Third != ICK_Identity) { |
| 190 | if (PrintedSomething) { |
| 191 | fprintf(stderr, " -> "); |
| 192 | } |
| 193 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 194 | PrintedSomething = true; |
| 195 | } |
| 196 | |
| 197 | if (!PrintedSomething) { |
| 198 | fprintf(stderr, "No conversions required"); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 203 | /// error. Useful for debugging overloading issues. |
| 204 | void UserDefinedConversionSequence::DebugPrint() const { |
| 205 | if (Before.First || Before.Second || Before.Third) { |
| 206 | Before.DebugPrint(); |
| 207 | fprintf(stderr, " -> "); |
| 208 | } |
| 209 | fprintf(stderr, "'%s'", ConversionFunction->getName()); |
| 210 | if (After.First || After.Second || After.Third) { |
| 211 | fprintf(stderr, " -> "); |
| 212 | After.DebugPrint(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 217 | /// error. Useful for debugging overloading issues. |
| 218 | void ImplicitConversionSequence::DebugPrint() const { |
| 219 | switch (ConversionKind) { |
| 220 | case StandardConversion: |
| 221 | fprintf(stderr, "Standard conversion: "); |
| 222 | Standard.DebugPrint(); |
| 223 | break; |
| 224 | case UserDefinedConversion: |
| 225 | fprintf(stderr, "User-defined conversion: "); |
| 226 | UserDefined.DebugPrint(); |
| 227 | break; |
| 228 | case EllipsisConversion: |
| 229 | fprintf(stderr, "Ellipsis conversion"); |
| 230 | break; |
| 231 | case BadConversion: |
| 232 | fprintf(stderr, "Bad conversion"); |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | fprintf(stderr, "\n"); |
| 237 | } |
| 238 | |
| 239 | // IsOverload - Determine whether the given New declaration is an |
| 240 | // overload of the Old declaration. This routine returns false if New |
| 241 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 242 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 243 | // function (or overload set). When it does return false and Old is an |
| 244 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
| 245 | // FunctionDecl that New cannot be overloaded with. |
| 246 | // |
| 247 | // Example: Given the following input: |
| 248 | // |
| 249 | // void f(int, float); // #1 |
| 250 | // void f(int, int); // #2 |
| 251 | // int f(int, int); // #3 |
| 252 | // |
| 253 | // When we process #1, there is no previous declaration of "f", |
| 254 | // so IsOverload will not be used. |
| 255 | // |
| 256 | // When we process #2, Old is a FunctionDecl for #1. By comparing the |
| 257 | // parameter types, we see that #1 and #2 are overloaded (since they |
| 258 | // have different signatures), so this routine returns false; |
| 259 | // MatchedDecl is unchanged. |
| 260 | // |
| 261 | // When we process #3, Old is an OverloadedFunctionDecl containing #1 |
| 262 | // and #2. We compare the signatures of #3 to #1 (they're overloaded, |
| 263 | // so we do nothing) and then #3 to #2. Since the signatures of #3 and |
| 264 | // #2 are identical (return types of functions are not part of the |
| 265 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 266 | // point to the FunctionDecl for #2. |
| 267 | bool |
| 268 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 269 | OverloadedFunctionDecl::function_iterator& MatchedDecl) |
| 270 | { |
| 271 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 272 | // Is this new function an overload of every function in the |
| 273 | // overload set? |
| 274 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 275 | FuncEnd = Ovl->function_end(); |
| 276 | for (; Func != FuncEnd; ++Func) { |
| 277 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 278 | MatchedDecl = Func; |
| 279 | return false; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // This function overloads every function in the overload set. |
| 284 | return true; |
| 285 | } else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
| 286 | // Is the function New an overload of the function Old? |
| 287 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 288 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 289 | |
| 290 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 291 | // determine whether they are overloads. If we find any mismatch |
| 292 | // in the signature, they are overloads. |
| 293 | |
| 294 | // If either of these functions is a K&R-style function (no |
| 295 | // prototype), then we consider them to have matching signatures. |
| 296 | if (isa<FunctionTypeNoProto>(OldQType.getTypePtr()) || |
| 297 | isa<FunctionTypeNoProto>(NewQType.getTypePtr())) |
| 298 | return false; |
| 299 | |
| 300 | FunctionTypeProto* OldType = cast<FunctionTypeProto>(OldQType.getTypePtr()); |
| 301 | FunctionTypeProto* NewType = cast<FunctionTypeProto>(NewQType.getTypePtr()); |
| 302 | |
| 303 | // The signature of a function includes the types of its |
| 304 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 305 | // of the ellipsis; see C++ DR 357). |
| 306 | if (OldQType != NewQType && |
| 307 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 308 | OldType->isVariadic() != NewType->isVariadic() || |
| 309 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 310 | NewType->arg_type_begin()))) |
| 311 | return true; |
| 312 | |
| 313 | // If the function is a class member, its signature includes the |
| 314 | // cv-qualifiers (if any) on the function itself. |
| 315 | // |
| 316 | // As part of this, also check whether one of the member functions |
| 317 | // is static, in which case they are not overloads (C++ |
| 318 | // 13.1p2). While not part of the definition of the signature, |
| 319 | // this check is important to determine whether these functions |
| 320 | // can be overloaded. |
| 321 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 322 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 323 | if (OldMethod && NewMethod && |
| 324 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 325 | OldQType.getCVRQualifiers() != NewQType.getCVRQualifiers()) |
| 326 | return true; |
| 327 | |
| 328 | // The signatures match; this is not an overload. |
| 329 | return false; |
| 330 | } else { |
| 331 | // (C++ 13p1): |
| 332 | // Only function declarations can be overloaded; object and type |
| 333 | // declarations cannot be overloaded. |
| 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 338 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 339 | /// from the given expression (Expr) to the given type (ToType). This |
| 340 | /// function returns an implicit conversion sequence that can be used |
| 341 | /// to perform the initialization. Given |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 342 | /// |
| 343 | /// void f(float f); |
| 344 | /// void g(int i) { f(i); } |
| 345 | /// |
| 346 | /// this routine would produce an implicit conversion sequence to |
| 347 | /// describe the initialization of f from i, which will be a standard |
| 348 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 349 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 350 | // |
| 351 | /// Note that this routine only determines how the conversion can be |
| 352 | /// performed; it does not actually perform the conversion. As such, |
| 353 | /// it will not produce any diagnostics if no conversion is available, |
| 354 | /// but will instead return an implicit conversion sequence of kind |
| 355 | /// "BadConversion". |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 356 | /// |
| 357 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 358 | /// not permitted. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 359 | ImplicitConversionSequence |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 360 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 361 | bool SuppressUserConversions) |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 362 | { |
| 363 | ImplicitConversionSequence ICS; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 364 | if (IsStandardConversion(From, ToType, ICS.Standard)) |
| 365 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 366 | else if (!SuppressUserConversions && |
| 367 | IsUserDefinedConversion(From, ToType, ICS.UserDefined)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 368 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 369 | // C++ [over.ics.user]p4: |
| 370 | // A conversion of an expression of class type to the same class |
| 371 | // type is given Exact Match rank, and a conversion of an |
| 372 | // expression of class type to a base class of that type is |
| 373 | // given Conversion rank, in spite of the fact that a copy |
| 374 | // constructor (i.e., a user-defined conversion function) is |
| 375 | // called for those cases. |
| 376 | if (CXXConstructorDecl *Constructor |
| 377 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 378 | if (Constructor->isCopyConstructor(Context)) { |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 379 | // Turn this into a "standard" conversion sequence, so that it |
| 380 | // gets ranked with standard conversion sequences. |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 381 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 382 | ICS.Standard.setAsIdentityConversion(); |
| 383 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 384 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 385 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 386 | if (IsDerivedFrom(From->getType().getUnqualifiedType(), |
| 387 | ToType.getUnqualifiedType())) |
| 388 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 389 | } |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 390 | } |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 391 | } else |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 392 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 393 | |
| 394 | return ICS; |
| 395 | } |
| 396 | |
| 397 | /// IsStandardConversion - Determines whether there is a standard |
| 398 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 399 | /// expression From to the type ToType. Standard conversion sequences |
| 400 | /// only consider non-class types; for conversions that involve class |
| 401 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 402 | /// contain the standard conversion sequence required to perform this |
| 403 | /// conversion and this routine will return true. Otherwise, this |
| 404 | /// routine will return false and the value of SCS is unspecified. |
| 405 | bool |
| 406 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
| 407 | StandardConversionSequence &SCS) |
| 408 | { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 409 | QualType FromType = From->getType(); |
| 410 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 411 | // There are no standard conversions for class types, so abort early. |
| 412 | if (FromType->isRecordType() || ToType->isRecordType()) |
| 413 | return false; |
| 414 | |
| 415 | // Standard conversions (C++ [conv]) |
| 416 | SCS.Deprecated = false; |
| 417 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 418 | SCS.CopyConstructor = 0; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 419 | |
| 420 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 421 | // array-to-pointer conversion, or function-to-pointer conversion |
| 422 | // (C++ 4p1). |
| 423 | |
| 424 | // Lvalue-to-rvalue conversion (C++ 4.1): |
| 425 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 426 | // converted to an rvalue. |
| 427 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
| 428 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame^] | 429 | !FromType->isFunctionType() && !FromType->isArrayType() && |
| 430 | !FromType->isOverloadType()) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 431 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 432 | |
| 433 | // If T is a non-class type, the type of the rvalue is the |
| 434 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
| 435 | // is T (C++ 4.1p1). |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 436 | FromType = FromType.getUnqualifiedType(); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 437 | } |
| 438 | // Array-to-pointer conversion (C++ 4.2) |
| 439 | else if (FromType->isArrayType()) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 440 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 441 | |
| 442 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 443 | // bound of T" can be converted to an rvalue of type "pointer to |
| 444 | // T" (C++ 4.2p1). |
| 445 | FromType = Context.getArrayDecayedType(FromType); |
| 446 | |
| 447 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 448 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 449 | SCS.Deprecated = true; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 450 | |
| 451 | // For the purpose of ranking in overload resolution |
| 452 | // (13.3.3.1.1), this conversion is considered an |
| 453 | // array-to-pointer conversion followed by a qualification |
| 454 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 455 | SCS.Second = ICK_Identity; |
| 456 | SCS.Third = ICK_Qualification; |
| 457 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 458 | return true; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | // Function-to-pointer conversion (C++ 4.3). |
| 462 | else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 463 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 464 | |
| 465 | // An lvalue of function type T can be converted to an rvalue of |
| 466 | // type "pointer to T." The result is a pointer to the |
| 467 | // function. (C++ 4.3p1). |
| 468 | FromType = Context.getPointerType(FromType); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 469 | } |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame^] | 470 | // Address of overloaded function (C++ [over.over]). |
| 471 | else if (FunctionDecl *Fn |
| 472 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
| 473 | SCS.First = ICK_Function_To_Pointer; |
| 474 | |
| 475 | // We were able to resolve the address of the overloaded function, |
| 476 | // so we can convert to the type of that function. |
| 477 | FromType = Fn->getType(); |
| 478 | if (ToType->isReferenceType()) |
| 479 | FromType = Context.getReferenceType(FromType); |
| 480 | else |
| 481 | FromType = Context.getPointerType(FromType); |
| 482 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | // We don't require any conversions for the first step. |
| 484 | else { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 485 | SCS.First = ICK_Identity; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // The second conversion can be an integral promotion, floating |
| 489 | // point promotion, integral conversion, floating point conversion, |
| 490 | // floating-integral conversion, pointer conversion, |
| 491 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
| 492 | if (Context.getCanonicalType(FromType).getUnqualifiedType() == |
| 493 | Context.getCanonicalType(ToType).getUnqualifiedType()) { |
| 494 | // The unqualified versions of the types are the same: there's no |
| 495 | // conversion to do. |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 496 | SCS.Second = ICK_Identity; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 497 | } |
| 498 | // Integral promotion (C++ 4.5). |
| 499 | else if (IsIntegralPromotion(From, FromType, ToType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 500 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 501 | FromType = ToType.getUnqualifiedType(); |
| 502 | } |
| 503 | // Floating point promotion (C++ 4.6). |
| 504 | else if (IsFloatingPointPromotion(FromType, ToType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 505 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | FromType = ToType.getUnqualifiedType(); |
| 507 | } |
| 508 | // Integral conversions (C++ 4.7). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 509 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 510 | else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 511 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 512 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 513 | FromType = ToType.getUnqualifiedType(); |
| 514 | } |
| 515 | // Floating point conversions (C++ 4.8). |
| 516 | else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 517 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 518 | FromType = ToType.getUnqualifiedType(); |
| 519 | } |
| 520 | // Floating-integral conversions (C++ 4.9). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 521 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 522 | else if ((FromType->isFloatingType() && |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 523 | ToType->isIntegralType() && !ToType->isBooleanType() && |
| 524 | !ToType->isEnumeralType()) || |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 525 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
| 526 | ToType->isFloatingType())) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 527 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 528 | FromType = ToType.getUnqualifiedType(); |
| 529 | } |
| 530 | // Pointer conversions (C++ 4.10). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 531 | else if (IsPointerConversion(From, FromType, ToType, FromType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 532 | SCS.Second = ICK_Pointer_Conversion; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 533 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 534 | // FIXME: Pointer to member conversions (4.11). |
| 535 | // Boolean conversions (C++ 4.12). |
| 536 | // FIXME: pointer-to-member type |
| 537 | else if (ToType->isBooleanType() && |
| 538 | (FromType->isArithmeticType() || |
| 539 | FromType->isEnumeralType() || |
| 540 | FromType->isPointerType())) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 541 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 542 | FromType = Context.BoolTy; |
| 543 | } else { |
| 544 | // No second conversion required. |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 545 | SCS.Second = ICK_Identity; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 548 | QualType CanonFrom; |
| 549 | QualType CanonTo; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 550 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 551 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 552 | SCS.Third = ICK_Qualification; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 553 | FromType = ToType; |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 554 | CanonFrom = Context.getCanonicalType(FromType); |
| 555 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 556 | } else { |
| 557 | // No conversion required |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 558 | SCS.Third = ICK_Identity; |
| 559 | |
| 560 | // C++ [over.best.ics]p6: |
| 561 | // [...] Any difference in top-level cv-qualification is |
| 562 | // subsumed by the initialization itself and does not constitute |
| 563 | // a conversion. [...] |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 564 | CanonFrom = Context.getCanonicalType(FromType); |
| 565 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 566 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 567 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 568 | FromType = ToType; |
| 569 | CanonFrom = CanonTo; |
| 570 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // If we have not converted the argument type to the parameter type, |
| 574 | // this is a bad conversion sequence. |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 575 | if (CanonFrom != CanonTo) |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 576 | return false; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 577 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 578 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 579 | return true; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 583 | /// expression From (whose potentially-adjusted type is FromType) to |
| 584 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 585 | /// sets PromotedType to the promoted type. |
| 586 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) |
| 587 | { |
| 588 | const BuiltinType *To = ToType->getAsBuiltinType(); |
Sebastian Redl | 12aee86 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 589 | // All integers are built-in. |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 590 | if (!To) { |
| 591 | return false; |
| 592 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 593 | |
| 594 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 595 | // unsigned short int can be converted to an rvalue of type int if |
| 596 | // int can represent all the values of the source type; otherwise, |
| 597 | // the source rvalue can be converted to an rvalue of type unsigned |
| 598 | // int (C++ 4.5p1). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 599 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 600 | if (// We can promote any signed, promotable integer type to an int |
| 601 | (FromType->isSignedIntegerType() || |
| 602 | // We can promote any unsigned integer type whose size is |
| 603 | // less than int to an int. |
| 604 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 605 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 606 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 609 | return To->getKind() == BuiltinType::UInt; |
| 610 | } |
| 611 | |
| 612 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 613 | // can be converted to an rvalue of the first of the following types |
| 614 | // that can represent all the values of its underlying type: int, |
| 615 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 616 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 617 | && ToType->isIntegerType()) { |
| 618 | // Determine whether the type we're converting from is signed or |
| 619 | // unsigned. |
| 620 | bool FromIsSigned; |
| 621 | uint64_t FromSize = Context.getTypeSize(FromType); |
| 622 | if (const EnumType *FromEnumType = FromType->getAsEnumType()) { |
| 623 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 624 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 625 | } else { |
| 626 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 627 | FromIsSigned = true; |
| 628 | } |
| 629 | |
| 630 | // The types we'll try to promote to, in the appropriate |
| 631 | // order. Try each of these types. |
| 632 | QualType PromoteTypes[4] = { |
| 633 | Context.IntTy, Context.UnsignedIntTy, |
| 634 | Context.LongTy, Context.UnsignedLongTy |
| 635 | }; |
| 636 | for (int Idx = 0; Idx < 0; ++Idx) { |
| 637 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 638 | if (FromSize < ToSize || |
| 639 | (FromSize == ToSize && |
| 640 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 641 | // We found the type that we can promote to. If this is the |
| 642 | // type we wanted, we have a promotion. Otherwise, no |
| 643 | // promotion. |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 644 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 645 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 651 | // rvalue of type int if int can represent all the values of the |
| 652 | // bit-field; otherwise, it can be converted to unsigned int if |
| 653 | // unsigned int can represent all the values of the bit-field. If |
| 654 | // the bit-field is larger yet, no integral promotion applies to |
| 655 | // it. If the bit-field has an enumerated type, it is treated as any |
| 656 | // other value of that type for promotion purposes (C++ 4.5p3). |
| 657 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(From)) { |
| 658 | using llvm::APSInt; |
| 659 | FieldDecl *MemberDecl = MemRef->getMemberDecl(); |
| 660 | APSInt BitWidth; |
| 661 | if (MemberDecl->isBitField() && |
| 662 | FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 663 | From->isIntegerConstantExpr(BitWidth, Context)) { |
| 664 | APSInt ToSize(Context.getTypeSize(ToType)); |
| 665 | |
| 666 | // Are we promoting to an int from a bitfield that fits in an int? |
| 667 | if (BitWidth < ToSize || |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 668 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 669 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 672 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 673 | // that fits into an unsigned int? |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 674 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 675 | return To->getKind() == BuiltinType::UInt; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 676 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 677 | |
| 678 | return false; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 683 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 684 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 685 | return true; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 686 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 687 | |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 692 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 693 | /// returns true and sets PromotedType to the promoted type. |
| 694 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) |
| 695 | { |
| 696 | /// An rvalue of type float can be converted to an rvalue of type |
| 697 | /// double. (C++ 4.6p1). |
| 698 | if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType()) |
| 699 | if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) |
| 700 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 701 | ToBuiltin->getKind() == BuiltinType::Double) |
| 702 | return true; |
| 703 | |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | /// IsPointerConversion - Determines whether the conversion of the |
| 708 | /// expression From, which has the (possibly adjusted) type FromType, |
| 709 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 710 | /// 4.10). If so, returns true and places the converted type (that |
| 711 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 712 | /// ConvertedType. |
| 713 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
| 714 | QualType& ConvertedType) |
| 715 | { |
| 716 | const PointerType* ToTypePtr = ToType->getAsPointerType(); |
| 717 | if (!ToTypePtr) |
| 718 | return false; |
| 719 | |
| 720 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
| 721 | if (From->isNullPointerConstant(Context)) { |
| 722 | ConvertedType = ToType; |
| 723 | return true; |
| 724 | } |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 725 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 726 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 727 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 728 | // 4.10p2). |
| 729 | if (FromType->isPointerType() && |
| 730 | FromType->getAsPointerType()->getPointeeType()->isObjectType() && |
| 731 | ToTypePtr->getPointeeType()->isVoidType()) { |
| 732 | // We need to produce a pointer to cv void, where cv is the same |
| 733 | // set of cv-qualifiers as we had on the incoming pointee type. |
| 734 | QualType toPointee = ToTypePtr->getPointeeType(); |
| 735 | unsigned Quals = Context.getCanonicalType(FromType)->getAsPointerType() |
| 736 | ->getPointeeType().getCVRQualifiers(); |
| 737 | |
| 738 | if (Context.getCanonicalType(ToTypePtr->getPointeeType()).getCVRQualifiers() |
| 739 | == Quals) { |
| 740 | // ToType is exactly the type we want. Use it. |
| 741 | ConvertedType = ToType; |
| 742 | } else { |
| 743 | // Build a new type with the right qualifiers. |
| 744 | ConvertedType |
| 745 | = Context.getPointerType(Context.VoidTy.getQualifiedType(Quals)); |
| 746 | } |
| 747 | return true; |
| 748 | } |
| 749 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 750 | // C++ [conv.ptr]p3: |
| 751 | // |
| 752 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 753 | // can be converted to an rvalue of type "pointer to cv B," where |
| 754 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 755 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 756 | // necessitates this conversion is ill-formed. The result of the |
| 757 | // conversion is a pointer to the base class sub-object of the |
| 758 | // derived class object. The null pointer value is converted to |
| 759 | // the null pointer value of the destination type. |
| 760 | // |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 761 | // Note that we do not check for ambiguity or inaccessibility |
| 762 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 763 | if (const PointerType *FromPtrType = FromType->getAsPointerType()) |
| 764 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) { |
| 765 | if (FromPtrType->getPointeeType()->isRecordType() && |
| 766 | ToPtrType->getPointeeType()->isRecordType() && |
| 767 | IsDerivedFrom(FromPtrType->getPointeeType(), |
| 768 | ToPtrType->getPointeeType())) { |
| 769 | // The conversion is okay. Now, we need to produce the type |
| 770 | // that results from this conversion, which will have the same |
| 771 | // qualifiers as the incoming type. |
| 772 | QualType CanonFromPointee |
| 773 | = Context.getCanonicalType(FromPtrType->getPointeeType()); |
| 774 | QualType ToPointee = ToPtrType->getPointeeType(); |
| 775 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| 776 | unsigned Quals = CanonFromPointee.getCVRQualifiers(); |
| 777 | |
| 778 | if (CanonToPointee.getCVRQualifiers() == Quals) { |
| 779 | // ToType is exactly the type we want. Use it. |
| 780 | ConvertedType = ToType; |
| 781 | } else { |
| 782 | // Build a new type with the right qualifiers. |
| 783 | ConvertedType |
| 784 | = Context.getPointerType(CanonToPointee.getQualifiedType(Quals)); |
| 785 | } |
| 786 | return true; |
| 787 | } |
| 788 | } |
| 789 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 790 | return false; |
| 791 | } |
| 792 | |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 793 | /// CheckPointerConversion - Check the pointer conversion from the |
| 794 | /// expression From to the type ToType. This routine checks for |
| 795 | /// ambiguous (FIXME: or inaccessible) derived-to-base pointer |
| 796 | /// conversions for which IsPointerConversion has already returned |
| 797 | /// true. It returns true and produces a diagnostic if there was an |
| 798 | /// error, or returns false otherwise. |
| 799 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType) { |
| 800 | QualType FromType = From->getType(); |
| 801 | |
| 802 | if (const PointerType *FromPtrType = FromType->getAsPointerType()) |
| 803 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) { |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 804 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 805 | /*DetectVirtual=*/false); |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 806 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 807 | ToPointeeType = ToPtrType->getPointeeType(); |
| 808 | if (FromPointeeType->isRecordType() && |
| 809 | ToPointeeType->isRecordType()) { |
| 810 | // We must have a derived-to-base conversion. Check an |
| 811 | // ambiguous or inaccessible conversion. |
Douglas Gregor | 651d1cc | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 812 | return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 813 | From->getExprLoc(), |
| 814 | From->getSourceRange()); |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | return false; |
| 819 | } |
| 820 | |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 821 | /// IsQualificationConversion - Determines whether the conversion from |
| 822 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 823 | /// (C++ 4.4). |
| 824 | bool |
| 825 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) |
| 826 | { |
| 827 | FromType = Context.getCanonicalType(FromType); |
| 828 | ToType = Context.getCanonicalType(ToType); |
| 829 | |
| 830 | // If FromType and ToType are the same type, this is not a |
| 831 | // qualification conversion. |
| 832 | if (FromType == ToType) |
| 833 | return false; |
| 834 | |
| 835 | // (C++ 4.4p4): |
| 836 | // A conversion can add cv-qualifiers at levels other than the first |
| 837 | // in multi-level pointers, subject to the following rules: [...] |
| 838 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 839 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 840 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 841 | // Within each iteration of the loop, we check the qualifiers to |
| 842 | // determine if this still looks like a qualification |
| 843 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 844 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 845 | // until there are no more pointers or pointers-to-members left to |
| 846 | // unwrap. |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 847 | UnwrappedAnyPointer = true; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 848 | |
| 849 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 850 | // 2,j, and similarly for volatile. |
Douglas Gregor | e5db4f7 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 851 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 852 | return false; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 854 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 855 | // every cv for 0 < k < j. |
| 856 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 857 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 858 | return false; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 860 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 861 | // include const. |
| 862 | PreviousToQualsIncludeConst |
| 863 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 864 | } |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 865 | |
| 866 | // We are left with FromType and ToType being the pointee types |
| 867 | // after unwrapping the original FromType and ToType the same number |
| 868 | // of types. If we unwrapped any pointers, and if FromType and |
| 869 | // ToType have the same unqualified type (since we checked |
| 870 | // qualifiers above), then this is a qualification conversion. |
| 871 | return UnwrappedAnyPointer && |
| 872 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 873 | } |
| 874 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 875 | /// IsUserDefinedConversion - Determines whether there is a |
| 876 | /// user-defined conversion sequence (C++ [over.ics.user]) that |
| 877 | /// converts expression From to the type ToType. If such a conversion |
| 878 | /// exists, User will contain the user-defined conversion sequence |
| 879 | /// that performs such a conversion and this routine will return |
| 880 | /// true. Otherwise, this routine returns false and User is |
| 881 | /// unspecified. |
| 882 | bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 883 | UserDefinedConversionSequence& User) |
| 884 | { |
| 885 | OverloadCandidateSet CandidateSet; |
| 886 | if (const CXXRecordType *ToRecordType |
| 887 | = dyn_cast_or_null<CXXRecordType>(ToType->getAsRecordType())) { |
| 888 | // C++ [over.match.ctor]p1: |
| 889 | // When objects of class type are direct-initialized (8.5), or |
| 890 | // copy-initialized from an expression of the same or a |
| 891 | // derived class type (8.5), overload resolution selects the |
| 892 | // constructor. [...] For copy-initialization, the candidate |
| 893 | // functions are all the converting constructors (12.3.1) of |
| 894 | // that class. The argument list is the expression-list within |
| 895 | // the parentheses of the initializer. |
| 896 | CXXRecordDecl *ToRecordDecl = ToRecordType->getDecl(); |
| 897 | const OverloadedFunctionDecl *Constructors = ToRecordDecl->getConstructors(); |
| 898 | for (OverloadedFunctionDecl::function_const_iterator func |
| 899 | = Constructors->function_begin(); |
| 900 | func != Constructors->function_end(); ++func) { |
| 901 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*func); |
| 902 | if (Constructor->isConvertingConstructor()) |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 903 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 904 | /*SuppressUserConversions=*/true); |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 908 | if (const CXXRecordType *FromRecordType |
| 909 | = dyn_cast_or_null<CXXRecordType>(From->getType()->getAsRecordType())) { |
| 910 | // Add all of the conversion functions as candidates. |
| 911 | // FIXME: Look for conversions in base classes! |
| 912 | CXXRecordDecl *FromRecordDecl = FromRecordType->getDecl(); |
| 913 | OverloadedFunctionDecl *Conversions |
| 914 | = FromRecordDecl->getConversionFunctions(); |
| 915 | for (OverloadedFunctionDecl::function_iterator Func |
| 916 | = Conversions->function_begin(); |
| 917 | Func != Conversions->function_end(); ++Func) { |
| 918 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 919 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 920 | } |
| 921 | } |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 922 | |
| 923 | OverloadCandidateSet::iterator Best; |
| 924 | switch (BestViableFunction(CandidateSet, Best)) { |
| 925 | case OR_Success: |
| 926 | // Record the standard conversion we used and the conversion function. |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 927 | if (CXXConstructorDecl *Constructor |
| 928 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 929 | // C++ [over.ics.user]p1: |
| 930 | // If the user-defined conversion is specified by a |
| 931 | // constructor (12.3.1), the initial standard conversion |
| 932 | // sequence converts the source type to the type required by |
| 933 | // the argument of the constructor. |
| 934 | // |
| 935 | // FIXME: What about ellipsis conversions? |
| 936 | QualType ThisType = Constructor->getThisType(Context); |
| 937 | User.Before = Best->Conversions[0].Standard; |
| 938 | User.ConversionFunction = Constructor; |
| 939 | User.After.setAsIdentityConversion(); |
| 940 | User.After.FromTypePtr |
| 941 | = ThisType->getAsPointerType()->getPointeeType().getAsOpaquePtr(); |
| 942 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
| 943 | return true; |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 944 | } else if (CXXConversionDecl *Conversion |
| 945 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 946 | // C++ [over.ics.user]p1: |
| 947 | // |
| 948 | // [...] If the user-defined conversion is specified by a |
| 949 | // conversion function (12.3.2), the initial standard |
| 950 | // conversion sequence converts the source type to the |
| 951 | // implicit object parameter of the conversion function. |
| 952 | User.Before = Best->Conversions[0].Standard; |
| 953 | User.ConversionFunction = Conversion; |
| 954 | |
| 955 | // C++ [over.ics.user]p2: |
| 956 | // The second standard conversion sequence converts the |
| 957 | // result of the user-defined conversion to the target type |
| 958 | // for the sequence. Since an implicit conversion sequence |
| 959 | // is an initialization, the special rules for |
| 960 | // initialization by user-defined conversion apply when |
| 961 | // selecting the best user-defined conversion for a |
| 962 | // user-defined conversion sequence (see 13.3.3 and |
| 963 | // 13.3.3.1). |
| 964 | User.After = Best->FinalConversion; |
| 965 | return true; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 966 | } else { |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 967 | assert(false && "Not a constructor or conversion function?"); |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 968 | return false; |
| 969 | } |
| 970 | |
| 971 | case OR_No_Viable_Function: |
| 972 | // No conversion here! We're done. |
| 973 | return false; |
| 974 | |
| 975 | case OR_Ambiguous: |
| 976 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 977 | // ambiguous conversion sequences. |
| 978 | return false; |
| 979 | } |
| 980 | |
| 981 | return false; |
| 982 | } |
| 983 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 984 | /// CompareImplicitConversionSequences - Compare two implicit |
| 985 | /// conversion sequences to determine whether one is better than the |
| 986 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
| 987 | ImplicitConversionSequence::CompareKind |
| 988 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 989 | const ImplicitConversionSequence& ICS2) |
| 990 | { |
| 991 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 992 | // conversion sequences (as defined in 13.3.3.1) |
| 993 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 994 | // conversion sequence than a user-defined conversion sequence or |
| 995 | // an ellipsis conversion sequence, and |
| 996 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 997 | // conversion sequence than an ellipsis conversion sequence |
| 998 | // (13.3.3.1.3). |
| 999 | // |
| 1000 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1001 | return ImplicitConversionSequence::Better; |
| 1002 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1003 | return ImplicitConversionSequence::Worse; |
| 1004 | |
| 1005 | // Two implicit conversion sequences of the same form are |
| 1006 | // indistinguishable conversion sequences unless one of the |
| 1007 | // following rules apply: (C++ 13.3.3.2p3): |
| 1008 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1009 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
| 1010 | else if (ICS1.ConversionKind == |
| 1011 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1012 | // User-defined conversion sequence U1 is a better conversion |
| 1013 | // sequence than another user-defined conversion sequence U2 if |
| 1014 | // they contain the same user-defined conversion function or |
| 1015 | // constructor and if the second standard conversion sequence of |
| 1016 | // U1 is better than the second standard conversion sequence of |
| 1017 | // U2 (C++ 13.3.3.2p3). |
| 1018 | if (ICS1.UserDefined.ConversionFunction == |
| 1019 | ICS2.UserDefined.ConversionFunction) |
| 1020 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1021 | ICS2.UserDefined.After); |
| 1022 | } |
| 1023 | |
| 1024 | return ImplicitConversionSequence::Indistinguishable; |
| 1025 | } |
| 1026 | |
| 1027 | /// CompareStandardConversionSequences - Compare two standard |
| 1028 | /// conversion sequences to determine whether one is better than the |
| 1029 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
| 1030 | ImplicitConversionSequence::CompareKind |
| 1031 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1032 | const StandardConversionSequence& SCS2) |
| 1033 | { |
| 1034 | // Standard conversion sequence S1 is a better conversion sequence |
| 1035 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1036 | |
| 1037 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1038 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1039 | // excluding any Lvalue Transformation; the identity conversion |
| 1040 | // sequence is considered to be a subsequence of any |
| 1041 | // non-identity conversion sequence) or, if not that, |
| 1042 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1043 | // Neither is a proper subsequence of the other. Do nothing. |
| 1044 | ; |
| 1045 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1046 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
| 1047 | (SCS1.Second == ICK_Identity && |
| 1048 | SCS1.Third == ICK_Identity)) |
| 1049 | // SCS1 is a proper subsequence of SCS2. |
| 1050 | return ImplicitConversionSequence::Better; |
| 1051 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1052 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
| 1053 | (SCS2.Second == ICK_Identity && |
| 1054 | SCS2.Third == ICK_Identity)) |
| 1055 | // SCS2 is a proper subsequence of SCS1. |
| 1056 | return ImplicitConversionSequence::Worse; |
| 1057 | |
| 1058 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1059 | // defined below), or, if not that, |
| 1060 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1061 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1062 | if (Rank1 < Rank2) |
| 1063 | return ImplicitConversionSequence::Better; |
| 1064 | else if (Rank2 < Rank1) |
| 1065 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1067 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1068 | // are indistinguishable unless one of the following rules |
| 1069 | // applies: |
| 1070 | |
| 1071 | // A conversion that is not a conversion of a pointer, or |
| 1072 | // pointer to member, to bool is better than another conversion |
| 1073 | // that is such a conversion. |
| 1074 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1075 | return SCS2.isPointerConversionToBool() |
| 1076 | ? ImplicitConversionSequence::Better |
| 1077 | : ImplicitConversionSequence::Worse; |
| 1078 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1079 | // C++ [over.ics.rank]p4b2: |
| 1080 | // |
| 1081 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1082 | // conversion of B* to A* is better than conversion of B* to |
| 1083 | // void*, and conversion of A* to void* is better than conversion |
| 1084 | // of B* to void*. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1085 | bool SCS1ConvertsToVoid |
| 1086 | = SCS1.isPointerConversionToVoidPointer(Context); |
| 1087 | bool SCS2ConvertsToVoid |
| 1088 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1089 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1090 | // Exactly one of the conversion sequences is a conversion to |
| 1091 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1092 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1093 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1094 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1095 | // Neither conversion sequence converts to a void pointer; compare |
| 1096 | // their derived-to-base conversions. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1097 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1098 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1099 | return DerivedCK; |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1100 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1101 | // Both conversion sequences are conversions to void |
| 1102 | // pointers. Compare the source types to determine if there's an |
| 1103 | // inheritance relationship in their sources. |
| 1104 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1105 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1106 | |
| 1107 | // Adjust the types we're converting from via the array-to-pointer |
| 1108 | // conversion, if we need to. |
| 1109 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1110 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1111 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1112 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1113 | |
| 1114 | QualType FromPointee1 |
| 1115 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1116 | QualType FromPointee2 |
| 1117 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1118 | |
| 1119 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1120 | return ImplicitConversionSequence::Better; |
| 1121 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1122 | return ImplicitConversionSequence::Worse; |
| 1123 | } |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1124 | |
| 1125 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1126 | // bullet 3). |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1127 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1128 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1129 | return QualCK; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1131 | // C++ [over.ics.rank]p3b4: |
| 1132 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1133 | // which the references refer are the same type except for |
| 1134 | // top-level cv-qualifiers, and the type to which the reference |
| 1135 | // initialized by S2 refers is more cv-qualified than the type |
| 1136 | // to which the reference initialized by S1 refers. |
| 1137 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
| 1138 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1139 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1140 | T1 = Context.getCanonicalType(T1); |
| 1141 | T2 = Context.getCanonicalType(T2); |
| 1142 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1143 | if (T2.isMoreQualifiedThan(T1)) |
| 1144 | return ImplicitConversionSequence::Better; |
| 1145 | else if (T1.isMoreQualifiedThan(T2)) |
| 1146 | return ImplicitConversionSequence::Worse; |
| 1147 | } |
| 1148 | } |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1149 | |
| 1150 | return ImplicitConversionSequence::Indistinguishable; |
| 1151 | } |
| 1152 | |
| 1153 | /// CompareQualificationConversions - Compares two standard conversion |
| 1154 | /// sequences to determine whether they can be ranked based on their |
| 1155 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1156 | ImplicitConversionSequence::CompareKind |
| 1157 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
| 1158 | const StandardConversionSequence& SCS2) |
| 1159 | { |
Douglas Gregor | 4459bbe | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1160 | // C++ 13.3.3.2p3: |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1161 | // -- S1 and S2 differ only in their qualification conversion and |
| 1162 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1163 | // cv-qualification signature of type T1 is a proper subset of |
| 1164 | // the cv-qualification signature of type T2, and S1 is not the |
| 1165 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1166 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1167 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1168 | return ImplicitConversionSequence::Indistinguishable; |
| 1169 | |
| 1170 | // FIXME: the example in the standard doesn't use a qualification |
| 1171 | // conversion (!) |
| 1172 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1173 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1174 | T1 = Context.getCanonicalType(T1); |
| 1175 | T2 = Context.getCanonicalType(T2); |
| 1176 | |
| 1177 | // If the types are the same, we won't learn anything by unwrapped |
| 1178 | // them. |
| 1179 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1180 | return ImplicitConversionSequence::Indistinguishable; |
| 1181 | |
| 1182 | ImplicitConversionSequence::CompareKind Result |
| 1183 | = ImplicitConversionSequence::Indistinguishable; |
| 1184 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1185 | // Within each iteration of the loop, we check the qualifiers to |
| 1186 | // determine if this still looks like a qualification |
| 1187 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1188 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1189 | // until there are no more pointers or pointers-to-members left |
| 1190 | // to unwrap. This essentially mimics what |
| 1191 | // IsQualificationConversion does, but here we're checking for a |
| 1192 | // strict subset of qualifiers. |
| 1193 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1194 | // The qualifiers are the same, so this doesn't tell us anything |
| 1195 | // about how the sequences rank. |
| 1196 | ; |
| 1197 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1198 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1199 | if (Result == ImplicitConversionSequence::Worse) |
| 1200 | // Neither has qualifiers that are a subset of the other's |
| 1201 | // qualifiers. |
| 1202 | return ImplicitConversionSequence::Indistinguishable; |
| 1203 | |
| 1204 | Result = ImplicitConversionSequence::Better; |
| 1205 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1206 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1207 | if (Result == ImplicitConversionSequence::Better) |
| 1208 | // Neither has qualifiers that are a subset of the other's |
| 1209 | // qualifiers. |
| 1210 | return ImplicitConversionSequence::Indistinguishable; |
| 1211 | |
| 1212 | Result = ImplicitConversionSequence::Worse; |
| 1213 | } else { |
| 1214 | // Qualifiers are disjoint. |
| 1215 | return ImplicitConversionSequence::Indistinguishable; |
| 1216 | } |
| 1217 | |
| 1218 | // If the types after this point are equivalent, we're done. |
| 1219 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1220 | break; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1223 | // Check that the winning standard conversion sequence isn't using |
| 1224 | // the deprecated string literal array to pointer conversion. |
| 1225 | switch (Result) { |
| 1226 | case ImplicitConversionSequence::Better: |
| 1227 | if (SCS1.Deprecated) |
| 1228 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1229 | break; |
| 1230 | |
| 1231 | case ImplicitConversionSequence::Indistinguishable: |
| 1232 | break; |
| 1233 | |
| 1234 | case ImplicitConversionSequence::Worse: |
| 1235 | if (SCS2.Deprecated) |
| 1236 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | return Result; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1243 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1244 | /// sequences to determine whether they can be ranked based on their |
| 1245 | /// various kinds of derived-to-base conversions (C++ [over.ics.rank]p4b3). |
| 1246 | ImplicitConversionSequence::CompareKind |
| 1247 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1248 | const StandardConversionSequence& SCS2) { |
| 1249 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1250 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1251 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1252 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1253 | |
| 1254 | // Adjust the types we're converting from via the array-to-pointer |
| 1255 | // conversion, if we need to. |
| 1256 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1257 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1258 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1259 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1260 | |
| 1261 | // Canonicalize all of the types. |
| 1262 | FromType1 = Context.getCanonicalType(FromType1); |
| 1263 | ToType1 = Context.getCanonicalType(ToType1); |
| 1264 | FromType2 = Context.getCanonicalType(FromType2); |
| 1265 | ToType2 = Context.getCanonicalType(ToType2); |
| 1266 | |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1267 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1268 | // |
| 1269 | // If class B is derived directly or indirectly from class A and |
| 1270 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Compare based on pointer conversions. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1273 | if (SCS1.Second == ICK_Pointer_Conversion && |
| 1274 | SCS2.Second == ICK_Pointer_Conversion) { |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1275 | QualType FromPointee1 |
| 1276 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1277 | QualType ToPointee1 |
| 1278 | = ToType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1279 | QualType FromPointee2 |
| 1280 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1281 | QualType ToPointee2 |
| 1282 | = ToType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1283 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1284 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1285 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1286 | return ImplicitConversionSequence::Better; |
| 1287 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1288 | return ImplicitConversionSequence::Worse; |
| 1289 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1290 | |
| 1291 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1292 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1293 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1294 | return ImplicitConversionSequence::Better; |
| 1295 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1296 | return ImplicitConversionSequence::Worse; |
| 1297 | } |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1300 | // Compare based on reference bindings. |
| 1301 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1302 | SCS1.Second == ICK_Derived_To_Base) { |
| 1303 | // -- binding of an expression of type C to a reference of type |
| 1304 | // B& is better than binding an expression of type C to a |
| 1305 | // reference of type A&, |
| 1306 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1307 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1308 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1309 | return ImplicitConversionSequence::Better; |
| 1310 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1311 | return ImplicitConversionSequence::Worse; |
| 1312 | } |
| 1313 | |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1314 | // -- binding of an expression of type B to a reference of type |
| 1315 | // A& is better than binding an expression of type C to a |
| 1316 | // reference of type A&, |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1317 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1318 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1319 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1320 | return ImplicitConversionSequence::Better; |
| 1321 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1322 | return ImplicitConversionSequence::Worse; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1328 | // A::* to C::*, |
| 1329 | |
| 1330 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1331 | // A::* to C::*, and |
| 1332 | |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1333 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1334 | SCS1.Second == ICK_Derived_To_Base) { |
| 1335 | // -- conversion of C to B is better than conversion of C to A, |
| 1336 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1337 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1338 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1339 | return ImplicitConversionSequence::Better; |
| 1340 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1341 | return ImplicitConversionSequence::Worse; |
| 1342 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1344 | // -- conversion of B to A is better than conversion of C to A. |
| 1345 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1346 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1347 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1348 | return ImplicitConversionSequence::Better; |
| 1349 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1350 | return ImplicitConversionSequence::Worse; |
| 1351 | } |
| 1352 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1353 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1354 | return ImplicitConversionSequence::Indistinguishable; |
| 1355 | } |
| 1356 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1357 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1358 | /// ToType from the expression From. Return the implicit conversion |
| 1359 | /// sequence required to pass this argument, which may be a bad |
| 1360 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1361 | /// a parameter of this type). If @p SuppressUserConversions, then we |
| 1362 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1363 | ImplicitConversionSequence |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1364 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
| 1365 | bool SuppressUserConversions) { |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1366 | if (!getLangOptions().CPlusPlus) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1367 | // In C, copy initialization is the same as performing an assignment. |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1368 | AssignConvertType ConvTy = |
| 1369 | CheckSingleAssignmentConstraints(ToType, From); |
| 1370 | ImplicitConversionSequence ICS; |
| 1371 | if (getLangOptions().NoExtensions? ConvTy != Compatible |
| 1372 | : ConvTy == Incompatible) |
| 1373 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1374 | else |
| 1375 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1376 | return ICS; |
| 1377 | } else if (ToType->isReferenceType()) { |
| 1378 | ImplicitConversionSequence ICS; |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1379 | CheckReferenceInit(From, ToType, &ICS, SuppressUserConversions); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1380 | return ICS; |
| 1381 | } else { |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1382 | return TryImplicitConversion(From, ToType, SuppressUserConversions); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | /// PerformArgumentPassing - Pass the argument Arg into a parameter of |
| 1387 | /// type ToType. Returns true (and emits a diagnostic) if there was |
| 1388 | /// an error, returns false if the initialization succeeded. |
| 1389 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
| 1390 | const char* Flavor) { |
| 1391 | if (!getLangOptions().CPlusPlus) { |
| 1392 | // In C, argument passing is the same as performing an assignment. |
| 1393 | QualType FromType = From->getType(); |
| 1394 | AssignConvertType ConvTy = |
| 1395 | CheckSingleAssignmentConstraints(ToType, From); |
| 1396 | |
| 1397 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 1398 | FromType, From, Flavor); |
| 1399 | } else if (ToType->isReferenceType()) { |
| 1400 | return CheckReferenceInit(From, ToType); |
| 1401 | } else { |
| 1402 | if (PerformImplicitConversion(From, ToType)) |
| 1403 | return Diag(From->getSourceRange().getBegin(), |
| 1404 | diag::err_typecheck_convert_incompatible, |
| 1405 | ToType.getAsString(), From->getType().getAsString(), |
| 1406 | Flavor, |
| 1407 | From->getSourceRange()); |
| 1408 | else |
| 1409 | return false; |
| 1410 | } |
| 1411 | } |
| 1412 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1413 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1414 | /// candidate functions, using the given function call arguments. If |
| 1415 | /// @p SuppressUserConversions, then don't allow user-defined |
| 1416 | /// conversions via constructors or conversion operators. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1417 | void |
| 1418 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
| 1419 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1420 | OverloadCandidateSet& CandidateSet, |
| 1421 | bool SuppressUserConversions) |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1422 | { |
| 1423 | const FunctionTypeProto* Proto |
| 1424 | = dyn_cast<FunctionTypeProto>(Function->getType()->getAsFunctionType()); |
| 1425 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1426 | assert(!isa<CXXConversionDecl>(Function) && |
| 1427 | "Use AddConversionCandidate for conversion functions"); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1428 | |
| 1429 | // Add this candidate |
| 1430 | CandidateSet.push_back(OverloadCandidate()); |
| 1431 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1432 | Candidate.Function = Function; |
| 1433 | |
| 1434 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1435 | |
| 1436 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 1437 | // parameters is viable only if it has an ellipsis in its parameter |
| 1438 | // list (8.3.5). |
| 1439 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 1440 | Candidate.Viable = false; |
| 1441 | return; |
| 1442 | } |
| 1443 | |
| 1444 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 1445 | // is viable only if the (m+1)st parameter has a default argument |
| 1446 | // (8.3.6). For the purposes of overload resolution, the |
| 1447 | // parameter list is truncated on the right, so that there are |
| 1448 | // exactly m parameters. |
| 1449 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 1450 | if (NumArgs < MinRequiredArgs) { |
| 1451 | // Not enough arguments. |
| 1452 | Candidate.Viable = false; |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | // Determine the implicit conversion sequences for each of the |
| 1457 | // arguments. |
| 1458 | Candidate.Viable = true; |
| 1459 | Candidate.Conversions.resize(NumArgs); |
| 1460 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 1461 | if (ArgIdx < NumArgsInProto) { |
| 1462 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 1463 | // exist for each argument an implicit conversion sequence |
| 1464 | // (13.3.3.1) that converts that argument to the corresponding |
| 1465 | // parameter of F. |
| 1466 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 1467 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1468 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 1469 | SuppressUserConversions); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1470 | if (Candidate.Conversions[ArgIdx].ConversionKind |
| 1471 | == ImplicitConversionSequence::BadConversion) |
| 1472 | Candidate.Viable = false; |
| 1473 | } else { |
| 1474 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 1475 | // argument for which there is no corresponding parameter is |
| 1476 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 1477 | Candidate.Conversions[ArgIdx].ConversionKind |
| 1478 | = ImplicitConversionSequence::EllipsisConversion; |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1483 | /// AddConversionCandidate - Add a C++ conversion function as a |
| 1484 | /// candidate in the candidate set (C++ [over.match.conv], |
| 1485 | /// C++ [over.match.copy]). From is the expression we're converting from, |
| 1486 | /// and ToType is the type that we're eventually trying to convert to |
| 1487 | /// (which may or may not be the same type as the type that the |
| 1488 | /// conversion function produces). |
| 1489 | void |
| 1490 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 1491 | Expr *From, QualType ToType, |
| 1492 | OverloadCandidateSet& CandidateSet) { |
| 1493 | // Add this candidate |
| 1494 | CandidateSet.push_back(OverloadCandidate()); |
| 1495 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1496 | Candidate.Function = Conversion; |
| 1497 | Candidate.FinalConversion.setAsIdentityConversion(); |
| 1498 | Candidate.FinalConversion.FromTypePtr |
| 1499 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 1500 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 1501 | |
| 1502 | // Determine the implicit conversion sequences for each of the |
| 1503 | // arguments. |
| 1504 | Candidate.Viable = true; |
| 1505 | Candidate.Conversions.resize(1); |
| 1506 | |
| 1507 | // FIXME: We need to follow the rules for the implicit object |
| 1508 | // parameter. |
| 1509 | QualType ImplicitObjectType |
| 1510 | = Context.getTypeDeclType(Conversion->getParent()); |
| 1511 | ImplicitObjectType |
| 1512 | = ImplicitObjectType.getQualifiedType(Conversion->getTypeQualifiers()); |
| 1513 | ImplicitObjectType = Context.getReferenceType(ImplicitObjectType); |
| 1514 | Candidate.Conversions[0] = TryCopyInitialization(From, ImplicitObjectType, |
| 1515 | true); |
| 1516 | if (Candidate.Conversions[0].ConversionKind |
| 1517 | == ImplicitConversionSequence::BadConversion) { |
| 1518 | Candidate.Viable = false; |
| 1519 | return; |
| 1520 | } |
| 1521 | |
| 1522 | // To determine what the conversion from the result of calling the |
| 1523 | // conversion function to the type we're eventually trying to |
| 1524 | // convert to (ToType), we need to synthesize a call to the |
| 1525 | // conversion function and attempt copy initialization from it. This |
| 1526 | // makes sure that we get the right semantics with respect to |
| 1527 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 1528 | // call on the stack and we don't need its arguments to be |
| 1529 | // well-formed. |
| 1530 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
| 1531 | SourceLocation()); |
| 1532 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
| 1533 | &ConversionRef); |
| 1534 | CallExpr Call(&ConversionFn, 0, 0, |
| 1535 | Conversion->getConversionType().getNonReferenceType(), |
| 1536 | SourceLocation()); |
| 1537 | ImplicitConversionSequence ICS = TryCopyInitialization(&Call, ToType, true); |
| 1538 | switch (ICS.ConversionKind) { |
| 1539 | case ImplicitConversionSequence::StandardConversion: |
| 1540 | Candidate.FinalConversion = ICS.Standard; |
| 1541 | break; |
| 1542 | |
| 1543 | case ImplicitConversionSequence::BadConversion: |
| 1544 | Candidate.Viable = false; |
| 1545 | break; |
| 1546 | |
| 1547 | default: |
| 1548 | assert(false && |
| 1549 | "Can only end up with a standard conversion sequence or failure"); |
| 1550 | } |
| 1551 | } |
| 1552 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1553 | /// AddOverloadCandidates - Add all of the function overloads in Ovl |
| 1554 | /// to the candidate set. |
| 1555 | void |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1556 | Sema::AddOverloadCandidates(const OverloadedFunctionDecl *Ovl, |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1557 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1558 | OverloadCandidateSet& CandidateSet, |
| 1559 | bool SuppressUserConversions) |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1560 | { |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 1561 | for (OverloadedFunctionDecl::function_const_iterator Func |
| 1562 | = Ovl->function_begin(); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1563 | Func != Ovl->function_end(); ++Func) |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1564 | AddOverloadCandidate(*Func, Args, NumArgs, CandidateSet, |
| 1565 | SuppressUserConversions); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 1569 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
| 1570 | bool |
| 1571 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
| 1572 | const OverloadCandidate& Cand2) |
| 1573 | { |
| 1574 | // Define viable functions to be better candidates than non-viable |
| 1575 | // functions. |
| 1576 | if (!Cand2.Viable) |
| 1577 | return Cand1.Viable; |
| 1578 | else if (!Cand1.Viable) |
| 1579 | return false; |
| 1580 | |
| 1581 | // FIXME: Deal with the implicit object parameter for static member |
| 1582 | // functions. (C++ 13.3.3p1). |
| 1583 | |
| 1584 | // (C++ 13.3.3p1): a viable function F1 is defined to be a better |
| 1585 | // function than another viable function F2 if for all arguments i, |
| 1586 | // ICSi(F1) is not a worse conversion sequence than ICSi(F2), and |
| 1587 | // then... |
| 1588 | unsigned NumArgs = Cand1.Conversions.size(); |
| 1589 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 1590 | bool HasBetterConversion = false; |
| 1591 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 1592 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 1593 | Cand2.Conversions[ArgIdx])) { |
| 1594 | case ImplicitConversionSequence::Better: |
| 1595 | // Cand1 has a better conversion sequence. |
| 1596 | HasBetterConversion = true; |
| 1597 | break; |
| 1598 | |
| 1599 | case ImplicitConversionSequence::Worse: |
| 1600 | // Cand1 can't be better than Cand2. |
| 1601 | return false; |
| 1602 | |
| 1603 | case ImplicitConversionSequence::Indistinguishable: |
| 1604 | // Do nothing. |
| 1605 | break; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | if (HasBetterConversion) |
| 1610 | return true; |
| 1611 | |
| 1612 | // FIXME: Several other bullets in (C++ 13.3.3p1) need to be implemented. |
| 1613 | |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1614 | // C++ [over.match.best]p1b4: |
| 1615 | // |
| 1616 | // -- the context is an initialization by user-defined conversion |
| 1617 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 1618 | // from the return type of F1 to the destination type (i.e., |
| 1619 | // the type of the entity being initialized) is a better |
| 1620 | // conversion sequence than the standard conversion sequence |
| 1621 | // from the return type of F2 to the destination type. |
| 1622 | if (isa<CXXConversionDecl>(Cand1.Function) && |
| 1623 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 1624 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 1625 | Cand2.FinalConversion)) { |
| 1626 | case ImplicitConversionSequence::Better: |
| 1627 | // Cand1 has a better conversion sequence. |
| 1628 | return true; |
| 1629 | |
| 1630 | case ImplicitConversionSequence::Worse: |
| 1631 | // Cand1 can't be better than Cand2. |
| 1632 | return false; |
| 1633 | |
| 1634 | case ImplicitConversionSequence::Indistinguishable: |
| 1635 | // Do nothing |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1640 | return false; |
| 1641 | } |
| 1642 | |
| 1643 | /// BestViableFunction - Computes the best viable function (C++ 13.3.3) |
| 1644 | /// within an overload candidate set. If overloading is successful, |
| 1645 | /// the result will be OR_Success and Best will be set to point to the |
| 1646 | /// best viable function within the candidate set. Otherwise, one of |
| 1647 | /// several kinds of errors will be returned; see |
| 1648 | /// Sema::OverloadingResult. |
| 1649 | Sema::OverloadingResult |
| 1650 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 1651 | OverloadCandidateSet::iterator& Best) |
| 1652 | { |
| 1653 | // Find the best viable function. |
| 1654 | Best = CandidateSet.end(); |
| 1655 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 1656 | Cand != CandidateSet.end(); ++Cand) { |
| 1657 | if (Cand->Viable) { |
| 1658 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 1659 | Best = Cand; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | // If we didn't find any viable functions, abort. |
| 1664 | if (Best == CandidateSet.end()) |
| 1665 | return OR_No_Viable_Function; |
| 1666 | |
| 1667 | // Make sure that this function is better than every other viable |
| 1668 | // function. If not, we have an ambiguity. |
| 1669 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 1670 | Cand != CandidateSet.end(); ++Cand) { |
| 1671 | if (Cand->Viable && |
| 1672 | Cand != Best && |
| 1673 | !isBetterOverloadCandidate(*Best, *Cand)) |
| 1674 | return OR_Ambiguous; |
| 1675 | } |
| 1676 | |
| 1677 | // Best is the best viable function. |
| 1678 | return OR_Success; |
| 1679 | } |
| 1680 | |
| 1681 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 1682 | /// diagnostic messages containing the candidates in the candidate |
| 1683 | /// set. If OnlyViable is true, only viable candidates will be printed. |
| 1684 | void |
| 1685 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
| 1686 | bool OnlyViable) |
| 1687 | { |
| 1688 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 1689 | LastCand = CandidateSet.end(); |
| 1690 | for (; Cand != LastCand; ++Cand) { |
| 1691 | if (Cand->Viable ||!OnlyViable) |
| 1692 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
| 1693 | } |
| 1694 | } |
| 1695 | |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame^] | 1696 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 1697 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 1698 | /// expression with overloaded function type and @p ToType is the type |
| 1699 | /// we're trying to resolve to. For example: |
| 1700 | /// |
| 1701 | /// @code |
| 1702 | /// int f(double); |
| 1703 | /// int f(int); |
| 1704 | /// |
| 1705 | /// int (*pfd)(double) = f; // selects f(double) |
| 1706 | /// @endcode |
| 1707 | /// |
| 1708 | /// This routine returns the resulting FunctionDecl if it could be |
| 1709 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 1710 | /// routine will emit diagnostics if there is an error. |
| 1711 | FunctionDecl * |
| 1712 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
| 1713 | bool Complain) { |
| 1714 | QualType FunctionType = ToType; |
| 1715 | if (const PointerLikeType *ToTypePtr = ToType->getAsPointerLikeType()) |
| 1716 | FunctionType = ToTypePtr->getPointeeType(); |
| 1717 | |
| 1718 | // We only look at pointers or references to functions. |
| 1719 | if (!FunctionType->isFunctionType()) |
| 1720 | return 0; |
| 1721 | |
| 1722 | // Find the actual overloaded function declaration. |
| 1723 | OverloadedFunctionDecl *Ovl = 0; |
| 1724 | |
| 1725 | // C++ [over.over]p1: |
| 1726 | // [...] [Note: any redundant set of parentheses surrounding the |
| 1727 | // overloaded function name is ignored (5.1). ] |
| 1728 | Expr *OvlExpr = From->IgnoreParens(); |
| 1729 | |
| 1730 | // C++ [over.over]p1: |
| 1731 | // [...] The overloaded function name can be preceded by the & |
| 1732 | // operator. |
| 1733 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 1734 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 1735 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 1736 | } |
| 1737 | |
| 1738 | // Try to dig out the overloaded function. |
| 1739 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) |
| 1740 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
| 1741 | |
| 1742 | // If there's no overloaded function declaration, we're done. |
| 1743 | if (!Ovl) |
| 1744 | return 0; |
| 1745 | |
| 1746 | // Look through all of the overloaded functions, searching for one |
| 1747 | // whose type matches exactly. |
| 1748 | // FIXME: When templates or using declarations come along, we'll actually |
| 1749 | // have to deal with duplicates, partial ordering, etc. For now, we |
| 1750 | // can just do a simple search. |
| 1751 | FunctionType = Context.getCanonicalType(FunctionType.getUnqualifiedType()); |
| 1752 | for (OverloadedFunctionDecl::function_iterator Fun = Ovl->function_begin(); |
| 1753 | Fun != Ovl->function_end(); ++Fun) { |
| 1754 | // C++ [over.over]p3: |
| 1755 | // Non-member functions and static member functions match |
| 1756 | // targets of type “pointer-to-function”or |
| 1757 | // “reference-to-function.” |
| 1758 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) |
| 1759 | if (!Method->isStatic()) |
| 1760 | continue; |
| 1761 | |
| 1762 | if (FunctionType == Context.getCanonicalType((*Fun)->getType())) |
| 1763 | return *Fun; |
| 1764 | } |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 1770 | /// a C++ overloaded function (possibly with some parentheses and |
| 1771 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 1772 | /// to the function declaration Fn, so patch up the expression E to |
| 1773 | /// refer (possibly indirectly) to Fn. |
| 1774 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 1775 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 1776 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 1777 | E->setType(PE->getSubExpr()->getType()); |
| 1778 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
| 1779 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
| 1780 | "Can only take the address of an overloaded function"); |
| 1781 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 1782 | E->setType(Context.getPointerType(E->getType())); |
| 1783 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
| 1784 | assert(isa<OverloadedFunctionDecl>(DR->getDecl()) && |
| 1785 | "Expected overloaded function"); |
| 1786 | DR->setDecl(Fn); |
| 1787 | E->setType(Fn->getType()); |
| 1788 | } else { |
| 1789 | assert(false && "Invalid reference to overloaded function"); |
| 1790 | } |
| 1791 | } |
| 1792 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1793 | } // end namespace clang |