Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeOrdering.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include <algorithm> |
| 24 | |
| 25 | namespace clang { |
| 26 | |
| 27 | /// GetConversionCategory - Retrieve the implicit conversion |
| 28 | /// category corresponding to the given implicit conversion kind. |
| 29 | ImplicitConversionCategory |
| 30 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 31 | static const ImplicitConversionCategory |
| 32 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 33 | ICC_Identity, |
| 34 | ICC_Lvalue_Transformation, |
| 35 | ICC_Lvalue_Transformation, |
| 36 | ICC_Lvalue_Transformation, |
| 37 | ICC_Qualification_Adjustment, |
| 38 | ICC_Promotion, |
| 39 | ICC_Promotion, |
| 40 | ICC_Conversion, |
| 41 | ICC_Conversion, |
| 42 | ICC_Conversion, |
| 43 | ICC_Conversion, |
| 44 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 45 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 46 | ICC_Conversion |
| 47 | }; |
| 48 | return Category[(int)Kind]; |
| 49 | } |
| 50 | |
| 51 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 52 | /// corresponding to the given implicit conversion kind. |
| 53 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 54 | static const ImplicitConversionRank |
| 55 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 56 | ICR_Exact_Match, |
| 57 | ICR_Exact_Match, |
| 58 | ICR_Exact_Match, |
| 59 | ICR_Exact_Match, |
| 60 | ICR_Exact_Match, |
| 61 | ICR_Promotion, |
| 62 | ICR_Promotion, |
| 63 | ICR_Conversion, |
| 64 | ICR_Conversion, |
| 65 | ICR_Conversion, |
| 66 | ICR_Conversion, |
| 67 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 68 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 69 | ICR_Conversion |
| 70 | }; |
| 71 | return Rank[(int)Kind]; |
| 72 | } |
| 73 | |
| 74 | /// GetImplicitConversionName - Return the name of this kind of |
| 75 | /// implicit conversion. |
| 76 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 77 | static const char* Name[(int)ICK_Num_Conversion_Kinds] = { |
| 78 | "No conversion", |
| 79 | "Lvalue-to-rvalue", |
| 80 | "Array-to-pointer", |
| 81 | "Function-to-pointer", |
| 82 | "Qualification", |
| 83 | "Integral promotion", |
| 84 | "Floating point promotion", |
| 85 | "Integral conversion", |
| 86 | "Floating conversion", |
| 87 | "Floating-integral conversion", |
| 88 | "Pointer conversion", |
| 89 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 90 | "Boolean conversion", |
| 91 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | }; |
| 93 | return Name[Kind]; |
| 94 | } |
| 95 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 96 | /// StandardConversionSequence - Set the standard conversion |
| 97 | /// sequence to the identity conversion. |
| 98 | void StandardConversionSequence::setAsIdentityConversion() { |
| 99 | First = ICK_Identity; |
| 100 | Second = ICK_Identity; |
| 101 | Third = ICK_Identity; |
| 102 | Deprecated = false; |
| 103 | ReferenceBinding = false; |
| 104 | DirectBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 105 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 109 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 110 | /// implicit conversions. |
| 111 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 112 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 113 | if (GetConversionRank(First) > Rank) |
| 114 | Rank = GetConversionRank(First); |
| 115 | if (GetConversionRank(Second) > Rank) |
| 116 | Rank = GetConversionRank(Second); |
| 117 | if (GetConversionRank(Third) > Rank) |
| 118 | Rank = GetConversionRank(Third); |
| 119 | return Rank; |
| 120 | } |
| 121 | |
| 122 | /// isPointerConversionToBool - Determines whether this conversion is |
| 123 | /// a conversion of a pointer or pointer-to-member to bool. This is |
| 124 | /// used as part of the ranking of standard conversion sequences |
| 125 | /// (C++ 13.3.3.2p4). |
| 126 | bool StandardConversionSequence::isPointerConversionToBool() const |
| 127 | { |
| 128 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 129 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 130 | |
| 131 | // Note that FromType has not necessarily been transformed by the |
| 132 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 133 | // check for their presence as well as checking whether FromType is |
| 134 | // a pointer. |
| 135 | if (ToType->isBooleanType() && |
| 136 | (FromType->isPointerType() || |
| 137 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 138 | return true; |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 143 | /// isPointerConversionToVoidPointer - Determines whether this |
| 144 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 145 | /// used as part of the ranking of standard conversion sequences (C++ |
| 146 | /// 13.3.3.2p4). |
| 147 | bool |
| 148 | StandardConversionSequence:: |
| 149 | isPointerConversionToVoidPointer(ASTContext& Context) const |
| 150 | { |
| 151 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 152 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 153 | |
| 154 | // Note that FromType has not necessarily been transformed by the |
| 155 | // array-to-pointer implicit conversion, so check for its presence |
| 156 | // and redo the conversion to get a pointer. |
| 157 | if (First == ICK_Array_To_Pointer) |
| 158 | FromType = Context.getArrayDecayedType(FromType); |
| 159 | |
| 160 | if (Second == ICK_Pointer_Conversion) |
| 161 | if (const PointerType* ToPtrType = ToType->getAsPointerType()) |
| 162 | return ToPtrType->getPointeeType()->isVoidType(); |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 167 | /// DebugPrint - Print this standard conversion sequence to standard |
| 168 | /// error. Useful for debugging overloading issues. |
| 169 | void StandardConversionSequence::DebugPrint() const { |
| 170 | bool PrintedSomething = false; |
| 171 | if (First != ICK_Identity) { |
| 172 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 173 | PrintedSomething = true; |
| 174 | } |
| 175 | |
| 176 | if (Second != ICK_Identity) { |
| 177 | if (PrintedSomething) { |
| 178 | fprintf(stderr, " -> "); |
| 179 | } |
| 180 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 181 | |
| 182 | if (CopyConstructor) { |
| 183 | fprintf(stderr, " (by copy constructor)"); |
| 184 | } else if (DirectBinding) { |
| 185 | fprintf(stderr, " (direct reference binding)"); |
| 186 | } else if (ReferenceBinding) { |
| 187 | fprintf(stderr, " (reference binding)"); |
| 188 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 189 | PrintedSomething = true; |
| 190 | } |
| 191 | |
| 192 | if (Third != ICK_Identity) { |
| 193 | if (PrintedSomething) { |
| 194 | fprintf(stderr, " -> "); |
| 195 | } |
| 196 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 197 | PrintedSomething = true; |
| 198 | } |
| 199 | |
| 200 | if (!PrintedSomething) { |
| 201 | fprintf(stderr, "No conversions required"); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 206 | /// error. Useful for debugging overloading issues. |
| 207 | void UserDefinedConversionSequence::DebugPrint() const { |
| 208 | if (Before.First || Before.Second || Before.Third) { |
| 209 | Before.DebugPrint(); |
| 210 | fprintf(stderr, " -> "); |
| 211 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 212 | fprintf(stderr, "'%s'", ConversionFunction->getName().c_str()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 213 | if (After.First || After.Second || After.Third) { |
| 214 | fprintf(stderr, " -> "); |
| 215 | After.DebugPrint(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 220 | /// error. Useful for debugging overloading issues. |
| 221 | void ImplicitConversionSequence::DebugPrint() const { |
| 222 | switch (ConversionKind) { |
| 223 | case StandardConversion: |
| 224 | fprintf(stderr, "Standard conversion: "); |
| 225 | Standard.DebugPrint(); |
| 226 | break; |
| 227 | case UserDefinedConversion: |
| 228 | fprintf(stderr, "User-defined conversion: "); |
| 229 | UserDefined.DebugPrint(); |
| 230 | break; |
| 231 | case EllipsisConversion: |
| 232 | fprintf(stderr, "Ellipsis conversion"); |
| 233 | break; |
| 234 | case BadConversion: |
| 235 | fprintf(stderr, "Bad conversion"); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | fprintf(stderr, "\n"); |
| 240 | } |
| 241 | |
| 242 | // IsOverload - Determine whether the given New declaration is an |
| 243 | // overload of the Old declaration. This routine returns false if New |
| 244 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 245 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 246 | // function (or overload set). When it does return false and Old is an |
| 247 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
| 248 | // FunctionDecl that New cannot be overloaded with. |
| 249 | // |
| 250 | // Example: Given the following input: |
| 251 | // |
| 252 | // void f(int, float); // #1 |
| 253 | // void f(int, int); // #2 |
| 254 | // int f(int, int); // #3 |
| 255 | // |
| 256 | // When we process #1, there is no previous declaration of "f", |
| 257 | // so IsOverload will not be used. |
| 258 | // |
| 259 | // When we process #2, Old is a FunctionDecl for #1. By comparing the |
| 260 | // parameter types, we see that #1 and #2 are overloaded (since they |
| 261 | // have different signatures), so this routine returns false; |
| 262 | // MatchedDecl is unchanged. |
| 263 | // |
| 264 | // When we process #3, Old is an OverloadedFunctionDecl containing #1 |
| 265 | // and #2. We compare the signatures of #3 to #1 (they're overloaded, |
| 266 | // so we do nothing) and then #3 to #2. Since the signatures of #3 and |
| 267 | // #2 are identical (return types of functions are not part of the |
| 268 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 269 | // point to the FunctionDecl for #2. |
| 270 | bool |
| 271 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 272 | OverloadedFunctionDecl::function_iterator& MatchedDecl) |
| 273 | { |
| 274 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 275 | // Is this new function an overload of every function in the |
| 276 | // overload set? |
| 277 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 278 | FuncEnd = Ovl->function_end(); |
| 279 | for (; Func != FuncEnd; ++Func) { |
| 280 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 281 | MatchedDecl = Func; |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // This function overloads every function in the overload set. |
| 287 | return true; |
| 288 | } else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
| 289 | // Is the function New an overload of the function Old? |
| 290 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 291 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 292 | |
| 293 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 294 | // determine whether they are overloads. If we find any mismatch |
| 295 | // in the signature, they are overloads. |
| 296 | |
| 297 | // If either of these functions is a K&R-style function (no |
| 298 | // prototype), then we consider them to have matching signatures. |
| 299 | if (isa<FunctionTypeNoProto>(OldQType.getTypePtr()) || |
| 300 | isa<FunctionTypeNoProto>(NewQType.getTypePtr())) |
| 301 | return false; |
| 302 | |
| 303 | FunctionTypeProto* OldType = cast<FunctionTypeProto>(OldQType.getTypePtr()); |
| 304 | FunctionTypeProto* NewType = cast<FunctionTypeProto>(NewQType.getTypePtr()); |
| 305 | |
| 306 | // The signature of a function includes the types of its |
| 307 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 308 | // of the ellipsis; see C++ DR 357). |
| 309 | if (OldQType != NewQType && |
| 310 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 311 | OldType->isVariadic() != NewType->isVariadic() || |
| 312 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 313 | NewType->arg_type_begin()))) |
| 314 | return true; |
| 315 | |
| 316 | // If the function is a class member, its signature includes the |
| 317 | // cv-qualifiers (if any) on the function itself. |
| 318 | // |
| 319 | // As part of this, also check whether one of the member functions |
| 320 | // is static, in which case they are not overloads (C++ |
| 321 | // 13.1p2). While not part of the definition of the signature, |
| 322 | // this check is important to determine whether these functions |
| 323 | // can be overloaded. |
| 324 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 325 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 326 | if (OldMethod && NewMethod && |
| 327 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 328 | OldQType.getCVRQualifiers() != NewQType.getCVRQualifiers()) |
| 329 | return true; |
| 330 | |
| 331 | // The signatures match; this is not an overload. |
| 332 | return false; |
| 333 | } else { |
| 334 | // (C++ 13p1): |
| 335 | // Only function declarations can be overloaded; object and type |
| 336 | // declarations cannot be overloaded. |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 341 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 342 | /// from the given expression (Expr) to the given type (ToType). This |
| 343 | /// function returns an implicit conversion sequence that can be used |
| 344 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 345 | /// |
| 346 | /// void f(float f); |
| 347 | /// void g(int i) { f(i); } |
| 348 | /// |
| 349 | /// this routine would produce an implicit conversion sequence to |
| 350 | /// describe the initialization of f from i, which will be a standard |
| 351 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 352 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 353 | // |
| 354 | /// Note that this routine only determines how the conversion can be |
| 355 | /// performed; it does not actually perform the conversion. As such, |
| 356 | /// it will not produce any diagnostics if no conversion is available, |
| 357 | /// but will instead return an implicit conversion sequence of kind |
| 358 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 359 | /// |
| 360 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 361 | /// not permitted. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 362 | ImplicitConversionSequence |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 363 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 364 | bool SuppressUserConversions) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 365 | { |
| 366 | ImplicitConversionSequence ICS; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 367 | if (IsStandardConversion(From, ToType, ICS.Standard)) |
| 368 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 369 | else if (!SuppressUserConversions && |
| 370 | IsUserDefinedConversion(From, ToType, ICS.UserDefined)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 371 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 372 | // C++ [over.ics.user]p4: |
| 373 | // A conversion of an expression of class type to the same class |
| 374 | // type is given Exact Match rank, and a conversion of an |
| 375 | // expression of class type to a base class of that type is |
| 376 | // given Conversion rank, in spite of the fact that a copy |
| 377 | // constructor (i.e., a user-defined conversion function) is |
| 378 | // called for those cases. |
| 379 | if (CXXConstructorDecl *Constructor |
| 380 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 381 | if (Constructor->isCopyConstructor(Context)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 382 | // Turn this into a "standard" conversion sequence, so that it |
| 383 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 384 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 385 | ICS.Standard.setAsIdentityConversion(); |
| 386 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 387 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 388 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 389 | if (IsDerivedFrom(From->getType().getUnqualifiedType(), |
| 390 | ToType.getUnqualifiedType())) |
| 391 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 392 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 393 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 394 | } else |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 395 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 396 | |
| 397 | return ICS; |
| 398 | } |
| 399 | |
| 400 | /// IsStandardConversion - Determines whether there is a standard |
| 401 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 402 | /// expression From to the type ToType. Standard conversion sequences |
| 403 | /// only consider non-class types; for conversions that involve class |
| 404 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 405 | /// contain the standard conversion sequence required to perform this |
| 406 | /// conversion and this routine will return true. Otherwise, this |
| 407 | /// routine will return false and the value of SCS is unspecified. |
| 408 | bool |
| 409 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
| 410 | StandardConversionSequence &SCS) |
| 411 | { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | QualType FromType = From->getType(); |
| 413 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 414 | // There are no standard conversions for class types, so abort early. |
| 415 | if (FromType->isRecordType() || ToType->isRecordType()) |
| 416 | return false; |
| 417 | |
| 418 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 419 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 420 | SCS.Deprecated = false; |
| 421 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 422 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 423 | |
| 424 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 425 | // array-to-pointer conversion, or function-to-pointer conversion |
| 426 | // (C++ 4p1). |
| 427 | |
| 428 | // Lvalue-to-rvalue conversion (C++ 4.1): |
| 429 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 430 | // converted to an rvalue. |
| 431 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
| 432 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 433 | !FromType->isFunctionType() && !FromType->isArrayType() && |
| 434 | !FromType->isOverloadType()) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 435 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 436 | |
| 437 | // If T is a non-class type, the type of the rvalue is the |
| 438 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
| 439 | // is T (C++ 4.1p1). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 440 | FromType = FromType.getUnqualifiedType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 441 | } |
| 442 | // Array-to-pointer conversion (C++ 4.2) |
| 443 | else if (FromType->isArrayType()) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 444 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 445 | |
| 446 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 447 | // bound of T" can be converted to an rvalue of type "pointer to |
| 448 | // T" (C++ 4.2p1). |
| 449 | FromType = Context.getArrayDecayedType(FromType); |
| 450 | |
| 451 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 452 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 453 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 454 | |
| 455 | // For the purpose of ranking in overload resolution |
| 456 | // (13.3.3.1.1), this conversion is considered an |
| 457 | // array-to-pointer conversion followed by a qualification |
| 458 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 459 | SCS.Second = ICK_Identity; |
| 460 | SCS.Third = ICK_Qualification; |
| 461 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 462 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | // Function-to-pointer conversion (C++ 4.3). |
| 466 | else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 467 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 468 | |
| 469 | // An lvalue of function type T can be converted to an rvalue of |
| 470 | // type "pointer to T." The result is a pointer to the |
| 471 | // function. (C++ 4.3p1). |
| 472 | FromType = Context.getPointerType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 473 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 474 | // Address of overloaded function (C++ [over.over]). |
| 475 | else if (FunctionDecl *Fn |
| 476 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
| 477 | SCS.First = ICK_Function_To_Pointer; |
| 478 | |
| 479 | // We were able to resolve the address of the overloaded function, |
| 480 | // so we can convert to the type of that function. |
| 481 | FromType = Fn->getType(); |
| 482 | if (ToType->isReferenceType()) |
| 483 | FromType = Context.getReferenceType(FromType); |
| 484 | else |
| 485 | FromType = Context.getPointerType(FromType); |
| 486 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 487 | // We don't require any conversions for the first step. |
| 488 | else { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 489 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // The second conversion can be an integral promotion, floating |
| 493 | // point promotion, integral conversion, floating point conversion, |
| 494 | // floating-integral conversion, pointer conversion, |
| 495 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
| 496 | if (Context.getCanonicalType(FromType).getUnqualifiedType() == |
| 497 | Context.getCanonicalType(ToType).getUnqualifiedType()) { |
| 498 | // The unqualified versions of the types are the same: there's no |
| 499 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 500 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 501 | } |
| 502 | // Integral promotion (C++ 4.5). |
| 503 | else if (IsIntegralPromotion(From, FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 504 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 505 | FromType = ToType.getUnqualifiedType(); |
| 506 | } |
| 507 | // Floating point promotion (C++ 4.6). |
| 508 | else if (IsFloatingPointPromotion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 509 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 510 | FromType = ToType.getUnqualifiedType(); |
| 511 | } |
| 512 | // Integral conversions (C++ 4.7). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 513 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 514 | else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 515 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 516 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 517 | FromType = ToType.getUnqualifiedType(); |
| 518 | } |
| 519 | // Floating point conversions (C++ 4.8). |
| 520 | else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 521 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 522 | FromType = ToType.getUnqualifiedType(); |
| 523 | } |
| 524 | // Floating-integral conversions (C++ 4.9). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 525 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 526 | else if ((FromType->isFloatingType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 527 | ToType->isIntegralType() && !ToType->isBooleanType() && |
| 528 | !ToType->isEnumeralType()) || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 529 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
| 530 | ToType->isFloatingType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 531 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | FromType = ToType.getUnqualifiedType(); |
| 533 | } |
| 534 | // Pointer conversions (C++ 4.10). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 535 | else if (IsPointerConversion(From, FromType, ToType, FromType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 536 | SCS.Second = ICK_Pointer_Conversion; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 537 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 538 | // FIXME: Pointer to member conversions (4.11). |
| 539 | // Boolean conversions (C++ 4.12). |
| 540 | // FIXME: pointer-to-member type |
| 541 | else if (ToType->isBooleanType() && |
| 542 | (FromType->isArithmeticType() || |
| 543 | FromType->isEnumeralType() || |
| 544 | FromType->isPointerType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 545 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 546 | FromType = Context.BoolTy; |
| 547 | } else { |
| 548 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 549 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 552 | QualType CanonFrom; |
| 553 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 554 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 555 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 556 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 557 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 558 | CanonFrom = Context.getCanonicalType(FromType); |
| 559 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 560 | } else { |
| 561 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 562 | SCS.Third = ICK_Identity; |
| 563 | |
| 564 | // C++ [over.best.ics]p6: |
| 565 | // [...] Any difference in top-level cv-qualification is |
| 566 | // subsumed by the initialization itself and does not constitute |
| 567 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 568 | CanonFrom = Context.getCanonicalType(FromType); |
| 569 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 570 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 571 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 572 | FromType = ToType; |
| 573 | CanonFrom = CanonTo; |
| 574 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // If we have not converted the argument type to the parameter type, |
| 578 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 579 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 580 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 582 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 583 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 587 | /// expression From (whose potentially-adjusted type is FromType) to |
| 588 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 589 | /// sets PromotedType to the promoted type. |
| 590 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) |
| 591 | { |
| 592 | const BuiltinType *To = ToType->getAsBuiltinType(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 593 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 594 | if (!To) { |
| 595 | return false; |
| 596 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 597 | |
| 598 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 599 | // unsigned short int can be converted to an rvalue of type int if |
| 600 | // int can represent all the values of the source type; otherwise, |
| 601 | // the source rvalue can be converted to an rvalue of type unsigned |
| 602 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 603 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 604 | if (// We can promote any signed, promotable integer type to an int |
| 605 | (FromType->isSignedIntegerType() || |
| 606 | // We can promote any unsigned integer type whose size is |
| 607 | // less than int to an int. |
| 608 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 609 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 610 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 613 | return To->getKind() == BuiltinType::UInt; |
| 614 | } |
| 615 | |
| 616 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 617 | // can be converted to an rvalue of the first of the following types |
| 618 | // that can represent all the values of its underlying type: int, |
| 619 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 620 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 621 | && ToType->isIntegerType()) { |
| 622 | // Determine whether the type we're converting from is signed or |
| 623 | // unsigned. |
| 624 | bool FromIsSigned; |
| 625 | uint64_t FromSize = Context.getTypeSize(FromType); |
| 626 | if (const EnumType *FromEnumType = FromType->getAsEnumType()) { |
| 627 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 628 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 629 | } else { |
| 630 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 631 | FromIsSigned = true; |
| 632 | } |
| 633 | |
| 634 | // The types we'll try to promote to, in the appropriate |
| 635 | // order. Try each of these types. |
| 636 | QualType PromoteTypes[4] = { |
| 637 | Context.IntTy, Context.UnsignedIntTy, |
| 638 | Context.LongTy, Context.UnsignedLongTy |
| 639 | }; |
| 640 | for (int Idx = 0; Idx < 0; ++Idx) { |
| 641 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 642 | if (FromSize < ToSize || |
| 643 | (FromSize == ToSize && |
| 644 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 645 | // We found the type that we can promote to. If this is the |
| 646 | // type we wanted, we have a promotion. Otherwise, no |
| 647 | // promotion. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 648 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 649 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 655 | // rvalue of type int if int can represent all the values of the |
| 656 | // bit-field; otherwise, it can be converted to unsigned int if |
| 657 | // unsigned int can represent all the values of the bit-field. If |
| 658 | // the bit-field is larger yet, no integral promotion applies to |
| 659 | // it. If the bit-field has an enumerated type, it is treated as any |
| 660 | // other value of that type for promotion purposes (C++ 4.5p3). |
| 661 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(From)) { |
| 662 | using llvm::APSInt; |
| 663 | FieldDecl *MemberDecl = MemRef->getMemberDecl(); |
| 664 | APSInt BitWidth; |
| 665 | if (MemberDecl->isBitField() && |
| 666 | FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 667 | From->isIntegerConstantExpr(BitWidth, Context)) { |
| 668 | APSInt ToSize(Context.getTypeSize(ToType)); |
| 669 | |
| 670 | // Are we promoting to an int from a bitfield that fits in an int? |
| 671 | if (BitWidth < ToSize || |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 672 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 673 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 676 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 677 | // that fits into an unsigned int? |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 678 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 679 | return To->getKind() == BuiltinType::UInt; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 680 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 681 | |
| 682 | return false; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 687 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 688 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 689 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 690 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 691 | |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 696 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 697 | /// returns true and sets PromotedType to the promoted type. |
| 698 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) |
| 699 | { |
| 700 | /// An rvalue of type float can be converted to an rvalue of type |
| 701 | /// double. (C++ 4.6p1). |
| 702 | if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType()) |
| 703 | if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) |
| 704 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 705 | ToBuiltin->getKind() == BuiltinType::Double) |
| 706 | return true; |
| 707 | |
| 708 | return false; |
| 709 | } |
| 710 | |
| 711 | /// IsPointerConversion - Determines whether the conversion of the |
| 712 | /// expression From, which has the (possibly adjusted) type FromType, |
| 713 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 714 | /// 4.10). If so, returns true and places the converted type (that |
| 715 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 716 | /// ConvertedType. |
| 717 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
| 718 | QualType& ConvertedType) |
| 719 | { |
| 720 | const PointerType* ToTypePtr = ToType->getAsPointerType(); |
| 721 | if (!ToTypePtr) |
| 722 | return false; |
| 723 | |
| 724 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
| 725 | if (From->isNullPointerConstant(Context)) { |
| 726 | ConvertedType = ToType; |
| 727 | return true; |
| 728 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 729 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 730 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 731 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 732 | // 4.10p2). |
| 733 | if (FromType->isPointerType() && |
| 734 | FromType->getAsPointerType()->getPointeeType()->isObjectType() && |
| 735 | ToTypePtr->getPointeeType()->isVoidType()) { |
| 736 | // We need to produce a pointer to cv void, where cv is the same |
| 737 | // set of cv-qualifiers as we had on the incoming pointee type. |
| 738 | QualType toPointee = ToTypePtr->getPointeeType(); |
| 739 | unsigned Quals = Context.getCanonicalType(FromType)->getAsPointerType() |
| 740 | ->getPointeeType().getCVRQualifiers(); |
| 741 | |
| 742 | if (Context.getCanonicalType(ToTypePtr->getPointeeType()).getCVRQualifiers() |
| 743 | == Quals) { |
| 744 | // ToType is exactly the type we want. Use it. |
| 745 | ConvertedType = ToType; |
| 746 | } else { |
| 747 | // Build a new type with the right qualifiers. |
| 748 | ConvertedType |
| 749 | = Context.getPointerType(Context.VoidTy.getQualifiedType(Quals)); |
| 750 | } |
| 751 | return true; |
| 752 | } |
| 753 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 754 | // C++ [conv.ptr]p3: |
| 755 | // |
| 756 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 757 | // can be converted to an rvalue of type "pointer to cv B," where |
| 758 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 759 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 760 | // necessitates this conversion is ill-formed. The result of the |
| 761 | // conversion is a pointer to the base class sub-object of the |
| 762 | // derived class object. The null pointer value is converted to |
| 763 | // the null pointer value of the destination type. |
| 764 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 765 | // Note that we do not check for ambiguity or inaccessibility |
| 766 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 767 | if (const PointerType *FromPtrType = FromType->getAsPointerType()) |
| 768 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) { |
| 769 | if (FromPtrType->getPointeeType()->isRecordType() && |
| 770 | ToPtrType->getPointeeType()->isRecordType() && |
| 771 | IsDerivedFrom(FromPtrType->getPointeeType(), |
| 772 | ToPtrType->getPointeeType())) { |
| 773 | // The conversion is okay. Now, we need to produce the type |
| 774 | // that results from this conversion, which will have the same |
| 775 | // qualifiers as the incoming type. |
| 776 | QualType CanonFromPointee |
| 777 | = Context.getCanonicalType(FromPtrType->getPointeeType()); |
| 778 | QualType ToPointee = ToPtrType->getPointeeType(); |
| 779 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| 780 | unsigned Quals = CanonFromPointee.getCVRQualifiers(); |
| 781 | |
| 782 | if (CanonToPointee.getCVRQualifiers() == Quals) { |
| 783 | // ToType is exactly the type we want. Use it. |
| 784 | ConvertedType = ToType; |
| 785 | } else { |
| 786 | // Build a new type with the right qualifiers. |
| 787 | ConvertedType |
| 788 | = Context.getPointerType(CanonToPointee.getQualifiedType(Quals)); |
| 789 | } |
| 790 | return true; |
| 791 | } |
| 792 | } |
| 793 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 794 | return false; |
| 795 | } |
| 796 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 797 | /// CheckPointerConversion - Check the pointer conversion from the |
| 798 | /// expression From to the type ToType. This routine checks for |
| 799 | /// ambiguous (FIXME: or inaccessible) derived-to-base pointer |
| 800 | /// conversions for which IsPointerConversion has already returned |
| 801 | /// true. It returns true and produces a diagnostic if there was an |
| 802 | /// error, or returns false otherwise. |
| 803 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType) { |
| 804 | QualType FromType = From->getType(); |
| 805 | |
| 806 | if (const PointerType *FromPtrType = FromType->getAsPointerType()) |
| 807 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 808 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 809 | /*DetectVirtual=*/false); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 810 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 811 | ToPointeeType = ToPtrType->getPointeeType(); |
| 812 | if (FromPointeeType->isRecordType() && |
| 813 | ToPointeeType->isRecordType()) { |
| 814 | // We must have a derived-to-base conversion. Check an |
| 815 | // ambiguous or inaccessible conversion. |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 816 | return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 817 | From->getExprLoc(), |
| 818 | From->getSourceRange()); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
| 822 | return false; |
| 823 | } |
| 824 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 825 | /// IsQualificationConversion - Determines whether the conversion from |
| 826 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 827 | /// (C++ 4.4). |
| 828 | bool |
| 829 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) |
| 830 | { |
| 831 | FromType = Context.getCanonicalType(FromType); |
| 832 | ToType = Context.getCanonicalType(ToType); |
| 833 | |
| 834 | // If FromType and ToType are the same type, this is not a |
| 835 | // qualification conversion. |
| 836 | if (FromType == ToType) |
| 837 | return false; |
| 838 | |
| 839 | // (C++ 4.4p4): |
| 840 | // A conversion can add cv-qualifiers at levels other than the first |
| 841 | // in multi-level pointers, subject to the following rules: [...] |
| 842 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 843 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 844 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 845 | // Within each iteration of the loop, we check the qualifiers to |
| 846 | // determine if this still looks like a qualification |
| 847 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 848 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 849 | // until there are no more pointers or pointers-to-members left to |
| 850 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 851 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 852 | |
| 853 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 854 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 855 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 856 | return false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 858 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 859 | // every cv for 0 < k < j. |
| 860 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 861 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 862 | return false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 864 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 865 | // include const. |
| 866 | PreviousToQualsIncludeConst |
| 867 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 869 | |
| 870 | // We are left with FromType and ToType being the pointee types |
| 871 | // after unwrapping the original FromType and ToType the same number |
| 872 | // of types. If we unwrapped any pointers, and if FromType and |
| 873 | // ToType have the same unqualified type (since we checked |
| 874 | // qualifiers above), then this is a qualification conversion. |
| 875 | return UnwrappedAnyPointer && |
| 876 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 877 | } |
| 878 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 879 | /// IsUserDefinedConversion - Determines whether there is a |
| 880 | /// user-defined conversion sequence (C++ [over.ics.user]) that |
| 881 | /// converts expression From to the type ToType. If such a conversion |
| 882 | /// exists, User will contain the user-defined conversion sequence |
| 883 | /// that performs such a conversion and this routine will return |
| 884 | /// true. Otherwise, this routine returns false and User is |
| 885 | /// unspecified. |
| 886 | bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 887 | UserDefinedConversionSequence& User) |
| 888 | { |
| 889 | OverloadCandidateSet CandidateSet; |
| 890 | if (const CXXRecordType *ToRecordType |
| 891 | = dyn_cast_or_null<CXXRecordType>(ToType->getAsRecordType())) { |
| 892 | // C++ [over.match.ctor]p1: |
| 893 | // When objects of class type are direct-initialized (8.5), or |
| 894 | // copy-initialized from an expression of the same or a |
| 895 | // derived class type (8.5), overload resolution selects the |
| 896 | // constructor. [...] For copy-initialization, the candidate |
| 897 | // functions are all the converting constructors (12.3.1) of |
| 898 | // that class. The argument list is the expression-list within |
| 899 | // the parentheses of the initializer. |
| 900 | CXXRecordDecl *ToRecordDecl = ToRecordType->getDecl(); |
| 901 | const OverloadedFunctionDecl *Constructors = ToRecordDecl->getConstructors(); |
| 902 | for (OverloadedFunctionDecl::function_const_iterator func |
| 903 | = Constructors->function_begin(); |
| 904 | func != Constructors->function_end(); ++func) { |
| 905 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*func); |
| 906 | if (Constructor->isConvertingConstructor()) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 907 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 908 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 912 | if (const CXXRecordType *FromRecordType |
| 913 | = dyn_cast_or_null<CXXRecordType>(From->getType()->getAsRecordType())) { |
| 914 | // Add all of the conversion functions as candidates. |
| 915 | // FIXME: Look for conversions in base classes! |
| 916 | CXXRecordDecl *FromRecordDecl = FromRecordType->getDecl(); |
| 917 | OverloadedFunctionDecl *Conversions |
| 918 | = FromRecordDecl->getConversionFunctions(); |
| 919 | for (OverloadedFunctionDecl::function_iterator Func |
| 920 | = Conversions->function_begin(); |
| 921 | Func != Conversions->function_end(); ++Func) { |
| 922 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 923 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 924 | } |
| 925 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 926 | |
| 927 | OverloadCandidateSet::iterator Best; |
| 928 | switch (BestViableFunction(CandidateSet, Best)) { |
| 929 | case OR_Success: |
| 930 | // Record the standard conversion we used and the conversion function. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 931 | if (CXXConstructorDecl *Constructor |
| 932 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 933 | // C++ [over.ics.user]p1: |
| 934 | // If the user-defined conversion is specified by a |
| 935 | // constructor (12.3.1), the initial standard conversion |
| 936 | // sequence converts the source type to the type required by |
| 937 | // the argument of the constructor. |
| 938 | // |
| 939 | // FIXME: What about ellipsis conversions? |
| 940 | QualType ThisType = Constructor->getThisType(Context); |
| 941 | User.Before = Best->Conversions[0].Standard; |
| 942 | User.ConversionFunction = Constructor; |
| 943 | User.After.setAsIdentityConversion(); |
| 944 | User.After.FromTypePtr |
| 945 | = ThisType->getAsPointerType()->getPointeeType().getAsOpaquePtr(); |
| 946 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
| 947 | return true; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 948 | } else if (CXXConversionDecl *Conversion |
| 949 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 950 | // C++ [over.ics.user]p1: |
| 951 | // |
| 952 | // [...] If the user-defined conversion is specified by a |
| 953 | // conversion function (12.3.2), the initial standard |
| 954 | // conversion sequence converts the source type to the |
| 955 | // implicit object parameter of the conversion function. |
| 956 | User.Before = Best->Conversions[0].Standard; |
| 957 | User.ConversionFunction = Conversion; |
| 958 | |
| 959 | // C++ [over.ics.user]p2: |
| 960 | // The second standard conversion sequence converts the |
| 961 | // result of the user-defined conversion to the target type |
| 962 | // for the sequence. Since an implicit conversion sequence |
| 963 | // is an initialization, the special rules for |
| 964 | // initialization by user-defined conversion apply when |
| 965 | // selecting the best user-defined conversion for a |
| 966 | // user-defined conversion sequence (see 13.3.3 and |
| 967 | // 13.3.3.1). |
| 968 | User.After = Best->FinalConversion; |
| 969 | return true; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 970 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 971 | assert(false && "Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 972 | return false; |
| 973 | } |
| 974 | |
| 975 | case OR_No_Viable_Function: |
| 976 | // No conversion here! We're done. |
| 977 | return false; |
| 978 | |
| 979 | case OR_Ambiguous: |
| 980 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 981 | // ambiguous conversion sequences. |
| 982 | return false; |
| 983 | } |
| 984 | |
| 985 | return false; |
| 986 | } |
| 987 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 988 | /// CompareImplicitConversionSequences - Compare two implicit |
| 989 | /// conversion sequences to determine whether one is better than the |
| 990 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
| 991 | ImplicitConversionSequence::CompareKind |
| 992 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 993 | const ImplicitConversionSequence& ICS2) |
| 994 | { |
| 995 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 996 | // conversion sequences (as defined in 13.3.3.1) |
| 997 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 998 | // conversion sequence than a user-defined conversion sequence or |
| 999 | // an ellipsis conversion sequence, and |
| 1000 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1001 | // conversion sequence than an ellipsis conversion sequence |
| 1002 | // (13.3.3.1.3). |
| 1003 | // |
| 1004 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1005 | return ImplicitConversionSequence::Better; |
| 1006 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1007 | return ImplicitConversionSequence::Worse; |
| 1008 | |
| 1009 | // Two implicit conversion sequences of the same form are |
| 1010 | // indistinguishable conversion sequences unless one of the |
| 1011 | // following rules apply: (C++ 13.3.3.2p3): |
| 1012 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1013 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
| 1014 | else if (ICS1.ConversionKind == |
| 1015 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1016 | // User-defined conversion sequence U1 is a better conversion |
| 1017 | // sequence than another user-defined conversion sequence U2 if |
| 1018 | // they contain the same user-defined conversion function or |
| 1019 | // constructor and if the second standard conversion sequence of |
| 1020 | // U1 is better than the second standard conversion sequence of |
| 1021 | // U2 (C++ 13.3.3.2p3). |
| 1022 | if (ICS1.UserDefined.ConversionFunction == |
| 1023 | ICS2.UserDefined.ConversionFunction) |
| 1024 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1025 | ICS2.UserDefined.After); |
| 1026 | } |
| 1027 | |
| 1028 | return ImplicitConversionSequence::Indistinguishable; |
| 1029 | } |
| 1030 | |
| 1031 | /// CompareStandardConversionSequences - Compare two standard |
| 1032 | /// conversion sequences to determine whether one is better than the |
| 1033 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
| 1034 | ImplicitConversionSequence::CompareKind |
| 1035 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1036 | const StandardConversionSequence& SCS2) |
| 1037 | { |
| 1038 | // Standard conversion sequence S1 is a better conversion sequence |
| 1039 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1040 | |
| 1041 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1042 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1043 | // excluding any Lvalue Transformation; the identity conversion |
| 1044 | // sequence is considered to be a subsequence of any |
| 1045 | // non-identity conversion sequence) or, if not that, |
| 1046 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1047 | // Neither is a proper subsequence of the other. Do nothing. |
| 1048 | ; |
| 1049 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1050 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
| 1051 | (SCS1.Second == ICK_Identity && |
| 1052 | SCS1.Third == ICK_Identity)) |
| 1053 | // SCS1 is a proper subsequence of SCS2. |
| 1054 | return ImplicitConversionSequence::Better; |
| 1055 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1056 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
| 1057 | (SCS2.Second == ICK_Identity && |
| 1058 | SCS2.Third == ICK_Identity)) |
| 1059 | // SCS2 is a proper subsequence of SCS1. |
| 1060 | return ImplicitConversionSequence::Worse; |
| 1061 | |
| 1062 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1063 | // defined below), or, if not that, |
| 1064 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1065 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1066 | if (Rank1 < Rank2) |
| 1067 | return ImplicitConversionSequence::Better; |
| 1068 | else if (Rank2 < Rank1) |
| 1069 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1071 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1072 | // are indistinguishable unless one of the following rules |
| 1073 | // applies: |
| 1074 | |
| 1075 | // A conversion that is not a conversion of a pointer, or |
| 1076 | // pointer to member, to bool is better than another conversion |
| 1077 | // that is such a conversion. |
| 1078 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1079 | return SCS2.isPointerConversionToBool() |
| 1080 | ? ImplicitConversionSequence::Better |
| 1081 | : ImplicitConversionSequence::Worse; |
| 1082 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1083 | // C++ [over.ics.rank]p4b2: |
| 1084 | // |
| 1085 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1086 | // conversion of B* to A* is better than conversion of B* to |
| 1087 | // void*, and conversion of A* to void* is better than conversion |
| 1088 | // of B* to void*. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1089 | bool SCS1ConvertsToVoid |
| 1090 | = SCS1.isPointerConversionToVoidPointer(Context); |
| 1091 | bool SCS2ConvertsToVoid |
| 1092 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1093 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1094 | // Exactly one of the conversion sequences is a conversion to |
| 1095 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1096 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1097 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1098 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1099 | // Neither conversion sequence converts to a void pointer; compare |
| 1100 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1101 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1102 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1103 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1104 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1105 | // Both conversion sequences are conversions to void |
| 1106 | // pointers. Compare the source types to determine if there's an |
| 1107 | // inheritance relationship in their sources. |
| 1108 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1109 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1110 | |
| 1111 | // Adjust the types we're converting from via the array-to-pointer |
| 1112 | // conversion, if we need to. |
| 1113 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1114 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1115 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1116 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1117 | |
| 1118 | QualType FromPointee1 |
| 1119 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1120 | QualType FromPointee2 |
| 1121 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1122 | |
| 1123 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1124 | return ImplicitConversionSequence::Better; |
| 1125 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1126 | return ImplicitConversionSequence::Worse; |
| 1127 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1130 | // bullet 3). |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1131 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1132 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1133 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1135 | // C++ [over.ics.rank]p3b4: |
| 1136 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1137 | // which the references refer are the same type except for |
| 1138 | // top-level cv-qualifiers, and the type to which the reference |
| 1139 | // initialized by S2 refers is more cv-qualified than the type |
| 1140 | // to which the reference initialized by S1 refers. |
| 1141 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
| 1142 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1143 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1144 | T1 = Context.getCanonicalType(T1); |
| 1145 | T2 = Context.getCanonicalType(T2); |
| 1146 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1147 | if (T2.isMoreQualifiedThan(T1)) |
| 1148 | return ImplicitConversionSequence::Better; |
| 1149 | else if (T1.isMoreQualifiedThan(T2)) |
| 1150 | return ImplicitConversionSequence::Worse; |
| 1151 | } |
| 1152 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1153 | |
| 1154 | return ImplicitConversionSequence::Indistinguishable; |
| 1155 | } |
| 1156 | |
| 1157 | /// CompareQualificationConversions - Compares two standard conversion |
| 1158 | /// sequences to determine whether they can be ranked based on their |
| 1159 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1160 | ImplicitConversionSequence::CompareKind |
| 1161 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
| 1162 | const StandardConversionSequence& SCS2) |
| 1163 | { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1164 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1165 | // -- S1 and S2 differ only in their qualification conversion and |
| 1166 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1167 | // cv-qualification signature of type T1 is a proper subset of |
| 1168 | // the cv-qualification signature of type T2, and S1 is not the |
| 1169 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1170 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1171 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1172 | return ImplicitConversionSequence::Indistinguishable; |
| 1173 | |
| 1174 | // FIXME: the example in the standard doesn't use a qualification |
| 1175 | // conversion (!) |
| 1176 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1177 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1178 | T1 = Context.getCanonicalType(T1); |
| 1179 | T2 = Context.getCanonicalType(T2); |
| 1180 | |
| 1181 | // If the types are the same, we won't learn anything by unwrapped |
| 1182 | // them. |
| 1183 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1184 | return ImplicitConversionSequence::Indistinguishable; |
| 1185 | |
| 1186 | ImplicitConversionSequence::CompareKind Result |
| 1187 | = ImplicitConversionSequence::Indistinguishable; |
| 1188 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1189 | // Within each iteration of the loop, we check the qualifiers to |
| 1190 | // determine if this still looks like a qualification |
| 1191 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1192 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1193 | // until there are no more pointers or pointers-to-members left |
| 1194 | // to unwrap. This essentially mimics what |
| 1195 | // IsQualificationConversion does, but here we're checking for a |
| 1196 | // strict subset of qualifiers. |
| 1197 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1198 | // The qualifiers are the same, so this doesn't tell us anything |
| 1199 | // about how the sequences rank. |
| 1200 | ; |
| 1201 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1202 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1203 | if (Result == ImplicitConversionSequence::Worse) |
| 1204 | // Neither has qualifiers that are a subset of the other's |
| 1205 | // qualifiers. |
| 1206 | return ImplicitConversionSequence::Indistinguishable; |
| 1207 | |
| 1208 | Result = ImplicitConversionSequence::Better; |
| 1209 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1210 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1211 | if (Result == ImplicitConversionSequence::Better) |
| 1212 | // Neither has qualifiers that are a subset of the other's |
| 1213 | // qualifiers. |
| 1214 | return ImplicitConversionSequence::Indistinguishable; |
| 1215 | |
| 1216 | Result = ImplicitConversionSequence::Worse; |
| 1217 | } else { |
| 1218 | // Qualifiers are disjoint. |
| 1219 | return ImplicitConversionSequence::Indistinguishable; |
| 1220 | } |
| 1221 | |
| 1222 | // If the types after this point are equivalent, we're done. |
| 1223 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1224 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1227 | // Check that the winning standard conversion sequence isn't using |
| 1228 | // the deprecated string literal array to pointer conversion. |
| 1229 | switch (Result) { |
| 1230 | case ImplicitConversionSequence::Better: |
| 1231 | if (SCS1.Deprecated) |
| 1232 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1233 | break; |
| 1234 | |
| 1235 | case ImplicitConversionSequence::Indistinguishable: |
| 1236 | break; |
| 1237 | |
| 1238 | case ImplicitConversionSequence::Worse: |
| 1239 | if (SCS2.Deprecated) |
| 1240 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1241 | break; |
| 1242 | } |
| 1243 | |
| 1244 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1247 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1248 | /// sequences to determine whether they can be ranked based on their |
| 1249 | /// various kinds of derived-to-base conversions (C++ [over.ics.rank]p4b3). |
| 1250 | ImplicitConversionSequence::CompareKind |
| 1251 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1252 | const StandardConversionSequence& SCS2) { |
| 1253 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1254 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1255 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1256 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1257 | |
| 1258 | // Adjust the types we're converting from via the array-to-pointer |
| 1259 | // conversion, if we need to. |
| 1260 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1261 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1262 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1263 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1264 | |
| 1265 | // Canonicalize all of the types. |
| 1266 | FromType1 = Context.getCanonicalType(FromType1); |
| 1267 | ToType1 = Context.getCanonicalType(ToType1); |
| 1268 | FromType2 = Context.getCanonicalType(FromType2); |
| 1269 | ToType2 = Context.getCanonicalType(ToType2); |
| 1270 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1271 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1272 | // |
| 1273 | // If class B is derived directly or indirectly from class A and |
| 1274 | // class C is derived directly or indirectly from B, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Compare based on pointer conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1277 | if (SCS1.Second == ICK_Pointer_Conversion && |
| 1278 | SCS2.Second == ICK_Pointer_Conversion) { |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1279 | QualType FromPointee1 |
| 1280 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1281 | QualType ToPointee1 |
| 1282 | = ToType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1283 | QualType FromPointee2 |
| 1284 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1285 | QualType ToPointee2 |
| 1286 | = ToType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1287 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1288 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1289 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1290 | return ImplicitConversionSequence::Better; |
| 1291 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1292 | return ImplicitConversionSequence::Worse; |
| 1293 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1294 | |
| 1295 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1296 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1297 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1298 | return ImplicitConversionSequence::Better; |
| 1299 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1300 | return ImplicitConversionSequence::Worse; |
| 1301 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1304 | // Compare based on reference bindings. |
| 1305 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1306 | SCS1.Second == ICK_Derived_To_Base) { |
| 1307 | // -- binding of an expression of type C to a reference of type |
| 1308 | // B& is better than binding an expression of type C to a |
| 1309 | // reference of type A&, |
| 1310 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1311 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1312 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1313 | return ImplicitConversionSequence::Better; |
| 1314 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1315 | return ImplicitConversionSequence::Worse; |
| 1316 | } |
| 1317 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1318 | // -- binding of an expression of type B to a reference of type |
| 1319 | // A& is better than binding an expression of type C to a |
| 1320 | // reference of type A&, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1321 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1322 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1323 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1324 | return ImplicitConversionSequence::Better; |
| 1325 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1326 | return ImplicitConversionSequence::Worse; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1332 | // A::* to C::*, |
| 1333 | |
| 1334 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1335 | // A::* to C::*, and |
| 1336 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1337 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1338 | SCS1.Second == ICK_Derived_To_Base) { |
| 1339 | // -- conversion of C to B is better than conversion of C to A, |
| 1340 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1341 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1342 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1343 | return ImplicitConversionSequence::Better; |
| 1344 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1345 | return ImplicitConversionSequence::Worse; |
| 1346 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1347 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1348 | // -- conversion of B to A is better than conversion of C to A. |
| 1349 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1350 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1351 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1352 | return ImplicitConversionSequence::Better; |
| 1353 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1354 | return ImplicitConversionSequence::Worse; |
| 1355 | } |
| 1356 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1357 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1358 | return ImplicitConversionSequence::Indistinguishable; |
| 1359 | } |
| 1360 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1361 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1362 | /// ToType from the expression From. Return the implicit conversion |
| 1363 | /// sequence required to pass this argument, which may be a bad |
| 1364 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1365 | /// a parameter of this type). If @p SuppressUserConversions, then we |
| 1366 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1367 | ImplicitConversionSequence |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1368 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
| 1369 | bool SuppressUserConversions) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1370 | if (!getLangOptions().CPlusPlus) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1371 | // In C, copy initialization is the same as performing an assignment. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1372 | AssignConvertType ConvTy = |
| 1373 | CheckSingleAssignmentConstraints(ToType, From); |
| 1374 | ImplicitConversionSequence ICS; |
| 1375 | if (getLangOptions().NoExtensions? ConvTy != Compatible |
| 1376 | : ConvTy == Incompatible) |
| 1377 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1378 | else |
| 1379 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1380 | return ICS; |
| 1381 | } else if (ToType->isReferenceType()) { |
| 1382 | ImplicitConversionSequence ICS; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1383 | CheckReferenceInit(From, ToType, &ICS, SuppressUserConversions); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1384 | return ICS; |
| 1385 | } else { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1386 | return TryImplicitConversion(From, ToType, SuppressUserConversions); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | /// PerformArgumentPassing - Pass the argument Arg into a parameter of |
| 1391 | /// type ToType. Returns true (and emits a diagnostic) if there was |
| 1392 | /// an error, returns false if the initialization succeeded. |
| 1393 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
| 1394 | const char* Flavor) { |
| 1395 | if (!getLangOptions().CPlusPlus) { |
| 1396 | // In C, argument passing is the same as performing an assignment. |
| 1397 | QualType FromType = From->getType(); |
| 1398 | AssignConvertType ConvTy = |
| 1399 | CheckSingleAssignmentConstraints(ToType, From); |
| 1400 | |
| 1401 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 1402 | FromType, From, Flavor); |
| 1403 | } else if (ToType->isReferenceType()) { |
| 1404 | return CheckReferenceInit(From, ToType); |
| 1405 | } else { |
| 1406 | if (PerformImplicitConversion(From, ToType)) |
| 1407 | return Diag(From->getSourceRange().getBegin(), |
| 1408 | diag::err_typecheck_convert_incompatible, |
| 1409 | ToType.getAsString(), From->getType().getAsString(), |
| 1410 | Flavor, |
| 1411 | From->getSourceRange()); |
| 1412 | else |
| 1413 | return false; |
| 1414 | } |
| 1415 | } |
| 1416 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1417 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1418 | /// candidate functions, using the given function call arguments. If |
| 1419 | /// @p SuppressUserConversions, then don't allow user-defined |
| 1420 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1421 | void |
| 1422 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
| 1423 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1424 | OverloadCandidateSet& CandidateSet, |
| 1425 | bool SuppressUserConversions) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1426 | { |
| 1427 | const FunctionTypeProto* Proto |
| 1428 | = dyn_cast<FunctionTypeProto>(Function->getType()->getAsFunctionType()); |
| 1429 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1430 | assert(!isa<CXXConversionDecl>(Function) && |
| 1431 | "Use AddConversionCandidate for conversion functions"); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Add this candidate |
| 1434 | CandidateSet.push_back(OverloadCandidate()); |
| 1435 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1436 | Candidate.Function = Function; |
| 1437 | |
| 1438 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1439 | |
| 1440 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 1441 | // parameters is viable only if it has an ellipsis in its parameter |
| 1442 | // list (8.3.5). |
| 1443 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 1444 | Candidate.Viable = false; |
| 1445 | return; |
| 1446 | } |
| 1447 | |
| 1448 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 1449 | // is viable only if the (m+1)st parameter has a default argument |
| 1450 | // (8.3.6). For the purposes of overload resolution, the |
| 1451 | // parameter list is truncated on the right, so that there are |
| 1452 | // exactly m parameters. |
| 1453 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 1454 | if (NumArgs < MinRequiredArgs) { |
| 1455 | // Not enough arguments. |
| 1456 | Candidate.Viable = false; |
| 1457 | return; |
| 1458 | } |
| 1459 | |
| 1460 | // Determine the implicit conversion sequences for each of the |
| 1461 | // arguments. |
| 1462 | Candidate.Viable = true; |
| 1463 | Candidate.Conversions.resize(NumArgs); |
| 1464 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 1465 | if (ArgIdx < NumArgsInProto) { |
| 1466 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 1467 | // exist for each argument an implicit conversion sequence |
| 1468 | // (13.3.3.1) that converts that argument to the corresponding |
| 1469 | // parameter of F. |
| 1470 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 1471 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1472 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 1473 | SuppressUserConversions); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1474 | if (Candidate.Conversions[ArgIdx].ConversionKind |
| 1475 | == ImplicitConversionSequence::BadConversion) |
| 1476 | Candidate.Viable = false; |
| 1477 | } else { |
| 1478 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 1479 | // argument for which there is no corresponding parameter is |
| 1480 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 1481 | Candidate.Conversions[ArgIdx].ConversionKind |
| 1482 | = ImplicitConversionSequence::EllipsisConversion; |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1487 | /// AddConversionCandidate - Add a C++ conversion function as a |
| 1488 | /// candidate in the candidate set (C++ [over.match.conv], |
| 1489 | /// C++ [over.match.copy]). From is the expression we're converting from, |
| 1490 | /// and ToType is the type that we're eventually trying to convert to |
| 1491 | /// (which may or may not be the same type as the type that the |
| 1492 | /// conversion function produces). |
| 1493 | void |
| 1494 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 1495 | Expr *From, QualType ToType, |
| 1496 | OverloadCandidateSet& CandidateSet) { |
| 1497 | // Add this candidate |
| 1498 | CandidateSet.push_back(OverloadCandidate()); |
| 1499 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1500 | Candidate.Function = Conversion; |
| 1501 | Candidate.FinalConversion.setAsIdentityConversion(); |
| 1502 | Candidate.FinalConversion.FromTypePtr |
| 1503 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 1504 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 1505 | |
| 1506 | // Determine the implicit conversion sequences for each of the |
| 1507 | // arguments. |
| 1508 | Candidate.Viable = true; |
| 1509 | Candidate.Conversions.resize(1); |
| 1510 | |
| 1511 | // FIXME: We need to follow the rules for the implicit object |
| 1512 | // parameter. |
| 1513 | QualType ImplicitObjectType |
| 1514 | = Context.getTypeDeclType(Conversion->getParent()); |
| 1515 | ImplicitObjectType |
| 1516 | = ImplicitObjectType.getQualifiedType(Conversion->getTypeQualifiers()); |
| 1517 | ImplicitObjectType = Context.getReferenceType(ImplicitObjectType); |
| 1518 | Candidate.Conversions[0] = TryCopyInitialization(From, ImplicitObjectType, |
| 1519 | true); |
| 1520 | if (Candidate.Conversions[0].ConversionKind |
| 1521 | == ImplicitConversionSequence::BadConversion) { |
| 1522 | Candidate.Viable = false; |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | // To determine what the conversion from the result of calling the |
| 1527 | // conversion function to the type we're eventually trying to |
| 1528 | // convert to (ToType), we need to synthesize a call to the |
| 1529 | // conversion function and attempt copy initialization from it. This |
| 1530 | // makes sure that we get the right semantics with respect to |
| 1531 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 1532 | // call on the stack and we don't need its arguments to be |
| 1533 | // well-formed. |
| 1534 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
| 1535 | SourceLocation()); |
| 1536 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1537 | &ConversionRef, false); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1538 | CallExpr Call(&ConversionFn, 0, 0, |
| 1539 | Conversion->getConversionType().getNonReferenceType(), |
| 1540 | SourceLocation()); |
| 1541 | ImplicitConversionSequence ICS = TryCopyInitialization(&Call, ToType, true); |
| 1542 | switch (ICS.ConversionKind) { |
| 1543 | case ImplicitConversionSequence::StandardConversion: |
| 1544 | Candidate.FinalConversion = ICS.Standard; |
| 1545 | break; |
| 1546 | |
| 1547 | case ImplicitConversionSequence::BadConversion: |
| 1548 | Candidate.Viable = false; |
| 1549 | break; |
| 1550 | |
| 1551 | default: |
| 1552 | assert(false && |
| 1553 | "Can only end up with a standard conversion sequence or failure"); |
| 1554 | } |
| 1555 | } |
| 1556 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1557 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 1558 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 1559 | /// of the built-in candidate, respectively. Args and NumArgs are the |
| 1560 | /// arguments being passed to the candidate. |
| 1561 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
| 1562 | Expr **Args, unsigned NumArgs, |
| 1563 | OverloadCandidateSet& CandidateSet) { |
| 1564 | // Add this candidate |
| 1565 | CandidateSet.push_back(OverloadCandidate()); |
| 1566 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1567 | Candidate.Function = 0; |
| 1568 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 1569 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 1570 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 1571 | |
| 1572 | // Determine the implicit conversion sequences for each of the |
| 1573 | // arguments. |
| 1574 | Candidate.Viable = true; |
| 1575 | Candidate.Conversions.resize(NumArgs); |
| 1576 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 1577 | Candidate.Conversions[ArgIdx] |
| 1578 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], false); |
| 1579 | if (Candidate.Conversions[ArgIdx].ConversionKind |
| 1580 | == ImplicitConversionSequence::BadConversion) |
| 1581 | Candidate.Viable = false; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 1586 | /// candidate operator functions for built-in operators (C++ |
| 1587 | /// [over.built]). The types are separated into pointer types and |
| 1588 | /// enumeration types. |
| 1589 | class BuiltinCandidateTypeSet { |
| 1590 | /// TypeSet - A set of types. |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1591 | typedef llvm::SmallPtrSet<void*, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1592 | |
| 1593 | /// PointerTypes - The set of pointer types that will be used in the |
| 1594 | /// built-in candidates. |
| 1595 | TypeSet PointerTypes; |
| 1596 | |
| 1597 | /// EnumerationTypes - The set of enumeration types that will be |
| 1598 | /// used in the built-in candidates. |
| 1599 | TypeSet EnumerationTypes; |
| 1600 | |
| 1601 | /// Context - The AST context in which we will build the type sets. |
| 1602 | ASTContext &Context; |
| 1603 | |
| 1604 | bool AddWithMoreQualifiedTypeVariants(QualType Ty); |
| 1605 | |
| 1606 | public: |
| 1607 | /// iterator - Iterates through the types that are part of the set. |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1608 | class iterator { |
| 1609 | TypeSet::iterator Base; |
| 1610 | |
| 1611 | public: |
| 1612 | typedef QualType value_type; |
| 1613 | typedef QualType reference; |
| 1614 | typedef QualType pointer; |
| 1615 | typedef std::ptrdiff_t difference_type; |
| 1616 | typedef std::input_iterator_tag iterator_category; |
| 1617 | |
| 1618 | iterator(TypeSet::iterator B) : Base(B) { } |
| 1619 | |
| 1620 | iterator& operator++() { |
| 1621 | ++Base; |
| 1622 | return *this; |
| 1623 | } |
| 1624 | |
| 1625 | iterator operator++(int) { |
| 1626 | iterator tmp(*this); |
| 1627 | ++(*this); |
| 1628 | return tmp; |
| 1629 | } |
| 1630 | |
| 1631 | reference operator*() const { |
| 1632 | return QualType::getFromOpaquePtr(*Base); |
| 1633 | } |
| 1634 | |
| 1635 | pointer operator->() const { |
| 1636 | return **this; |
| 1637 | } |
| 1638 | |
| 1639 | friend bool operator==(iterator LHS, iterator RHS) { |
| 1640 | return LHS.Base == RHS.Base; |
| 1641 | } |
| 1642 | |
| 1643 | friend bool operator!=(iterator LHS, iterator RHS) { |
| 1644 | return LHS.Base != RHS.Base; |
| 1645 | } |
| 1646 | }; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1647 | |
| 1648 | BuiltinCandidateTypeSet(ASTContext &Context) : Context(Context) { } |
| 1649 | |
| 1650 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions = true); |
| 1651 | |
| 1652 | /// pointer_begin - First pointer type found; |
| 1653 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 1654 | |
| 1655 | /// pointer_end - Last pointer type found; |
| 1656 | iterator pointer_end() { return PointerTypes.end(); } |
| 1657 | |
| 1658 | /// enumeration_begin - First enumeration type found; |
| 1659 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 1660 | |
| 1661 | /// enumeration_end - Last enumeration type found; |
| 1662 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 1663 | }; |
| 1664 | |
| 1665 | /// AddWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
| 1666 | /// the set of pointer types along with any more-qualified variants of |
| 1667 | /// that type. For example, if @p Ty is "int const *", this routine |
| 1668 | /// will add "int const *", "int const volatile *", "int const |
| 1669 | /// restrict *", and "int const volatile restrict *" to the set of |
| 1670 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 1671 | /// false otherwise. |
| 1672 | bool BuiltinCandidateTypeSet::AddWithMoreQualifiedTypeVariants(QualType Ty) { |
| 1673 | // Insert this type. |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1674 | if (!PointerTypes.insert(Ty.getAsOpaquePtr())) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1675 | return false; |
| 1676 | |
| 1677 | if (const PointerType *PointerTy = Ty->getAsPointerType()) { |
| 1678 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 1679 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 1680 | |
| 1681 | // FIXME: Do we have to add CVR qualifiers at *all* levels to deal |
| 1682 | // with all pointer conversions that don't cast away constness? |
| 1683 | if (!PointeeTy.isConstQualified()) |
| 1684 | AddWithMoreQualifiedTypeVariants |
| 1685 | (Context.getPointerType(PointeeTy.withConst())); |
| 1686 | if (!PointeeTy.isVolatileQualified()) |
| 1687 | AddWithMoreQualifiedTypeVariants |
| 1688 | (Context.getPointerType(PointeeTy.withVolatile())); |
| 1689 | if (!PointeeTy.isRestrictQualified()) |
| 1690 | AddWithMoreQualifiedTypeVariants |
| 1691 | (Context.getPointerType(PointeeTy.withRestrict())); |
| 1692 | } |
| 1693 | |
| 1694 | return true; |
| 1695 | } |
| 1696 | |
| 1697 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 1698 | /// Ty can be implicit converted to the given set of @p Types. We're |
| 1699 | /// primarily interested in pointer types, enumeration types, |
| 1700 | void BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 1701 | bool AllowUserConversions) { |
| 1702 | // Only deal with canonical types. |
| 1703 | Ty = Context.getCanonicalType(Ty); |
| 1704 | |
| 1705 | // Look through reference types; they aren't part of the type of an |
| 1706 | // expression for the purposes of conversions. |
| 1707 | if (const ReferenceType *RefTy = Ty->getAsReferenceType()) |
| 1708 | Ty = RefTy->getPointeeType(); |
| 1709 | |
| 1710 | // We don't care about qualifiers on the type. |
| 1711 | Ty = Ty.getUnqualifiedType(); |
| 1712 | |
| 1713 | if (const PointerType *PointerTy = Ty->getAsPointerType()) { |
| 1714 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 1715 | |
| 1716 | // Insert our type, and its more-qualified variants, into the set |
| 1717 | // of types. |
| 1718 | if (!AddWithMoreQualifiedTypeVariants(Ty)) |
| 1719 | return; |
| 1720 | |
| 1721 | // Add 'cv void*' to our set of types. |
| 1722 | if (!Ty->isVoidType()) { |
| 1723 | QualType QualVoid |
| 1724 | = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 1725 | AddWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
| 1726 | } |
| 1727 | |
| 1728 | // If this is a pointer to a class type, add pointers to its bases |
| 1729 | // (with the same level of cv-qualification as the original |
| 1730 | // derived class, of course). |
| 1731 | if (const RecordType *PointeeRec = PointeeTy->getAsRecordType()) { |
| 1732 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 1733 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 1734 | Base != ClassDecl->bases_end(); ++Base) { |
| 1735 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
| 1736 | BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 1737 | |
| 1738 | // Add the pointer type, recursively, so that we get all of |
| 1739 | // the indirect base classes, too. |
| 1740 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false); |
| 1741 | } |
| 1742 | } |
| 1743 | } else if (Ty->isEnumeralType()) { |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1744 | EnumerationTypes.insert(Ty.getAsOpaquePtr()); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1745 | } else if (AllowUserConversions) { |
| 1746 | if (const RecordType *TyRec = Ty->getAsRecordType()) { |
| 1747 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 1748 | // FIXME: Visit conversion functions in the base classes, too. |
| 1749 | OverloadedFunctionDecl *Conversions |
| 1750 | = ClassDecl->getConversionFunctions(); |
| 1751 | for (OverloadedFunctionDecl::function_iterator Func |
| 1752 | = Conversions->function_begin(); |
| 1753 | Func != Conversions->function_end(); ++Func) { |
| 1754 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 1755 | AddTypesConvertedFrom(Conv->getConversionType(), false); |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | /// AddBuiltinCandidates - Add the appropriate built-in operator |
| 1762 | /// overloads to the candidate set (C++ [over.built]), based on the |
| 1763 | /// operator @p Op and the arguments given. For example, if the |
| 1764 | /// operator is a binary '+', this routine might add |
| 1765 | /// "int operator+(int, int)" |
| 1766 | /// to cover integer addition. |
| 1767 | void |
| 1768 | Sema::AddBuiltinBinaryOperatorCandidates(OverloadedOperatorKind Op, |
| 1769 | Expr **Args, |
| 1770 | OverloadCandidateSet& CandidateSet) { |
| 1771 | // The set of "promoted arithmetic types", which are the arithmetic |
| 1772 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 1773 | // that the first few of these types are the promoted integral |
| 1774 | // types; these types need to be first. |
| 1775 | // FIXME: What about complex? |
| 1776 | const unsigned FirstIntegralType = 0; |
| 1777 | const unsigned LastIntegralType = 13; |
| 1778 | const unsigned FirstPromotedIntegralType = 7, |
| 1779 | LastPromotedIntegralType = 13; |
| 1780 | const unsigned FirstPromotedArithmeticType = 7, |
| 1781 | LastPromotedArithmeticType = 16; |
| 1782 | const unsigned NumArithmeticTypes = 16; |
| 1783 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
| 1784 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 1785 | Context.SignedCharTy, Context.ShortTy, |
| 1786 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 1787 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 1788 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 1789 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 1790 | }; |
| 1791 | |
| 1792 | // Find all of the types that the arguments can convert to, but only |
| 1793 | // if the operator we're looking at has built-in operator candidates |
| 1794 | // that make use of these types. |
| 1795 | BuiltinCandidateTypeSet CandidateTypes(Context); |
| 1796 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 1797 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
| 1798 | Op == OO_Plus || Op == OO_Minus || Op == OO_Equal || |
| 1799 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
| 1800 | Op == OO_ArrowStar) { |
| 1801 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) |
| 1802 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType()); |
| 1803 | } |
| 1804 | |
| 1805 | bool isComparison = false; |
| 1806 | switch (Op) { |
| 1807 | case OO_None: |
| 1808 | case NUM_OVERLOADED_OPERATORS: |
| 1809 | assert(false && "Expected an overloaded operator"); |
| 1810 | break; |
| 1811 | |
| 1812 | case OO_New: |
| 1813 | case OO_Delete: |
| 1814 | case OO_Array_New: |
| 1815 | case OO_Array_Delete: |
| 1816 | case OO_Tilde: |
| 1817 | case OO_Exclaim: |
| 1818 | case OO_PlusPlus: |
| 1819 | case OO_MinusMinus: |
| 1820 | case OO_Arrow: |
| 1821 | case OO_Call: |
| 1822 | assert(false && "Expected a binary operator"); |
| 1823 | break; |
| 1824 | |
| 1825 | case OO_Comma: |
| 1826 | // C++ [over.match.oper]p3: |
| 1827 | // -- For the operator ',', the unary operator '&', or the |
| 1828 | // operator '->', the built-in candidates set is empty. |
| 1829 | // We don't check '&' or '->' here, since they are unary operators. |
| 1830 | break; |
| 1831 | |
| 1832 | case OO_Less: |
| 1833 | case OO_Greater: |
| 1834 | case OO_LessEqual: |
| 1835 | case OO_GreaterEqual: |
| 1836 | case OO_EqualEqual: |
| 1837 | case OO_ExclaimEqual: |
| 1838 | // C++ [over.built]p15: |
| 1839 | // |
| 1840 | // For every pointer or enumeration type T, there exist |
| 1841 | // candidate operator functions of the form |
| 1842 | // |
| 1843 | // bool operator<(T, T); |
| 1844 | // bool operator>(T, T); |
| 1845 | // bool operator<=(T, T); |
| 1846 | // bool operator>=(T, T); |
| 1847 | // bool operator==(T, T); |
| 1848 | // bool operator!=(T, T); |
| 1849 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 1850 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 1851 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 1852 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 1853 | } |
| 1854 | for (BuiltinCandidateTypeSet::iterator Enum |
| 1855 | = CandidateTypes.enumeration_begin(); |
| 1856 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 1857 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 1858 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 1859 | } |
| 1860 | |
| 1861 | // Fall through. |
| 1862 | isComparison = true; |
| 1863 | |
| 1864 | case OO_Plus: |
| 1865 | case OO_Minus: |
| 1866 | if (!isComparison) { |
| 1867 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 1868 | |
| 1869 | // C++ [over.built]p13: |
| 1870 | // |
| 1871 | // For every cv-qualified or cv-unqualified object type T |
| 1872 | // there exist candidate operator functions of the form |
| 1873 | // |
| 1874 | // T* operator+(T*, ptrdiff_t); |
| 1875 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 1876 | // T* operator-(T*, ptrdiff_t); |
| 1877 | // T* operator+(ptrdiff_t, T*); |
| 1878 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 1879 | // |
| 1880 | // C++ [over.built]p14: |
| 1881 | // |
| 1882 | // For every T, where T is a pointer to object type, there |
| 1883 | // exist candidate operator functions of the form |
| 1884 | // |
| 1885 | // ptrdiff_t operator-(T, T); |
| 1886 | for (BuiltinCandidateTypeSet::iterator Ptr |
| 1887 | = CandidateTypes.pointer_begin(); |
| 1888 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 1889 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 1890 | |
| 1891 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 1892 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 1893 | |
| 1894 | if (Op == OO_Plus) { |
| 1895 | // T* operator+(ptrdiff_t, T*); |
| 1896 | ParamTypes[0] = ParamTypes[1]; |
| 1897 | ParamTypes[1] = *Ptr; |
| 1898 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 1899 | } else { |
| 1900 | // ptrdiff_t operator-(T, T); |
| 1901 | ParamTypes[1] = *Ptr; |
| 1902 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 1903 | Args, 2, CandidateSet); |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | // Fall through |
| 1908 | |
| 1909 | case OO_Star: |
| 1910 | case OO_Slash: |
| 1911 | // C++ [over.built]p12: |
| 1912 | // |
| 1913 | // For every pair of promoted arithmetic types L and R, there |
| 1914 | // exist candidate operator functions of the form |
| 1915 | // |
| 1916 | // LR operator*(L, R); |
| 1917 | // LR operator/(L, R); |
| 1918 | // LR operator+(L, R); |
| 1919 | // LR operator-(L, R); |
| 1920 | // bool operator<(L, R); |
| 1921 | // bool operator>(L, R); |
| 1922 | // bool operator<=(L, R); |
| 1923 | // bool operator>=(L, R); |
| 1924 | // bool operator==(L, R); |
| 1925 | // bool operator!=(L, R); |
| 1926 | // |
| 1927 | // where LR is the result of the usual arithmetic conversions |
| 1928 | // between types L and R. |
| 1929 | for (unsigned Left = FirstPromotedArithmeticType; |
| 1930 | Left < LastPromotedArithmeticType; ++Left) { |
| 1931 | for (unsigned Right = FirstPromotedArithmeticType; |
| 1932 | Right < LastPromotedArithmeticType; ++Right) { |
| 1933 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 1934 | QualType Result |
| 1935 | = isComparison? Context.BoolTy |
| 1936 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 1937 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 1938 | } |
| 1939 | } |
| 1940 | break; |
| 1941 | |
| 1942 | case OO_Percent: |
| 1943 | case OO_Amp: |
| 1944 | case OO_Caret: |
| 1945 | case OO_Pipe: |
| 1946 | case OO_LessLess: |
| 1947 | case OO_GreaterGreater: |
| 1948 | // C++ [over.built]p17: |
| 1949 | // |
| 1950 | // For every pair of promoted integral types L and R, there |
| 1951 | // exist candidate operator functions of the form |
| 1952 | // |
| 1953 | // LR operator%(L, R); |
| 1954 | // LR operator&(L, R); |
| 1955 | // LR operator^(L, R); |
| 1956 | // LR operator|(L, R); |
| 1957 | // L operator<<(L, R); |
| 1958 | // L operator>>(L, R); |
| 1959 | // |
| 1960 | // where LR is the result of the usual arithmetic conversions |
| 1961 | // between types L and R. |
| 1962 | for (unsigned Left = FirstPromotedIntegralType; |
| 1963 | Left < LastPromotedIntegralType; ++Left) { |
| 1964 | for (unsigned Right = FirstPromotedIntegralType; |
| 1965 | Right < LastPromotedIntegralType; ++Right) { |
| 1966 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 1967 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 1968 | ? LandR[0] |
| 1969 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 1970 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 1971 | } |
| 1972 | } |
| 1973 | break; |
| 1974 | |
| 1975 | case OO_Equal: |
| 1976 | // C++ [over.built]p20: |
| 1977 | // |
| 1978 | // For every pair (T, VQ), where T is an enumeration or |
| 1979 | // (FIXME:) pointer to member type and VQ is either volatile or |
| 1980 | // empty, there exist candidate operator functions of the form |
| 1981 | // |
| 1982 | // VQ T& operator=(VQ T&, T); |
| 1983 | for (BuiltinCandidateTypeSet::iterator Enum |
| 1984 | = CandidateTypes.enumeration_begin(); |
| 1985 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 1986 | QualType ParamTypes[2]; |
| 1987 | |
| 1988 | // T& operator=(T&, T) |
| 1989 | ParamTypes[0] = Context.getReferenceType(*Enum); |
| 1990 | ParamTypes[1] = *Enum; |
| 1991 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 1992 | |
| 1993 | // volatile T& operator=(volatile T&, T) |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1994 | ParamTypes[0] = Context.getReferenceType((*Enum).withVolatile()); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1995 | ParamTypes[1] = *Enum; |
| 1996 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 1997 | } |
| 1998 | // Fall through. |
| 1999 | |
| 2000 | case OO_PlusEqual: |
| 2001 | case OO_MinusEqual: |
| 2002 | // C++ [over.built]p19: |
| 2003 | // |
| 2004 | // For every pair (T, VQ), where T is any type and VQ is either |
| 2005 | // volatile or empty, there exist candidate operator functions |
| 2006 | // of the form |
| 2007 | // |
| 2008 | // T*VQ& operator=(T*VQ&, T*); |
| 2009 | // |
| 2010 | // C++ [over.built]p21: |
| 2011 | // |
| 2012 | // For every pair (T, VQ), where T is a cv-qualified or |
| 2013 | // cv-unqualified object type and VQ is either volatile or |
| 2014 | // empty, there exist candidate operator functions of the form |
| 2015 | // |
| 2016 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 2017 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 2018 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2019 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2020 | QualType ParamTypes[2]; |
| 2021 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 2022 | |
| 2023 | // non-volatile version |
| 2024 | ParamTypes[0] = Context.getReferenceType(*Ptr); |
| 2025 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2026 | |
| 2027 | // volatile version |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 2028 | ParamTypes[0] = Context.getReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2029 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2030 | } |
| 2031 | // Fall through. |
| 2032 | |
| 2033 | case OO_StarEqual: |
| 2034 | case OO_SlashEqual: |
| 2035 | // C++ [over.built]p18: |
| 2036 | // |
| 2037 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 2038 | // VQ is either volatile or empty, and R is a promoted |
| 2039 | // arithmetic type, there exist candidate operator functions of |
| 2040 | // the form |
| 2041 | // |
| 2042 | // VQ L& operator=(VQ L&, R); |
| 2043 | // VQ L& operator*=(VQ L&, R); |
| 2044 | // VQ L& operator/=(VQ L&, R); |
| 2045 | // VQ L& operator+=(VQ L&, R); |
| 2046 | // VQ L& operator-=(VQ L&, R); |
| 2047 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 2048 | for (unsigned Right = FirstPromotedArithmeticType; |
| 2049 | Right < LastPromotedArithmeticType; ++Right) { |
| 2050 | QualType ParamTypes[2]; |
| 2051 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 2052 | |
| 2053 | // Add this built-in operator as a candidate (VQ is empty). |
| 2054 | ParamTypes[0] = Context.getReferenceType(ArithmeticTypes[Left]); |
| 2055 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2056 | |
| 2057 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 2058 | ParamTypes[0] = ArithmeticTypes[Left].withVolatile(); |
| 2059 | ParamTypes[0] = Context.getReferenceType(ParamTypes[0]); |
| 2060 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2061 | } |
| 2062 | } |
| 2063 | break; |
| 2064 | |
| 2065 | case OO_PercentEqual: |
| 2066 | case OO_LessLessEqual: |
| 2067 | case OO_GreaterGreaterEqual: |
| 2068 | case OO_AmpEqual: |
| 2069 | case OO_CaretEqual: |
| 2070 | case OO_PipeEqual: |
| 2071 | // C++ [over.built]p22: |
| 2072 | // |
| 2073 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 2074 | // is either volatile or empty, and R is a promoted integral |
| 2075 | // type, there exist candidate operator functions of the form |
| 2076 | // |
| 2077 | // VQ L& operator%=(VQ L&, R); |
| 2078 | // VQ L& operator<<=(VQ L&, R); |
| 2079 | // VQ L& operator>>=(VQ L&, R); |
| 2080 | // VQ L& operator&=(VQ L&, R); |
| 2081 | // VQ L& operator^=(VQ L&, R); |
| 2082 | // VQ L& operator|=(VQ L&, R); |
| 2083 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 2084 | for (unsigned Right = FirstPromotedIntegralType; |
| 2085 | Right < LastPromotedIntegralType; ++Right) { |
| 2086 | QualType ParamTypes[2]; |
| 2087 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 2088 | |
| 2089 | // Add this built-in operator as a candidate (VQ is empty). |
| 2090 | // FIXME: We should be caching these declarations somewhere, |
| 2091 | // rather than re-building them every time. |
| 2092 | ParamTypes[0] = Context.getReferenceType(ArithmeticTypes[Left]); |
| 2093 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2094 | |
| 2095 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 2096 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 2097 | ParamTypes[0].addVolatile(); |
| 2098 | ParamTypes[0] = Context.getReferenceType(ParamTypes[0]); |
| 2099 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2100 | } |
| 2101 | } |
| 2102 | break; |
| 2103 | |
| 2104 | case OO_AmpAmp: |
| 2105 | case OO_PipePipe: { |
| 2106 | // C++ [over.operator]p23: |
| 2107 | // |
| 2108 | // There also exist candidate operator functions of the form |
| 2109 | // |
| 2110 | // bool operator!(bool); [In Unary version] |
| 2111 | // bool operator&&(bool, bool); |
| 2112 | // bool operator||(bool, bool); |
| 2113 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
| 2114 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 2115 | break; |
| 2116 | } |
| 2117 | |
| 2118 | case OO_Subscript: |
| 2119 | // C++ [over.built]p13: |
| 2120 | // |
| 2121 | // For every cv-qualified or cv-unqualified object type T there |
| 2122 | // exist candidate operator functions of the form |
| 2123 | // |
| 2124 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 2125 | // T& operator[](T*, ptrdiff_t); |
| 2126 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 2127 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 2128 | // T& operator[](ptrdiff_t, T*); |
| 2129 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2130 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2131 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 2132 | QualType PointeeType = (*Ptr)->getAsPointerType()->getPointeeType(); |
| 2133 | QualType ResultTy = Context.getReferenceType(PointeeType); |
| 2134 | |
| 2135 | // T& operator[](T*, ptrdiff_t) |
| 2136 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 2137 | |
| 2138 | // T& operator[](ptrdiff_t, T*); |
| 2139 | ParamTypes[0] = ParamTypes[1]; |
| 2140 | ParamTypes[1] = *Ptr; |
| 2141 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 2142 | } |
| 2143 | break; |
| 2144 | |
| 2145 | case OO_ArrowStar: |
| 2146 | // FIXME: No support for pointer-to-members yet. |
| 2147 | break; |
| 2148 | } |
| 2149 | } |
| 2150 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2151 | /// AddOverloadCandidates - Add all of the function overloads in Ovl |
| 2152 | /// to the candidate set. |
| 2153 | void |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2154 | Sema::AddOverloadCandidates(const OverloadedFunctionDecl *Ovl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2155 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2156 | OverloadCandidateSet& CandidateSet, |
| 2157 | bool SuppressUserConversions) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2158 | { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2159 | for (OverloadedFunctionDecl::function_const_iterator Func |
| 2160 | = Ovl->function_begin(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2161 | Func != Ovl->function_end(); ++Func) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2162 | AddOverloadCandidate(*Func, Args, NumArgs, CandidateSet, |
| 2163 | SuppressUserConversions); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 2167 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
| 2168 | bool |
| 2169 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
| 2170 | const OverloadCandidate& Cand2) |
| 2171 | { |
| 2172 | // Define viable functions to be better candidates than non-viable |
| 2173 | // functions. |
| 2174 | if (!Cand2.Viable) |
| 2175 | return Cand1.Viable; |
| 2176 | else if (!Cand1.Viable) |
| 2177 | return false; |
| 2178 | |
| 2179 | // FIXME: Deal with the implicit object parameter for static member |
| 2180 | // functions. (C++ 13.3.3p1). |
| 2181 | |
| 2182 | // (C++ 13.3.3p1): a viable function F1 is defined to be a better |
| 2183 | // function than another viable function F2 if for all arguments i, |
| 2184 | // ICSi(F1) is not a worse conversion sequence than ICSi(F2), and |
| 2185 | // then... |
| 2186 | unsigned NumArgs = Cand1.Conversions.size(); |
| 2187 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 2188 | bool HasBetterConversion = false; |
| 2189 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2190 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 2191 | Cand2.Conversions[ArgIdx])) { |
| 2192 | case ImplicitConversionSequence::Better: |
| 2193 | // Cand1 has a better conversion sequence. |
| 2194 | HasBetterConversion = true; |
| 2195 | break; |
| 2196 | |
| 2197 | case ImplicitConversionSequence::Worse: |
| 2198 | // Cand1 can't be better than Cand2. |
| 2199 | return false; |
| 2200 | |
| 2201 | case ImplicitConversionSequence::Indistinguishable: |
| 2202 | // Do nothing. |
| 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | if (HasBetterConversion) |
| 2208 | return true; |
| 2209 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2210 | // FIXME: Several other bullets in (C++ 13.3.3p1) need to be |
| 2211 | // implemented, but they require template support. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2212 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2213 | // C++ [over.match.best]p1b4: |
| 2214 | // |
| 2215 | // -- the context is an initialization by user-defined conversion |
| 2216 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 2217 | // from the return type of F1 to the destination type (i.e., |
| 2218 | // the type of the entity being initialized) is a better |
| 2219 | // conversion sequence than the standard conversion sequence |
| 2220 | // from the return type of F2 to the destination type. |
| 2221 | if (isa<CXXConversionDecl>(Cand1.Function) && |
| 2222 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 2223 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 2224 | Cand2.FinalConversion)) { |
| 2225 | case ImplicitConversionSequence::Better: |
| 2226 | // Cand1 has a better conversion sequence. |
| 2227 | return true; |
| 2228 | |
| 2229 | case ImplicitConversionSequence::Worse: |
| 2230 | // Cand1 can't be better than Cand2. |
| 2231 | return false; |
| 2232 | |
| 2233 | case ImplicitConversionSequence::Indistinguishable: |
| 2234 | // Do nothing |
| 2235 | break; |
| 2236 | } |
| 2237 | } |
| 2238 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2239 | return false; |
| 2240 | } |
| 2241 | |
| 2242 | /// BestViableFunction - Computes the best viable function (C++ 13.3.3) |
| 2243 | /// within an overload candidate set. If overloading is successful, |
| 2244 | /// the result will be OR_Success and Best will be set to point to the |
| 2245 | /// best viable function within the candidate set. Otherwise, one of |
| 2246 | /// several kinds of errors will be returned; see |
| 2247 | /// Sema::OverloadingResult. |
| 2248 | Sema::OverloadingResult |
| 2249 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 2250 | OverloadCandidateSet::iterator& Best) |
| 2251 | { |
| 2252 | // Find the best viable function. |
| 2253 | Best = CandidateSet.end(); |
| 2254 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2255 | Cand != CandidateSet.end(); ++Cand) { |
| 2256 | if (Cand->Viable) { |
| 2257 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 2258 | Best = Cand; |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | // If we didn't find any viable functions, abort. |
| 2263 | if (Best == CandidateSet.end()) |
| 2264 | return OR_No_Viable_Function; |
| 2265 | |
| 2266 | // Make sure that this function is better than every other viable |
| 2267 | // function. If not, we have an ambiguity. |
| 2268 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2269 | Cand != CandidateSet.end(); ++Cand) { |
| 2270 | if (Cand->Viable && |
| 2271 | Cand != Best && |
| 2272 | !isBetterOverloadCandidate(*Best, *Cand)) |
| 2273 | return OR_Ambiguous; |
| 2274 | } |
| 2275 | |
| 2276 | // Best is the best viable function. |
| 2277 | return OR_Success; |
| 2278 | } |
| 2279 | |
| 2280 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 2281 | /// diagnostic messages containing the candidates in the candidate |
| 2282 | /// set. If OnlyViable is true, only viable candidates will be printed. |
| 2283 | void |
| 2284 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
| 2285 | bool OnlyViable) |
| 2286 | { |
| 2287 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 2288 | LastCand = CandidateSet.end(); |
| 2289 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2290 | if (Cand->Viable || !OnlyViable) { |
| 2291 | if (Cand->Function) { |
| 2292 | // Normal function |
| 2293 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
| 2294 | } else { |
| 2295 | // FIXME: We need to get the identifier in here |
| 2296 | // FIXME: Do we want the error message to point at the |
| 2297 | // operator? (built-ins won't have a location) |
| 2298 | QualType FnType |
| 2299 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 2300 | Cand->BuiltinTypes.ParamTypes, |
| 2301 | Cand->Conversions.size(), |
| 2302 | false, 0); |
| 2303 | |
| 2304 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate, |
| 2305 | FnType.getAsString()); |
| 2306 | } |
| 2307 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2308 | } |
| 2309 | } |
| 2310 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 2311 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 2312 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 2313 | /// expression with overloaded function type and @p ToType is the type |
| 2314 | /// we're trying to resolve to. For example: |
| 2315 | /// |
| 2316 | /// @code |
| 2317 | /// int f(double); |
| 2318 | /// int f(int); |
| 2319 | /// |
| 2320 | /// int (*pfd)(double) = f; // selects f(double) |
| 2321 | /// @endcode |
| 2322 | /// |
| 2323 | /// This routine returns the resulting FunctionDecl if it could be |
| 2324 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 2325 | /// routine will emit diagnostics if there is an error. |
| 2326 | FunctionDecl * |
| 2327 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
| 2328 | bool Complain) { |
| 2329 | QualType FunctionType = ToType; |
| 2330 | if (const PointerLikeType *ToTypePtr = ToType->getAsPointerLikeType()) |
| 2331 | FunctionType = ToTypePtr->getPointeeType(); |
| 2332 | |
| 2333 | // We only look at pointers or references to functions. |
| 2334 | if (!FunctionType->isFunctionType()) |
| 2335 | return 0; |
| 2336 | |
| 2337 | // Find the actual overloaded function declaration. |
| 2338 | OverloadedFunctionDecl *Ovl = 0; |
| 2339 | |
| 2340 | // C++ [over.over]p1: |
| 2341 | // [...] [Note: any redundant set of parentheses surrounding the |
| 2342 | // overloaded function name is ignored (5.1). ] |
| 2343 | Expr *OvlExpr = From->IgnoreParens(); |
| 2344 | |
| 2345 | // C++ [over.over]p1: |
| 2346 | // [...] The overloaded function name can be preceded by the & |
| 2347 | // operator. |
| 2348 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 2349 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 2350 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 2351 | } |
| 2352 | |
| 2353 | // Try to dig out the overloaded function. |
| 2354 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) |
| 2355 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
| 2356 | |
| 2357 | // If there's no overloaded function declaration, we're done. |
| 2358 | if (!Ovl) |
| 2359 | return 0; |
| 2360 | |
| 2361 | // Look through all of the overloaded functions, searching for one |
| 2362 | // whose type matches exactly. |
| 2363 | // FIXME: When templates or using declarations come along, we'll actually |
| 2364 | // have to deal with duplicates, partial ordering, etc. For now, we |
| 2365 | // can just do a simple search. |
| 2366 | FunctionType = Context.getCanonicalType(FunctionType.getUnqualifiedType()); |
| 2367 | for (OverloadedFunctionDecl::function_iterator Fun = Ovl->function_begin(); |
| 2368 | Fun != Ovl->function_end(); ++Fun) { |
| 2369 | // C++ [over.over]p3: |
| 2370 | // Non-member functions and static member functions match |
| 2371 | // targets of type “pointer-to-function”or |
| 2372 | // “reference-to-function.” |
| 2373 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) |
| 2374 | if (!Method->isStatic()) |
| 2375 | continue; |
| 2376 | |
| 2377 | if (FunctionType == Context.getCanonicalType((*Fun)->getType())) |
| 2378 | return *Fun; |
| 2379 | } |
| 2380 | |
| 2381 | return 0; |
| 2382 | } |
| 2383 | |
| 2384 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 2385 | /// a C++ overloaded function (possibly with some parentheses and |
| 2386 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 2387 | /// to the function declaration Fn, so patch up the expression E to |
| 2388 | /// refer (possibly indirectly) to Fn. |
| 2389 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 2390 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 2391 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 2392 | E->setType(PE->getSubExpr()->getType()); |
| 2393 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
| 2394 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
| 2395 | "Can only take the address of an overloaded function"); |
| 2396 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 2397 | E->setType(Context.getPointerType(E->getType())); |
| 2398 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
| 2399 | assert(isa<OverloadedFunctionDecl>(DR->getDecl()) && |
| 2400 | "Expected overloaded function"); |
| 2401 | DR->setDecl(Fn); |
| 2402 | E->setType(Fn->getType()); |
| 2403 | } else { |
| 2404 | assert(false && "Invalid reference to overloaded function"); |
| 2405 | } |
| 2406 | } |
| 2407 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2408 | } // end namespace clang |