Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Douglas Gregor | 3d4492e | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include <algorithm> |
| 25 | |
| 26 | namespace clang { |
| 27 | |
| 28 | /// GetConversionCategory - Retrieve the implicit conversion |
| 29 | /// category corresponding to the given implicit conversion kind. |
| 30 | ImplicitConversionCategory |
| 31 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 32 | static const ImplicitConversionCategory |
| 33 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 34 | ICC_Identity, |
| 35 | ICC_Lvalue_Transformation, |
| 36 | ICC_Lvalue_Transformation, |
| 37 | ICC_Lvalue_Transformation, |
| 38 | ICC_Qualification_Adjustment, |
| 39 | ICC_Promotion, |
| 40 | ICC_Promotion, |
| 41 | ICC_Conversion, |
| 42 | ICC_Conversion, |
| 43 | ICC_Conversion, |
| 44 | ICC_Conversion, |
| 45 | ICC_Conversion, |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 46 | ICC_Conversion, |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 47 | ICC_Conversion |
| 48 | }; |
| 49 | return Category[(int)Kind]; |
| 50 | } |
| 51 | |
| 52 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 53 | /// corresponding to the given implicit conversion kind. |
| 54 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 55 | static const ImplicitConversionRank |
| 56 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 57 | ICR_Exact_Match, |
| 58 | ICR_Exact_Match, |
| 59 | ICR_Exact_Match, |
| 60 | ICR_Exact_Match, |
| 61 | ICR_Exact_Match, |
| 62 | ICR_Promotion, |
| 63 | ICR_Promotion, |
| 64 | ICR_Conversion, |
| 65 | ICR_Conversion, |
| 66 | ICR_Conversion, |
| 67 | ICR_Conversion, |
| 68 | ICR_Conversion, |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 69 | ICR_Conversion, |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 70 | ICR_Conversion |
| 71 | }; |
| 72 | return Rank[(int)Kind]; |
| 73 | } |
| 74 | |
| 75 | /// GetImplicitConversionName - Return the name of this kind of |
| 76 | /// implicit conversion. |
| 77 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 78 | static const char* Name[(int)ICK_Num_Conversion_Kinds] = { |
| 79 | "No conversion", |
| 80 | "Lvalue-to-rvalue", |
| 81 | "Array-to-pointer", |
| 82 | "Function-to-pointer", |
| 83 | "Qualification", |
| 84 | "Integral promotion", |
| 85 | "Floating point promotion", |
| 86 | "Integral conversion", |
| 87 | "Floating conversion", |
| 88 | "Floating-integral conversion", |
| 89 | "Pointer conversion", |
| 90 | "Pointer-to-member conversion", |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 91 | "Boolean conversion", |
| 92 | "Derived-to-base conversion" |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 93 | }; |
| 94 | return Name[Kind]; |
| 95 | } |
| 96 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 97 | /// StandardConversionSequence - Set the standard conversion |
| 98 | /// sequence to the identity conversion. |
| 99 | void StandardConversionSequence::setAsIdentityConversion() { |
| 100 | First = ICK_Identity; |
| 101 | Second = ICK_Identity; |
| 102 | Third = ICK_Identity; |
| 103 | Deprecated = false; |
| 104 | ReferenceBinding = false; |
| 105 | DirectBinding = false; |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 106 | CopyConstructor = 0; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 109 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 110 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 111 | /// implicit conversions. |
| 112 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 113 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 114 | if (GetConversionRank(First) > Rank) |
| 115 | Rank = GetConversionRank(First); |
| 116 | if (GetConversionRank(Second) > Rank) |
| 117 | Rank = GetConversionRank(Second); |
| 118 | if (GetConversionRank(Third) > Rank) |
| 119 | Rank = GetConversionRank(Third); |
| 120 | return Rank; |
| 121 | } |
| 122 | |
| 123 | /// isPointerConversionToBool - Determines whether this conversion is |
| 124 | /// a conversion of a pointer or pointer-to-member to bool. This is |
| 125 | /// used as part of the ranking of standard conversion sequences |
| 126 | /// (C++ 13.3.3.2p4). |
| 127 | bool StandardConversionSequence::isPointerConversionToBool() const |
| 128 | { |
| 129 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 130 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 131 | |
| 132 | // Note that FromType has not necessarily been transformed by the |
| 133 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 134 | // check for their presence as well as checking whether FromType is |
| 135 | // a pointer. |
| 136 | if (ToType->isBooleanType() && |
| 137 | (FromType->isPointerType() || |
| 138 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 139 | return true; |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 144 | /// isPointerConversionToVoidPointer - Determines whether this |
| 145 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 146 | /// used as part of the ranking of standard conversion sequences (C++ |
| 147 | /// 13.3.3.2p4). |
| 148 | bool |
| 149 | StandardConversionSequence:: |
| 150 | isPointerConversionToVoidPointer(ASTContext& Context) const |
| 151 | { |
| 152 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 153 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 154 | |
| 155 | // Note that FromType has not necessarily been transformed by the |
| 156 | // array-to-pointer implicit conversion, so check for its presence |
| 157 | // and redo the conversion to get a pointer. |
| 158 | if (First == ICK_Array_To_Pointer) |
| 159 | FromType = Context.getArrayDecayedType(FromType); |
| 160 | |
| 161 | if (Second == ICK_Pointer_Conversion) |
| 162 | if (const PointerType* ToPtrType = ToType->getAsPointerType()) |
| 163 | return ToPtrType->getPointeeType()->isVoidType(); |
| 164 | |
| 165 | return false; |
| 166 | } |
| 167 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 168 | /// DebugPrint - Print this standard conversion sequence to standard |
| 169 | /// error. Useful for debugging overloading issues. |
| 170 | void StandardConversionSequence::DebugPrint() const { |
| 171 | bool PrintedSomething = false; |
| 172 | if (First != ICK_Identity) { |
| 173 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 174 | PrintedSomething = true; |
| 175 | } |
| 176 | |
| 177 | if (Second != ICK_Identity) { |
| 178 | if (PrintedSomething) { |
| 179 | fprintf(stderr, " -> "); |
| 180 | } |
| 181 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 182 | |
| 183 | if (CopyConstructor) { |
| 184 | fprintf(stderr, " (by copy constructor)"); |
| 185 | } else if (DirectBinding) { |
| 186 | fprintf(stderr, " (direct reference binding)"); |
| 187 | } else if (ReferenceBinding) { |
| 188 | fprintf(stderr, " (reference binding)"); |
| 189 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 190 | PrintedSomething = true; |
| 191 | } |
| 192 | |
| 193 | if (Third != ICK_Identity) { |
| 194 | if (PrintedSomething) { |
| 195 | fprintf(stderr, " -> "); |
| 196 | } |
| 197 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 198 | PrintedSomething = true; |
| 199 | } |
| 200 | |
| 201 | if (!PrintedSomething) { |
| 202 | fprintf(stderr, "No conversions required"); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 207 | /// error. Useful for debugging overloading issues. |
| 208 | void UserDefinedConversionSequence::DebugPrint() const { |
| 209 | if (Before.First || Before.Second || Before.Third) { |
| 210 | Before.DebugPrint(); |
| 211 | fprintf(stderr, " -> "); |
| 212 | } |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 213 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 214 | if (After.First || After.Second || After.Third) { |
| 215 | fprintf(stderr, " -> "); |
| 216 | After.DebugPrint(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 221 | /// error. Useful for debugging overloading issues. |
| 222 | void ImplicitConversionSequence::DebugPrint() const { |
| 223 | switch (ConversionKind) { |
| 224 | case StandardConversion: |
| 225 | fprintf(stderr, "Standard conversion: "); |
| 226 | Standard.DebugPrint(); |
| 227 | break; |
| 228 | case UserDefinedConversion: |
| 229 | fprintf(stderr, "User-defined conversion: "); |
| 230 | UserDefined.DebugPrint(); |
| 231 | break; |
| 232 | case EllipsisConversion: |
| 233 | fprintf(stderr, "Ellipsis conversion"); |
| 234 | break; |
| 235 | case BadConversion: |
| 236 | fprintf(stderr, "Bad conversion"); |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | fprintf(stderr, "\n"); |
| 241 | } |
| 242 | |
| 243 | // IsOverload - Determine whether the given New declaration is an |
| 244 | // overload of the Old declaration. This routine returns false if New |
| 245 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 246 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 247 | // function (or overload set). When it does return false and Old is an |
| 248 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
| 249 | // FunctionDecl that New cannot be overloaded with. |
| 250 | // |
| 251 | // Example: Given the following input: |
| 252 | // |
| 253 | // void f(int, float); // #1 |
| 254 | // void f(int, int); // #2 |
| 255 | // int f(int, int); // #3 |
| 256 | // |
| 257 | // When we process #1, there is no previous declaration of "f", |
| 258 | // so IsOverload will not be used. |
| 259 | // |
| 260 | // When we process #2, Old is a FunctionDecl for #1. By comparing the |
| 261 | // parameter types, we see that #1 and #2 are overloaded (since they |
| 262 | // have different signatures), so this routine returns false; |
| 263 | // MatchedDecl is unchanged. |
| 264 | // |
| 265 | // When we process #3, Old is an OverloadedFunctionDecl containing #1 |
| 266 | // and #2. We compare the signatures of #3 to #1 (they're overloaded, |
| 267 | // so we do nothing) and then #3 to #2. Since the signatures of #3 and |
| 268 | // #2 are identical (return types of functions are not part of the |
| 269 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 270 | // point to the FunctionDecl for #2. |
| 271 | bool |
| 272 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 273 | OverloadedFunctionDecl::function_iterator& MatchedDecl) |
| 274 | { |
| 275 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 276 | // Is this new function an overload of every function in the |
| 277 | // overload set? |
| 278 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 279 | FuncEnd = Ovl->function_end(); |
| 280 | for (; Func != FuncEnd; ++Func) { |
| 281 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 282 | MatchedDecl = Func; |
| 283 | return false; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // This function overloads every function in the overload set. |
| 288 | return true; |
| 289 | } else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
| 290 | // Is the function New an overload of the function Old? |
| 291 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 292 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 293 | |
| 294 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 295 | // determine whether they are overloads. If we find any mismatch |
| 296 | // in the signature, they are overloads. |
| 297 | |
| 298 | // If either of these functions is a K&R-style function (no |
| 299 | // prototype), then we consider them to have matching signatures. |
| 300 | if (isa<FunctionTypeNoProto>(OldQType.getTypePtr()) || |
| 301 | isa<FunctionTypeNoProto>(NewQType.getTypePtr())) |
| 302 | return false; |
| 303 | |
| 304 | FunctionTypeProto* OldType = cast<FunctionTypeProto>(OldQType.getTypePtr()); |
| 305 | FunctionTypeProto* NewType = cast<FunctionTypeProto>(NewQType.getTypePtr()); |
| 306 | |
| 307 | // The signature of a function includes the types of its |
| 308 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 309 | // of the ellipsis; see C++ DR 357). |
| 310 | if (OldQType != NewQType && |
| 311 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 312 | OldType->isVariadic() != NewType->isVariadic() || |
| 313 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 314 | NewType->arg_type_begin()))) |
| 315 | return true; |
| 316 | |
| 317 | // If the function is a class member, its signature includes the |
| 318 | // cv-qualifiers (if any) on the function itself. |
| 319 | // |
| 320 | // As part of this, also check whether one of the member functions |
| 321 | // is static, in which case they are not overloads (C++ |
| 322 | // 13.1p2). While not part of the definition of the signature, |
| 323 | // this check is important to determine whether these functions |
| 324 | // can be overloaded. |
| 325 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 326 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 327 | if (OldMethod && NewMethod && |
| 328 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | a7b56a3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 329 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 330 | return true; |
| 331 | |
| 332 | // The signatures match; this is not an overload. |
| 333 | return false; |
| 334 | } else { |
| 335 | // (C++ 13p1): |
| 336 | // Only function declarations can be overloaded; object and type |
| 337 | // declarations cannot be overloaded. |
| 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 342 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 343 | /// from the given expression (Expr) to the given type (ToType). This |
| 344 | /// function returns an implicit conversion sequence that can be used |
| 345 | /// to perform the initialization. Given |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 346 | /// |
| 347 | /// void f(float f); |
| 348 | /// void g(int i) { f(i); } |
| 349 | /// |
| 350 | /// this routine would produce an implicit conversion sequence to |
| 351 | /// describe the initialization of f from i, which will be a standard |
| 352 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 353 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 354 | // |
| 355 | /// Note that this routine only determines how the conversion can be |
| 356 | /// performed; it does not actually perform the conversion. As such, |
| 357 | /// it will not produce any diagnostics if no conversion is available, |
| 358 | /// but will instead return an implicit conversion sequence of kind |
| 359 | /// "BadConversion". |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 360 | /// |
| 361 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 362 | /// not permitted. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 363 | ImplicitConversionSequence |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 364 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 365 | bool SuppressUserConversions) |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 366 | { |
| 367 | ImplicitConversionSequence ICS; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 368 | if (IsStandardConversion(From, ToType, ICS.Standard)) |
| 369 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 370 | else if (!SuppressUserConversions && |
| 371 | IsUserDefinedConversion(From, ToType, ICS.UserDefined)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 372 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 373 | // C++ [over.ics.user]p4: |
| 374 | // A conversion of an expression of class type to the same class |
| 375 | // type is given Exact Match rank, and a conversion of an |
| 376 | // expression of class type to a base class of that type is |
| 377 | // given Conversion rank, in spite of the fact that a copy |
| 378 | // constructor (i.e., a user-defined conversion function) is |
| 379 | // called for those cases. |
| 380 | if (CXXConstructorDecl *Constructor |
| 381 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
| 382 | if (Constructor->isCopyConstructor(Context)) { |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 383 | // Turn this into a "standard" conversion sequence, so that it |
| 384 | // gets ranked with standard conversion sequences. |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 385 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 386 | ICS.Standard.setAsIdentityConversion(); |
| 387 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 388 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 389 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 390 | if (IsDerivedFrom(From->getType().getUnqualifiedType(), |
| 391 | ToType.getUnqualifiedType())) |
| 392 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 393 | } |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 394 | } |
Douglas Gregor | e640ab6 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 395 | } else |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 396 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 397 | |
| 398 | return ICS; |
| 399 | } |
| 400 | |
| 401 | /// IsStandardConversion - Determines whether there is a standard |
| 402 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 403 | /// expression From to the type ToType. Standard conversion sequences |
| 404 | /// only consider non-class types; for conversions that involve class |
| 405 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 406 | /// contain the standard conversion sequence required to perform this |
| 407 | /// conversion and this routine will return true. Otherwise, this |
| 408 | /// routine will return false and the value of SCS is unspecified. |
| 409 | bool |
| 410 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
| 411 | StandardConversionSequence &SCS) |
| 412 | { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 413 | QualType FromType = From->getType(); |
| 414 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 415 | // There are no standard conversions for class types, so abort early. |
| 416 | if (FromType->isRecordType() || ToType->isRecordType()) |
| 417 | return false; |
| 418 | |
| 419 | // Standard conversions (C++ [conv]) |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 420 | SCS.setAsIdentityConversion(); |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 421 | SCS.Deprecated = false; |
| 422 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 423 | SCS.CopyConstructor = 0; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 424 | |
| 425 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 426 | // array-to-pointer conversion, or function-to-pointer conversion |
| 427 | // (C++ 4p1). |
| 428 | |
| 429 | // Lvalue-to-rvalue conversion (C++ 4.1): |
| 430 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 431 | // converted to an rvalue. |
| 432 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
| 433 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 434 | !FromType->isFunctionType() && !FromType->isArrayType() && |
| 435 | !FromType->isOverloadType()) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 436 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 437 | |
| 438 | // If T is a non-class type, the type of the rvalue is the |
| 439 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
| 440 | // is T (C++ 4.1p1). |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 441 | FromType = FromType.getUnqualifiedType(); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 442 | } |
| 443 | // Array-to-pointer conversion (C++ 4.2) |
| 444 | else if (FromType->isArrayType()) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 445 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 446 | |
| 447 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 448 | // bound of T" can be converted to an rvalue of type "pointer to |
| 449 | // T" (C++ 4.2p1). |
| 450 | FromType = Context.getArrayDecayedType(FromType); |
| 451 | |
| 452 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 453 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 454 | SCS.Deprecated = true; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 455 | |
| 456 | // For the purpose of ranking in overload resolution |
| 457 | // (13.3.3.1.1), this conversion is considered an |
| 458 | // array-to-pointer conversion followed by a qualification |
| 459 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 460 | SCS.Second = ICK_Identity; |
| 461 | SCS.Third = ICK_Qualification; |
| 462 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 463 | return true; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | // Function-to-pointer conversion (C++ 4.3). |
| 467 | else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 468 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 469 | |
| 470 | // An lvalue of function type T can be converted to an rvalue of |
| 471 | // type "pointer to T." The result is a pointer to the |
| 472 | // function. (C++ 4.3p1). |
| 473 | FromType = Context.getPointerType(FromType); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | } |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 475 | // Address of overloaded function (C++ [over.over]). |
| 476 | else if (FunctionDecl *Fn |
| 477 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
| 478 | SCS.First = ICK_Function_To_Pointer; |
| 479 | |
| 480 | // We were able to resolve the address of the overloaded function, |
| 481 | // so we can convert to the type of that function. |
| 482 | FromType = Fn->getType(); |
| 483 | if (ToType->isReferenceType()) |
| 484 | FromType = Context.getReferenceType(FromType); |
| 485 | else |
| 486 | FromType = Context.getPointerType(FromType); |
| 487 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 488 | // We don't require any conversions for the first step. |
| 489 | else { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 490 | SCS.First = ICK_Identity; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // The second conversion can be an integral promotion, floating |
| 494 | // point promotion, integral conversion, floating point conversion, |
| 495 | // floating-integral conversion, pointer conversion, |
| 496 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
| 497 | if (Context.getCanonicalType(FromType).getUnqualifiedType() == |
| 498 | Context.getCanonicalType(ToType).getUnqualifiedType()) { |
| 499 | // The unqualified versions of the types are the same: there's no |
| 500 | // conversion to do. |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 501 | SCS.Second = ICK_Identity; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 502 | } |
| 503 | // Integral promotion (C++ 4.5). |
| 504 | else if (IsIntegralPromotion(From, FromType, ToType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 505 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | FromType = ToType.getUnqualifiedType(); |
| 507 | } |
| 508 | // Floating point promotion (C++ 4.6). |
| 509 | else if (IsFloatingPointPromotion(FromType, ToType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 510 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 511 | FromType = ToType.getUnqualifiedType(); |
| 512 | } |
| 513 | // Integral conversions (C++ 4.7). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 514 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 515 | else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 516 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 517 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 518 | FromType = ToType.getUnqualifiedType(); |
| 519 | } |
| 520 | // Floating point conversions (C++ 4.8). |
| 521 | else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 522 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 523 | FromType = ToType.getUnqualifiedType(); |
| 524 | } |
| 525 | // Floating-integral conversions (C++ 4.9). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 526 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 527 | else if ((FromType->isFloatingType() && |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 528 | ToType->isIntegralType() && !ToType->isBooleanType() && |
| 529 | !ToType->isEnumeralType()) || |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 530 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
| 531 | ToType->isFloatingType())) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 532 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 533 | FromType = ToType.getUnqualifiedType(); |
| 534 | } |
| 535 | // Pointer conversions (C++ 4.10). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 536 | else if (IsPointerConversion(From, FromType, ToType, FromType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 537 | SCS.Second = ICK_Pointer_Conversion; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 538 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 539 | // FIXME: Pointer to member conversions (4.11). |
| 540 | // Boolean conversions (C++ 4.12). |
| 541 | // FIXME: pointer-to-member type |
| 542 | else if (ToType->isBooleanType() && |
| 543 | (FromType->isArithmeticType() || |
| 544 | FromType->isEnumeralType() || |
| 545 | FromType->isPointerType())) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 546 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 547 | FromType = Context.BoolTy; |
| 548 | } else { |
| 549 | // No second conversion required. |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 550 | SCS.Second = ICK_Identity; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 553 | QualType CanonFrom; |
| 554 | QualType CanonTo; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 555 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 556 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 557 | SCS.Third = ICK_Qualification; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 558 | FromType = ToType; |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 559 | CanonFrom = Context.getCanonicalType(FromType); |
| 560 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 561 | } else { |
| 562 | // No conversion required |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 563 | SCS.Third = ICK_Identity; |
| 564 | |
| 565 | // C++ [over.best.ics]p6: |
| 566 | // [...] Any difference in top-level cv-qualification is |
| 567 | // subsumed by the initialization itself and does not constitute |
| 568 | // a conversion. [...] |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 569 | CanonFrom = Context.getCanonicalType(FromType); |
| 570 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 571 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 572 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 573 | FromType = ToType; |
| 574 | CanonFrom = CanonTo; |
| 575 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // If we have not converted the argument type to the parameter type, |
| 579 | // this is a bad conversion sequence. |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 580 | if (CanonFrom != CanonTo) |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 581 | return false; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 582 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 583 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 584 | return true; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 588 | /// expression From (whose potentially-adjusted type is FromType) to |
| 589 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 590 | /// sets PromotedType to the promoted type. |
| 591 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) |
| 592 | { |
| 593 | const BuiltinType *To = ToType->getAsBuiltinType(); |
Sebastian Redl | 12aee86 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 594 | // All integers are built-in. |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 595 | if (!To) { |
| 596 | return false; |
| 597 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | |
| 599 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 600 | // unsigned short int can be converted to an rvalue of type int if |
| 601 | // int can represent all the values of the source type; otherwise, |
| 602 | // the source rvalue can be converted to an rvalue of type unsigned |
| 603 | // int (C++ 4.5p1). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 604 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 605 | if (// We can promote any signed, promotable integer type to an int |
| 606 | (FromType->isSignedIntegerType() || |
| 607 | // We can promote any unsigned integer type whose size is |
| 608 | // less than int to an int. |
| 609 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 610 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 611 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 614 | return To->getKind() == BuiltinType::UInt; |
| 615 | } |
| 616 | |
| 617 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 618 | // can be converted to an rvalue of the first of the following types |
| 619 | // that can represent all the values of its underlying type: int, |
| 620 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 621 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 622 | && ToType->isIntegerType()) { |
| 623 | // Determine whether the type we're converting from is signed or |
| 624 | // unsigned. |
| 625 | bool FromIsSigned; |
| 626 | uint64_t FromSize = Context.getTypeSize(FromType); |
| 627 | if (const EnumType *FromEnumType = FromType->getAsEnumType()) { |
| 628 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 629 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 630 | } else { |
| 631 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 632 | FromIsSigned = true; |
| 633 | } |
| 634 | |
| 635 | // The types we'll try to promote to, in the appropriate |
| 636 | // order. Try each of these types. |
| 637 | QualType PromoteTypes[4] = { |
| 638 | Context.IntTy, Context.UnsignedIntTy, |
| 639 | Context.LongTy, Context.UnsignedLongTy |
| 640 | }; |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 641 | for (int Idx = 0; Idx < 4; ++Idx) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 642 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 643 | if (FromSize < ToSize || |
| 644 | (FromSize == ToSize && |
| 645 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 646 | // We found the type that we can promote to. If this is the |
| 647 | // type we wanted, we have a promotion. Otherwise, no |
| 648 | // promotion. |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 649 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 650 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 656 | // rvalue of type int if int can represent all the values of the |
| 657 | // bit-field; otherwise, it can be converted to unsigned int if |
| 658 | // unsigned int can represent all the values of the bit-field. If |
| 659 | // the bit-field is larger yet, no integral promotion applies to |
| 660 | // it. If the bit-field has an enumerated type, it is treated as any |
| 661 | // other value of that type for promotion purposes (C++ 4.5p3). |
| 662 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(From)) { |
| 663 | using llvm::APSInt; |
| 664 | FieldDecl *MemberDecl = MemRef->getMemberDecl(); |
| 665 | APSInt BitWidth; |
| 666 | if (MemberDecl->isBitField() && |
| 667 | FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 668 | From->isIntegerConstantExpr(BitWidth, Context)) { |
| 669 | APSInt ToSize(Context.getTypeSize(ToType)); |
| 670 | |
| 671 | // Are we promoting to an int from a bitfield that fits in an int? |
| 672 | if (BitWidth < ToSize || |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 673 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 674 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 677 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 678 | // that fits into an unsigned int? |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 679 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 680 | return To->getKind() == BuiltinType::UInt; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 681 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 682 | |
| 683 | return false; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 688 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 689 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 690 | return true; |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 691 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 692 | |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 697 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 698 | /// returns true and sets PromotedType to the promoted type. |
| 699 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) |
| 700 | { |
| 701 | /// An rvalue of type float can be converted to an rvalue of type |
| 702 | /// double. (C++ 4.6p1). |
| 703 | if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType()) |
| 704 | if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) |
| 705 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 706 | ToBuiltin->getKind() == BuiltinType::Double) |
| 707 | return true; |
| 708 | |
| 709 | return false; |
| 710 | } |
| 711 | |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 712 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 713 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 714 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 715 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 716 | /// the right set of qualifiers on its pointee. |
| 717 | static QualType |
| 718 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
| 719 | QualType ToPointee, QualType ToType, |
| 720 | ASTContext &Context) { |
| 721 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 722 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| 723 | unsigned Quals = CanonFromPointee.getCVRQualifiers(); |
| 724 | |
| 725 | // Exact qualifier match -> return the pointer type we're converting to. |
| 726 | if (CanonToPointee.getCVRQualifiers() == Quals) { |
| 727 | // ToType is exactly what we need. Return it. |
| 728 | if (ToType.getTypePtr()) |
| 729 | return ToType; |
| 730 | |
| 731 | // Build a pointer to ToPointee. It has the right qualifiers |
| 732 | // already. |
| 733 | return Context.getPointerType(ToPointee); |
| 734 | } |
| 735 | |
| 736 | // Just build a canonical type that has the right qualifiers. |
| 737 | return Context.getPointerType(CanonToPointee.getQualifiedType(Quals)); |
| 738 | } |
| 739 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 740 | /// IsPointerConversion - Determines whether the conversion of the |
| 741 | /// expression From, which has the (possibly adjusted) type FromType, |
| 742 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 743 | /// 4.10). If so, returns true and places the converted type (that |
| 744 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 745 | /// ConvertedType. |
Douglas Gregor | 9036ef7 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 746 | /// |
| 747 | /// This routine also supports conversions to and from block pointers. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 748 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
| 749 | QualType& ConvertedType) |
| 750 | { |
Douglas Gregor | 9036ef7 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 751 | // Blocks: Block pointers can be converted to void*. |
| 752 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
| 753 | ToType->getAsPointerType()->getPointeeType()->isVoidType()) { |
| 754 | ConvertedType = ToType; |
| 755 | return true; |
| 756 | } |
| 757 | // Blocks: A null pointer constant can be converted to a block |
| 758 | // pointer type. |
| 759 | if (ToType->isBlockPointerType() && From->isNullPointerConstant(Context)) { |
| 760 | ConvertedType = ToType; |
| 761 | return true; |
| 762 | } |
| 763 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 764 | const PointerType* ToTypePtr = ToType->getAsPointerType(); |
| 765 | if (!ToTypePtr) |
| 766 | return false; |
| 767 | |
| 768 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
| 769 | if (From->isNullPointerConstant(Context)) { |
| 770 | ConvertedType = ToType; |
| 771 | return true; |
| 772 | } |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 774 | // Beyond this point, both types need to be pointers. |
| 775 | const PointerType *FromTypePtr = FromType->getAsPointerType(); |
| 776 | if (!FromTypePtr) |
| 777 | return false; |
| 778 | |
| 779 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 780 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 781 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 782 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 783 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 784 | // 4.10p2). |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 785 | if (FromPointeeType->isIncompleteOrObjectType() && ToPointeeType->isVoidType()) { |
Douglas Gregor | 8bb7ad8 | 2008-11-27 00:52:49 +0000 | [diff] [blame^] | 786 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 787 | ToPointeeType, |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 788 | ToType, Context); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 789 | return true; |
| 790 | } |
| 791 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 792 | // C++ [conv.ptr]p3: |
| 793 | // |
| 794 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 795 | // can be converted to an rvalue of type "pointer to cv B," where |
| 796 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 797 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 798 | // necessitates this conversion is ill-formed. The result of the |
| 799 | // conversion is a pointer to the base class sub-object of the |
| 800 | // derived class object. The null pointer value is converted to |
| 801 | // the null pointer value of the destination type. |
| 802 | // |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 803 | // Note that we do not check for ambiguity or inaccessibility |
| 804 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 805 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 806 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 8bb7ad8 | 2008-11-27 00:52:49 +0000 | [diff] [blame^] | 807 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 808 | ToPointeeType, |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 809 | ToType, Context); |
| 810 | return true; |
| 811 | } |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 813 | // Objective C++: We're able to convert from a pointer to an |
| 814 | // interface to a pointer to a different interface. |
| 815 | const ObjCInterfaceType* FromIface = FromPointeeType->getAsObjCInterfaceType(); |
| 816 | const ObjCInterfaceType* ToIface = ToPointeeType->getAsObjCInterfaceType(); |
| 817 | if (FromIface && ToIface && |
| 818 | Context.canAssignObjCInterfaces(ToIface, FromIface)) { |
Douglas Gregor | 8bb7ad8 | 2008-11-27 00:52:49 +0000 | [diff] [blame^] | 819 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 820 | ToPointeeType, |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 821 | ToType, Context); |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | // Objective C++: We're able to convert between "id" and a pointer |
| 826 | // to any interface (in both directions). |
| 827 | if ((FromIface && Context.isObjCIdType(ToPointeeType)) |
| 828 | || (ToIface && Context.isObjCIdType(FromPointeeType))) { |
Douglas Gregor | 8bb7ad8 | 2008-11-27 00:52:49 +0000 | [diff] [blame^] | 829 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 830 | ToPointeeType, |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 831 | ToType, Context); |
| 832 | return true; |
| 833 | } |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 835 | return false; |
| 836 | } |
| 837 | |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 838 | /// CheckPointerConversion - Check the pointer conversion from the |
| 839 | /// expression From to the type ToType. This routine checks for |
| 840 | /// ambiguous (FIXME: or inaccessible) derived-to-base pointer |
| 841 | /// conversions for which IsPointerConversion has already returned |
| 842 | /// true. It returns true and produces a diagnostic if there was an |
| 843 | /// error, or returns false otherwise. |
| 844 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType) { |
| 845 | QualType FromType = From->getType(); |
| 846 | |
| 847 | if (const PointerType *FromPtrType = FromType->getAsPointerType()) |
| 848 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) { |
Sebastian Redl | 9ac68aa | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 849 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 850 | /*DetectVirtual=*/false); |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 851 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 852 | ToPointeeType = ToPtrType->getPointeeType(); |
| 853 | if (FromPointeeType->isRecordType() && |
| 854 | ToPointeeType->isRecordType()) { |
| 855 | // We must have a derived-to-base conversion. Check an |
| 856 | // ambiguous or inaccessible conversion. |
Douglas Gregor | 651d1cc | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 857 | return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 858 | From->getExprLoc(), |
| 859 | From->getSourceRange()); |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | return false; |
| 864 | } |
| 865 | |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 866 | /// IsQualificationConversion - Determines whether the conversion from |
| 867 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 868 | /// (C++ 4.4). |
| 869 | bool |
| 870 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) |
| 871 | { |
| 872 | FromType = Context.getCanonicalType(FromType); |
| 873 | ToType = Context.getCanonicalType(ToType); |
| 874 | |
| 875 | // If FromType and ToType are the same type, this is not a |
| 876 | // qualification conversion. |
| 877 | if (FromType == ToType) |
| 878 | return false; |
| 879 | |
| 880 | // (C++ 4.4p4): |
| 881 | // A conversion can add cv-qualifiers at levels other than the first |
| 882 | // in multi-level pointers, subject to the following rules: [...] |
| 883 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 884 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 885 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 886 | // Within each iteration of the loop, we check the qualifiers to |
| 887 | // determine if this still looks like a qualification |
| 888 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 889 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 890 | // until there are no more pointers or pointers-to-members left to |
| 891 | // unwrap. |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 892 | UnwrappedAnyPointer = true; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 893 | |
| 894 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 895 | // 2,j, and similarly for volatile. |
Douglas Gregor | e5db4f7 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 896 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 897 | return false; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 899 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 900 | // every cv for 0 < k < j. |
| 901 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 902 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 903 | return false; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 904 | |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 905 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 906 | // include const. |
| 907 | PreviousToQualsIncludeConst |
| 908 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 909 | } |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 910 | |
| 911 | // We are left with FromType and ToType being the pointee types |
| 912 | // after unwrapping the original FromType and ToType the same number |
| 913 | // of types. If we unwrapped any pointers, and if FromType and |
| 914 | // ToType have the same unqualified type (since we checked |
| 915 | // qualifiers above), then this is a qualification conversion. |
| 916 | return UnwrappedAnyPointer && |
| 917 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 918 | } |
| 919 | |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 920 | /// IsUserDefinedConversion - Determines whether there is a |
| 921 | /// user-defined conversion sequence (C++ [over.ics.user]) that |
| 922 | /// converts expression From to the type ToType. If such a conversion |
| 923 | /// exists, User will contain the user-defined conversion sequence |
| 924 | /// that performs such a conversion and this routine will return |
| 925 | /// true. Otherwise, this routine returns false and User is |
| 926 | /// unspecified. |
| 927 | bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 928 | UserDefinedConversionSequence& User) |
| 929 | { |
| 930 | OverloadCandidateSet CandidateSet; |
| 931 | if (const CXXRecordType *ToRecordType |
| 932 | = dyn_cast_or_null<CXXRecordType>(ToType->getAsRecordType())) { |
| 933 | // C++ [over.match.ctor]p1: |
| 934 | // When objects of class type are direct-initialized (8.5), or |
| 935 | // copy-initialized from an expression of the same or a |
| 936 | // derived class type (8.5), overload resolution selects the |
| 937 | // constructor. [...] For copy-initialization, the candidate |
| 938 | // functions are all the converting constructors (12.3.1) of |
| 939 | // that class. The argument list is the expression-list within |
| 940 | // the parentheses of the initializer. |
| 941 | CXXRecordDecl *ToRecordDecl = ToRecordType->getDecl(); |
| 942 | const OverloadedFunctionDecl *Constructors = ToRecordDecl->getConstructors(); |
| 943 | for (OverloadedFunctionDecl::function_const_iterator func |
| 944 | = Constructors->function_begin(); |
| 945 | func != Constructors->function_end(); ++func) { |
| 946 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*func); |
| 947 | if (Constructor->isConvertingConstructor()) |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 948 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 949 | /*SuppressUserConversions=*/true); |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 953 | if (const CXXRecordType *FromRecordType |
| 954 | = dyn_cast_or_null<CXXRecordType>(From->getType()->getAsRecordType())) { |
| 955 | // Add all of the conversion functions as candidates. |
| 956 | // FIXME: Look for conversions in base classes! |
| 957 | CXXRecordDecl *FromRecordDecl = FromRecordType->getDecl(); |
| 958 | OverloadedFunctionDecl *Conversions |
| 959 | = FromRecordDecl->getConversionFunctions(); |
| 960 | for (OverloadedFunctionDecl::function_iterator Func |
| 961 | = Conversions->function_begin(); |
| 962 | Func != Conversions->function_end(); ++Func) { |
| 963 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 964 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 965 | } |
| 966 | } |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 967 | |
| 968 | OverloadCandidateSet::iterator Best; |
| 969 | switch (BestViableFunction(CandidateSet, Best)) { |
| 970 | case OR_Success: |
| 971 | // Record the standard conversion we used and the conversion function. |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 972 | if (CXXConstructorDecl *Constructor |
| 973 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 974 | // C++ [over.ics.user]p1: |
| 975 | // If the user-defined conversion is specified by a |
| 976 | // constructor (12.3.1), the initial standard conversion |
| 977 | // sequence converts the source type to the type required by |
| 978 | // the argument of the constructor. |
| 979 | // |
| 980 | // FIXME: What about ellipsis conversions? |
| 981 | QualType ThisType = Constructor->getThisType(Context); |
| 982 | User.Before = Best->Conversions[0].Standard; |
| 983 | User.ConversionFunction = Constructor; |
| 984 | User.After.setAsIdentityConversion(); |
| 985 | User.After.FromTypePtr |
| 986 | = ThisType->getAsPointerType()->getPointeeType().getAsOpaquePtr(); |
| 987 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
| 988 | return true; |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 989 | } else if (CXXConversionDecl *Conversion |
| 990 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 991 | // C++ [over.ics.user]p1: |
| 992 | // |
| 993 | // [...] If the user-defined conversion is specified by a |
| 994 | // conversion function (12.3.2), the initial standard |
| 995 | // conversion sequence converts the source type to the |
| 996 | // implicit object parameter of the conversion function. |
| 997 | User.Before = Best->Conversions[0].Standard; |
| 998 | User.ConversionFunction = Conversion; |
| 999 | |
| 1000 | // C++ [over.ics.user]p2: |
| 1001 | // The second standard conversion sequence converts the |
| 1002 | // result of the user-defined conversion to the target type |
| 1003 | // for the sequence. Since an implicit conversion sequence |
| 1004 | // is an initialization, the special rules for |
| 1005 | // initialization by user-defined conversion apply when |
| 1006 | // selecting the best user-defined conversion for a |
| 1007 | // user-defined conversion sequence (see 13.3.3 and |
| 1008 | // 13.3.3.1). |
| 1009 | User.After = Best->FinalConversion; |
| 1010 | return true; |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1011 | } else { |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1012 | assert(false && "Not a constructor or conversion function?"); |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1013 | return false; |
| 1014 | } |
| 1015 | |
| 1016 | case OR_No_Viable_Function: |
| 1017 | // No conversion here! We're done. |
| 1018 | return false; |
| 1019 | |
| 1020 | case OR_Ambiguous: |
| 1021 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 1022 | // ambiguous conversion sequences. |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1029 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1030 | /// conversion sequences to determine whether one is better than the |
| 1031 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
| 1032 | ImplicitConversionSequence::CompareKind |
| 1033 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1034 | const ImplicitConversionSequence& ICS2) |
| 1035 | { |
| 1036 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1037 | // conversion sequences (as defined in 13.3.3.1) |
| 1038 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1039 | // conversion sequence than a user-defined conversion sequence or |
| 1040 | // an ellipsis conversion sequence, and |
| 1041 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1042 | // conversion sequence than an ellipsis conversion sequence |
| 1043 | // (13.3.3.1.3). |
| 1044 | // |
| 1045 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1046 | return ImplicitConversionSequence::Better; |
| 1047 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1048 | return ImplicitConversionSequence::Worse; |
| 1049 | |
| 1050 | // Two implicit conversion sequences of the same form are |
| 1051 | // indistinguishable conversion sequences unless one of the |
| 1052 | // following rules apply: (C++ 13.3.3.2p3): |
| 1053 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1054 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
| 1055 | else if (ICS1.ConversionKind == |
| 1056 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1057 | // User-defined conversion sequence U1 is a better conversion |
| 1058 | // sequence than another user-defined conversion sequence U2 if |
| 1059 | // they contain the same user-defined conversion function or |
| 1060 | // constructor and if the second standard conversion sequence of |
| 1061 | // U1 is better than the second standard conversion sequence of |
| 1062 | // U2 (C++ 13.3.3.2p3). |
| 1063 | if (ICS1.UserDefined.ConversionFunction == |
| 1064 | ICS2.UserDefined.ConversionFunction) |
| 1065 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1066 | ICS2.UserDefined.After); |
| 1067 | } |
| 1068 | |
| 1069 | return ImplicitConversionSequence::Indistinguishable; |
| 1070 | } |
| 1071 | |
| 1072 | /// CompareStandardConversionSequences - Compare two standard |
| 1073 | /// conversion sequences to determine whether one is better than the |
| 1074 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
| 1075 | ImplicitConversionSequence::CompareKind |
| 1076 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1077 | const StandardConversionSequence& SCS2) |
| 1078 | { |
| 1079 | // Standard conversion sequence S1 is a better conversion sequence |
| 1080 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1081 | |
| 1082 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1083 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1084 | // excluding any Lvalue Transformation; the identity conversion |
| 1085 | // sequence is considered to be a subsequence of any |
| 1086 | // non-identity conversion sequence) or, if not that, |
| 1087 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1088 | // Neither is a proper subsequence of the other. Do nothing. |
| 1089 | ; |
| 1090 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1091 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
| 1092 | (SCS1.Second == ICK_Identity && |
| 1093 | SCS1.Third == ICK_Identity)) |
| 1094 | // SCS1 is a proper subsequence of SCS2. |
| 1095 | return ImplicitConversionSequence::Better; |
| 1096 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1097 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
| 1098 | (SCS2.Second == ICK_Identity && |
| 1099 | SCS2.Third == ICK_Identity)) |
| 1100 | // SCS2 is a proper subsequence of SCS1. |
| 1101 | return ImplicitConversionSequence::Worse; |
| 1102 | |
| 1103 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1104 | // defined below), or, if not that, |
| 1105 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1106 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1107 | if (Rank1 < Rank2) |
| 1108 | return ImplicitConversionSequence::Better; |
| 1109 | else if (Rank2 < Rank1) |
| 1110 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1112 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1113 | // are indistinguishable unless one of the following rules |
| 1114 | // applies: |
| 1115 | |
| 1116 | // A conversion that is not a conversion of a pointer, or |
| 1117 | // pointer to member, to bool is better than another conversion |
| 1118 | // that is such a conversion. |
| 1119 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1120 | return SCS2.isPointerConversionToBool() |
| 1121 | ? ImplicitConversionSequence::Better |
| 1122 | : ImplicitConversionSequence::Worse; |
| 1123 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1124 | // C++ [over.ics.rank]p4b2: |
| 1125 | // |
| 1126 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1127 | // conversion of B* to A* is better than conversion of B* to |
| 1128 | // void*, and conversion of A* to void* is better than conversion |
| 1129 | // of B* to void*. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1130 | bool SCS1ConvertsToVoid |
| 1131 | = SCS1.isPointerConversionToVoidPointer(Context); |
| 1132 | bool SCS2ConvertsToVoid |
| 1133 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1134 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1135 | // Exactly one of the conversion sequences is a conversion to |
| 1136 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1137 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1138 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1139 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1140 | // Neither conversion sequence converts to a void pointer; compare |
| 1141 | // their derived-to-base conversions. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1142 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1143 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1144 | return DerivedCK; |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1145 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1146 | // Both conversion sequences are conversions to void |
| 1147 | // pointers. Compare the source types to determine if there's an |
| 1148 | // inheritance relationship in their sources. |
| 1149 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1150 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1151 | |
| 1152 | // Adjust the types we're converting from via the array-to-pointer |
| 1153 | // conversion, if we need to. |
| 1154 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1155 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1156 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1157 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1158 | |
| 1159 | QualType FromPointee1 |
| 1160 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1161 | QualType FromPointee2 |
| 1162 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1163 | |
| 1164 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1165 | return ImplicitConversionSequence::Better; |
| 1166 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1167 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1168 | |
| 1169 | // Objective-C++: If one interface is more specific than the |
| 1170 | // other, it is the better one. |
| 1171 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1172 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1173 | if (FromIface1 && FromIface1) { |
| 1174 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1175 | return ImplicitConversionSequence::Better; |
| 1176 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1177 | return ImplicitConversionSequence::Worse; |
| 1178 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1179 | } |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1182 | // bullet 3). |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1183 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1184 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1185 | return QualCK; |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1187 | // C++ [over.ics.rank]p3b4: |
| 1188 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1189 | // which the references refer are the same type except for |
| 1190 | // top-level cv-qualifiers, and the type to which the reference |
| 1191 | // initialized by S2 refers is more cv-qualified than the type |
| 1192 | // to which the reference initialized by S1 refers. |
| 1193 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
| 1194 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1195 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1196 | T1 = Context.getCanonicalType(T1); |
| 1197 | T2 = Context.getCanonicalType(T2); |
| 1198 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1199 | if (T2.isMoreQualifiedThan(T1)) |
| 1200 | return ImplicitConversionSequence::Better; |
| 1201 | else if (T1.isMoreQualifiedThan(T2)) |
| 1202 | return ImplicitConversionSequence::Worse; |
| 1203 | } |
| 1204 | } |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1205 | |
| 1206 | return ImplicitConversionSequence::Indistinguishable; |
| 1207 | } |
| 1208 | |
| 1209 | /// CompareQualificationConversions - Compares two standard conversion |
| 1210 | /// sequences to determine whether they can be ranked based on their |
| 1211 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1212 | ImplicitConversionSequence::CompareKind |
| 1213 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
| 1214 | const StandardConversionSequence& SCS2) |
| 1215 | { |
Douglas Gregor | 4459bbe | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1216 | // C++ 13.3.3.2p3: |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1217 | // -- S1 and S2 differ only in their qualification conversion and |
| 1218 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1219 | // cv-qualification signature of type T1 is a proper subset of |
| 1220 | // the cv-qualification signature of type T2, and S1 is not the |
| 1221 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1222 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1223 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1224 | return ImplicitConversionSequence::Indistinguishable; |
| 1225 | |
| 1226 | // FIXME: the example in the standard doesn't use a qualification |
| 1227 | // conversion (!) |
| 1228 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1229 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1230 | T1 = Context.getCanonicalType(T1); |
| 1231 | T2 = Context.getCanonicalType(T2); |
| 1232 | |
| 1233 | // If the types are the same, we won't learn anything by unwrapped |
| 1234 | // them. |
| 1235 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1236 | return ImplicitConversionSequence::Indistinguishable; |
| 1237 | |
| 1238 | ImplicitConversionSequence::CompareKind Result |
| 1239 | = ImplicitConversionSequence::Indistinguishable; |
| 1240 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1241 | // Within each iteration of the loop, we check the qualifiers to |
| 1242 | // determine if this still looks like a qualification |
| 1243 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1244 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1245 | // until there are no more pointers or pointers-to-members left |
| 1246 | // to unwrap. This essentially mimics what |
| 1247 | // IsQualificationConversion does, but here we're checking for a |
| 1248 | // strict subset of qualifiers. |
| 1249 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1250 | // The qualifiers are the same, so this doesn't tell us anything |
| 1251 | // about how the sequences rank. |
| 1252 | ; |
| 1253 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1254 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1255 | if (Result == ImplicitConversionSequence::Worse) |
| 1256 | // Neither has qualifiers that are a subset of the other's |
| 1257 | // qualifiers. |
| 1258 | return ImplicitConversionSequence::Indistinguishable; |
| 1259 | |
| 1260 | Result = ImplicitConversionSequence::Better; |
| 1261 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1262 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1263 | if (Result == ImplicitConversionSequence::Better) |
| 1264 | // Neither has qualifiers that are a subset of the other's |
| 1265 | // qualifiers. |
| 1266 | return ImplicitConversionSequence::Indistinguishable; |
| 1267 | |
| 1268 | Result = ImplicitConversionSequence::Worse; |
| 1269 | } else { |
| 1270 | // Qualifiers are disjoint. |
| 1271 | return ImplicitConversionSequence::Indistinguishable; |
| 1272 | } |
| 1273 | |
| 1274 | // If the types after this point are equivalent, we're done. |
| 1275 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1276 | break; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Douglas Gregor | ccc0ccc | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1279 | // Check that the winning standard conversion sequence isn't using |
| 1280 | // the deprecated string literal array to pointer conversion. |
| 1281 | switch (Result) { |
| 1282 | case ImplicitConversionSequence::Better: |
| 1283 | if (SCS1.Deprecated) |
| 1284 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1285 | break; |
| 1286 | |
| 1287 | case ImplicitConversionSequence::Indistinguishable: |
| 1288 | break; |
| 1289 | |
| 1290 | case ImplicitConversionSequence::Worse: |
| 1291 | if (SCS2.Deprecated) |
| 1292 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1293 | break; |
| 1294 | } |
| 1295 | |
| 1296 | return Result; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1299 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1300 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1301 | /// various kinds of derived-to-base conversions (C++ |
| 1302 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1303 | /// conversions between Objective-C interface types. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1304 | ImplicitConversionSequence::CompareKind |
| 1305 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1306 | const StandardConversionSequence& SCS2) { |
| 1307 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1308 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1309 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1310 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1311 | |
| 1312 | // Adjust the types we're converting from via the array-to-pointer |
| 1313 | // conversion, if we need to. |
| 1314 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1315 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1316 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1317 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1318 | |
| 1319 | // Canonicalize all of the types. |
| 1320 | FromType1 = Context.getCanonicalType(FromType1); |
| 1321 | ToType1 = Context.getCanonicalType(ToType1); |
| 1322 | FromType2 = Context.getCanonicalType(FromType2); |
| 1323 | ToType2 = Context.getCanonicalType(ToType2); |
| 1324 | |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1325 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1326 | // |
| 1327 | // If class B is derived directly or indirectly from class A and |
| 1328 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1329 | // |
| 1330 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1331 | // interfaces. |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Compare based on pointer conversions. |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1334 | if (SCS1.Second == ICK_Pointer_Conversion && |
| 1335 | SCS2.Second == ICK_Pointer_Conversion) { |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1336 | QualType FromPointee1 |
| 1337 | = FromType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1338 | QualType ToPointee1 |
| 1339 | = ToType1->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1340 | QualType FromPointee2 |
| 1341 | = FromType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
| 1342 | QualType ToPointee2 |
| 1343 | = ToType2->getAsPointerType()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1344 | |
| 1345 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1346 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1347 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAsObjCInterfaceType(); |
| 1348 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAsObjCInterfaceType(); |
| 1349 | |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1350 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1351 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1352 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1353 | return ImplicitConversionSequence::Better; |
| 1354 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1355 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (ToIface1 && ToIface2) { |
| 1358 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1359 | return ImplicitConversionSequence::Better; |
| 1360 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1361 | return ImplicitConversionSequence::Worse; |
| 1362 | } |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1363 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1364 | |
| 1365 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1366 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1367 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1368 | return ImplicitConversionSequence::Better; |
| 1369 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1370 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (FromIface1 && FromIface2) { |
| 1373 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1374 | return ImplicitConversionSequence::Better; |
| 1375 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1376 | return ImplicitConversionSequence::Worse; |
| 1377 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1378 | } |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1381 | // Compare based on reference bindings. |
| 1382 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1383 | SCS1.Second == ICK_Derived_To_Base) { |
| 1384 | // -- binding of an expression of type C to a reference of type |
| 1385 | // B& is better than binding an expression of type C to a |
| 1386 | // reference of type A&, |
| 1387 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1388 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1389 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1390 | return ImplicitConversionSequence::Better; |
| 1391 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1392 | return ImplicitConversionSequence::Worse; |
| 1393 | } |
| 1394 | |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1395 | // -- binding of an expression of type B to a reference of type |
| 1396 | // A& is better than binding an expression of type C to a |
| 1397 | // reference of type A&, |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1398 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1399 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1400 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1401 | return ImplicitConversionSequence::Better; |
| 1402 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1403 | return ImplicitConversionSequence::Worse; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | |
| 1408 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1409 | // A::* to C::*, |
| 1410 | |
| 1411 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1412 | // A::* to C::*, and |
| 1413 | |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1414 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1415 | SCS1.Second == ICK_Derived_To_Base) { |
| 1416 | // -- conversion of C to B is better than conversion of C to A, |
| 1417 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1418 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1419 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1420 | return ImplicitConversionSequence::Better; |
| 1421 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1422 | return ImplicitConversionSequence::Worse; |
| 1423 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1424 | |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1425 | // -- conversion of B to A is better than conversion of C to A. |
| 1426 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1427 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1428 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1429 | return ImplicitConversionSequence::Better; |
| 1430 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1431 | return ImplicitConversionSequence::Worse; |
| 1432 | } |
| 1433 | } |
Douglas Gregor | 0e34338 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | 1404650 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1435 | return ImplicitConversionSequence::Indistinguishable; |
| 1436 | } |
| 1437 | |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1438 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1439 | /// ToType from the expression From. Return the implicit conversion |
| 1440 | /// sequence required to pass this argument, which may be a bad |
| 1441 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1442 | /// a parameter of this type). If @p SuppressUserConversions, then we |
| 1443 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1444 | ImplicitConversionSequence |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1445 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
| 1446 | bool SuppressUserConversions) { |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1447 | if (!getLangOptions().CPlusPlus) { |
Douglas Gregor | b72e9da | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1448 | // In C, copy initialization is the same as performing an assignment. |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1449 | AssignConvertType ConvTy = |
| 1450 | CheckSingleAssignmentConstraints(ToType, From); |
| 1451 | ImplicitConversionSequence ICS; |
| 1452 | if (getLangOptions().NoExtensions? ConvTy != Compatible |
| 1453 | : ConvTy == Incompatible) |
| 1454 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1455 | else |
| 1456 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1457 | return ICS; |
| 1458 | } else if (ToType->isReferenceType()) { |
| 1459 | ImplicitConversionSequence ICS; |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1460 | CheckReferenceInit(From, ToType, &ICS, SuppressUserConversions); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1461 | return ICS; |
| 1462 | } else { |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1463 | return TryImplicitConversion(From, ToType, SuppressUserConversions); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /// PerformArgumentPassing - Pass the argument Arg into a parameter of |
| 1468 | /// type ToType. Returns true (and emits a diagnostic) if there was |
| 1469 | /// an error, returns false if the initialization succeeded. |
| 1470 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
| 1471 | const char* Flavor) { |
| 1472 | if (!getLangOptions().CPlusPlus) { |
| 1473 | // In C, argument passing is the same as performing an assignment. |
| 1474 | QualType FromType = From->getType(); |
| 1475 | AssignConvertType ConvTy = |
| 1476 | CheckSingleAssignmentConstraints(ToType, From); |
| 1477 | |
| 1478 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 1479 | FromType, From, Flavor); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1480 | } |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1481 | |
| 1482 | if (ToType->isReferenceType()) |
| 1483 | return CheckReferenceInit(From, ToType); |
| 1484 | |
| 1485 | if (!PerformImplicitConversion(From, ToType)) |
| 1486 | return false; |
| 1487 | |
| 1488 | return Diag(From->getSourceRange().getBegin(), |
| 1489 | diag::err_typecheck_convert_incompatible) |
| 1490 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Douglas Gregor | 81c2915 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1493 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 1494 | /// parameter of the given member function (@c Method) from the |
| 1495 | /// expression @p From. |
| 1496 | ImplicitConversionSequence |
| 1497 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 1498 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
| 1499 | unsigned MethodQuals = Method->getTypeQualifiers(); |
| 1500 | QualType ImplicitParamType = ClassType.getQualifiedType(MethodQuals); |
| 1501 | |
| 1502 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 1503 | // to exit early. |
| 1504 | ImplicitConversionSequence ICS; |
| 1505 | ICS.Standard.setAsIdentityConversion(); |
| 1506 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1507 | |
| 1508 | // We need to have an object of class type. |
| 1509 | QualType FromType = From->getType(); |
| 1510 | if (!FromType->isRecordType()) |
| 1511 | return ICS; |
| 1512 | |
| 1513 | // The implicit object parmeter is has the type "reference to cv X", |
| 1514 | // where X is the class of which the function is a member |
| 1515 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 1516 | // conversion sequence for the argument, we are not allowed to |
| 1517 | // create temporaries or perform user-defined conversions |
| 1518 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 1519 | // reference binding here, that allows class rvalues to bind to |
| 1520 | // non-constant references. |
| 1521 | |
| 1522 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 1523 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 1524 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 1525 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 1526 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 1527 | return ICS; |
| 1528 | |
| 1529 | // Check that we have either the same type or a derived type. It |
| 1530 | // affects the conversion rank. |
| 1531 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 1532 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 1533 | ICS.Standard.Second = ICK_Identity; |
| 1534 | else if (IsDerivedFrom(FromType, ClassType)) |
| 1535 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1536 | else |
| 1537 | return ICS; |
| 1538 | |
| 1539 | // Success. Mark this as a reference binding. |
| 1540 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1541 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 1542 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 1543 | ICS.Standard.ReferenceBinding = true; |
| 1544 | ICS.Standard.DirectBinding = true; |
| 1545 | return ICS; |
| 1546 | } |
| 1547 | |
| 1548 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 1549 | /// the implicit object parameter for the given Method with the given |
| 1550 | /// expression. |
| 1551 | bool |
| 1552 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
| 1553 | QualType ImplicitParamType |
| 1554 | = Method->getThisType(Context)->getAsPointerType()->getPointeeType(); |
| 1555 | ImplicitConversionSequence ICS |
| 1556 | = TryObjectArgumentInitialization(From, Method); |
| 1557 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 1558 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1559 | diag::err_implicit_object_parameter_init) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1560 | << ImplicitParamType << From->getType() << From->getSourceRange(); |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1561 | |
| 1562 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
| 1563 | CheckDerivedToBaseConversion(From->getType(), ImplicitParamType, |
| 1564 | From->getSourceRange().getBegin(), |
| 1565 | From->getSourceRange())) |
| 1566 | return true; |
| 1567 | |
| 1568 | ImpCastExprToType(From, ImplicitParamType, /*isLvalue=*/true); |
| 1569 | return false; |
| 1570 | } |
| 1571 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1572 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1573 | /// candidate functions, using the given function call arguments. If |
| 1574 | /// @p SuppressUserConversions, then don't allow user-defined |
| 1575 | /// conversions via constructors or conversion operators. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1576 | void |
| 1577 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
| 1578 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1579 | OverloadCandidateSet& CandidateSet, |
| 1580 | bool SuppressUserConversions) |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1581 | { |
| 1582 | const FunctionTypeProto* Proto |
| 1583 | = dyn_cast<FunctionTypeProto>(Function->getType()->getAsFunctionType()); |
| 1584 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1585 | assert(!isa<CXXConversionDecl>(Function) && |
| 1586 | "Use AddConversionCandidate for conversion functions"); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1587 | |
| 1588 | // Add this candidate |
| 1589 | CandidateSet.push_back(OverloadCandidate()); |
| 1590 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1591 | Candidate.Function = Function; |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 1592 | Candidate.IsSurrogate = false; |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1593 | |
| 1594 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1595 | |
| 1596 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 1597 | // parameters is viable only if it has an ellipsis in its parameter |
| 1598 | // list (8.3.5). |
| 1599 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 1600 | Candidate.Viable = false; |
| 1601 | return; |
| 1602 | } |
| 1603 | |
| 1604 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 1605 | // is viable only if the (m+1)st parameter has a default argument |
| 1606 | // (8.3.6). For the purposes of overload resolution, the |
| 1607 | // parameter list is truncated on the right, so that there are |
| 1608 | // exactly m parameters. |
| 1609 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 1610 | if (NumArgs < MinRequiredArgs) { |
| 1611 | // Not enough arguments. |
| 1612 | Candidate.Viable = false; |
| 1613 | return; |
| 1614 | } |
| 1615 | |
| 1616 | // Determine the implicit conversion sequences for each of the |
| 1617 | // arguments. |
| 1618 | Candidate.Viable = true; |
| 1619 | Candidate.Conversions.resize(NumArgs); |
| 1620 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 1621 | if (ArgIdx < NumArgsInProto) { |
| 1622 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 1623 | // exist for each argument an implicit conversion sequence |
| 1624 | // (13.3.3.1) that converts that argument to the corresponding |
| 1625 | // parameter of F. |
| 1626 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 1627 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1628 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 1629 | SuppressUserConversions); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1630 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1631 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1632 | Candidate.Viable = false; |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1635 | } else { |
| 1636 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 1637 | // argument for which there is no corresponding parameter is |
| 1638 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 1639 | Candidate.Conversions[ArgIdx].ConversionKind |
| 1640 | = ImplicitConversionSequence::EllipsisConversion; |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1645 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 1646 | /// of candidate functions, using the given function call arguments |
| 1647 | /// and the object argument (@c Object). For example, in a call |
| 1648 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 1649 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 1650 | /// allow user-defined conversions via constructors or conversion |
| 1651 | /// operators. |
| 1652 | void |
| 1653 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 1654 | Expr **Args, unsigned NumArgs, |
| 1655 | OverloadCandidateSet& CandidateSet, |
| 1656 | bool SuppressUserConversions) |
| 1657 | { |
| 1658 | const FunctionTypeProto* Proto |
| 1659 | = dyn_cast<FunctionTypeProto>(Method->getType()->getAsFunctionType()); |
| 1660 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
| 1661 | assert(!isa<CXXConversionDecl>(Method) && |
| 1662 | "Use AddConversionCandidate for conversion functions"); |
| 1663 | |
| 1664 | // Add this candidate |
| 1665 | CandidateSet.push_back(OverloadCandidate()); |
| 1666 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1667 | Candidate.Function = Method; |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 1668 | Candidate.IsSurrogate = false; |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1669 | |
| 1670 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1671 | |
| 1672 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 1673 | // parameters is viable only if it has an ellipsis in its parameter |
| 1674 | // list (8.3.5). |
| 1675 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 1676 | Candidate.Viable = false; |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 1681 | // is viable only if the (m+1)st parameter has a default argument |
| 1682 | // (8.3.6). For the purposes of overload resolution, the |
| 1683 | // parameter list is truncated on the right, so that there are |
| 1684 | // exactly m parameters. |
| 1685 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 1686 | if (NumArgs < MinRequiredArgs) { |
| 1687 | // Not enough arguments. |
| 1688 | Candidate.Viable = false; |
| 1689 | return; |
| 1690 | } |
| 1691 | |
| 1692 | Candidate.Viable = true; |
| 1693 | Candidate.Conversions.resize(NumArgs + 1); |
| 1694 | |
| 1695 | // Determine the implicit conversion sequence for the object |
| 1696 | // parameter. |
| 1697 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
| 1698 | if (Candidate.Conversions[0].ConversionKind |
| 1699 | == ImplicitConversionSequence::BadConversion) { |
| 1700 | Candidate.Viable = false; |
| 1701 | return; |
| 1702 | } |
| 1703 | |
| 1704 | // Determine the implicit conversion sequences for each of the |
| 1705 | // arguments. |
| 1706 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 1707 | if (ArgIdx < NumArgsInProto) { |
| 1708 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 1709 | // exist for each argument an implicit conversion sequence |
| 1710 | // (13.3.3.1) that converts that argument to the corresponding |
| 1711 | // parameter of F. |
| 1712 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 1713 | Candidate.Conversions[ArgIdx + 1] |
| 1714 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 1715 | SuppressUserConversions); |
| 1716 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 1717 | == ImplicitConversionSequence::BadConversion) { |
| 1718 | Candidate.Viable = false; |
| 1719 | break; |
| 1720 | } |
| 1721 | } else { |
| 1722 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 1723 | // argument for which there is no corresponding parameter is |
| 1724 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 1725 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 1726 | = ImplicitConversionSequence::EllipsisConversion; |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1731 | /// AddConversionCandidate - Add a C++ conversion function as a |
| 1732 | /// candidate in the candidate set (C++ [over.match.conv], |
| 1733 | /// C++ [over.match.copy]). From is the expression we're converting from, |
| 1734 | /// and ToType is the type that we're eventually trying to convert to |
| 1735 | /// (which may or may not be the same type as the type that the |
| 1736 | /// conversion function produces). |
| 1737 | void |
| 1738 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 1739 | Expr *From, QualType ToType, |
| 1740 | OverloadCandidateSet& CandidateSet) { |
| 1741 | // Add this candidate |
| 1742 | CandidateSet.push_back(OverloadCandidate()); |
| 1743 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1744 | Candidate.Function = Conversion; |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 1745 | Candidate.IsSurrogate = false; |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1746 | Candidate.FinalConversion.setAsIdentityConversion(); |
| 1747 | Candidate.FinalConversion.FromTypePtr |
| 1748 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 1749 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 1750 | |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1751 | // Determine the implicit conversion sequence for the implicit |
| 1752 | // object parameter. |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1753 | Candidate.Viable = true; |
| 1754 | Candidate.Conversions.resize(1); |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1755 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1756 | |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1757 | if (Candidate.Conversions[0].ConversionKind |
| 1758 | == ImplicitConversionSequence::BadConversion) { |
| 1759 | Candidate.Viable = false; |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | // To determine what the conversion from the result of calling the |
| 1764 | // conversion function to the type we're eventually trying to |
| 1765 | // convert to (ToType), we need to synthesize a call to the |
| 1766 | // conversion function and attempt copy initialization from it. This |
| 1767 | // makes sure that we get the right semantics with respect to |
| 1768 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 1769 | // call on the stack and we don't need its arguments to be |
| 1770 | // well-formed. |
| 1771 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
| 1772 | SourceLocation()); |
| 1773 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1774 | &ConversionRef, false); |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1775 | CallExpr Call(&ConversionFn, 0, 0, |
| 1776 | Conversion->getConversionType().getNonReferenceType(), |
| 1777 | SourceLocation()); |
| 1778 | ImplicitConversionSequence ICS = TryCopyInitialization(&Call, ToType, true); |
| 1779 | switch (ICS.ConversionKind) { |
| 1780 | case ImplicitConversionSequence::StandardConversion: |
| 1781 | Candidate.FinalConversion = ICS.Standard; |
| 1782 | break; |
| 1783 | |
| 1784 | case ImplicitConversionSequence::BadConversion: |
| 1785 | Candidate.Viable = false; |
| 1786 | break; |
| 1787 | |
| 1788 | default: |
| 1789 | assert(false && |
| 1790 | "Can only end up with a standard conversion sequence or failure"); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 1794 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 1795 | /// converts the given @c Object to a function pointer via the |
| 1796 | /// conversion function @c Conversion, and then attempts to call it |
| 1797 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 1798 | /// the type of function that we'll eventually be calling. |
| 1799 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
| 1800 | const FunctionTypeProto *Proto, |
| 1801 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 1802 | OverloadCandidateSet& CandidateSet) { |
| 1803 | CandidateSet.push_back(OverloadCandidate()); |
| 1804 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 1805 | Candidate.Function = 0; |
| 1806 | Candidate.Surrogate = Conversion; |
| 1807 | Candidate.Viable = true; |
| 1808 | Candidate.IsSurrogate = true; |
| 1809 | Candidate.Conversions.resize(NumArgs + 1); |
| 1810 | |
| 1811 | // Determine the implicit conversion sequence for the implicit |
| 1812 | // object parameter. |
| 1813 | ImplicitConversionSequence ObjectInit |
| 1814 | = TryObjectArgumentInitialization(Object, Conversion); |
| 1815 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 1816 | Candidate.Viable = false; |
| 1817 | return; |
| 1818 | } |
| 1819 | |
| 1820 | // The first conversion is actually a user-defined conversion whose |
| 1821 | // first conversion is ObjectInit's standard conversion (which is |
| 1822 | // effectively a reference binding). Record it as such. |
| 1823 | Candidate.Conversions[0].ConversionKind |
| 1824 | = ImplicitConversionSequence::UserDefinedConversion; |
| 1825 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 1826 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
| 1827 | Candidate.Conversions[0].UserDefined.After |
| 1828 | = Candidate.Conversions[0].UserDefined.Before; |
| 1829 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 1830 | |
| 1831 | // Find the |
| 1832 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1833 | |
| 1834 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 1835 | // parameters is viable only if it has an ellipsis in its parameter |
| 1836 | // list (8.3.5). |
| 1837 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 1838 | Candidate.Viable = false; |
| 1839 | return; |
| 1840 | } |
| 1841 | |
| 1842 | // Function types don't have any default arguments, so just check if |
| 1843 | // we have enough arguments. |
| 1844 | if (NumArgs < NumArgsInProto) { |
| 1845 | // Not enough arguments. |
| 1846 | Candidate.Viable = false; |
| 1847 | return; |
| 1848 | } |
| 1849 | |
| 1850 | // Determine the implicit conversion sequences for each of the |
| 1851 | // arguments. |
| 1852 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 1853 | if (ArgIdx < NumArgsInProto) { |
| 1854 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 1855 | // exist for each argument an implicit conversion sequence |
| 1856 | // (13.3.3.1) that converts that argument to the corresponding |
| 1857 | // parameter of F. |
| 1858 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 1859 | Candidate.Conversions[ArgIdx + 1] |
| 1860 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 1861 | /*SuppressUserConversions=*/false); |
| 1862 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 1863 | == ImplicitConversionSequence::BadConversion) { |
| 1864 | Candidate.Viable = false; |
| 1865 | break; |
| 1866 | } |
| 1867 | } else { |
| 1868 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 1869 | // argument for which there is no corresponding parameter is |
| 1870 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 1871 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 1872 | = ImplicitConversionSequence::EllipsisConversion; |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 1877 | /// IsAcceptableNonMemberOperatorCandidate - Determine whether Fn is |
| 1878 | /// an acceptable non-member overloaded operator for a call whose |
| 1879 | /// arguments have types T1 (and, if non-empty, T2). This routine |
| 1880 | /// implements the check in C++ [over.match.oper]p3b2 concerning |
| 1881 | /// enumeration types. |
| 1882 | static bool |
| 1883 | IsAcceptableNonMemberOperatorCandidate(FunctionDecl *Fn, |
| 1884 | QualType T1, QualType T2, |
| 1885 | ASTContext &Context) { |
| 1886 | if (T1->isRecordType() || (!T2.isNull() && T2->isRecordType())) |
| 1887 | return true; |
| 1888 | |
| 1889 | const FunctionTypeProto *Proto = Fn->getType()->getAsFunctionTypeProto(); |
| 1890 | if (Proto->getNumArgs() < 1) |
| 1891 | return false; |
| 1892 | |
| 1893 | if (T1->isEnumeralType()) { |
| 1894 | QualType ArgType = Proto->getArgType(0).getNonReferenceType(); |
| 1895 | if (Context.getCanonicalType(T1).getUnqualifiedType() |
| 1896 | == Context.getCanonicalType(ArgType).getUnqualifiedType()) |
| 1897 | return true; |
| 1898 | } |
| 1899 | |
| 1900 | if (Proto->getNumArgs() < 2) |
| 1901 | return false; |
| 1902 | |
| 1903 | if (!T2.isNull() && T2->isEnumeralType()) { |
| 1904 | QualType ArgType = Proto->getArgType(1).getNonReferenceType(); |
| 1905 | if (Context.getCanonicalType(T2).getUnqualifiedType() |
| 1906 | == Context.getCanonicalType(ArgType).getUnqualifiedType()) |
| 1907 | return true; |
| 1908 | } |
| 1909 | |
| 1910 | return false; |
| 1911 | } |
| 1912 | |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1913 | /// AddOperatorCandidates - Add the overloaded operator candidates for |
| 1914 | /// the operator Op that was used in an operator expression such as "x |
| 1915 | /// Op y". S is the scope in which the expression occurred (used for |
| 1916 | /// name lookup of the operator), Args/NumArgs provides the operator |
| 1917 | /// arguments, and CandidateSet will store the added overload |
| 1918 | /// candidates. (C++ [over.match.oper]). |
| 1919 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
| 1920 | Expr **Args, unsigned NumArgs, |
| 1921 | OverloadCandidateSet& CandidateSet) { |
| 1922 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 1923 | |
| 1924 | // C++ [over.match.oper]p3: |
| 1925 | // For a unary operator @ with an operand of a type whose |
| 1926 | // cv-unqualified version is T1, and for a binary operator @ with |
| 1927 | // a left operand of a type whose cv-unqualified version is T1 and |
| 1928 | // a right operand of a type whose cv-unqualified version is T2, |
| 1929 | // three sets of candidate functions, designated member |
| 1930 | // candidates, non-member candidates and built-in candidates, are |
| 1931 | // constructed as follows: |
| 1932 | QualType T1 = Args[0]->getType(); |
| 1933 | QualType T2; |
| 1934 | if (NumArgs > 1) |
| 1935 | T2 = Args[1]->getType(); |
| 1936 | |
| 1937 | // -- If T1 is a class type, the set of member candidates is the |
| 1938 | // result of the qualified lookup of T1::operator@ |
| 1939 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 1940 | // empty. |
| 1941 | if (const RecordType *T1Rec = T1->getAsRecordType()) { |
| 1942 | IdentifierResolver::iterator I |
| 1943 | = IdResolver.begin(OpName, cast<CXXRecordType>(T1Rec)->getDecl(), |
| 1944 | /*LookInParentCtx=*/false); |
| 1945 | NamedDecl *MemberOps = (I == IdResolver.end())? 0 : *I; |
| 1946 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(MemberOps)) |
| 1947 | AddMethodCandidate(Method, Args[0], Args+1, NumArgs - 1, CandidateSet, |
| 1948 | /*SuppressUserConversions=*/false); |
| 1949 | else if (OverloadedFunctionDecl *Ovl |
| 1950 | = dyn_cast_or_null<OverloadedFunctionDecl>(MemberOps)) { |
| 1951 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 1952 | FEnd = Ovl->function_end(); |
| 1953 | F != FEnd; ++F) { |
| 1954 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*F)) |
| 1955 | AddMethodCandidate(Method, Args[0], Args+1, NumArgs - 1, CandidateSet, |
| 1956 | /*SuppressUserConversions=*/false); |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | // -- The set of non-member candidates is the result of the |
| 1962 | // unqualified lookup of operator@ in the context of the |
| 1963 | // expression according to the usual rules for name lookup in |
| 1964 | // unqualified function calls (3.4.2) except that all member |
| 1965 | // functions are ignored. However, if no operand has a class |
| 1966 | // type, only those non-member functions in the lookup set |
| 1967 | // that have a first parameter of type T1 or “reference to |
| 1968 | // (possibly cv-qualified) T1”, when T1 is an enumeration |
| 1969 | // type, or (if there is a right operand) a second parameter |
| 1970 | // of type T2 or “reference to (possibly cv-qualified) T2”, |
| 1971 | // when T2 is an enumeration type, are candidate functions. |
| 1972 | { |
| 1973 | NamedDecl *NonMemberOps = 0; |
| 1974 | for (IdentifierResolver::iterator I |
| 1975 | = IdResolver.begin(OpName, CurContext, true/*LookInParentCtx*/); |
| 1976 | I != IdResolver.end(); ++I) { |
| 1977 | // We don't need to check the identifier namespace, because |
| 1978 | // operator names can only be ordinary identifiers. |
| 1979 | |
| 1980 | // Ignore member functions. |
| 1981 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(*I)) { |
| 1982 | if (SD->getDeclContext()->isCXXRecord()) |
| 1983 | continue; |
| 1984 | } |
| 1985 | |
| 1986 | // We found something with this name. We're done. |
| 1987 | NonMemberOps = *I; |
| 1988 | break; |
| 1989 | } |
| 1990 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 1991 | if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(NonMemberOps)) { |
| 1992 | if (IsAcceptableNonMemberOperatorCandidate(FD, T1, T2, Context)) |
| 1993 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 1994 | /*SuppressUserConversions=*/false); |
| 1995 | } else if (OverloadedFunctionDecl *Ovl |
| 1996 | = dyn_cast_or_null<OverloadedFunctionDecl>(NonMemberOps)) { |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1997 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 1998 | FEnd = Ovl->function_end(); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 1999 | F != FEnd; ++F) { |
| 2000 | if (IsAcceptableNonMemberOperatorCandidate(*F, T1, T2, Context)) |
| 2001 | AddOverloadCandidate(*F, Args, NumArgs, CandidateSet, |
| 2002 | /*SuppressUserConversions=*/false); |
| 2003 | } |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | // Add builtin overload candidates (C++ [over.built]). |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2008 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2011 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2012 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2013 | /// of the built-in candidate, respectively. Args and NumArgs are the |
| 2014 | /// arguments being passed to the candidate. |
| 2015 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
| 2016 | Expr **Args, unsigned NumArgs, |
| 2017 | OverloadCandidateSet& CandidateSet) { |
| 2018 | // Add this candidate |
| 2019 | CandidateSet.push_back(OverloadCandidate()); |
| 2020 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2021 | Candidate.Function = 0; |
| 2022 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2023 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2024 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2025 | |
| 2026 | // Determine the implicit conversion sequences for each of the |
| 2027 | // arguments. |
| 2028 | Candidate.Viable = true; |
| 2029 | Candidate.Conversions.resize(NumArgs); |
| 2030 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2031 | Candidate.Conversions[ArgIdx] |
| 2032 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], false); |
| 2033 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2034 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2035 | Candidate.Viable = false; |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2036 | break; |
| 2037 | } |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2042 | /// candidate operator functions for built-in operators (C++ |
| 2043 | /// [over.built]). The types are separated into pointer types and |
| 2044 | /// enumeration types. |
| 2045 | class BuiltinCandidateTypeSet { |
| 2046 | /// TypeSet - A set of types. |
Douglas Gregor | 3d4492e | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 2047 | typedef llvm::SmallPtrSet<void*, 8> TypeSet; |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2048 | |
| 2049 | /// PointerTypes - The set of pointer types that will be used in the |
| 2050 | /// built-in candidates. |
| 2051 | TypeSet PointerTypes; |
| 2052 | |
| 2053 | /// EnumerationTypes - The set of enumeration types that will be |
| 2054 | /// used in the built-in candidates. |
| 2055 | TypeSet EnumerationTypes; |
| 2056 | |
| 2057 | /// Context - The AST context in which we will build the type sets. |
| 2058 | ASTContext &Context; |
| 2059 | |
| 2060 | bool AddWithMoreQualifiedTypeVariants(QualType Ty); |
| 2061 | |
| 2062 | public: |
| 2063 | /// iterator - Iterates through the types that are part of the set. |
Douglas Gregor | 3d4492e | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 2064 | class iterator { |
| 2065 | TypeSet::iterator Base; |
| 2066 | |
| 2067 | public: |
| 2068 | typedef QualType value_type; |
| 2069 | typedef QualType reference; |
| 2070 | typedef QualType pointer; |
| 2071 | typedef std::ptrdiff_t difference_type; |
| 2072 | typedef std::input_iterator_tag iterator_category; |
| 2073 | |
| 2074 | iterator(TypeSet::iterator B) : Base(B) { } |
| 2075 | |
| 2076 | iterator& operator++() { |
| 2077 | ++Base; |
| 2078 | return *this; |
| 2079 | } |
| 2080 | |
| 2081 | iterator operator++(int) { |
| 2082 | iterator tmp(*this); |
| 2083 | ++(*this); |
| 2084 | return tmp; |
| 2085 | } |
| 2086 | |
| 2087 | reference operator*() const { |
| 2088 | return QualType::getFromOpaquePtr(*Base); |
| 2089 | } |
| 2090 | |
| 2091 | pointer operator->() const { |
| 2092 | return **this; |
| 2093 | } |
| 2094 | |
| 2095 | friend bool operator==(iterator LHS, iterator RHS) { |
| 2096 | return LHS.Base == RHS.Base; |
| 2097 | } |
| 2098 | |
| 2099 | friend bool operator!=(iterator LHS, iterator RHS) { |
| 2100 | return LHS.Base != RHS.Base; |
| 2101 | } |
| 2102 | }; |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2103 | |
| 2104 | BuiltinCandidateTypeSet(ASTContext &Context) : Context(Context) { } |
| 2105 | |
| 2106 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions = true); |
| 2107 | |
| 2108 | /// pointer_begin - First pointer type found; |
| 2109 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2110 | |
| 2111 | /// pointer_end - Last pointer type found; |
| 2112 | iterator pointer_end() { return PointerTypes.end(); } |
| 2113 | |
| 2114 | /// enumeration_begin - First enumeration type found; |
| 2115 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2116 | |
| 2117 | /// enumeration_end - Last enumeration type found; |
| 2118 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2119 | }; |
| 2120 | |
| 2121 | /// AddWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
| 2122 | /// the set of pointer types along with any more-qualified variants of |
| 2123 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2124 | /// will add "int const *", "int const volatile *", "int const |
| 2125 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2126 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2127 | /// false otherwise. |
| 2128 | bool BuiltinCandidateTypeSet::AddWithMoreQualifiedTypeVariants(QualType Ty) { |
| 2129 | // Insert this type. |
Douglas Gregor | 3d4492e | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 2130 | if (!PointerTypes.insert(Ty.getAsOpaquePtr())) |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2131 | return false; |
| 2132 | |
| 2133 | if (const PointerType *PointerTy = Ty->getAsPointerType()) { |
| 2134 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2135 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2136 | |
| 2137 | // FIXME: Do we have to add CVR qualifiers at *all* levels to deal |
| 2138 | // with all pointer conversions that don't cast away constness? |
| 2139 | if (!PointeeTy.isConstQualified()) |
| 2140 | AddWithMoreQualifiedTypeVariants |
| 2141 | (Context.getPointerType(PointeeTy.withConst())); |
| 2142 | if (!PointeeTy.isVolatileQualified()) |
| 2143 | AddWithMoreQualifiedTypeVariants |
| 2144 | (Context.getPointerType(PointeeTy.withVolatile())); |
| 2145 | if (!PointeeTy.isRestrictQualified()) |
| 2146 | AddWithMoreQualifiedTypeVariants |
| 2147 | (Context.getPointerType(PointeeTy.withRestrict())); |
| 2148 | } |
| 2149 | |
| 2150 | return true; |
| 2151 | } |
| 2152 | |
| 2153 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2154 | /// Ty can be implicit converted to the given set of @p Types. We're |
| 2155 | /// primarily interested in pointer types, enumeration types, |
| 2156 | void BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2157 | bool AllowUserConversions) { |
| 2158 | // Only deal with canonical types. |
| 2159 | Ty = Context.getCanonicalType(Ty); |
| 2160 | |
| 2161 | // Look through reference types; they aren't part of the type of an |
| 2162 | // expression for the purposes of conversions. |
| 2163 | if (const ReferenceType *RefTy = Ty->getAsReferenceType()) |
| 2164 | Ty = RefTy->getPointeeType(); |
| 2165 | |
| 2166 | // We don't care about qualifiers on the type. |
| 2167 | Ty = Ty.getUnqualifiedType(); |
| 2168 | |
| 2169 | if (const PointerType *PointerTy = Ty->getAsPointerType()) { |
| 2170 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2171 | |
| 2172 | // Insert our type, and its more-qualified variants, into the set |
| 2173 | // of types. |
| 2174 | if (!AddWithMoreQualifiedTypeVariants(Ty)) |
| 2175 | return; |
| 2176 | |
| 2177 | // Add 'cv void*' to our set of types. |
| 2178 | if (!Ty->isVoidType()) { |
| 2179 | QualType QualVoid |
| 2180 | = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 2181 | AddWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
| 2182 | } |
| 2183 | |
| 2184 | // If this is a pointer to a class type, add pointers to its bases |
| 2185 | // (with the same level of cv-qualification as the original |
| 2186 | // derived class, of course). |
| 2187 | if (const RecordType *PointeeRec = PointeeTy->getAsRecordType()) { |
| 2188 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 2189 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2190 | Base != ClassDecl->bases_end(); ++Base) { |
| 2191 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
| 2192 | BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 2193 | |
| 2194 | // Add the pointer type, recursively, so that we get all of |
| 2195 | // the indirect base classes, too. |
| 2196 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false); |
| 2197 | } |
| 2198 | } |
| 2199 | } else if (Ty->isEnumeralType()) { |
Douglas Gregor | 3d4492e | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 2200 | EnumerationTypes.insert(Ty.getAsOpaquePtr()); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2201 | } else if (AllowUserConversions) { |
| 2202 | if (const RecordType *TyRec = Ty->getAsRecordType()) { |
| 2203 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 2204 | // FIXME: Visit conversion functions in the base classes, too. |
| 2205 | OverloadedFunctionDecl *Conversions |
| 2206 | = ClassDecl->getConversionFunctions(); |
| 2207 | for (OverloadedFunctionDecl::function_iterator Func |
| 2208 | = Conversions->function_begin(); |
| 2209 | Func != Conversions->function_end(); ++Func) { |
| 2210 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 2211 | AddTypesConvertedFrom(Conv->getConversionType(), false); |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2217 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 2218 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 2219 | /// on the operator @p Op and the arguments given. For example, if the |
| 2220 | /// operator is a binary '+', this routine might add "int |
| 2221 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2222 | void |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2223 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 2224 | Expr **Args, unsigned NumArgs, |
| 2225 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2226 | // The set of "promoted arithmetic types", which are the arithmetic |
| 2227 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 2228 | // that the first few of these types are the promoted integral |
| 2229 | // types; these types need to be first. |
| 2230 | // FIXME: What about complex? |
| 2231 | const unsigned FirstIntegralType = 0; |
| 2232 | const unsigned LastIntegralType = 13; |
| 2233 | const unsigned FirstPromotedIntegralType = 7, |
| 2234 | LastPromotedIntegralType = 13; |
| 2235 | const unsigned FirstPromotedArithmeticType = 7, |
| 2236 | LastPromotedArithmeticType = 16; |
| 2237 | const unsigned NumArithmeticTypes = 16; |
| 2238 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
| 2239 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 2240 | Context.SignedCharTy, Context.ShortTy, |
| 2241 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 2242 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 2243 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 2244 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 2245 | }; |
| 2246 | |
| 2247 | // Find all of the types that the arguments can convert to, but only |
| 2248 | // if the operator we're looking at has built-in operator candidates |
| 2249 | // that make use of these types. |
| 2250 | BuiltinCandidateTypeSet CandidateTypes(Context); |
| 2251 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 2252 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2253 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2254 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2255 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
| 2256 | (Op == OO_Star && NumArgs == 1)) { |
| 2257 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2258 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType()); |
| 2259 | } |
| 2260 | |
| 2261 | bool isComparison = false; |
| 2262 | switch (Op) { |
| 2263 | case OO_None: |
| 2264 | case NUM_OVERLOADED_OPERATORS: |
| 2265 | assert(false && "Expected an overloaded operator"); |
| 2266 | break; |
| 2267 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2268 | case OO_Star: // '*' is either unary or binary |
| 2269 | if (NumArgs == 1) |
| 2270 | goto UnaryStar; |
| 2271 | else |
| 2272 | goto BinaryStar; |
| 2273 | break; |
| 2274 | |
| 2275 | case OO_Plus: // '+' is either unary or binary |
| 2276 | if (NumArgs == 1) |
| 2277 | goto UnaryPlus; |
| 2278 | else |
| 2279 | goto BinaryPlus; |
| 2280 | break; |
| 2281 | |
| 2282 | case OO_Minus: // '-' is either unary or binary |
| 2283 | if (NumArgs == 1) |
| 2284 | goto UnaryMinus; |
| 2285 | else |
| 2286 | goto BinaryMinus; |
| 2287 | break; |
| 2288 | |
| 2289 | case OO_Amp: // '&' is either unary or binary |
| 2290 | if (NumArgs == 1) |
| 2291 | goto UnaryAmp; |
| 2292 | else |
| 2293 | goto BinaryAmp; |
| 2294 | |
| 2295 | case OO_PlusPlus: |
| 2296 | case OO_MinusMinus: |
| 2297 | // C++ [over.built]p3: |
| 2298 | // |
| 2299 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 2300 | // is either volatile or empty, there exist candidate operator |
| 2301 | // functions of the form |
| 2302 | // |
| 2303 | // VQ T& operator++(VQ T&); |
| 2304 | // T operator++(VQ T&, int); |
| 2305 | // |
| 2306 | // C++ [over.built]p4: |
| 2307 | // |
| 2308 | // For every pair (T, VQ), where T is an arithmetic type other |
| 2309 | // than bool, and VQ is either volatile or empty, there exist |
| 2310 | // candidate operator functions of the form |
| 2311 | // |
| 2312 | // VQ T& operator--(VQ T&); |
| 2313 | // T operator--(VQ T&, int); |
| 2314 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 2315 | Arith < NumArithmeticTypes; ++Arith) { |
| 2316 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 2317 | QualType ParamTypes[2] |
| 2318 | = { Context.getReferenceType(ArithTy), Context.IntTy }; |
| 2319 | |
| 2320 | // Non-volatile version. |
| 2321 | if (NumArgs == 1) |
| 2322 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2323 | else |
| 2324 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 2325 | |
| 2326 | // Volatile version |
| 2327 | ParamTypes[0] = Context.getReferenceType(ArithTy.withVolatile()); |
| 2328 | if (NumArgs == 1) |
| 2329 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2330 | else |
| 2331 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 2332 | } |
| 2333 | |
| 2334 | // C++ [over.built]p5: |
| 2335 | // |
| 2336 | // For every pair (T, VQ), where T is a cv-qualified or |
| 2337 | // cv-unqualified object type, and VQ is either volatile or |
| 2338 | // empty, there exist candidate operator functions of the form |
| 2339 | // |
| 2340 | // T*VQ& operator++(T*VQ&); |
| 2341 | // T*VQ& operator--(T*VQ&); |
| 2342 | // T* operator++(T*VQ&, int); |
| 2343 | // T* operator--(T*VQ&, int); |
| 2344 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2345 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2346 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 24a90a5 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2347 | if (!(*Ptr)->getAsPointerType()->getPointeeType()->isIncompleteOrObjectType()) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2348 | continue; |
| 2349 | |
| 2350 | QualType ParamTypes[2] = { |
| 2351 | Context.getReferenceType(*Ptr), Context.IntTy |
| 2352 | }; |
| 2353 | |
| 2354 | // Without volatile |
| 2355 | if (NumArgs == 1) |
| 2356 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2357 | else |
| 2358 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2359 | |
| 2360 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 2361 | // With volatile |
| 2362 | ParamTypes[0] = Context.getReferenceType((*Ptr).withVolatile()); |
| 2363 | if (NumArgs == 1) |
| 2364 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2365 | else |
| 2366 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2367 | } |
| 2368 | } |
| 2369 | break; |
| 2370 | |
| 2371 | UnaryStar: |
| 2372 | // C++ [over.built]p6: |
| 2373 | // For every cv-qualified or cv-unqualified object type T, there |
| 2374 | // exist candidate operator functions of the form |
| 2375 | // |
| 2376 | // T& operator*(T*); |
| 2377 | // |
| 2378 | // C++ [over.built]p7: |
| 2379 | // For every function type T, there exist candidate operator |
| 2380 | // functions of the form |
| 2381 | // T& operator*(T*); |
| 2382 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2383 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2384 | QualType ParamTy = *Ptr; |
| 2385 | QualType PointeeTy = ParamTy->getAsPointerType()->getPointeeType(); |
| 2386 | AddBuiltinCandidate(Context.getReferenceType(PointeeTy), |
| 2387 | &ParamTy, Args, 1, CandidateSet); |
| 2388 | } |
| 2389 | break; |
| 2390 | |
| 2391 | UnaryPlus: |
| 2392 | // C++ [over.built]p8: |
| 2393 | // For every type T, there exist candidate operator functions of |
| 2394 | // the form |
| 2395 | // |
| 2396 | // T* operator+(T*); |
| 2397 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2398 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2399 | QualType ParamTy = *Ptr; |
| 2400 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 2401 | } |
| 2402 | |
| 2403 | // Fall through |
| 2404 | |
| 2405 | UnaryMinus: |
| 2406 | // C++ [over.built]p9: |
| 2407 | // For every promoted arithmetic type T, there exist candidate |
| 2408 | // operator functions of the form |
| 2409 | // |
| 2410 | // T operator+(T); |
| 2411 | // T operator-(T); |
| 2412 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 2413 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 2414 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 2415 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 2416 | } |
| 2417 | break; |
| 2418 | |
| 2419 | case OO_Tilde: |
| 2420 | // C++ [over.built]p10: |
| 2421 | // For every promoted integral type T, there exist candidate |
| 2422 | // operator functions of the form |
| 2423 | // |
| 2424 | // T operator~(T); |
| 2425 | for (unsigned Int = FirstPromotedIntegralType; |
| 2426 | Int < LastPromotedIntegralType; ++Int) { |
| 2427 | QualType IntTy = ArithmeticTypes[Int]; |
| 2428 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 2429 | } |
| 2430 | break; |
| 2431 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2432 | case OO_New: |
| 2433 | case OO_Delete: |
| 2434 | case OO_Array_New: |
| 2435 | case OO_Array_Delete: |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2436 | case OO_Call: |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2437 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2438 | break; |
| 2439 | |
| 2440 | case OO_Comma: |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2441 | UnaryAmp: |
| 2442 | case OO_Arrow: |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2443 | // C++ [over.match.oper]p3: |
| 2444 | // -- For the operator ',', the unary operator '&', or the |
| 2445 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2446 | break; |
| 2447 | |
| 2448 | case OO_Less: |
| 2449 | case OO_Greater: |
| 2450 | case OO_LessEqual: |
| 2451 | case OO_GreaterEqual: |
| 2452 | case OO_EqualEqual: |
| 2453 | case OO_ExclaimEqual: |
| 2454 | // C++ [over.built]p15: |
| 2455 | // |
| 2456 | // For every pointer or enumeration type T, there exist |
| 2457 | // candidate operator functions of the form |
| 2458 | // |
| 2459 | // bool operator<(T, T); |
| 2460 | // bool operator>(T, T); |
| 2461 | // bool operator<=(T, T); |
| 2462 | // bool operator>=(T, T); |
| 2463 | // bool operator==(T, T); |
| 2464 | // bool operator!=(T, T); |
| 2465 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2466 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2467 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 2468 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 2469 | } |
| 2470 | for (BuiltinCandidateTypeSet::iterator Enum |
| 2471 | = CandidateTypes.enumeration_begin(); |
| 2472 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 2473 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 2474 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 2475 | } |
| 2476 | |
| 2477 | // Fall through. |
| 2478 | isComparison = true; |
| 2479 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2480 | BinaryPlus: |
| 2481 | BinaryMinus: |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2482 | if (!isComparison) { |
| 2483 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 2484 | |
| 2485 | // C++ [over.built]p13: |
| 2486 | // |
| 2487 | // For every cv-qualified or cv-unqualified object type T |
| 2488 | // there exist candidate operator functions of the form |
| 2489 | // |
| 2490 | // T* operator+(T*, ptrdiff_t); |
| 2491 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 2492 | // T* operator-(T*, ptrdiff_t); |
| 2493 | // T* operator+(ptrdiff_t, T*); |
| 2494 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 2495 | // |
| 2496 | // C++ [over.built]p14: |
| 2497 | // |
| 2498 | // For every T, where T is a pointer to object type, there |
| 2499 | // exist candidate operator functions of the form |
| 2500 | // |
| 2501 | // ptrdiff_t operator-(T, T); |
| 2502 | for (BuiltinCandidateTypeSet::iterator Ptr |
| 2503 | = CandidateTypes.pointer_begin(); |
| 2504 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2505 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 2506 | |
| 2507 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 2508 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2509 | |
| 2510 | if (Op == OO_Plus) { |
| 2511 | // T* operator+(ptrdiff_t, T*); |
| 2512 | ParamTypes[0] = ParamTypes[1]; |
| 2513 | ParamTypes[1] = *Ptr; |
| 2514 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2515 | } else { |
| 2516 | // ptrdiff_t operator-(T, T); |
| 2517 | ParamTypes[1] = *Ptr; |
| 2518 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 2519 | Args, 2, CandidateSet); |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | // Fall through |
| 2524 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2525 | case OO_Slash: |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2526 | BinaryStar: |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2527 | // C++ [over.built]p12: |
| 2528 | // |
| 2529 | // For every pair of promoted arithmetic types L and R, there |
| 2530 | // exist candidate operator functions of the form |
| 2531 | // |
| 2532 | // LR operator*(L, R); |
| 2533 | // LR operator/(L, R); |
| 2534 | // LR operator+(L, R); |
| 2535 | // LR operator-(L, R); |
| 2536 | // bool operator<(L, R); |
| 2537 | // bool operator>(L, R); |
| 2538 | // bool operator<=(L, R); |
| 2539 | // bool operator>=(L, R); |
| 2540 | // bool operator==(L, R); |
| 2541 | // bool operator!=(L, R); |
| 2542 | // |
| 2543 | // where LR is the result of the usual arithmetic conversions |
| 2544 | // between types L and R. |
| 2545 | for (unsigned Left = FirstPromotedArithmeticType; |
| 2546 | Left < LastPromotedArithmeticType; ++Left) { |
| 2547 | for (unsigned Right = FirstPromotedArithmeticType; |
| 2548 | Right < LastPromotedArithmeticType; ++Right) { |
| 2549 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 2550 | QualType Result |
| 2551 | = isComparison? Context.BoolTy |
| 2552 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 2553 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 2554 | } |
| 2555 | } |
| 2556 | break; |
| 2557 | |
| 2558 | case OO_Percent: |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2559 | BinaryAmp: |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2560 | case OO_Caret: |
| 2561 | case OO_Pipe: |
| 2562 | case OO_LessLess: |
| 2563 | case OO_GreaterGreater: |
| 2564 | // C++ [over.built]p17: |
| 2565 | // |
| 2566 | // For every pair of promoted integral types L and R, there |
| 2567 | // exist candidate operator functions of the form |
| 2568 | // |
| 2569 | // LR operator%(L, R); |
| 2570 | // LR operator&(L, R); |
| 2571 | // LR operator^(L, R); |
| 2572 | // LR operator|(L, R); |
| 2573 | // L operator<<(L, R); |
| 2574 | // L operator>>(L, R); |
| 2575 | // |
| 2576 | // where LR is the result of the usual arithmetic conversions |
| 2577 | // between types L and R. |
| 2578 | for (unsigned Left = FirstPromotedIntegralType; |
| 2579 | Left < LastPromotedIntegralType; ++Left) { |
| 2580 | for (unsigned Right = FirstPromotedIntegralType; |
| 2581 | Right < LastPromotedIntegralType; ++Right) { |
| 2582 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 2583 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 2584 | ? LandR[0] |
| 2585 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 2586 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 2587 | } |
| 2588 | } |
| 2589 | break; |
| 2590 | |
| 2591 | case OO_Equal: |
| 2592 | // C++ [over.built]p20: |
| 2593 | // |
| 2594 | // For every pair (T, VQ), where T is an enumeration or |
| 2595 | // (FIXME:) pointer to member type and VQ is either volatile or |
| 2596 | // empty, there exist candidate operator functions of the form |
| 2597 | // |
| 2598 | // VQ T& operator=(VQ T&, T); |
| 2599 | for (BuiltinCandidateTypeSet::iterator Enum |
| 2600 | = CandidateTypes.enumeration_begin(); |
| 2601 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 2602 | QualType ParamTypes[2]; |
| 2603 | |
| 2604 | // T& operator=(T&, T) |
| 2605 | ParamTypes[0] = Context.getReferenceType(*Enum); |
| 2606 | ParamTypes[1] = *Enum; |
| 2607 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2608 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2609 | if (!Context.getCanonicalType(*Enum).isVolatileQualified()) { |
| 2610 | // volatile T& operator=(volatile T&, T) |
| 2611 | ParamTypes[0] = Context.getReferenceType((*Enum).withVolatile()); |
| 2612 | ParamTypes[1] = *Enum; |
| 2613 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2614 | } |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2615 | } |
| 2616 | // Fall through. |
| 2617 | |
| 2618 | case OO_PlusEqual: |
| 2619 | case OO_MinusEqual: |
| 2620 | // C++ [over.built]p19: |
| 2621 | // |
| 2622 | // For every pair (T, VQ), where T is any type and VQ is either |
| 2623 | // volatile or empty, there exist candidate operator functions |
| 2624 | // of the form |
| 2625 | // |
| 2626 | // T*VQ& operator=(T*VQ&, T*); |
| 2627 | // |
| 2628 | // C++ [over.built]p21: |
| 2629 | // |
| 2630 | // For every pair (T, VQ), where T is a cv-qualified or |
| 2631 | // cv-unqualified object type and VQ is either volatile or |
| 2632 | // empty, there exist candidate operator functions of the form |
| 2633 | // |
| 2634 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 2635 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 2636 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2637 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2638 | QualType ParamTypes[2]; |
| 2639 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 2640 | |
| 2641 | // non-volatile version |
| 2642 | ParamTypes[0] = Context.getReferenceType(*Ptr); |
| 2643 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2644 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2645 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 2646 | // volatile version |
| 2647 | ParamTypes[0] = Context.getReferenceType((*Ptr).withVolatile()); |
| 2648 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2649 | } |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2650 | } |
| 2651 | // Fall through. |
| 2652 | |
| 2653 | case OO_StarEqual: |
| 2654 | case OO_SlashEqual: |
| 2655 | // C++ [over.built]p18: |
| 2656 | // |
| 2657 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 2658 | // VQ is either volatile or empty, and R is a promoted |
| 2659 | // arithmetic type, there exist candidate operator functions of |
| 2660 | // the form |
| 2661 | // |
| 2662 | // VQ L& operator=(VQ L&, R); |
| 2663 | // VQ L& operator*=(VQ L&, R); |
| 2664 | // VQ L& operator/=(VQ L&, R); |
| 2665 | // VQ L& operator+=(VQ L&, R); |
| 2666 | // VQ L& operator-=(VQ L&, R); |
| 2667 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 2668 | for (unsigned Right = FirstPromotedArithmeticType; |
| 2669 | Right < LastPromotedArithmeticType; ++Right) { |
| 2670 | QualType ParamTypes[2]; |
| 2671 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 2672 | |
| 2673 | // Add this built-in operator as a candidate (VQ is empty). |
| 2674 | ParamTypes[0] = Context.getReferenceType(ArithmeticTypes[Left]); |
| 2675 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2676 | |
| 2677 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 2678 | ParamTypes[0] = ArithmeticTypes[Left].withVolatile(); |
| 2679 | ParamTypes[0] = Context.getReferenceType(ParamTypes[0]); |
| 2680 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2681 | } |
| 2682 | } |
| 2683 | break; |
| 2684 | |
| 2685 | case OO_PercentEqual: |
| 2686 | case OO_LessLessEqual: |
| 2687 | case OO_GreaterGreaterEqual: |
| 2688 | case OO_AmpEqual: |
| 2689 | case OO_CaretEqual: |
| 2690 | case OO_PipeEqual: |
| 2691 | // C++ [over.built]p22: |
| 2692 | // |
| 2693 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 2694 | // is either volatile or empty, and R is a promoted integral |
| 2695 | // type, there exist candidate operator functions of the form |
| 2696 | // |
| 2697 | // VQ L& operator%=(VQ L&, R); |
| 2698 | // VQ L& operator<<=(VQ L&, R); |
| 2699 | // VQ L& operator>>=(VQ L&, R); |
| 2700 | // VQ L& operator&=(VQ L&, R); |
| 2701 | // VQ L& operator^=(VQ L&, R); |
| 2702 | // VQ L& operator|=(VQ L&, R); |
| 2703 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 2704 | for (unsigned Right = FirstPromotedIntegralType; |
| 2705 | Right < LastPromotedIntegralType; ++Right) { |
| 2706 | QualType ParamTypes[2]; |
| 2707 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 2708 | |
| 2709 | // Add this built-in operator as a candidate (VQ is empty). |
| 2710 | // FIXME: We should be caching these declarations somewhere, |
| 2711 | // rather than re-building them every time. |
| 2712 | ParamTypes[0] = Context.getReferenceType(ArithmeticTypes[Left]); |
| 2713 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2714 | |
| 2715 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 2716 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 2717 | ParamTypes[0].addVolatile(); |
| 2718 | ParamTypes[0] = Context.getReferenceType(ParamTypes[0]); |
| 2719 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 2720 | } |
| 2721 | } |
| 2722 | break; |
| 2723 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2724 | case OO_Exclaim: { |
| 2725 | // C++ [over.operator]p23: |
| 2726 | // |
| 2727 | // There also exist candidate operator functions of the form |
| 2728 | // |
| 2729 | // bool operator!(bool); |
| 2730 | // bool operator&&(bool, bool); [BELOW] |
| 2731 | // bool operator||(bool, bool); [BELOW] |
| 2732 | QualType ParamTy = Context.BoolTy; |
| 2733 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 2734 | break; |
| 2735 | } |
| 2736 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2737 | case OO_AmpAmp: |
| 2738 | case OO_PipePipe: { |
| 2739 | // C++ [over.operator]p23: |
| 2740 | // |
| 2741 | // There also exist candidate operator functions of the form |
| 2742 | // |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2743 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2744 | // bool operator&&(bool, bool); |
| 2745 | // bool operator||(bool, bool); |
| 2746 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
| 2747 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 2748 | break; |
| 2749 | } |
| 2750 | |
| 2751 | case OO_Subscript: |
| 2752 | // C++ [over.built]p13: |
| 2753 | // |
| 2754 | // For every cv-qualified or cv-unqualified object type T there |
| 2755 | // exist candidate operator functions of the form |
| 2756 | // |
| 2757 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 2758 | // T& operator[](T*, ptrdiff_t); |
| 2759 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 2760 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 2761 | // T& operator[](ptrdiff_t, T*); |
| 2762 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2763 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2764 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 2765 | QualType PointeeType = (*Ptr)->getAsPointerType()->getPointeeType(); |
| 2766 | QualType ResultTy = Context.getReferenceType(PointeeType); |
| 2767 | |
| 2768 | // T& operator[](T*, ptrdiff_t) |
| 2769 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 2770 | |
| 2771 | // T& operator[](ptrdiff_t, T*); |
| 2772 | ParamTypes[0] = ParamTypes[1]; |
| 2773 | ParamTypes[1] = *Ptr; |
| 2774 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 2775 | } |
| 2776 | break; |
| 2777 | |
| 2778 | case OO_ArrowStar: |
| 2779 | // FIXME: No support for pointer-to-members yet. |
| 2780 | break; |
| 2781 | } |
| 2782 | } |
| 2783 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2784 | /// AddOverloadCandidates - Add all of the function overloads in Ovl |
| 2785 | /// to the candidate set. |
| 2786 | void |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2787 | Sema::AddOverloadCandidates(const OverloadedFunctionDecl *Ovl, |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2788 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2789 | OverloadCandidateSet& CandidateSet, |
| 2790 | bool SuppressUserConversions) |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2791 | { |
Douglas Gregor | 5870a95 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 2792 | for (OverloadedFunctionDecl::function_const_iterator Func |
| 2793 | = Ovl->function_begin(); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2794 | Func != Ovl->function_end(); ++Func) |
Douglas Gregor | a3b34bb | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2795 | AddOverloadCandidate(*Func, Args, NumArgs, CandidateSet, |
| 2796 | SuppressUserConversions); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 2800 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
| 2801 | bool |
| 2802 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
| 2803 | const OverloadCandidate& Cand2) |
| 2804 | { |
| 2805 | // Define viable functions to be better candidates than non-viable |
| 2806 | // functions. |
| 2807 | if (!Cand2.Viable) |
| 2808 | return Cand1.Viable; |
| 2809 | else if (!Cand1.Viable) |
| 2810 | return false; |
| 2811 | |
| 2812 | // FIXME: Deal with the implicit object parameter for static member |
| 2813 | // functions. (C++ 13.3.3p1). |
| 2814 | |
| 2815 | // (C++ 13.3.3p1): a viable function F1 is defined to be a better |
| 2816 | // function than another viable function F2 if for all arguments i, |
| 2817 | // ICSi(F1) is not a worse conversion sequence than ICSi(F2), and |
| 2818 | // then... |
| 2819 | unsigned NumArgs = Cand1.Conversions.size(); |
| 2820 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 2821 | bool HasBetterConversion = false; |
| 2822 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2823 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 2824 | Cand2.Conversions[ArgIdx])) { |
| 2825 | case ImplicitConversionSequence::Better: |
| 2826 | // Cand1 has a better conversion sequence. |
| 2827 | HasBetterConversion = true; |
| 2828 | break; |
| 2829 | |
| 2830 | case ImplicitConversionSequence::Worse: |
| 2831 | // Cand1 can't be better than Cand2. |
| 2832 | return false; |
| 2833 | |
| 2834 | case ImplicitConversionSequence::Indistinguishable: |
| 2835 | // Do nothing. |
| 2836 | break; |
| 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | if (HasBetterConversion) |
| 2841 | return true; |
| 2842 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2843 | // FIXME: Several other bullets in (C++ 13.3.3p1) need to be |
| 2844 | // implemented, but they require template support. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2846 | // C++ [over.match.best]p1b4: |
| 2847 | // |
| 2848 | // -- the context is an initialization by user-defined conversion |
| 2849 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 2850 | // from the return type of F1 to the destination type (i.e., |
| 2851 | // the type of the entity being initialized) is a better |
| 2852 | // conversion sequence than the standard conversion sequence |
| 2853 | // from the return type of F2 to the destination type. |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2854 | if (Cand1.Function && Cand2.Function && |
| 2855 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | 60714f9 | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2856 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 2857 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 2858 | Cand2.FinalConversion)) { |
| 2859 | case ImplicitConversionSequence::Better: |
| 2860 | // Cand1 has a better conversion sequence. |
| 2861 | return true; |
| 2862 | |
| 2863 | case ImplicitConversionSequence::Worse: |
| 2864 | // Cand1 can't be better than Cand2. |
| 2865 | return false; |
| 2866 | |
| 2867 | case ImplicitConversionSequence::Indistinguishable: |
| 2868 | // Do nothing |
| 2869 | break; |
| 2870 | } |
| 2871 | } |
| 2872 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2873 | return false; |
| 2874 | } |
| 2875 | |
| 2876 | /// BestViableFunction - Computes the best viable function (C++ 13.3.3) |
| 2877 | /// within an overload candidate set. If overloading is successful, |
| 2878 | /// the result will be OR_Success and Best will be set to point to the |
| 2879 | /// best viable function within the candidate set. Otherwise, one of |
| 2880 | /// several kinds of errors will be returned; see |
| 2881 | /// Sema::OverloadingResult. |
| 2882 | Sema::OverloadingResult |
| 2883 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 2884 | OverloadCandidateSet::iterator& Best) |
| 2885 | { |
| 2886 | // Find the best viable function. |
| 2887 | Best = CandidateSet.end(); |
| 2888 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2889 | Cand != CandidateSet.end(); ++Cand) { |
| 2890 | if (Cand->Viable) { |
| 2891 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 2892 | Best = Cand; |
| 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | // If we didn't find any viable functions, abort. |
| 2897 | if (Best == CandidateSet.end()) |
| 2898 | return OR_No_Viable_Function; |
| 2899 | |
| 2900 | // Make sure that this function is better than every other viable |
| 2901 | // function. If not, we have an ambiguity. |
| 2902 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2903 | Cand != CandidateSet.end(); ++Cand) { |
| 2904 | if (Cand->Viable && |
| 2905 | Cand != Best && |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2906 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 2907 | Best = CandidateSet.end(); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2908 | return OR_Ambiguous; |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2909 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | // Best is the best viable function. |
| 2913 | return OR_Success; |
| 2914 | } |
| 2915 | |
| 2916 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 2917 | /// diagnostic messages containing the candidates in the candidate |
| 2918 | /// set. If OnlyViable is true, only viable candidates will be printed. |
| 2919 | void |
| 2920 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
| 2921 | bool OnlyViable) |
| 2922 | { |
| 2923 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 2924 | LastCand = CandidateSet.end(); |
| 2925 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2926 | if (Cand->Viable || !OnlyViable) { |
| 2927 | if (Cand->Function) { |
| 2928 | // Normal function |
| 2929 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2930 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 30c8ddf | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 2931 | // Desugar the type of the surrogate down to a function type, |
| 2932 | // retaining as many typedefs as possible while still showing |
| 2933 | // the function type (and, therefore, its parameter types). |
| 2934 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 2935 | bool isReference = false; |
| 2936 | bool isPointer = false; |
| 2937 | if (const ReferenceType *FnTypeRef = FnType->getAsReferenceType()) { |
| 2938 | FnType = FnTypeRef->getPointeeType(); |
| 2939 | isReference = true; |
| 2940 | } |
| 2941 | if (const PointerType *FnTypePtr = FnType->getAsPointerType()) { |
| 2942 | FnType = FnTypePtr->getPointeeType(); |
| 2943 | isPointer = true; |
| 2944 | } |
| 2945 | // Desugar down to a function type. |
| 2946 | FnType = QualType(FnType->getAsFunctionType(), 0); |
| 2947 | // Reconstruct the pointer/reference as appropriate. |
| 2948 | if (isPointer) FnType = Context.getPointerType(FnType); |
| 2949 | if (isReference) FnType = Context.getReferenceType(FnType); |
| 2950 | |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2951 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2952 | << FnType; |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2953 | } else { |
| 2954 | // FIXME: We need to get the identifier in here |
| 2955 | // FIXME: Do we want the error message to point at the |
| 2956 | // operator? (built-ins won't have a location) |
| 2957 | QualType FnType |
| 2958 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 2959 | Cand->BuiltinTypes.ParamTypes, |
| 2960 | Cand->Conversions.size(), |
| 2961 | false, 0); |
| 2962 | |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2963 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2964 | } |
| 2965 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2966 | } |
| 2967 | } |
| 2968 | |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 2969 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 2970 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 2971 | /// expression with overloaded function type and @p ToType is the type |
| 2972 | /// we're trying to resolve to. For example: |
| 2973 | /// |
| 2974 | /// @code |
| 2975 | /// int f(double); |
| 2976 | /// int f(int); |
| 2977 | /// |
| 2978 | /// int (*pfd)(double) = f; // selects f(double) |
| 2979 | /// @endcode |
| 2980 | /// |
| 2981 | /// This routine returns the resulting FunctionDecl if it could be |
| 2982 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 2983 | /// routine will emit diagnostics if there is an error. |
| 2984 | FunctionDecl * |
| 2985 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
| 2986 | bool Complain) { |
| 2987 | QualType FunctionType = ToType; |
| 2988 | if (const PointerLikeType *ToTypePtr = ToType->getAsPointerLikeType()) |
| 2989 | FunctionType = ToTypePtr->getPointeeType(); |
| 2990 | |
| 2991 | // We only look at pointers or references to functions. |
| 2992 | if (!FunctionType->isFunctionType()) |
| 2993 | return 0; |
| 2994 | |
| 2995 | // Find the actual overloaded function declaration. |
| 2996 | OverloadedFunctionDecl *Ovl = 0; |
| 2997 | |
| 2998 | // C++ [over.over]p1: |
| 2999 | // [...] [Note: any redundant set of parentheses surrounding the |
| 3000 | // overloaded function name is ignored (5.1). ] |
| 3001 | Expr *OvlExpr = From->IgnoreParens(); |
| 3002 | |
| 3003 | // C++ [over.over]p1: |
| 3004 | // [...] The overloaded function name can be preceded by the & |
| 3005 | // operator. |
| 3006 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 3007 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 3008 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 3009 | } |
| 3010 | |
| 3011 | // Try to dig out the overloaded function. |
| 3012 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) |
| 3013 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
| 3014 | |
| 3015 | // If there's no overloaded function declaration, we're done. |
| 3016 | if (!Ovl) |
| 3017 | return 0; |
| 3018 | |
| 3019 | // Look through all of the overloaded functions, searching for one |
| 3020 | // whose type matches exactly. |
| 3021 | // FIXME: When templates or using declarations come along, we'll actually |
| 3022 | // have to deal with duplicates, partial ordering, etc. For now, we |
| 3023 | // can just do a simple search. |
| 3024 | FunctionType = Context.getCanonicalType(FunctionType.getUnqualifiedType()); |
| 3025 | for (OverloadedFunctionDecl::function_iterator Fun = Ovl->function_begin(); |
| 3026 | Fun != Ovl->function_end(); ++Fun) { |
| 3027 | // C++ [over.over]p3: |
| 3028 | // Non-member functions and static member functions match |
| 3029 | // targets of type “pointer-to-function”or |
| 3030 | // “reference-to-function.” |
| 3031 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) |
| 3032 | if (!Method->isStatic()) |
| 3033 | continue; |
| 3034 | |
| 3035 | if (FunctionType == Context.getCanonicalType((*Fun)->getType())) |
| 3036 | return *Fun; |
| 3037 | } |
| 3038 | |
| 3039 | return 0; |
| 3040 | } |
| 3041 | |
Douglas Gregor | 3ed006b | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3042 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
| 3043 | /// (which eventually refers to the set of overloaded functions in |
| 3044 | /// Ovl) and the call arguments Args/NumArgs, attempt to resolve the |
| 3045 | /// function call down to a specific function. If overload resolution |
Douglas Gregor | bf4f058 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3046 | /// succeeds, returns the function declaration produced by overload |
| 3047 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 3ed006b | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3048 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | bf4f058 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3049 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, OverloadedFunctionDecl *Ovl, |
| 3050 | SourceLocation LParenLoc, |
| 3051 | Expr **Args, unsigned NumArgs, |
| 3052 | SourceLocation *CommaLocs, |
| 3053 | SourceLocation RParenLoc) { |
Douglas Gregor | 3ed006b | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3054 | OverloadCandidateSet CandidateSet; |
| 3055 | AddOverloadCandidates(Ovl, Args, NumArgs, CandidateSet); |
| 3056 | OverloadCandidateSet::iterator Best; |
| 3057 | switch (BestViableFunction(CandidateSet, Best)) { |
Douglas Gregor | bf4f058 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3058 | case OR_Success: |
| 3059 | return Best->Function; |
Douglas Gregor | 3ed006b | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3060 | |
| 3061 | case OR_No_Viable_Function: |
| 3062 | Diag(Fn->getSourceRange().getBegin(), |
| 3063 | diag::err_ovl_no_viable_function_in_call) |
| 3064 | << Ovl->getDeclName() << (unsigned)CandidateSet.size() |
| 3065 | << Fn->getSourceRange(); |
| 3066 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 3067 | break; |
| 3068 | |
| 3069 | case OR_Ambiguous: |
| 3070 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
| 3071 | << Ovl->getDeclName() << Fn->getSourceRange(); |
| 3072 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3073 | break; |
| 3074 | } |
| 3075 | |
| 3076 | // Overload resolution failed. Destroy all of the subexpressions and |
| 3077 | // return NULL. |
| 3078 | Fn->Destroy(Context); |
| 3079 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 3080 | Args[Arg]->Destroy(Context); |
| 3081 | return 0; |
| 3082 | } |
| 3083 | |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 3084 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 3085 | /// type (C++ [over.call.object]), which can end up invoking an |
| 3086 | /// overloaded function call operator (@c operator()) or performing a |
| 3087 | /// user-defined conversion on the object argument. |
| 3088 | Action::ExprResult |
| 3089 | Sema::BuildCallToObjectOfClassType(Expr *Object, SourceLocation LParenLoc, |
| 3090 | Expr **Args, unsigned NumArgs, |
| 3091 | SourceLocation *CommaLocs, |
| 3092 | SourceLocation RParenLoc) { |
| 3093 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
| 3094 | const RecordType *Record = Object->getType()->getAsRecordType(); |
| 3095 | |
| 3096 | // C++ [over.call.object]p1: |
| 3097 | // If the primary-expression E in the function call syntax |
| 3098 | // evaluates to a class object of type “cv T”, then the set of |
| 3099 | // candidate functions includes at least the function call |
| 3100 | // operators of T. The function call operators of T are obtained by |
| 3101 | // ordinary lookup of the name operator() in the context of |
| 3102 | // (E).operator(). |
| 3103 | OverloadCandidateSet CandidateSet; |
| 3104 | IdentifierResolver::iterator I |
| 3105 | = IdResolver.begin(Context.DeclarationNames.getCXXOperatorName(OO_Call), |
| 3106 | cast<CXXRecordType>(Record)->getDecl(), |
| 3107 | /*LookInParentCtx=*/false); |
| 3108 | NamedDecl *MemberOps = (I == IdResolver.end())? 0 : *I; |
| 3109 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(MemberOps)) |
| 3110 | AddMethodCandidate(Method, Object, Args, NumArgs, CandidateSet, |
| 3111 | /*SuppressUserConversions=*/false); |
| 3112 | else if (OverloadedFunctionDecl *Ovl |
| 3113 | = dyn_cast_or_null<OverloadedFunctionDecl>(MemberOps)) { |
| 3114 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 3115 | FEnd = Ovl->function_end(); |
| 3116 | F != FEnd; ++F) { |
| 3117 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*F)) |
| 3118 | AddMethodCandidate(Method, Object, Args, NumArgs, CandidateSet, |
| 3119 | /*SuppressUserConversions=*/false); |
| 3120 | } |
| 3121 | } |
| 3122 | |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3123 | // C++ [over.call.object]p2: |
| 3124 | // In addition, for each conversion function declared in T of the |
| 3125 | // form |
| 3126 | // |
| 3127 | // operator conversion-type-id () cv-qualifier; |
| 3128 | // |
| 3129 | // where cv-qualifier is the same cv-qualification as, or a |
| 3130 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | 261afa7 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 3131 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 3132 | // R", or the type "reference to pointer to function of |
| 3133 | // (P1,...,Pn) returning R", or the type "reference to function |
| 3134 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3135 | // is also considered as a candidate function. Similarly, |
| 3136 | // surrogate call functions are added to the set of candidate |
| 3137 | // functions for each conversion function declared in an |
| 3138 | // accessible base class provided the function is not hidden |
| 3139 | // within T by another intervening declaration. |
| 3140 | // |
| 3141 | // FIXME: Look in base classes for more conversion operators! |
| 3142 | OverloadedFunctionDecl *Conversions |
| 3143 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Douglas Gregor | 30c8ddf | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3144 | for (OverloadedFunctionDecl::function_iterator |
| 3145 | Func = Conversions->function_begin(), |
| 3146 | FuncEnd = Conversions->function_end(); |
| 3147 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3148 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 3149 | |
| 3150 | // Strip the reference type (if any) and then the pointer type (if |
| 3151 | // any) to get down to what might be a function type. |
| 3152 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 3153 | if (const PointerType *ConvPtrType = ConvType->getAsPointerType()) |
| 3154 | ConvType = ConvPtrType->getPointeeType(); |
| 3155 | |
| 3156 | if (const FunctionTypeProto *Proto = ConvType->getAsFunctionTypeProto()) |
| 3157 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 3158 | } |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 3159 | |
| 3160 | // Perform overload resolution. |
| 3161 | OverloadCandidateSet::iterator Best; |
| 3162 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3163 | case OR_Success: |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3164 | // Overload resolution succeeded; we'll build the appropriate call |
| 3165 | // below. |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 3166 | break; |
| 3167 | |
| 3168 | case OR_No_Viable_Function: |
Sebastian Redl | fd9f2ac | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 3169 | Diag(Object->getSourceRange().getBegin(), |
| 3170 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3171 | << Object->getType() << (unsigned)CandidateSet.size() |
Sebastian Redl | fd9f2ac | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 3172 | << Object->getSourceRange(); |
| 3173 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 3174 | break; |
| 3175 | |
| 3176 | case OR_Ambiguous: |
| 3177 | Diag(Object->getSourceRange().getBegin(), |
| 3178 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3179 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 3180 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3181 | break; |
| 3182 | } |
| 3183 | |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3184 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 3185 | // We had an error; delete all of the subexpressions and return |
| 3186 | // the error. |
| 3187 | delete Object; |
| 3188 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3189 | delete Args[ArgIdx]; |
| 3190 | return true; |
| 3191 | } |
| 3192 | |
Douglas Gregor | 67fdb5b | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3193 | if (Best->Function == 0) { |
| 3194 | // Since there is no function declaration, this is one of the |
| 3195 | // surrogate candidates. Dig out the conversion function. |
| 3196 | CXXConversionDecl *Conv |
| 3197 | = cast<CXXConversionDecl>( |
| 3198 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 3199 | |
| 3200 | // We selected one of the surrogate functions that converts the |
| 3201 | // object parameter to a function pointer. Perform the conversion |
| 3202 | // on the object argument, then let ActOnCallExpr finish the job. |
| 3203 | // FIXME: Represent the user-defined conversion in the AST! |
| 3204 | ImpCastExprToType(Object, |
| 3205 | Conv->getConversionType().getNonReferenceType(), |
| 3206 | Conv->getConversionType()->isReferenceType()); |
| 3207 | return ActOnCallExpr((ExprTy*)Object, LParenLoc, (ExprTy**)Args, NumArgs, |
| 3208 | CommaLocs, RParenLoc); |
| 3209 | } |
| 3210 | |
| 3211 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 3212 | // that calls this method, using Object for the implicit object |
| 3213 | // parameter and passing along the remaining arguments. |
| 3214 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 10f3c50 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 3215 | const FunctionTypeProto *Proto = Method->getType()->getAsFunctionTypeProto(); |
| 3216 | |
| 3217 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3218 | unsigned NumArgsToCheck = NumArgs; |
| 3219 | |
| 3220 | // Build the full argument list for the method call (the |
| 3221 | // implicit object parameter is placed at the beginning of the |
| 3222 | // list). |
| 3223 | Expr **MethodArgs; |
| 3224 | if (NumArgs < NumArgsInProto) { |
| 3225 | NumArgsToCheck = NumArgsInProto; |
| 3226 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 3227 | } else { |
| 3228 | MethodArgs = new Expr*[NumArgs + 1]; |
| 3229 | } |
| 3230 | MethodArgs[0] = Object; |
| 3231 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3232 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
| 3233 | |
| 3234 | Expr *NewFn = new DeclRefExpr(Method, Method->getType(), |
| 3235 | SourceLocation()); |
| 3236 | UsualUnaryConversions(NewFn); |
| 3237 | |
| 3238 | // Once we've built TheCall, all of the expressions are properly |
| 3239 | // owned. |
| 3240 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 3241 | llvm::OwningPtr<CXXOperatorCallExpr> |
| 3242 | TheCall(new CXXOperatorCallExpr(NewFn, MethodArgs, NumArgs + 1, |
| 3243 | ResultTy, RParenLoc)); |
| 3244 | delete [] MethodArgs; |
| 3245 | |
| 3246 | // Initialize the implicit object parameter. |
| 3247 | if (!PerformObjectArgumentInitialization(Object, Method)) |
| 3248 | return true; |
| 3249 | TheCall->setArg(0, Object); |
| 3250 | |
| 3251 | // Check the argument types. |
| 3252 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 3253 | QualType ProtoArgType = Proto->getArgType(i); |
| 3254 | |
| 3255 | Expr *Arg; |
| 3256 | if (i < NumArgs) |
| 3257 | Arg = Args[i]; |
| 3258 | else |
| 3259 | Arg = new CXXDefaultArgExpr(Method->getParamDecl(i)); |
| 3260 | QualType ArgType = Arg->getType(); |
| 3261 | |
| 3262 | // Pass the argument. |
| 3263 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 3264 | return true; |
| 3265 | |
| 3266 | TheCall->setArg(i + 1, Arg); |
| 3267 | } |
| 3268 | |
| 3269 | // If this is a variadic call, handle args passed through "...". |
| 3270 | if (Proto->isVariadic()) { |
| 3271 | // Promote the arguments (C99 6.5.2.2p7). |
| 3272 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 3273 | Expr *Arg = Args[i]; |
| 3274 | DefaultArgumentPromotion(Arg); |
| 3275 | TheCall->setArg(i + 1, Arg); |
| 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | return CheckFunctionCall(Method, TheCall.take()); |
| 3280 | } |
| 3281 | |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3282 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
| 3283 | /// (if one exists), where @c Base is an expression of class type and |
| 3284 | /// @c Member is the name of the member we're trying to find. |
| 3285 | Action::ExprResult |
| 3286 | Sema::BuildOverloadedArrowExpr(Expr *Base, SourceLocation OpLoc, |
| 3287 | SourceLocation MemberLoc, |
| 3288 | IdentifierInfo &Member) { |
| 3289 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
| 3290 | |
| 3291 | // C++ [over.ref]p1: |
| 3292 | // |
| 3293 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 3294 | // for a class object x of type T if T::operator->() exists and if |
| 3295 | // the operator is selected as the best match function by the |
| 3296 | // overload resolution mechanism (13.3). |
| 3297 | // FIXME: look in base classes. |
| 3298 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 3299 | OverloadCandidateSet CandidateSet; |
| 3300 | const RecordType *BaseRecord = Base->getType()->getAsRecordType(); |
| 3301 | IdentifierResolver::iterator I |
| 3302 | = IdResolver.begin(OpName, cast<CXXRecordType>(BaseRecord)->getDecl(), |
| 3303 | /*LookInParentCtx=*/false); |
| 3304 | NamedDecl *MemberOps = (I == IdResolver.end())? 0 : *I; |
| 3305 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(MemberOps)) |
| 3306 | AddMethodCandidate(Method, Base, 0, 0, CandidateSet, |
| 3307 | /*SuppressUserConversions=*/false); |
| 3308 | else if (OverloadedFunctionDecl *Ovl |
| 3309 | = dyn_cast_or_null<OverloadedFunctionDecl>(MemberOps)) { |
| 3310 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 3311 | FEnd = Ovl->function_end(); |
| 3312 | F != FEnd; ++F) { |
| 3313 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*F)) |
| 3314 | AddMethodCandidate(Method, Base, 0, 0, CandidateSet, |
| 3315 | /*SuppressUserConversions=*/false); |
| 3316 | } |
| 3317 | } |
| 3318 | |
Douglas Gregor | 9c690e9 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 3319 | llvm::OwningPtr<Expr> BasePtr(Base); |
| 3320 | |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3321 | // Perform overload resolution. |
| 3322 | OverloadCandidateSet::iterator Best; |
| 3323 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3324 | case OR_Success: |
| 3325 | // Overload resolution succeeded; we'll build the call below. |
| 3326 | break; |
| 3327 | |
| 3328 | case OR_No_Viable_Function: |
| 3329 | if (CandidateSet.empty()) |
| 3330 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3331 | << BasePtr->getType() << BasePtr->getSourceRange(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3332 | else |
| 3333 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Sebastian Redl | fd9f2ac | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 3334 | << "operator->" << (unsigned)CandidateSet.size() |
| 3335 | << BasePtr->getSourceRange(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3336 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3337 | return true; |
| 3338 | |
| 3339 | case OR_Ambiguous: |
| 3340 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3341 | << "operator->" << BasePtr->getSourceRange(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3342 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3343 | return true; |
| 3344 | } |
| 3345 | |
| 3346 | // Convert the object parameter. |
| 3347 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 9c690e9 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 3348 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3349 | return true; |
Douglas Gregor | 9c690e9 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 3350 | |
| 3351 | // No concerns about early exits now. |
| 3352 | BasePtr.take(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Build the operator call. |
| 3355 | Expr *FnExpr = new DeclRefExpr(Method, Method->getType(), SourceLocation()); |
| 3356 | UsualUnaryConversions(FnExpr); |
| 3357 | Base = new CXXOperatorCallExpr(FnExpr, &Base, 1, |
| 3358 | Method->getResultType().getNonReferenceType(), |
| 3359 | OpLoc); |
| 3360 | return ActOnMemberReferenceExpr(Base, OpLoc, tok::arrow, MemberLoc, Member); |
| 3361 | } |
| 3362 | |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3363 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 3364 | /// a C++ overloaded function (possibly with some parentheses and |
| 3365 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 3366 | /// to the function declaration Fn, so patch up the expression E to |
| 3367 | /// refer (possibly indirectly) to Fn. |
| 3368 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 3369 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 3370 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 3371 | E->setType(PE->getSubExpr()->getType()); |
| 3372 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
| 3373 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
| 3374 | "Can only take the address of an overloaded function"); |
| 3375 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 3376 | E->setType(Context.getPointerType(E->getType())); |
| 3377 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
| 3378 | assert(isa<OverloadedFunctionDecl>(DR->getDecl()) && |
| 3379 | "Expected overloaded function"); |
| 3380 | DR->setDecl(Fn); |
| 3381 | E->setType(Fn->getType()); |
| 3382 | } else { |
| 3383 | assert(false && "Invalid reference to overloaded function"); |
| 3384 | } |
| 3385 | } |
| 3386 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3387 | } // end namespace clang |