Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include <algorithm> |
| 26 | |
| 27 | namespace clang { |
| 28 | |
| 29 | /// GetConversionCategory - Retrieve the implicit conversion |
| 30 | /// category corresponding to the given implicit conversion kind. |
| 31 | ImplicitConversionCategory |
| 32 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 33 | static const ImplicitConversionCategory |
| 34 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 35 | ICC_Identity, |
| 36 | ICC_Lvalue_Transformation, |
| 37 | ICC_Lvalue_Transformation, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Qualification_Adjustment, |
| 40 | ICC_Promotion, |
| 41 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 42 | ICC_Promotion, |
| 43 | ICC_Conversion, |
| 44 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 45 | ICC_Conversion, |
| 46 | ICC_Conversion, |
| 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 50 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 51 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 52 | ICC_Conversion |
| 53 | }; |
| 54 | return Category[(int)Kind]; |
| 55 | } |
| 56 | |
| 57 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 58 | /// corresponding to the given implicit conversion kind. |
| 59 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 60 | static const ImplicitConversionRank |
| 61 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 62 | ICR_Exact_Match, |
| 63 | ICR_Exact_Match, |
| 64 | ICR_Exact_Match, |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Promotion, |
| 68 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 69 | ICR_Promotion, |
| 70 | ICR_Conversion, |
| 71 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 72 | ICR_Conversion, |
| 73 | ICR_Conversion, |
| 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 77 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 78 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 79 | ICR_Conversion |
| 80 | }; |
| 81 | return Rank[(int)Kind]; |
| 82 | } |
| 83 | |
| 84 | /// GetImplicitConversionName - Return the name of this kind of |
| 85 | /// implicit conversion. |
| 86 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 87 | static const char* Name[(int)ICK_Num_Conversion_Kinds] = { |
| 88 | "No conversion", |
| 89 | "Lvalue-to-rvalue", |
| 90 | "Array-to-pointer", |
| 91 | "Function-to-pointer", |
| 92 | "Qualification", |
| 93 | "Integral promotion", |
| 94 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 95 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 96 | "Integral conversion", |
| 97 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 98 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 99 | "Floating-integral conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex-real conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Pointer conversion", |
| 102 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 103 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 104 | "Compatible-types conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | }; |
| 107 | return Name[Kind]; |
| 108 | } |
| 109 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 110 | /// StandardConversionSequence - Set the standard conversion |
| 111 | /// sequence to the identity conversion. |
| 112 | void StandardConversionSequence::setAsIdentityConversion() { |
| 113 | First = ICK_Identity; |
| 114 | Second = ICK_Identity; |
| 115 | Third = ICK_Identity; |
| 116 | Deprecated = false; |
| 117 | ReferenceBinding = false; |
| 118 | DirectBinding = false; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 119 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 120 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 123 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 124 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 125 | /// implicit conversions. |
| 126 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 127 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 128 | if (GetConversionRank(First) > Rank) |
| 129 | Rank = GetConversionRank(First); |
| 130 | if (GetConversionRank(Second) > Rank) |
| 131 | Rank = GetConversionRank(Second); |
| 132 | if (GetConversionRank(Third) > Rank) |
| 133 | Rank = GetConversionRank(Third); |
| 134 | return Rank; |
| 135 | } |
| 136 | |
| 137 | /// isPointerConversionToBool - Determines whether this conversion is |
| 138 | /// a conversion of a pointer or pointer-to-member to bool. This is |
| 139 | /// used as part of the ranking of standard conversion sequences |
| 140 | /// (C++ 13.3.3.2p4). |
| 141 | bool StandardConversionSequence::isPointerConversionToBool() const |
| 142 | { |
| 143 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 144 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 145 | |
| 146 | // Note that FromType has not necessarily been transformed by the |
| 147 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 148 | // check for their presence as well as checking whether FromType is |
| 149 | // a pointer. |
| 150 | if (ToType->isBooleanType() && |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 151 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 152 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 153 | return true; |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 158 | /// isPointerConversionToVoidPointer - Determines whether this |
| 159 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 160 | /// used as part of the ranking of standard conversion sequences (C++ |
| 161 | /// 13.3.3.2p4). |
| 162 | bool |
| 163 | StandardConversionSequence:: |
| 164 | isPointerConversionToVoidPointer(ASTContext& Context) const |
| 165 | { |
| 166 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 167 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
| 175 | if (Second == ICK_Pointer_Conversion) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
| 185 | bool PrintedSomething = false; |
| 186 | if (First != ICK_Identity) { |
| 187 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 188 | PrintedSomething = true; |
| 189 | } |
| 190 | |
| 191 | if (Second != ICK_Identity) { |
| 192 | if (PrintedSomething) { |
| 193 | fprintf(stderr, " -> "); |
| 194 | } |
| 195 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 196 | |
| 197 | if (CopyConstructor) { |
| 198 | fprintf(stderr, " (by copy constructor)"); |
| 199 | } else if (DirectBinding) { |
| 200 | fprintf(stderr, " (direct reference binding)"); |
| 201 | } else if (ReferenceBinding) { |
| 202 | fprintf(stderr, " (reference binding)"); |
| 203 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | PrintedSomething = true; |
| 205 | } |
| 206 | |
| 207 | if (Third != ICK_Identity) { |
| 208 | if (PrintedSomething) { |
| 209 | fprintf(stderr, " -> "); |
| 210 | } |
| 211 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 212 | PrintedSomething = true; |
| 213 | } |
| 214 | |
| 215 | if (!PrintedSomething) { |
| 216 | fprintf(stderr, "No conversions required"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 221 | /// error. Useful for debugging overloading issues. |
| 222 | void UserDefinedConversionSequence::DebugPrint() const { |
| 223 | if (Before.First || Before.Second || Before.Third) { |
| 224 | Before.DebugPrint(); |
| 225 | fprintf(stderr, " -> "); |
| 226 | } |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 227 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | if (After.First || After.Second || After.Third) { |
| 229 | fprintf(stderr, " -> "); |
| 230 | After.DebugPrint(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 235 | /// error. Useful for debugging overloading issues. |
| 236 | void ImplicitConversionSequence::DebugPrint() const { |
| 237 | switch (ConversionKind) { |
| 238 | case StandardConversion: |
| 239 | fprintf(stderr, "Standard conversion: "); |
| 240 | Standard.DebugPrint(); |
| 241 | break; |
| 242 | case UserDefinedConversion: |
| 243 | fprintf(stderr, "User-defined conversion: "); |
| 244 | UserDefined.DebugPrint(); |
| 245 | break; |
| 246 | case EllipsisConversion: |
| 247 | fprintf(stderr, "Ellipsis conversion"); |
| 248 | break; |
| 249 | case BadConversion: |
| 250 | fprintf(stderr, "Bad conversion"); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | fprintf(stderr, "\n"); |
| 255 | } |
| 256 | |
| 257 | // IsOverload - Determine whether the given New declaration is an |
| 258 | // overload of the Old declaration. This routine returns false if New |
| 259 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 260 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 261 | // function (or overload set). When it does return false and Old is an |
| 262 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
| 263 | // FunctionDecl that New cannot be overloaded with. |
| 264 | // |
| 265 | // Example: Given the following input: |
| 266 | // |
| 267 | // void f(int, float); // #1 |
| 268 | // void f(int, int); // #2 |
| 269 | // int f(int, int); // #3 |
| 270 | // |
| 271 | // When we process #1, there is no previous declaration of "f", |
| 272 | // so IsOverload will not be used. |
| 273 | // |
| 274 | // When we process #2, Old is a FunctionDecl for #1. By comparing the |
| 275 | // parameter types, we see that #1 and #2 are overloaded (since they |
| 276 | // have different signatures), so this routine returns false; |
| 277 | // MatchedDecl is unchanged. |
| 278 | // |
| 279 | // When we process #3, Old is an OverloadedFunctionDecl containing #1 |
| 280 | // and #2. We compare the signatures of #3 to #1 (they're overloaded, |
| 281 | // so we do nothing) and then #3 to #2. Since the signatures of #3 and |
| 282 | // #2 are identical (return types of functions are not part of the |
| 283 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 284 | // point to the FunctionDecl for #2. |
| 285 | bool |
| 286 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 287 | OverloadedFunctionDecl::function_iterator& MatchedDecl) |
| 288 | { |
| 289 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 290 | // Is this new function an overload of every function in the |
| 291 | // overload set? |
| 292 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 293 | FuncEnd = Ovl->function_end(); |
| 294 | for (; Func != FuncEnd; ++Func) { |
| 295 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 296 | MatchedDecl = Func; |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // This function overloads every function in the overload set. |
| 302 | return true; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 303 | } else if (FunctionTemplateDecl *Old = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 304 | return IsOverload(New, Old->getTemplatedDecl(), MatchedDecl); |
| 305 | else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 306 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 307 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 308 | |
| 309 | // C++ [temp.fct]p2: |
| 310 | // A function template can be overloaded with other function templates |
| 311 | // and with normal (non-template) functions. |
| 312 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 313 | return true; |
| 314 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 315 | // Is the function New an overload of the function Old? |
| 316 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 317 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 318 | |
| 319 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 320 | // determine whether they are overloads. If we find any mismatch |
| 321 | // in the signature, they are overloads. |
| 322 | |
| 323 | // If either of these functions is a K&R-style function (no |
| 324 | // prototype), then we consider them to have matching signatures. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 325 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 326 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 327 | return false; |
| 328 | |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 329 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 330 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 331 | |
| 332 | // The signature of a function includes the types of its |
| 333 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 334 | // of the ellipsis; see C++ DR 357). |
| 335 | if (OldQType != NewQType && |
| 336 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 337 | OldType->isVariadic() != NewType->isVariadic() || |
| 338 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 339 | NewType->arg_type_begin()))) |
| 340 | return true; |
| 341 | |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 342 | // C++ [temp.over.link]p4: |
| 343 | // The signature of a function template consists of its function |
| 344 | // signature, its return type and its template parameter list. The names |
| 345 | // of the template parameters are significant only for establishing the |
| 346 | // relationship between the template parameters and the rest of the |
| 347 | // signature. |
| 348 | // |
| 349 | // We check the return type and template parameter lists for function |
| 350 | // templates first; the remaining checks follow. |
| 351 | if (NewTemplate && |
| 352 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 353 | OldTemplate->getTemplateParameters(), |
| 354 | false, false, SourceLocation()) || |
| 355 | OldType->getResultType() != NewType->getResultType())) |
| 356 | return true; |
| 357 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 358 | // If the function is a class member, its signature includes the |
| 359 | // cv-qualifiers (if any) on the function itself. |
| 360 | // |
| 361 | // As part of this, also check whether one of the member functions |
| 362 | // is static, in which case they are not overloads (C++ |
| 363 | // 13.1p2). While not part of the definition of the signature, |
| 364 | // this check is important to determine whether these functions |
| 365 | // can be overloaded. |
| 366 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 367 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 368 | if (OldMethod && NewMethod && |
| 369 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 370 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 371 | return true; |
| 372 | |
| 373 | // The signatures match; this is not an overload. |
| 374 | return false; |
| 375 | } else { |
| 376 | // (C++ 13p1): |
| 377 | // Only function declarations can be overloaded; object and type |
| 378 | // declarations cannot be overloaded. |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 383 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 384 | /// from the given expression (Expr) to the given type (ToType). This |
| 385 | /// function returns an implicit conversion sequence that can be used |
| 386 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 387 | /// |
| 388 | /// void f(float f); |
| 389 | /// void g(int i) { f(i); } |
| 390 | /// |
| 391 | /// this routine would produce an implicit conversion sequence to |
| 392 | /// describe the initialization of f from i, which will be a standard |
| 393 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 394 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 395 | // |
| 396 | /// Note that this routine only determines how the conversion can be |
| 397 | /// performed; it does not actually perform the conversion. As such, |
| 398 | /// it will not produce any diagnostics if no conversion is available, |
| 399 | /// but will instead return an implicit conversion sequence of kind |
| 400 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 401 | /// |
| 402 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 403 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 404 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 405 | /// permitted. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 406 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 407 | /// no matter its actual lvalueness. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 408 | ImplicitConversionSequence |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 409 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 410 | bool SuppressUserConversions, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 411 | bool AllowExplicit, bool ForceRValue) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | { |
| 413 | ImplicitConversionSequence ICS; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 414 | if (IsStandardConversion(From, ToType, ICS.Standard)) |
| 415 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 416 | else if (getLangOptions().CPlusPlus && |
| 417 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 418 | !SuppressUserConversions, AllowExplicit, |
| 419 | ForceRValue)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 420 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 421 | // C++ [over.ics.user]p4: |
| 422 | // A conversion of an expression of class type to the same class |
| 423 | // type is given Exact Match rank, and a conversion of an |
| 424 | // expression of class type to a base class of that type is |
| 425 | // given Conversion rank, in spite of the fact that a copy |
| 426 | // constructor (i.e., a user-defined conversion function) is |
| 427 | // called for those cases. |
| 428 | if (CXXConstructorDecl *Constructor |
| 429 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 430 | QualType FromCanon |
| 431 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 432 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 433 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 434 | // Turn this into a "standard" conversion sequence, so that it |
| 435 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 436 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 437 | ICS.Standard.setAsIdentityConversion(); |
| 438 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 439 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 440 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 441 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 442 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 443 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 444 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 445 | |
| 446 | // C++ [over.best.ics]p4: |
| 447 | // However, when considering the argument of a user-defined |
| 448 | // conversion function that is a candidate by 13.3.1.3 when |
| 449 | // invoked for the copying of the temporary in the second step |
| 450 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 451 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 452 | // ellipsis conversion sequences are allowed. |
| 453 | if (SuppressUserConversions && |
| 454 | ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) |
| 455 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 456 | } else |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 457 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 458 | |
| 459 | return ICS; |
| 460 | } |
| 461 | |
| 462 | /// IsStandardConversion - Determines whether there is a standard |
| 463 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 464 | /// expression From to the type ToType. Standard conversion sequences |
| 465 | /// only consider non-class types; for conversions that involve class |
| 466 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 467 | /// contain the standard conversion sequence required to perform this |
| 468 | /// conversion and this routine will return true. Otherwise, this |
| 469 | /// routine will return false and the value of SCS is unspecified. |
| 470 | bool |
| 471 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
| 472 | StandardConversionSequence &SCS) |
| 473 | { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | QualType FromType = From->getType(); |
| 475 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 476 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 477 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 478 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 479 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 480 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 481 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 483 | // There are no standard conversions for class types in C++, so |
| 484 | // abort early. When overloading in C, however, we do permit |
| 485 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 486 | if (getLangOptions().CPlusPlus) |
| 487 | return false; |
| 488 | |
| 489 | // When we're overloading in C, we allow, as standard conversions, |
| 490 | } |
| 491 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 492 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 493 | // array-to-pointer conversion, or function-to-pointer conversion |
| 494 | // (C++ 4p1). |
| 495 | |
| 496 | // Lvalue-to-rvalue conversion (C++ 4.1): |
| 497 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 498 | // converted to an rvalue. |
| 499 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
| 500 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 501 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 502 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 503 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | |
| 505 | // If T is a non-class type, the type of the rvalue is the |
| 506 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 507 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 508 | // just strip the qualifiers because they don't matter. |
| 509 | |
| 510 | // FIXME: Doesn't see through to qualifiers behind a typedef! |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 511 | FromType = FromType.getUnqualifiedType(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 512 | } |
| 513 | // Array-to-pointer conversion (C++ 4.2) |
| 514 | else if (FromType->isArrayType()) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 515 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 516 | |
| 517 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 518 | // bound of T" can be converted to an rvalue of type "pointer to |
| 519 | // T" (C++ 4.2p1). |
| 520 | FromType = Context.getArrayDecayedType(FromType); |
| 521 | |
| 522 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 523 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 524 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 525 | |
| 526 | // For the purpose of ranking in overload resolution |
| 527 | // (13.3.3.1.1), this conversion is considered an |
| 528 | // array-to-pointer conversion followed by a qualification |
| 529 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 530 | SCS.Second = ICK_Identity; |
| 531 | SCS.Third = ICK_Qualification; |
| 532 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 533 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | // Function-to-pointer conversion (C++ 4.3). |
| 537 | else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 538 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 539 | |
| 540 | // An lvalue of function type T can be converted to an rvalue of |
| 541 | // type "pointer to T." The result is a pointer to the |
| 542 | // function. (C++ 4.3p1). |
| 543 | FromType = Context.getPointerType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 544 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 545 | // Address of overloaded function (C++ [over.over]). |
| 546 | else if (FunctionDecl *Fn |
| 547 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
| 548 | SCS.First = ICK_Function_To_Pointer; |
| 549 | |
| 550 | // We were able to resolve the address of the overloaded function, |
| 551 | // so we can convert to the type of that function. |
| 552 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 553 | if (ToType->isLValueReferenceType()) |
| 554 | FromType = Context.getLValueReferenceType(FromType); |
| 555 | else if (ToType->isRValueReferenceType()) |
| 556 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 557 | else if (ToType->isMemberPointerType()) { |
| 558 | // Resolve address only succeeds if both sides are member pointers, |
| 559 | // but it doesn't have to be the same class. See DR 247. |
| 560 | // Note that this means that the type of &Derived::fn can be |
| 561 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 562 | // base class, even if it was brought into the derived class via a |
| 563 | // using declaration. The standard isn't clear on this issue at all. |
| 564 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 565 | FromType = Context.getMemberPointerType(FromType, |
| 566 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 567 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 568 | FromType = Context.getPointerType(FromType); |
| 569 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 570 | // We don't require any conversions for the first step. |
| 571 | else { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 572 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // The second conversion can be an integral promotion, floating |
| 576 | // point promotion, integral conversion, floating point conversion, |
| 577 | // floating-integral conversion, pointer conversion, |
| 578 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 579 | // For overloading in C, this can also be a "compatible-type" |
| 580 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 581 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 582 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 583 | // The unqualified versions of the types are the same: there's no |
| 584 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 585 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 586 | } |
| 587 | // Integral promotion (C++ 4.5). |
| 588 | else if (IsIntegralPromotion(From, FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 589 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 590 | FromType = ToType.getUnqualifiedType(); |
| 591 | } |
| 592 | // Floating point promotion (C++ 4.6). |
| 593 | else if (IsFloatingPointPromotion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 594 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 595 | FromType = ToType.getUnqualifiedType(); |
| 596 | } |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 597 | // Complex promotion (Clang extension) |
| 598 | else if (IsComplexPromotion(FromType, ToType)) { |
| 599 | SCS.Second = ICK_Complex_Promotion; |
| 600 | FromType = ToType.getUnqualifiedType(); |
| 601 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 602 | // Integral conversions (C++ 4.7). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 603 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 604 | else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 605 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 606 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 607 | FromType = ToType.getUnqualifiedType(); |
| 608 | } |
| 609 | // Floating point conversions (C++ 4.8). |
| 610 | else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 611 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 612 | FromType = ToType.getUnqualifiedType(); |
| 613 | } |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 614 | // Complex conversions (C99 6.3.1.6) |
| 615 | else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 616 | SCS.Second = ICK_Complex_Conversion; |
| 617 | FromType = ToType.getUnqualifiedType(); |
| 618 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 619 | // Floating-integral conversions (C++ 4.9). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 620 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 621 | else if ((FromType->isFloatingType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 622 | ToType->isIntegralType() && !ToType->isBooleanType() && |
| 623 | !ToType->isEnumeralType()) || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 624 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
| 625 | ToType->isFloatingType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 626 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 627 | FromType = ToType.getUnqualifiedType(); |
| 628 | } |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 629 | // Complex-real conversions (C99 6.3.1.7) |
| 630 | else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 631 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 632 | SCS.Second = ICK_Complex_Real; |
| 633 | FromType = ToType.getUnqualifiedType(); |
| 634 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 635 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 636 | else if (IsPointerConversion(From, FromType, ToType, FromType, |
| 637 | IncompatibleObjC)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 638 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 639 | SCS.IncompatibleObjC = IncompatibleObjC; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 640 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 641 | // Pointer to member conversions (4.11). |
| 642 | else if (IsMemberPointerConversion(From, FromType, ToType, FromType)) { |
| 643 | SCS.Second = ICK_Pointer_Member; |
| 644 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 645 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 646 | else if (ToType->isBooleanType() && |
| 647 | (FromType->isArithmeticType() || |
| 648 | FromType->isEnumeralType() || |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 649 | FromType->isPointerType() || |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 650 | FromType->isBlockPointerType() || |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 651 | FromType->isMemberPointerType() || |
| 652 | FromType->isNullPtrType())) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 653 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 654 | FromType = Context.BoolTy; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 655 | } |
| 656 | // Compatible conversions (Clang extension for C function overloading) |
| 657 | else if (!getLangOptions().CPlusPlus && |
| 658 | Context.typesAreCompatible(ToType, FromType)) { |
| 659 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 660 | } else { |
| 661 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 662 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 665 | QualType CanonFrom; |
| 666 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 667 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 668 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 669 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 670 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 671 | CanonFrom = Context.getCanonicalType(FromType); |
| 672 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 673 | } else { |
| 674 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 675 | SCS.Third = ICK_Identity; |
| 676 | |
| 677 | // C++ [over.best.ics]p6: |
| 678 | // [...] Any difference in top-level cv-qualification is |
| 679 | // subsumed by the initialization itself and does not constitute |
| 680 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 681 | CanonFrom = Context.getCanonicalType(FromType); |
| 682 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 683 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 684 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 685 | FromType = ToType; |
| 686 | CanonFrom = CanonTo; |
| 687 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // If we have not converted the argument type to the parameter type, |
| 691 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 692 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 693 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 695 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 696 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 700 | /// expression From (whose potentially-adjusted type is FromType) to |
| 701 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 702 | /// sets PromotedType to the promoted type. |
| 703 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) |
| 704 | { |
| 705 | const BuiltinType *To = ToType->getAsBuiltinType(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 706 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 707 | if (!To) { |
| 708 | return false; |
| 709 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 710 | |
| 711 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 712 | // unsigned short int can be converted to an rvalue of type int if |
| 713 | // int can represent all the values of the source type; otherwise, |
| 714 | // the source rvalue can be converted to an rvalue of type unsigned |
| 715 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 716 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 717 | if (// We can promote any signed, promotable integer type to an int |
| 718 | (FromType->isSignedIntegerType() || |
| 719 | // We can promote any unsigned integer type whose size is |
| 720 | // less than int to an int. |
| 721 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 722 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 723 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 726 | return To->getKind() == BuiltinType::UInt; |
| 727 | } |
| 728 | |
| 729 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 730 | // can be converted to an rvalue of the first of the following types |
| 731 | // that can represent all the values of its underlying type: int, |
| 732 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 733 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 734 | && ToType->isIntegerType()) { |
| 735 | // Determine whether the type we're converting from is signed or |
| 736 | // unsigned. |
| 737 | bool FromIsSigned; |
| 738 | uint64_t FromSize = Context.getTypeSize(FromType); |
| 739 | if (const EnumType *FromEnumType = FromType->getAsEnumType()) { |
| 740 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 741 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 742 | } else { |
| 743 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 744 | FromIsSigned = true; |
| 745 | } |
| 746 | |
| 747 | // The types we'll try to promote to, in the appropriate |
| 748 | // order. Try each of these types. |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 749 | QualType PromoteTypes[6] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 750 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 751 | Context.LongTy, Context.UnsignedLongTy , |
| 752 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 753 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 754 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 755 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 756 | if (FromSize < ToSize || |
| 757 | (FromSize == ToSize && |
| 758 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 759 | // We found the type that we can promote to. If this is the |
| 760 | // type we wanted, we have a promotion. Otherwise, no |
| 761 | // promotion. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 762 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 763 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 769 | // rvalue of type int if int can represent all the values of the |
| 770 | // bit-field; otherwise, it can be converted to unsigned int if |
| 771 | // unsigned int can represent all the values of the bit-field. If |
| 772 | // the bit-field is larger yet, no integral promotion applies to |
| 773 | // it. If the bit-field has an enumerated type, it is treated as any |
| 774 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 775 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 776 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 777 | using llvm::APSInt; |
| 778 | if (From) |
| 779 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 780 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 781 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 782 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 783 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 784 | ToSize = Context.getTypeSize(ToType); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 785 | |
| 786 | // Are we promoting to an int from a bitfield that fits in an int? |
| 787 | if (BitWidth < ToSize || |
| 788 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 789 | return To->getKind() == BuiltinType::Int; |
| 790 | } |
| 791 | |
| 792 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 793 | // that fits into an unsigned int? |
| 794 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 795 | return To->getKind() == BuiltinType::UInt; |
| 796 | } |
| 797 | |
| 798 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 799 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 800 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 802 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 803 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 804 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 805 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 806 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 807 | |
| 808 | return false; |
| 809 | } |
| 810 | |
| 811 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 812 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 813 | /// returns true and sets PromotedType to the promoted type. |
| 814 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) |
| 815 | { |
| 816 | /// An rvalue of type float can be converted to an rvalue of type |
| 817 | /// double. (C++ 4.6p1). |
| 818 | if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType()) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 819 | if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 820 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 821 | ToBuiltin->getKind() == BuiltinType::Double) |
| 822 | return true; |
| 823 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 824 | // C99 6.3.1.5p1: |
| 825 | // When a float is promoted to double or long double, or a |
| 826 | // double is promoted to long double [...]. |
| 827 | if (!getLangOptions().CPlusPlus && |
| 828 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 829 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 830 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 831 | return true; |
| 832 | } |
| 833 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 834 | return false; |
| 835 | } |
| 836 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 837 | /// \brief Determine if a conversion is a complex promotion. |
| 838 | /// |
| 839 | /// A complex promotion is defined as a complex -> complex conversion |
| 840 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 841 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 842 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
| 843 | const ComplexType *FromComplex = FromType->getAsComplexType(); |
| 844 | if (!FromComplex) |
| 845 | return false; |
| 846 | |
| 847 | const ComplexType *ToComplex = ToType->getAsComplexType(); |
| 848 | if (!ToComplex) |
| 849 | return false; |
| 850 | |
| 851 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 852 | ToComplex->getElementType()) || |
| 853 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 854 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 857 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 858 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 859 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 860 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 861 | /// the right set of qualifiers on its pointee. |
| 862 | static QualType |
| 863 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
| 864 | QualType ToPointee, QualType ToType, |
| 865 | ASTContext &Context) { |
| 866 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 867 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| 868 | unsigned Quals = CanonFromPointee.getCVRQualifiers(); |
| 869 | |
| 870 | // Exact qualifier match -> return the pointer type we're converting to. |
| 871 | if (CanonToPointee.getCVRQualifiers() == Quals) { |
| 872 | // ToType is exactly what we need. Return it. |
| 873 | if (ToType.getTypePtr()) |
| 874 | return ToType; |
| 875 | |
| 876 | // Build a pointer to ToPointee. It has the right qualifiers |
| 877 | // already. |
| 878 | return Context.getPointerType(ToPointee); |
| 879 | } |
| 880 | |
| 881 | // Just build a canonical type that has the right qualifiers. |
| 882 | return Context.getPointerType(CanonToPointee.getQualifiedType(Quals)); |
| 883 | } |
| 884 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 885 | /// IsPointerConversion - Determines whether the conversion of the |
| 886 | /// expression From, which has the (possibly adjusted) type FromType, |
| 887 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 888 | /// 4.10). If so, returns true and places the converted type (that |
| 889 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 890 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 891 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 892 | /// This routine also supports conversions to and from block pointers |
| 893 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 894 | /// pointers to interfaces. FIXME: Once we've determined the |
| 895 | /// appropriate overloading rules for Objective-C, we may want to |
| 896 | /// split the Objective-C checks into a different routine; however, |
| 897 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 898 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 899 | /// set if the conversion is an allowed Objective-C conversion that |
| 900 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 901 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 902 | QualType& ConvertedType, |
| 903 | bool &IncompatibleObjC) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 904 | { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 905 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 906 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 907 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 909 | // Conversion from a null pointer constant to any Objective-C pointer type. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 910 | if (ToType->isObjCObjectPointerType() && |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 911 | From->isNullPointerConstant(Context)) { |
| 912 | ConvertedType = ToType; |
| 913 | return true; |
| 914 | } |
| 915 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 916 | // Blocks: Block pointers can be converted to void*. |
| 917 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 918 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 919 | ConvertedType = ToType; |
| 920 | return true; |
| 921 | } |
| 922 | // Blocks: A null pointer constant can be converted to a block |
| 923 | // pointer type. |
| 924 | if (ToType->isBlockPointerType() && From->isNullPointerConstant(Context)) { |
| 925 | ConvertedType = ToType; |
| 926 | return true; |
| 927 | } |
| 928 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 929 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 930 | // pointer constant. |
| 931 | if (ToType->isNullPtrType() && From->isNullPointerConstant(Context)) { |
| 932 | ConvertedType = ToType; |
| 933 | return true; |
| 934 | } |
| 935 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 936 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 937 | if (!ToTypePtr) |
| 938 | return false; |
| 939 | |
| 940 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
| 941 | if (From->isNullPointerConstant(Context)) { |
| 942 | ConvertedType = ToType; |
| 943 | return true; |
| 944 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 945 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 946 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 947 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 948 | if (!FromTypePtr) |
| 949 | return false; |
| 950 | |
| 951 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 952 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 953 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 954 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 955 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 956 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 957 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 958 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 959 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 960 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 961 | return true; |
| 962 | } |
| 963 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 964 | // When we're overloading in C, we allow a special kind of pointer |
| 965 | // conversion for compatible-but-not-identical pointee types. |
| 966 | if (!getLangOptions().CPlusPlus && |
| 967 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
| 968 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 969 | ToPointeeType, |
| 970 | ToType, Context); |
| 971 | return true; |
| 972 | } |
| 973 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 974 | // C++ [conv.ptr]p3: |
| 975 | // |
| 976 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 977 | // can be converted to an rvalue of type "pointer to cv B," where |
| 978 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 979 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 980 | // necessitates this conversion is ill-formed. The result of the |
| 981 | // conversion is a pointer to the base class sub-object of the |
| 982 | // derived class object. The null pointer value is converted to |
| 983 | // the null pointer value of the destination type. |
| 984 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 985 | // Note that we do not check for ambiguity or inaccessibility |
| 986 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 987 | if (getLangOptions().CPlusPlus && |
| 988 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 989 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 990 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
| 991 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 992 | ToType, Context); |
| 993 | return true; |
| 994 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 995 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 996 | return false; |
| 997 | } |
| 998 | |
| 999 | /// isObjCPointerConversion - Determines whether this is an |
| 1000 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1001 | /// with the same arguments and return values. |
| 1002 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
| 1003 | QualType& ConvertedType, |
| 1004 | bool &IncompatibleObjC) { |
| 1005 | if (!getLangOptions().ObjC1) |
| 1006 | return false; |
| 1007 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1008 | // First, we handle all conversions on ObjC object pointer types. |
| 1009 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAsObjCObjectPointerType(); |
| 1010 | const ObjCObjectPointerType *FromObjCPtr = |
| 1011 | FromType->getAsObjCObjectPointerType(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1012 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1013 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1014 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1015 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1016 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1017 | ConvertedType = ToType; |
| 1018 | return true; |
| 1019 | } |
| 1020 | // Conversions with Objective-C's id<...>. |
| 1021 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
| 1022 | ToObjCPtr->isObjCQualifiedIdType()) && |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1023 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
| 1024 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1025 | ConvertedType = ToType; |
| 1026 | return true; |
| 1027 | } |
| 1028 | // Objective C++: We're able to convert from a pointer to an |
| 1029 | // interface to a pointer to a different interface. |
| 1030 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1031 | ConvertedType = ToType; |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
| 1035 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1036 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1037 | // interfaces, which is permitted. However, we're going to |
| 1038 | // complain about it. |
| 1039 | IncompatibleObjC = true; |
| 1040 | ConvertedType = FromType; |
| 1041 | return true; |
| 1042 | } |
| 1043 | } |
| 1044 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1045 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1046 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1047 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1048 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1049 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1050 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1051 | return false; |
| 1052 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1053 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1054 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1055 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1056 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1057 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1058 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1059 | return false; |
| 1060 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1061 | // If we have pointers to pointers, recursively check whether this |
| 1062 | // is an Objective-C conversion. |
| 1063 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1064 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1065 | IncompatibleObjC)) { |
| 1066 | // We always complain about this conversion. |
| 1067 | IncompatibleObjC = true; |
| 1068 | ConvertedType = ToType; |
| 1069 | return true; |
| 1070 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1071 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1072 | // differences in the argument and result types are in Objective-C |
| 1073 | // pointer conversions. If so, we permit the conversion (but |
| 1074 | // complain about it). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1075 | const FunctionProtoType *FromFunctionType |
| 1076 | = FromPointeeType->getAsFunctionProtoType(); |
| 1077 | const FunctionProtoType *ToFunctionType |
| 1078 | = ToPointeeType->getAsFunctionProtoType(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1079 | if (FromFunctionType && ToFunctionType) { |
| 1080 | // If the function types are exactly the same, this isn't an |
| 1081 | // Objective-C pointer conversion. |
| 1082 | if (Context.getCanonicalType(FromPointeeType) |
| 1083 | == Context.getCanonicalType(ToPointeeType)) |
| 1084 | return false; |
| 1085 | |
| 1086 | // Perform the quick checks that will tell us whether these |
| 1087 | // function types are obviously different. |
| 1088 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1089 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1090 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1091 | return false; |
| 1092 | |
| 1093 | bool HasObjCConversion = false; |
| 1094 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1095 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1096 | // Okay, the types match exactly. Nothing to do. |
| 1097 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1098 | ToFunctionType->getResultType(), |
| 1099 | ConvertedType, IncompatibleObjC)) { |
| 1100 | // Okay, we have an Objective-C pointer conversion. |
| 1101 | HasObjCConversion = true; |
| 1102 | } else { |
| 1103 | // Function types are too different. Abort. |
| 1104 | return false; |
| 1105 | } |
| 1106 | |
| 1107 | // Check argument types. |
| 1108 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1109 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1110 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1111 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1112 | if (Context.getCanonicalType(FromArgType) |
| 1113 | == Context.getCanonicalType(ToArgType)) { |
| 1114 | // Okay, the types match exactly. Nothing to do. |
| 1115 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1116 | ConvertedType, IncompatibleObjC)) { |
| 1117 | // Okay, we have an Objective-C pointer conversion. |
| 1118 | HasObjCConversion = true; |
| 1119 | } else { |
| 1120 | // Argument types are too different. Abort. |
| 1121 | return false; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | if (HasObjCConversion) { |
| 1126 | // We had an Objective-C conversion. Allow this pointer |
| 1127 | // conversion, but complain about it. |
| 1128 | ConvertedType = ToType; |
| 1129 | IncompatibleObjC = true; |
| 1130 | return true; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1134 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1137 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1138 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1139 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1140 | /// conversions for which IsPointerConversion has already returned |
| 1141 | /// true. It returns true and produces a diagnostic if there was an |
| 1142 | /// error, or returns false otherwise. |
| 1143 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType) { |
| 1144 | QualType FromType = From->getType(); |
| 1145 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1146 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1147 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1148 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1149 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1150 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1151 | if (FromPointeeType->isRecordType() && |
| 1152 | ToPointeeType->isRecordType()) { |
| 1153 | // We must have a derived-to-base conversion. Check an |
| 1154 | // ambiguous or inaccessible conversion. |
Douglas Gregor | 0575d4a | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 1155 | return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1156 | From->getExprLoc(), |
| 1157 | From->getSourceRange()); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1160 | if (const ObjCObjectPointerType *FromPtrType = |
| 1161 | FromType->getAsObjCObjectPointerType()) |
| 1162 | if (const ObjCObjectPointerType *ToPtrType = |
| 1163 | ToType->getAsObjCObjectPointerType()) { |
| 1164 | // Objective-C++ conversions are always okay. |
| 1165 | // FIXME: We should have a different class of conversions for the |
| 1166 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1167 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1168 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1169 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1170 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
| 1173 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1174 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1175 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1176 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1177 | /// If so, returns true and places the converted type (that might differ from |
| 1178 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1179 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
| 1180 | QualType ToType, QualType &ConvertedType) |
| 1181 | { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1182 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1183 | if (!ToTypePtr) |
| 1184 | return false; |
| 1185 | |
| 1186 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
| 1187 | if (From->isNullPointerConstant(Context)) { |
| 1188 | ConvertedType = ToType; |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1193 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1194 | if (!FromTypePtr) |
| 1195 | return false; |
| 1196 | |
| 1197 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1198 | // where D is derived from B (C++ 4.11p2). |
| 1199 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1200 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1201 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1202 | |
| 1203 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1204 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1205 | ToClass.getTypePtr()); |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1213 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1214 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1215 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1216 | /// true and produces a diagnostic if there was an error, or returns false |
| 1217 | /// otherwise. |
| 1218 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType) { |
| 1219 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1220 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1221 | if (!FromPtrType) |
| 1222 | return false; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1223 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1224 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1225 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1226 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1227 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1228 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1229 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1230 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1231 | // FIXME: What about dependent types? |
| 1232 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1233 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1234 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1235 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1236 | /*DetectVirtual=*/true); |
| 1237 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1238 | assert(DerivationOkay && |
| 1239 | "Should not have been called if derivation isn't OK."); |
| 1240 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1241 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1242 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1243 | getUnqualifiedType())) { |
| 1244 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1245 | Paths.clear(); |
| 1246 | Paths.setRecordingPaths(true); |
| 1247 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1248 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1249 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1250 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1251 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1252 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1253 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1254 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1255 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1257 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1258 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1259 | << FromClass << ToClass << QualType(VBase, 0) |
| 1260 | << From->getSourceRange(); |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
| 1266 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1267 | /// IsQualificationConversion - Determines whether the conversion from |
| 1268 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1269 | /// (C++ 4.4). |
| 1270 | bool |
| 1271 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) |
| 1272 | { |
| 1273 | FromType = Context.getCanonicalType(FromType); |
| 1274 | ToType = Context.getCanonicalType(ToType); |
| 1275 | |
| 1276 | // If FromType and ToType are the same type, this is not a |
| 1277 | // qualification conversion. |
| 1278 | if (FromType == ToType) |
| 1279 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1280 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1281 | // (C++ 4.4p4): |
| 1282 | // A conversion can add cv-qualifiers at levels other than the first |
| 1283 | // in multi-level pointers, subject to the following rules: [...] |
| 1284 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1285 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1286 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1287 | // Within each iteration of the loop, we check the qualifiers to |
| 1288 | // determine if this still looks like a qualification |
| 1289 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1290 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1291 | // until there are no more pointers or pointers-to-members left to |
| 1292 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1293 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1294 | |
| 1295 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1296 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1297 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1298 | return false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1300 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1301 | // every cv for 0 < k < j. |
| 1302 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1303 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1304 | return false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1306 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1307 | // include const. |
| 1308 | PreviousToQualsIncludeConst |
| 1309 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1310 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1311 | |
| 1312 | // We are left with FromType and ToType being the pointee types |
| 1313 | // after unwrapping the original FromType and ToType the same number |
| 1314 | // of types. If we unwrapped any pointers, and if FromType and |
| 1315 | // ToType have the same unqualified type (since we checked |
| 1316 | // qualifiers above), then this is a qualification conversion. |
| 1317 | return UnwrappedAnyPointer && |
| 1318 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1319 | } |
| 1320 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1321 | /// Determines whether there is a user-defined conversion sequence |
| 1322 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1323 | /// ToType. If such a conversion exists, User will contain the |
| 1324 | /// user-defined conversion sequence that performs such a conversion |
| 1325 | /// and this routine will return true. Otherwise, this routine returns |
| 1326 | /// false and User is unspecified. |
| 1327 | /// |
| 1328 | /// \param AllowConversionFunctions true if the conversion should |
| 1329 | /// consider conversion functions at all. If false, only constructors |
| 1330 | /// will be considered. |
| 1331 | /// |
| 1332 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1333 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1334 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1335 | /// |
| 1336 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1337 | /// for overload resolution. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1338 | bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1339 | UserDefinedConversionSequence& User, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1340 | bool AllowConversionFunctions, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1341 | bool AllowExplicit, bool ForceRValue) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1342 | { |
| 1343 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1344 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1345 | if (CXXRecordDecl *ToRecordDecl |
| 1346 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1347 | // C++ [over.match.ctor]p1: |
| 1348 | // When objects of class type are direct-initialized (8.5), or |
| 1349 | // copy-initialized from an expression of the same or a |
| 1350 | // derived class type (8.5), overload resolution selects the |
| 1351 | // constructor. [...] For copy-initialization, the candidate |
| 1352 | // functions are all the converting constructors (12.3.1) of |
| 1353 | // that class. The argument list is the expression-list within |
| 1354 | // the parentheses of the initializer. |
| 1355 | DeclarationName ConstructorName |
| 1356 | = Context.DeclarationNames.getCXXConstructorName( |
| 1357 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1358 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1359 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1360 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1361 | Con != ConEnd; ++Con) { |
| 1362 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 1363 | if (Constructor->isConvertingConstructor()) |
| 1364 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1365 | /*SuppressUserConversions=*/true, ForceRValue); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1366 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1370 | if (!AllowConversionFunctions) { |
| 1371 | // Don't allow any conversion functions to enter the overload set. |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1372 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1373 | = From->getType()->getAs<RecordType>()) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1374 | if (CXXRecordDecl *FromRecordDecl |
| 1375 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1376 | // Add all of the conversion functions as candidates. |
| 1377 | // FIXME: Look for conversions in base classes! |
| 1378 | OverloadedFunctionDecl *Conversions |
| 1379 | = FromRecordDecl->getConversionFunctions(); |
| 1380 | for (OverloadedFunctionDecl::function_iterator Func |
| 1381 | = Conversions->function_begin(); |
| 1382 | Func != Conversions->function_end(); ++Func) { |
| 1383 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 1384 | if (AllowExplicit || !Conv->isExplicit()) |
| 1385 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1386 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1387 | } |
| 1388 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1389 | |
| 1390 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1391 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1392 | case OR_Success: |
| 1393 | // Record the standard conversion we used and the conversion function. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1394 | if (CXXConstructorDecl *Constructor |
| 1395 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1396 | // C++ [over.ics.user]p1: |
| 1397 | // If the user-defined conversion is specified by a |
| 1398 | // constructor (12.3.1), the initial standard conversion |
| 1399 | // sequence converts the source type to the type required by |
| 1400 | // the argument of the constructor. |
| 1401 | // |
| 1402 | // FIXME: What about ellipsis conversions? |
| 1403 | QualType ThisType = Constructor->getThisType(Context); |
| 1404 | User.Before = Best->Conversions[0].Standard; |
| 1405 | User.ConversionFunction = Constructor; |
| 1406 | User.After.setAsIdentityConversion(); |
| 1407 | User.After.FromTypePtr |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1408 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1409 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
| 1410 | return true; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1411 | } else if (CXXConversionDecl *Conversion |
| 1412 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1413 | // C++ [over.ics.user]p1: |
| 1414 | // |
| 1415 | // [...] If the user-defined conversion is specified by a |
| 1416 | // conversion function (12.3.2), the initial standard |
| 1417 | // conversion sequence converts the source type to the |
| 1418 | // implicit object parameter of the conversion function. |
| 1419 | User.Before = Best->Conversions[0].Standard; |
| 1420 | User.ConversionFunction = Conversion; |
| 1421 | |
| 1422 | // C++ [over.ics.user]p2: |
| 1423 | // The second standard conversion sequence converts the |
| 1424 | // result of the user-defined conversion to the target type |
| 1425 | // for the sequence. Since an implicit conversion sequence |
| 1426 | // is an initialization, the special rules for |
| 1427 | // initialization by user-defined conversion apply when |
| 1428 | // selecting the best user-defined conversion for a |
| 1429 | // user-defined conversion sequence (see 13.3.3 and |
| 1430 | // 13.3.3.1). |
| 1431 | User.After = Best->FinalConversion; |
| 1432 | return true; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1433 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1434 | assert(false && "Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1435 | return false; |
| 1436 | } |
| 1437 | |
| 1438 | case OR_No_Viable_Function: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1439 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1440 | // No conversion here! We're done. |
| 1441 | return false; |
| 1442 | |
| 1443 | case OR_Ambiguous: |
| 1444 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 1445 | // ambiguous conversion sequences. |
| 1446 | return false; |
| 1447 | } |
| 1448 | |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1452 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1453 | /// conversion sequences to determine whether one is better than the |
| 1454 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
| 1455 | ImplicitConversionSequence::CompareKind |
| 1456 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1457 | const ImplicitConversionSequence& ICS2) |
| 1458 | { |
| 1459 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1460 | // conversion sequences (as defined in 13.3.3.1) |
| 1461 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1462 | // conversion sequence than a user-defined conversion sequence or |
| 1463 | // an ellipsis conversion sequence, and |
| 1464 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1465 | // conversion sequence than an ellipsis conversion sequence |
| 1466 | // (13.3.3.1.3). |
| 1467 | // |
| 1468 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1469 | return ImplicitConversionSequence::Better; |
| 1470 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1471 | return ImplicitConversionSequence::Worse; |
| 1472 | |
| 1473 | // Two implicit conversion sequences of the same form are |
| 1474 | // indistinguishable conversion sequences unless one of the |
| 1475 | // following rules apply: (C++ 13.3.3.2p3): |
| 1476 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1477 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
| 1478 | else if (ICS1.ConversionKind == |
| 1479 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1480 | // User-defined conversion sequence U1 is a better conversion |
| 1481 | // sequence than another user-defined conversion sequence U2 if |
| 1482 | // they contain the same user-defined conversion function or |
| 1483 | // constructor and if the second standard conversion sequence of |
| 1484 | // U1 is better than the second standard conversion sequence of |
| 1485 | // U2 (C++ 13.3.3.2p3). |
| 1486 | if (ICS1.UserDefined.ConversionFunction == |
| 1487 | ICS2.UserDefined.ConversionFunction) |
| 1488 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1489 | ICS2.UserDefined.After); |
| 1490 | } |
| 1491 | |
| 1492 | return ImplicitConversionSequence::Indistinguishable; |
| 1493 | } |
| 1494 | |
| 1495 | /// CompareStandardConversionSequences - Compare two standard |
| 1496 | /// conversion sequences to determine whether one is better than the |
| 1497 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
| 1498 | ImplicitConversionSequence::CompareKind |
| 1499 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1500 | const StandardConversionSequence& SCS2) |
| 1501 | { |
| 1502 | // Standard conversion sequence S1 is a better conversion sequence |
| 1503 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1504 | |
| 1505 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1506 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1507 | // excluding any Lvalue Transformation; the identity conversion |
| 1508 | // sequence is considered to be a subsequence of any |
| 1509 | // non-identity conversion sequence) or, if not that, |
| 1510 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1511 | // Neither is a proper subsequence of the other. Do nothing. |
| 1512 | ; |
| 1513 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1514 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
| 1515 | (SCS1.Second == ICK_Identity && |
| 1516 | SCS1.Third == ICK_Identity)) |
| 1517 | // SCS1 is a proper subsequence of SCS2. |
| 1518 | return ImplicitConversionSequence::Better; |
| 1519 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1520 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
| 1521 | (SCS2.Second == ICK_Identity && |
| 1522 | SCS2.Third == ICK_Identity)) |
| 1523 | // SCS2 is a proper subsequence of SCS1. |
| 1524 | return ImplicitConversionSequence::Worse; |
| 1525 | |
| 1526 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1527 | // defined below), or, if not that, |
| 1528 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1529 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1530 | if (Rank1 < Rank2) |
| 1531 | return ImplicitConversionSequence::Better; |
| 1532 | else if (Rank2 < Rank1) |
| 1533 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1534 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1535 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1536 | // are indistinguishable unless one of the following rules |
| 1537 | // applies: |
| 1538 | |
| 1539 | // A conversion that is not a conversion of a pointer, or |
| 1540 | // pointer to member, to bool is better than another conversion |
| 1541 | // that is such a conversion. |
| 1542 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1543 | return SCS2.isPointerConversionToBool() |
| 1544 | ? ImplicitConversionSequence::Better |
| 1545 | : ImplicitConversionSequence::Worse; |
| 1546 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1547 | // C++ [over.ics.rank]p4b2: |
| 1548 | // |
| 1549 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1550 | // conversion of B* to A* is better than conversion of B* to |
| 1551 | // void*, and conversion of A* to void* is better than conversion |
| 1552 | // of B* to void*. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1553 | bool SCS1ConvertsToVoid |
| 1554 | = SCS1.isPointerConversionToVoidPointer(Context); |
| 1555 | bool SCS2ConvertsToVoid |
| 1556 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1557 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1558 | // Exactly one of the conversion sequences is a conversion to |
| 1559 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1560 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1561 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1562 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1563 | // Neither conversion sequence converts to a void pointer; compare |
| 1564 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1565 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1566 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1567 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1568 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1569 | // Both conversion sequences are conversions to void |
| 1570 | // pointers. Compare the source types to determine if there's an |
| 1571 | // inheritance relationship in their sources. |
| 1572 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1573 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1574 | |
| 1575 | // Adjust the types we're converting from via the array-to-pointer |
| 1576 | // conversion, if we need to. |
| 1577 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1578 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1579 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1580 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1581 | |
| 1582 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1583 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1584 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1585 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1588 | return ImplicitConversionSequence::Better; |
| 1589 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1590 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1591 | |
| 1592 | // Objective-C++: If one interface is more specific than the |
| 1593 | // other, it is the better one. |
| 1594 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1595 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1596 | if (FromIface1 && FromIface1) { |
| 1597 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1598 | return ImplicitConversionSequence::Better; |
| 1599 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1600 | return ImplicitConversionSequence::Worse; |
| 1601 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1602 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1603 | |
| 1604 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1605 | // bullet 3). |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1606 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1607 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1608 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1610 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1611 | // C++0x [over.ics.rank]p3b4: |
| 1612 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1613 | // implicit object parameter of a non-static member function declared |
| 1614 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1615 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1616 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1617 | // or if the member function in this case has a ref qualifier. |
| 1618 | // (Of course, we don't have ref qualifiers yet.) |
| 1619 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1620 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1621 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1622 | |
| 1623 | // C++ [over.ics.rank]p3b4: |
| 1624 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1625 | // which the references refer are the same type except for |
| 1626 | // top-level cv-qualifiers, and the type to which the reference |
| 1627 | // initialized by S2 refers is more cv-qualified than the type |
| 1628 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1629 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1630 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1631 | T1 = Context.getCanonicalType(T1); |
| 1632 | T2 = Context.getCanonicalType(T2); |
| 1633 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1634 | if (T2.isMoreQualifiedThan(T1)) |
| 1635 | return ImplicitConversionSequence::Better; |
| 1636 | else if (T1.isMoreQualifiedThan(T2)) |
| 1637 | return ImplicitConversionSequence::Worse; |
| 1638 | } |
| 1639 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1640 | |
| 1641 | return ImplicitConversionSequence::Indistinguishable; |
| 1642 | } |
| 1643 | |
| 1644 | /// CompareQualificationConversions - Compares two standard conversion |
| 1645 | /// sequences to determine whether they can be ranked based on their |
| 1646 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1647 | ImplicitConversionSequence::CompareKind |
| 1648 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
| 1649 | const StandardConversionSequence& SCS2) |
| 1650 | { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1651 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1652 | // -- S1 and S2 differ only in their qualification conversion and |
| 1653 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1654 | // cv-qualification signature of type T1 is a proper subset of |
| 1655 | // the cv-qualification signature of type T2, and S1 is not the |
| 1656 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1657 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1658 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1659 | return ImplicitConversionSequence::Indistinguishable; |
| 1660 | |
| 1661 | // FIXME: the example in the standard doesn't use a qualification |
| 1662 | // conversion (!) |
| 1663 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1664 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1665 | T1 = Context.getCanonicalType(T1); |
| 1666 | T2 = Context.getCanonicalType(T2); |
| 1667 | |
| 1668 | // If the types are the same, we won't learn anything by unwrapped |
| 1669 | // them. |
| 1670 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1671 | return ImplicitConversionSequence::Indistinguishable; |
| 1672 | |
| 1673 | ImplicitConversionSequence::CompareKind Result |
| 1674 | = ImplicitConversionSequence::Indistinguishable; |
| 1675 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1676 | // Within each iteration of the loop, we check the qualifiers to |
| 1677 | // determine if this still looks like a qualification |
| 1678 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1679 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1680 | // until there are no more pointers or pointers-to-members left |
| 1681 | // to unwrap. This essentially mimics what |
| 1682 | // IsQualificationConversion does, but here we're checking for a |
| 1683 | // strict subset of qualifiers. |
| 1684 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1685 | // The qualifiers are the same, so this doesn't tell us anything |
| 1686 | // about how the sequences rank. |
| 1687 | ; |
| 1688 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1689 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1690 | if (Result == ImplicitConversionSequence::Worse) |
| 1691 | // Neither has qualifiers that are a subset of the other's |
| 1692 | // qualifiers. |
| 1693 | return ImplicitConversionSequence::Indistinguishable; |
| 1694 | |
| 1695 | Result = ImplicitConversionSequence::Better; |
| 1696 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1697 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1698 | if (Result == ImplicitConversionSequence::Better) |
| 1699 | // Neither has qualifiers that are a subset of the other's |
| 1700 | // qualifiers. |
| 1701 | return ImplicitConversionSequence::Indistinguishable; |
| 1702 | |
| 1703 | Result = ImplicitConversionSequence::Worse; |
| 1704 | } else { |
| 1705 | // Qualifiers are disjoint. |
| 1706 | return ImplicitConversionSequence::Indistinguishable; |
| 1707 | } |
| 1708 | |
| 1709 | // If the types after this point are equivalent, we're done. |
| 1710 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1711 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1714 | // Check that the winning standard conversion sequence isn't using |
| 1715 | // the deprecated string literal array to pointer conversion. |
| 1716 | switch (Result) { |
| 1717 | case ImplicitConversionSequence::Better: |
| 1718 | if (SCS1.Deprecated) |
| 1719 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1720 | break; |
| 1721 | |
| 1722 | case ImplicitConversionSequence::Indistinguishable: |
| 1723 | break; |
| 1724 | |
| 1725 | case ImplicitConversionSequence::Worse: |
| 1726 | if (SCS2.Deprecated) |
| 1727 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1728 | break; |
| 1729 | } |
| 1730 | |
| 1731 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1734 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1735 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1736 | /// various kinds of derived-to-base conversions (C++ |
| 1737 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1738 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1739 | ImplicitConversionSequence::CompareKind |
| 1740 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1741 | const StandardConversionSequence& SCS2) { |
| 1742 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1743 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1744 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1745 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1746 | |
| 1747 | // Adjust the types we're converting from via the array-to-pointer |
| 1748 | // conversion, if we need to. |
| 1749 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1750 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1751 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1752 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1753 | |
| 1754 | // Canonicalize all of the types. |
| 1755 | FromType1 = Context.getCanonicalType(FromType1); |
| 1756 | ToType1 = Context.getCanonicalType(ToType1); |
| 1757 | FromType2 = Context.getCanonicalType(FromType2); |
| 1758 | ToType2 = Context.getCanonicalType(ToType2); |
| 1759 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1760 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1761 | // |
| 1762 | // If class B is derived directly or indirectly from class A and |
| 1763 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1764 | // |
| 1765 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1766 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Compare based on pointer conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1769 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1770 | SCS2.Second == ICK_Pointer_Conversion && |
| 1771 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1772 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1773 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1774 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1775 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1776 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1777 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1778 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1779 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1780 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1781 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1782 | |
| 1783 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1784 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1785 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAsObjCInterfaceType(); |
| 1786 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAsObjCInterfaceType(); |
| 1787 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1788 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1789 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1790 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1791 | return ImplicitConversionSequence::Better; |
| 1792 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1793 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1794 | |
| 1795 | if (ToIface1 && ToIface2) { |
| 1796 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1797 | return ImplicitConversionSequence::Better; |
| 1798 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1799 | return ImplicitConversionSequence::Worse; |
| 1800 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1801 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1802 | |
| 1803 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1804 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1805 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1806 | return ImplicitConversionSequence::Better; |
| 1807 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1808 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1809 | |
| 1810 | if (FromIface1 && FromIface2) { |
| 1811 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1812 | return ImplicitConversionSequence::Better; |
| 1813 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1814 | return ImplicitConversionSequence::Worse; |
| 1815 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1816 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1819 | // Compare based on reference bindings. |
| 1820 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1821 | SCS1.Second == ICK_Derived_To_Base) { |
| 1822 | // -- binding of an expression of type C to a reference of type |
| 1823 | // B& is better than binding an expression of type C to a |
| 1824 | // reference of type A&, |
| 1825 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1826 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1827 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1828 | return ImplicitConversionSequence::Better; |
| 1829 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1830 | return ImplicitConversionSequence::Worse; |
| 1831 | } |
| 1832 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1833 | // -- binding of an expression of type B to a reference of type |
| 1834 | // A& is better than binding an expression of type C to a |
| 1835 | // reference of type A&, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1836 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1837 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1838 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1839 | return ImplicitConversionSequence::Better; |
| 1840 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1841 | return ImplicitConversionSequence::Worse; |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | |
| 1846 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1847 | // A::* to C::*, |
| 1848 | |
| 1849 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1850 | // A::* to C::*, and |
| 1851 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1852 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1853 | SCS1.Second == ICK_Derived_To_Base) { |
| 1854 | // -- conversion of C to B is better than conversion of C to A, |
| 1855 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1856 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1857 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1858 | return ImplicitConversionSequence::Better; |
| 1859 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1860 | return ImplicitConversionSequence::Worse; |
| 1861 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1863 | // -- conversion of B to A is better than conversion of C to A. |
| 1864 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1865 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1866 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1867 | return ImplicitConversionSequence::Better; |
| 1868 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1869 | return ImplicitConversionSequence::Worse; |
| 1870 | } |
| 1871 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1873 | return ImplicitConversionSequence::Indistinguishable; |
| 1874 | } |
| 1875 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1876 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1877 | /// ToType from the expression From. Return the implicit conversion |
| 1878 | /// sequence required to pass this argument, which may be a bad |
| 1879 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1880 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1881 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 1882 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1883 | ImplicitConversionSequence |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1884 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1885 | bool SuppressUserConversions, bool ForceRValue) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1886 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1887 | ImplicitConversionSequence ICS; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1888 | CheckReferenceInit(From, ToType, &ICS, SuppressUserConversions, |
| 1889 | /*AllowExplicit=*/false, ForceRValue); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1890 | return ICS; |
| 1891 | } else { |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1892 | return TryImplicitConversion(From, ToType, SuppressUserConversions, |
| 1893 | ForceRValue); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1897 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 1898 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 1899 | /// an error, returns false if the initialization succeeded. Elidable should |
| 1900 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 1901 | /// differently in C++0x for this case. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1902 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1903 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1904 | if (!getLangOptions().CPlusPlus) { |
| 1905 | // In C, argument passing is the same as performing an assignment. |
| 1906 | QualType FromType = From->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1907 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1908 | AssignConvertType ConvTy = |
| 1909 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1910 | if (ConvTy != Compatible && |
| 1911 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 1912 | ConvTy = Compatible; |
| 1913 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1914 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 1915 | FromType, From, Flavor); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1916 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1917 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1918 | if (ToType->isReferenceType()) |
| 1919 | return CheckReferenceInit(From, ToType); |
| 1920 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1921 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 1922 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1923 | return false; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1924 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1925 | return Diag(From->getSourceRange().getBegin(), |
| 1926 | diag::err_typecheck_convert_incompatible) |
| 1927 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1930 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 1931 | /// parameter of the given member function (@c Method) from the |
| 1932 | /// expression @p From. |
| 1933 | ImplicitConversionSequence |
| 1934 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 1935 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
| 1936 | unsigned MethodQuals = Method->getTypeQualifiers(); |
| 1937 | QualType ImplicitParamType = ClassType.getQualifiedType(MethodQuals); |
| 1938 | |
| 1939 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 1940 | // to exit early. |
| 1941 | ImplicitConversionSequence ICS; |
| 1942 | ICS.Standard.setAsIdentityConversion(); |
| 1943 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 1944 | |
| 1945 | // We need to have an object of class type. |
| 1946 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1947 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 1948 | FromType = PT->getPointeeType(); |
| 1949 | |
| 1950 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1951 | |
| 1952 | // The implicit object parmeter is has the type "reference to cv X", |
| 1953 | // where X is the class of which the function is a member |
| 1954 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 1955 | // conversion sequence for the argument, we are not allowed to |
| 1956 | // create temporaries or perform user-defined conversions |
| 1957 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 1958 | // reference binding here, that allows class rvalues to bind to |
| 1959 | // non-constant references. |
| 1960 | |
| 1961 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 1962 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 1963 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 1964 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 1965 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 1966 | return ICS; |
| 1967 | |
| 1968 | // Check that we have either the same type or a derived type. It |
| 1969 | // affects the conversion rank. |
| 1970 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 1971 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 1972 | ICS.Standard.Second = ICK_Identity; |
| 1973 | else if (IsDerivedFrom(FromType, ClassType)) |
| 1974 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 1975 | else |
| 1976 | return ICS; |
| 1977 | |
| 1978 | // Success. Mark this as a reference binding. |
| 1979 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 1980 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 1981 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 1982 | ICS.Standard.ReferenceBinding = true; |
| 1983 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 1984 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1985 | return ICS; |
| 1986 | } |
| 1987 | |
| 1988 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 1989 | /// the implicit object parameter for the given Method with the given |
| 1990 | /// expression. |
| 1991 | bool |
| 1992 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 1993 | QualType FromRecordType, DestType; |
| 1994 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1995 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 1996 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1997 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 1998 | FromRecordType = PT->getPointeeType(); |
| 1999 | DestType = Method->getThisType(Context); |
| 2000 | } else { |
| 2001 | FromRecordType = From->getType(); |
| 2002 | DestType = ImplicitParamRecordType; |
| 2003 | } |
| 2004 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2005 | ImplicitConversionSequence ICS |
| 2006 | = TryObjectArgumentInitialization(From, Method); |
| 2007 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2008 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2009 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2010 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
| 2011 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2012 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2013 | CheckDerivedToBaseConversion(FromRecordType, |
| 2014 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2015 | From->getSourceRange().getBegin(), |
| 2016 | From->getSourceRange())) |
| 2017 | return true; |
| 2018 | |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 2019 | ImpCastExprToType(From, DestType, CastExpr::CK_Unknown, /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2020 | return false; |
| 2021 | } |
| 2022 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2023 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2024 | /// expression From to bool (C++0x [conv]p3). |
| 2025 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
| 2026 | return TryImplicitConversion(From, Context.BoolTy, false, true); |
| 2027 | } |
| 2028 | |
| 2029 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2030 | /// of the expression From to bool (C++0x [conv]p3). |
| 2031 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2032 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2033 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2034 | return false; |
| 2035 | |
| 2036 | return Diag(From->getSourceRange().getBegin(), |
| 2037 | diag::err_typecheck_bool_condition) |
| 2038 | << From->getType() << From->getSourceRange(); |
| 2039 | } |
| 2040 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2041 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2042 | /// candidate functions, using the given function call arguments. If |
| 2043 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2044 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2045 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2046 | /// hacky way to implement the overloading rules for elidable copy |
| 2047 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2048 | void |
| 2049 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
| 2050 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2051 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2052 | bool SuppressUserConversions, |
| 2053 | bool ForceRValue) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2054 | { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2055 | const FunctionProtoType* Proto |
| 2056 | = dyn_cast<FunctionProtoType>(Function->getType()->getAsFunctionType()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2057 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2058 | assert(!isa<CXXConversionDecl>(Function) && |
| 2059 | "Use AddConversionCandidate for conversion functions"); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2060 | assert(!Function->getDescribedFunctionTemplate() && |
| 2061 | "Use AddTemplateOverloadCandidate for function templates"); |
| 2062 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2063 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2064 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2065 | // If we get here, it's because we're calling a member function |
| 2066 | // that is named without a member access expression (e.g., |
| 2067 | // "this->f") that was either written explicitly or created |
| 2068 | // implicitly. This can happen with a qualified call to a member |
| 2069 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2070 | // object argument (C++ [over.call.func]p3). |
| 2071 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
| 2072 | SuppressUserConversions, ForceRValue); |
| 2073 | return; |
| 2074 | } |
| 2075 | // We treat a constructor like a non-member function, since its object |
| 2076 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2080 | // Add this candidate |
| 2081 | CandidateSet.push_back(OverloadCandidate()); |
| 2082 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2083 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2084 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2085 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2086 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2087 | |
| 2088 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2089 | |
| 2090 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2091 | // parameters is viable only if it has an ellipsis in its parameter |
| 2092 | // list (8.3.5). |
| 2093 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2094 | Candidate.Viable = false; |
| 2095 | return; |
| 2096 | } |
| 2097 | |
| 2098 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2099 | // is viable only if the (m+1)st parameter has a default argument |
| 2100 | // (8.3.6). For the purposes of overload resolution, the |
| 2101 | // parameter list is truncated on the right, so that there are |
| 2102 | // exactly m parameters. |
| 2103 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 2104 | if (NumArgs < MinRequiredArgs) { |
| 2105 | // Not enough arguments. |
| 2106 | Candidate.Viable = false; |
| 2107 | return; |
| 2108 | } |
| 2109 | |
| 2110 | // Determine the implicit conversion sequences for each of the |
| 2111 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2112 | Candidate.Conversions.resize(NumArgs); |
| 2113 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2114 | if (ArgIdx < NumArgsInProto) { |
| 2115 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2116 | // exist for each argument an implicit conversion sequence |
| 2117 | // (13.3.3.1) that converts that argument to the corresponding |
| 2118 | // parameter of F. |
| 2119 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2120 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2121 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2122 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2123 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2124 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2125 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2126 | break; |
| 2127 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2128 | } else { |
| 2129 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2130 | // argument for which there is no corresponding parameter is |
| 2131 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2132 | Candidate.Conversions[ArgIdx].ConversionKind |
| 2133 | = ImplicitConversionSequence::EllipsisConversion; |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2138 | /// \brief Add all of the function declarations in the given function set to |
| 2139 | /// the overload canddiate set. |
| 2140 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2141 | Expr **Args, unsigned NumArgs, |
| 2142 | OverloadCandidateSet& CandidateSet, |
| 2143 | bool SuppressUserConversions) { |
| 2144 | for (FunctionSet::const_iterator F = Functions.begin(), |
| 2145 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2146 | F != FEnd; ++F) { |
| 2147 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) |
| 2148 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2149 | SuppressUserConversions); |
| 2150 | else |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2151 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*F), |
| 2152 | /*FIXME: explicit args */false, 0, 0, |
| 2153 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2154 | SuppressUserConversions); |
| 2155 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2158 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2159 | /// of candidate functions, using the given function call arguments |
| 2160 | /// and the object argument (@c Object). For example, in a call |
| 2161 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2162 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2163 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2164 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2165 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2166 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2167 | void |
| 2168 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2169 | Expr **Args, unsigned NumArgs, |
| 2170 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2171 | bool SuppressUserConversions, bool ForceRValue) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2172 | { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2173 | const FunctionProtoType* Proto |
| 2174 | = dyn_cast<FunctionProtoType>(Method->getType()->getAsFunctionType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2175 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2176 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2177 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2178 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2179 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2180 | |
| 2181 | // Add this candidate |
| 2182 | CandidateSet.push_back(OverloadCandidate()); |
| 2183 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2184 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2185 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2186 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2187 | |
| 2188 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2189 | |
| 2190 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2191 | // parameters is viable only if it has an ellipsis in its parameter |
| 2192 | // list (8.3.5). |
| 2193 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2194 | Candidate.Viable = false; |
| 2195 | return; |
| 2196 | } |
| 2197 | |
| 2198 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2199 | // is viable only if the (m+1)st parameter has a default argument |
| 2200 | // (8.3.6). For the purposes of overload resolution, the |
| 2201 | // parameter list is truncated on the right, so that there are |
| 2202 | // exactly m parameters. |
| 2203 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2204 | if (NumArgs < MinRequiredArgs) { |
| 2205 | // Not enough arguments. |
| 2206 | Candidate.Viable = false; |
| 2207 | return; |
| 2208 | } |
| 2209 | |
| 2210 | Candidate.Viable = true; |
| 2211 | Candidate.Conversions.resize(NumArgs + 1); |
| 2212 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2213 | if (Method->isStatic() || !Object) |
| 2214 | // The implicit object argument is ignored. |
| 2215 | Candidate.IgnoreObjectArgument = true; |
| 2216 | else { |
| 2217 | // Determine the implicit conversion sequence for the object |
| 2218 | // parameter. |
| 2219 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
| 2220 | if (Candidate.Conversions[0].ConversionKind |
| 2221 | == ImplicitConversionSequence::BadConversion) { |
| 2222 | Candidate.Viable = false; |
| 2223 | return; |
| 2224 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | // Determine the implicit conversion sequences for each of the |
| 2228 | // arguments. |
| 2229 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2230 | if (ArgIdx < NumArgsInProto) { |
| 2231 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2232 | // exist for each argument an implicit conversion sequence |
| 2233 | // (13.3.3.1) that converts that argument to the corresponding |
| 2234 | // parameter of F. |
| 2235 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2236 | Candidate.Conversions[ArgIdx + 1] |
| 2237 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2238 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2239 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2240 | == ImplicitConversionSequence::BadConversion) { |
| 2241 | Candidate.Viable = false; |
| 2242 | break; |
| 2243 | } |
| 2244 | } else { |
| 2245 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2246 | // argument for which there is no corresponding parameter is |
| 2247 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2248 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2249 | = ImplicitConversionSequence::EllipsisConversion; |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2254 | /// \brief Add a C++ function template as a candidate in the candidate set, |
| 2255 | /// using template argument deduction to produce an appropriate function |
| 2256 | /// template specialization. |
| 2257 | void |
| 2258 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2259 | bool HasExplicitTemplateArgs, |
| 2260 | const TemplateArgument *ExplicitTemplateArgs, |
| 2261 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2262 | Expr **Args, unsigned NumArgs, |
| 2263 | OverloadCandidateSet& CandidateSet, |
| 2264 | bool SuppressUserConversions, |
| 2265 | bool ForceRValue) { |
| 2266 | // C++ [over.match.funcs]p7: |
| 2267 | // In each case where a candidate is a function template, candidate |
| 2268 | // function template specializations are generated using template argument |
| 2269 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
| 2270 | // candidate functions in the usual way.113) A given name can refer to one |
| 2271 | // or more function templates and also to a set of overloaded non-template |
| 2272 | // functions. In such a case, the candidate functions generated from each |
| 2273 | // function template are combined with the set of non-template candidate |
| 2274 | // functions. |
| 2275 | TemplateDeductionInfo Info(Context); |
| 2276 | FunctionDecl *Specialization = 0; |
| 2277 | if (TemplateDeductionResult Result |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2278 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2279 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2280 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2281 | // FIXME: Record what happened with template argument deduction, so |
| 2282 | // that we can give the user a beautiful diagnostic. |
| 2283 | (void)Result; |
| 2284 | return; |
| 2285 | } |
| 2286 | |
| 2287 | // Add the function template specialization produced by template argument |
| 2288 | // deduction as a candidate. |
| 2289 | assert(Specialization && "Missing function template specialization?"); |
| 2290 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2291 | SuppressUserConversions, ForceRValue); |
| 2292 | } |
| 2293 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2294 | /// AddConversionCandidate - Add a C++ conversion function as a |
| 2295 | /// candidate in the candidate set (C++ [over.match.conv], |
| 2296 | /// C++ [over.match.copy]). From is the expression we're converting from, |
| 2297 | /// and ToType is the type that we're eventually trying to convert to |
| 2298 | /// (which may or may not be the same type as the type that the |
| 2299 | /// conversion function produces). |
| 2300 | void |
| 2301 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2302 | Expr *From, QualType ToType, |
| 2303 | OverloadCandidateSet& CandidateSet) { |
| 2304 | // Add this candidate |
| 2305 | CandidateSet.push_back(OverloadCandidate()); |
| 2306 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2307 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2308 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2309 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2310 | Candidate.FinalConversion.setAsIdentityConversion(); |
| 2311 | Candidate.FinalConversion.FromTypePtr |
| 2312 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2313 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2314 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2315 | // Determine the implicit conversion sequence for the implicit |
| 2316 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2317 | Candidate.Viable = true; |
| 2318 | Candidate.Conversions.resize(1); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2319 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2320 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2321 | if (Candidate.Conversions[0].ConversionKind |
| 2322 | == ImplicitConversionSequence::BadConversion) { |
| 2323 | Candidate.Viable = false; |
| 2324 | return; |
| 2325 | } |
| 2326 | |
| 2327 | // To determine what the conversion from the result of calling the |
| 2328 | // conversion function to the type we're eventually trying to |
| 2329 | // convert to (ToType), we need to synthesize a call to the |
| 2330 | // conversion function and attempt copy initialization from it. This |
| 2331 | // makes sure that we get the right semantics with respect to |
| 2332 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2333 | // call on the stack and we don't need its arguments to be |
| 2334 | // well-formed. |
| 2335 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
| 2336 | SourceLocation()); |
| 2337 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2338 | CastExpr::CK_Unknown, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2339 | &ConversionRef, false); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2340 | |
| 2341 | // Note that it is safe to allocate CallExpr on the stack here because |
| 2342 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2343 | // allocator). |
| 2344 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2345 | Conversion->getConversionType().getNonReferenceType(), |
| 2346 | SourceLocation()); |
| 2347 | ImplicitConversionSequence ICS = TryCopyInitialization(&Call, ToType, true); |
| 2348 | switch (ICS.ConversionKind) { |
| 2349 | case ImplicitConversionSequence::StandardConversion: |
| 2350 | Candidate.FinalConversion = ICS.Standard; |
| 2351 | break; |
| 2352 | |
| 2353 | case ImplicitConversionSequence::BadConversion: |
| 2354 | Candidate.Viable = false; |
| 2355 | break; |
| 2356 | |
| 2357 | default: |
| 2358 | assert(false && |
| 2359 | "Can only end up with a standard conversion sequence or failure"); |
| 2360 | } |
| 2361 | } |
| 2362 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2363 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2364 | /// converts the given @c Object to a function pointer via the |
| 2365 | /// conversion function @c Conversion, and then attempts to call it |
| 2366 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2367 | /// the type of function that we'll eventually be calling. |
| 2368 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2369 | const FunctionProtoType *Proto, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2370 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2371 | OverloadCandidateSet& CandidateSet) { |
| 2372 | CandidateSet.push_back(OverloadCandidate()); |
| 2373 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2374 | Candidate.Function = 0; |
| 2375 | Candidate.Surrogate = Conversion; |
| 2376 | Candidate.Viable = true; |
| 2377 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2378 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2379 | Candidate.Conversions.resize(NumArgs + 1); |
| 2380 | |
| 2381 | // Determine the implicit conversion sequence for the implicit |
| 2382 | // object parameter. |
| 2383 | ImplicitConversionSequence ObjectInit |
| 2384 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2385 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2386 | Candidate.Viable = false; |
| 2387 | return; |
| 2388 | } |
| 2389 | |
| 2390 | // The first conversion is actually a user-defined conversion whose |
| 2391 | // first conversion is ObjectInit's standard conversion (which is |
| 2392 | // effectively a reference binding). Record it as such. |
| 2393 | Candidate.Conversions[0].ConversionKind |
| 2394 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2395 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2396 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
| 2397 | Candidate.Conversions[0].UserDefined.After |
| 2398 | = Candidate.Conversions[0].UserDefined.Before; |
| 2399 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2400 | |
| 2401 | // Find the |
| 2402 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2403 | |
| 2404 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2405 | // parameters is viable only if it has an ellipsis in its parameter |
| 2406 | // list (8.3.5). |
| 2407 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2408 | Candidate.Viable = false; |
| 2409 | return; |
| 2410 | } |
| 2411 | |
| 2412 | // Function types don't have any default arguments, so just check if |
| 2413 | // we have enough arguments. |
| 2414 | if (NumArgs < NumArgsInProto) { |
| 2415 | // Not enough arguments. |
| 2416 | Candidate.Viable = false; |
| 2417 | return; |
| 2418 | } |
| 2419 | |
| 2420 | // Determine the implicit conversion sequences for each of the |
| 2421 | // arguments. |
| 2422 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2423 | if (ArgIdx < NumArgsInProto) { |
| 2424 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2425 | // exist for each argument an implicit conversion sequence |
| 2426 | // (13.3.3.1) that converts that argument to the corresponding |
| 2427 | // parameter of F. |
| 2428 | QualType ParamType = Proto->getArgType(ArgIdx); |
| 2429 | Candidate.Conversions[ArgIdx + 1] |
| 2430 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
| 2431 | /*SuppressUserConversions=*/false); |
| 2432 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2433 | == ImplicitConversionSequence::BadConversion) { |
| 2434 | Candidate.Viable = false; |
| 2435 | break; |
| 2436 | } |
| 2437 | } else { |
| 2438 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2439 | // argument for which there is no corresponding parameter is |
| 2440 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
| 2441 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
| 2442 | = ImplicitConversionSequence::EllipsisConversion; |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2447 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2448 | // operator overloading logic over to the scheme used by binary operators, which |
| 2449 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2450 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2451 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2452 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2453 | OverloadCandidateSet& CandidateSet, |
| 2454 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2455 | |
| 2456 | FunctionSet Functions; |
| 2457 | |
| 2458 | QualType T1 = Args[0]->getType(); |
| 2459 | QualType T2; |
| 2460 | if (NumArgs > 1) |
| 2461 | T2 = Args[1]->getType(); |
| 2462 | |
| 2463 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2464 | if (S) |
| 2465 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2466 | ArgumentDependentLookup(OpName, Args, NumArgs, Functions); |
| 2467 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2468 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 2469 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
| 2470 | } |
| 2471 | |
| 2472 | /// \brief Add overload candidates for overloaded operators that are |
| 2473 | /// member functions. |
| 2474 | /// |
| 2475 | /// Add the overloaded operator candidates that are member functions |
| 2476 | /// for the operator Op that was used in an operator expression such |
| 2477 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2478 | /// CandidateSet will store the added overload candidates. (C++ |
| 2479 | /// [over.match.oper]). |
| 2480 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2481 | SourceLocation OpLoc, |
| 2482 | Expr **Args, unsigned NumArgs, |
| 2483 | OverloadCandidateSet& CandidateSet, |
| 2484 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2485 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2486 | |
| 2487 | // C++ [over.match.oper]p3: |
| 2488 | // For a unary operator @ with an operand of a type whose |
| 2489 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2490 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2491 | // a right operand of a type whose cv-unqualified version is T2, |
| 2492 | // three sets of candidate functions, designated member |
| 2493 | // candidates, non-member candidates and built-in candidates, are |
| 2494 | // constructed as follows: |
| 2495 | QualType T1 = Args[0]->getType(); |
| 2496 | QualType T2; |
| 2497 | if (NumArgs > 1) |
| 2498 | T2 = Args[1]->getType(); |
| 2499 | |
| 2500 | // -- If T1 is a class type, the set of member candidates is the |
| 2501 | // result of the qualified lookup of T1::operator@ |
| 2502 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2503 | // empty. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2504 | // FIXME: Lookup in base classes, too! |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2505 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2506 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2507 | for (llvm::tie(Oper, OperEnd) = T1Rec->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2508 | Oper != OperEnd; ++Oper) |
| 2509 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], |
| 2510 | Args+1, NumArgs - 1, CandidateSet, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2511 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2512 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2515 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2516 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2517 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2518 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2519 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2520 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2521 | /// (at the beginning of the argument list) that will be contextually |
| 2522 | /// converted to bool. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2523 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
| 2524 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2525 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2526 | bool IsAssignmentOperator, |
| 2527 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2528 | // Add this candidate |
| 2529 | CandidateSet.push_back(OverloadCandidate()); |
| 2530 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2531 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2532 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2533 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2534 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2535 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2536 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2537 | |
| 2538 | // Determine the implicit conversion sequences for each of the |
| 2539 | // arguments. |
| 2540 | Candidate.Viable = true; |
| 2541 | Candidate.Conversions.resize(NumArgs); |
| 2542 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2543 | // C++ [over.match.oper]p4: |
| 2544 | // For the built-in assignment operators, conversions of the |
| 2545 | // left operand are restricted as follows: |
| 2546 | // -- no temporaries are introduced to hold the left operand, and |
| 2547 | // -- no user-defined conversions are applied to the left |
| 2548 | // operand to achieve a type match with the left-most |
| 2549 | // parameter of a built-in candidate. |
| 2550 | // |
| 2551 | // We block these conversions by turning off user-defined |
| 2552 | // conversions, since that is the only way that initialization of |
| 2553 | // a reference to a non-class type can occur from something that |
| 2554 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2555 | if (ArgIdx < NumContextualBoolArguments) { |
| 2556 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
| 2557 | "Contextual conversion to bool requires bool type"); |
| 2558 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2559 | } else { |
| 2560 | Candidate.Conversions[ArgIdx] |
| 2561 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
| 2562 | ArgIdx == 0 && IsAssignmentOperator); |
| 2563 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2564 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2565 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2566 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2567 | break; |
| 2568 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2573 | /// candidate operator functions for built-in operators (C++ |
| 2574 | /// [over.built]). The types are separated into pointer types and |
| 2575 | /// enumeration types. |
| 2576 | class BuiltinCandidateTypeSet { |
| 2577 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2578 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2579 | |
| 2580 | /// PointerTypes - The set of pointer types that will be used in the |
| 2581 | /// built-in candidates. |
| 2582 | TypeSet PointerTypes; |
| 2583 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2584 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2585 | /// used in the built-in candidates. |
| 2586 | TypeSet MemberPointerTypes; |
| 2587 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2588 | /// EnumerationTypes - The set of enumeration types that will be |
| 2589 | /// used in the built-in candidates. |
| 2590 | TypeSet EnumerationTypes; |
| 2591 | |
| 2592 | /// Context - The AST context in which we will build the type sets. |
| 2593 | ASTContext &Context; |
| 2594 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2595 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| 2596 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2597 | |
| 2598 | public: |
| 2599 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2600 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2601 | |
| 2602 | BuiltinCandidateTypeSet(ASTContext &Context) : Context(Context) { } |
| 2603 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2604 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions, |
| 2605 | bool AllowExplicitConversions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2606 | |
| 2607 | /// pointer_begin - First pointer type found; |
| 2608 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2609 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2610 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2611 | iterator pointer_end() { return PointerTypes.end(); } |
| 2612 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2613 | /// member_pointer_begin - First member pointer type found; |
| 2614 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2615 | |
| 2616 | /// member_pointer_end - Past the last member pointer type found; |
| 2617 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2618 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2619 | /// enumeration_begin - First enumeration type found; |
| 2620 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2621 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2622 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2623 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2624 | }; |
| 2625 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2626 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2627 | /// the set of pointer types along with any more-qualified variants of |
| 2628 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2629 | /// will add "int const *", "int const volatile *", "int const |
| 2630 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2631 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2632 | /// false otherwise. |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2633 | bool |
| 2634 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2635 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2636 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2637 | return false; |
| 2638 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2639 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2640 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2641 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2642 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2643 | // FIXME: Do we have to add CVR qualifiers at *all* levels to deal with all |
| 2644 | // pointer conversions that don't cast away constness? |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2645 | if (!PointeeTy.isConstQualified()) |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2646 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2647 | (Context.getPointerType(PointeeTy.withConst())); |
| 2648 | if (!PointeeTy.isVolatileQualified()) |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2649 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2650 | (Context.getPointerType(PointeeTy.withVolatile())); |
| 2651 | if (!PointeeTy.isRestrictQualified()) |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2652 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2653 | (Context.getPointerType(PointeeTy.withRestrict())); |
| 2654 | } |
| 2655 | |
| 2656 | return true; |
| 2657 | } |
| 2658 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2659 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 2660 | /// to the set of pointer types along with any more-qualified variants of |
| 2661 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2662 | /// will add "int const *", "int const volatile *", "int const |
| 2663 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2664 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2665 | /// false otherwise. |
| 2666 | bool |
| 2667 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 2668 | QualType Ty) { |
| 2669 | // Insert this type. |
| 2670 | if (!MemberPointerTypes.insert(Ty)) |
| 2671 | return false; |
| 2672 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2673 | if (const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>()) { |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2674 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2675 | const Type *ClassTy = PointerTy->getClass(); |
| 2676 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2677 | |
| 2678 | if (!PointeeTy.isConstQualified()) |
| 2679 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2680 | (Context.getMemberPointerType(PointeeTy.withConst(), ClassTy)); |
| 2681 | if (!PointeeTy.isVolatileQualified()) |
| 2682 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2683 | (Context.getMemberPointerType(PointeeTy.withVolatile(), ClassTy)); |
| 2684 | if (!PointeeTy.isRestrictQualified()) |
| 2685 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2686 | (Context.getMemberPointerType(PointeeTy.withRestrict(), ClassTy)); |
| 2687 | } |
| 2688 | |
| 2689 | return true; |
| 2690 | } |
| 2691 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2692 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2693 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2694 | /// primarily interested in pointer types and enumeration types. We also |
| 2695 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2696 | /// AllowUserConversions is true if we should look at the conversion |
| 2697 | /// functions of a class type, and AllowExplicitConversions if we |
| 2698 | /// should also include the explicit conversion functions of a class |
| 2699 | /// type. |
| 2700 | void |
| 2701 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2702 | bool AllowUserConversions, |
| 2703 | bool AllowExplicitConversions) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2704 | // Only deal with canonical types. |
| 2705 | Ty = Context.getCanonicalType(Ty); |
| 2706 | |
| 2707 | // Look through reference types; they aren't part of the type of an |
| 2708 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2709 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2710 | Ty = RefTy->getPointeeType(); |
| 2711 | |
| 2712 | // We don't care about qualifiers on the type. |
| 2713 | Ty = Ty.getUnqualifiedType(); |
| 2714 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2715 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2716 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2717 | |
| 2718 | // Insert our type, and its more-qualified variants, into the set |
| 2719 | // of types. |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2720 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | |
| 2723 | // Add 'cv void*' to our set of types. |
| 2724 | if (!Ty->isVoidType()) { |
| 2725 | QualType QualVoid |
| 2726 | = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2727 | AddPointerWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | // If this is a pointer to a class type, add pointers to its bases |
| 2731 | // (with the same level of cv-qualification as the original |
| 2732 | // derived class, of course). |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2733 | if (const RecordType *PointeeRec = PointeeTy->getAs<RecordType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2734 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 2735 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2736 | Base != ClassDecl->bases_end(); ++Base) { |
| 2737 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
| 2738 | BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 2739 | |
| 2740 | // Add the pointer type, recursively, so that we get all of |
| 2741 | // the indirect base classes, too. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2742 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2743 | } |
| 2744 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2745 | } else if (Ty->isMemberPointerType()) { |
| 2746 | // Member pointers are far easier, since the pointee can't be converted. |
| 2747 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 2748 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2749 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2750 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2751 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2752 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2753 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 2754 | // FIXME: Visit conversion functions in the base classes, too. |
| 2755 | OverloadedFunctionDecl *Conversions |
| 2756 | = ClassDecl->getConversionFunctions(); |
| 2757 | for (OverloadedFunctionDecl::function_iterator Func |
| 2758 | = Conversions->function_begin(); |
| 2759 | Func != Conversions->function_end(); ++Func) { |
| 2760 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2761 | if (AllowExplicitConversions || !Conv->isExplicit()) |
| 2762 | AddTypesConvertedFrom(Conv->getConversionType(), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | } |
| 2766 | } |
| 2767 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2768 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 2769 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 2770 | /// on the operator @p Op and the arguments given. For example, if the |
| 2771 | /// operator is a binary '+', this routine might add "int |
| 2772 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2773 | void |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2774 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 2775 | Expr **Args, unsigned NumArgs, |
| 2776 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2777 | // The set of "promoted arithmetic types", which are the arithmetic |
| 2778 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 2779 | // that the first few of these types are the promoted integral |
| 2780 | // types; these types need to be first. |
| 2781 | // FIXME: What about complex? |
| 2782 | const unsigned FirstIntegralType = 0; |
| 2783 | const unsigned LastIntegralType = 13; |
| 2784 | const unsigned FirstPromotedIntegralType = 7, |
| 2785 | LastPromotedIntegralType = 13; |
| 2786 | const unsigned FirstPromotedArithmeticType = 7, |
| 2787 | LastPromotedArithmeticType = 16; |
| 2788 | const unsigned NumArithmeticTypes = 16; |
| 2789 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2790 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 2791 | // Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2792 | Context.SignedCharTy, Context.ShortTy, |
| 2793 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 2794 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 2795 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 2796 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 2797 | }; |
| 2798 | |
| 2799 | // Find all of the types that the arguments can convert to, but only |
| 2800 | // if the operator we're looking at has built-in operator candidates |
| 2801 | // that make use of these types. |
| 2802 | BuiltinCandidateTypeSet CandidateTypes(Context); |
| 2803 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 2804 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2805 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2806 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2807 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2808 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2809 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2810 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 2811 | true, |
| 2812 | (Op == OO_Exclaim || |
| 2813 | Op == OO_AmpAmp || |
| 2814 | Op == OO_PipePipe)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | bool isComparison = false; |
| 2818 | switch (Op) { |
| 2819 | case OO_None: |
| 2820 | case NUM_OVERLOADED_OPERATORS: |
| 2821 | assert(false && "Expected an overloaded operator"); |
| 2822 | break; |
| 2823 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2824 | case OO_Star: // '*' is either unary or binary |
| 2825 | if (NumArgs == 1) |
| 2826 | goto UnaryStar; |
| 2827 | else |
| 2828 | goto BinaryStar; |
| 2829 | break; |
| 2830 | |
| 2831 | case OO_Plus: // '+' is either unary or binary |
| 2832 | if (NumArgs == 1) |
| 2833 | goto UnaryPlus; |
| 2834 | else |
| 2835 | goto BinaryPlus; |
| 2836 | break; |
| 2837 | |
| 2838 | case OO_Minus: // '-' is either unary or binary |
| 2839 | if (NumArgs == 1) |
| 2840 | goto UnaryMinus; |
| 2841 | else |
| 2842 | goto BinaryMinus; |
| 2843 | break; |
| 2844 | |
| 2845 | case OO_Amp: // '&' is either unary or binary |
| 2846 | if (NumArgs == 1) |
| 2847 | goto UnaryAmp; |
| 2848 | else |
| 2849 | goto BinaryAmp; |
| 2850 | |
| 2851 | case OO_PlusPlus: |
| 2852 | case OO_MinusMinus: |
| 2853 | // C++ [over.built]p3: |
| 2854 | // |
| 2855 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 2856 | // is either volatile or empty, there exist candidate operator |
| 2857 | // functions of the form |
| 2858 | // |
| 2859 | // VQ T& operator++(VQ T&); |
| 2860 | // T operator++(VQ T&, int); |
| 2861 | // |
| 2862 | // C++ [over.built]p4: |
| 2863 | // |
| 2864 | // For every pair (T, VQ), where T is an arithmetic type other |
| 2865 | // than bool, and VQ is either volatile or empty, there exist |
| 2866 | // candidate operator functions of the form |
| 2867 | // |
| 2868 | // VQ T& operator--(VQ T&); |
| 2869 | // T operator--(VQ T&, int); |
| 2870 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 2871 | Arith < NumArithmeticTypes; ++Arith) { |
| 2872 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 2873 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2874 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2875 | |
| 2876 | // Non-volatile version. |
| 2877 | if (NumArgs == 1) |
| 2878 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2879 | else |
| 2880 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 2881 | |
| 2882 | // Volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2883 | ParamTypes[0] = Context.getLValueReferenceType(ArithTy.withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2884 | if (NumArgs == 1) |
| 2885 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2886 | else |
| 2887 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 2888 | } |
| 2889 | |
| 2890 | // C++ [over.built]p5: |
| 2891 | // |
| 2892 | // For every pair (T, VQ), where T is a cv-qualified or |
| 2893 | // cv-unqualified object type, and VQ is either volatile or |
| 2894 | // empty, there exist candidate operator functions of the form |
| 2895 | // |
| 2896 | // T*VQ& operator++(T*VQ&); |
| 2897 | // T*VQ& operator--(T*VQ&); |
| 2898 | // T* operator++(T*VQ&, int); |
| 2899 | // T* operator--(T*VQ&, int); |
| 2900 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2901 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2902 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2903 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2904 | continue; |
| 2905 | |
| 2906 | QualType ParamTypes[2] = { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2907 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2908 | }; |
| 2909 | |
| 2910 | // Without volatile |
| 2911 | if (NumArgs == 1) |
| 2912 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2913 | else |
| 2914 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2915 | |
| 2916 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 2917 | // With volatile |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2918 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2919 | if (NumArgs == 1) |
| 2920 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 2921 | else |
| 2922 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 2923 | } |
| 2924 | } |
| 2925 | break; |
| 2926 | |
| 2927 | UnaryStar: |
| 2928 | // C++ [over.built]p6: |
| 2929 | // For every cv-qualified or cv-unqualified object type T, there |
| 2930 | // exist candidate operator functions of the form |
| 2931 | // |
| 2932 | // T& operator*(T*); |
| 2933 | // |
| 2934 | // C++ [over.built]p7: |
| 2935 | // For every function type T, there exist candidate operator |
| 2936 | // functions of the form |
| 2937 | // T& operator*(T*); |
| 2938 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2939 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2940 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2941 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2942 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2943 | &ParamTy, Args, 1, CandidateSet); |
| 2944 | } |
| 2945 | break; |
| 2946 | |
| 2947 | UnaryPlus: |
| 2948 | // C++ [over.built]p8: |
| 2949 | // For every type T, there exist candidate operator functions of |
| 2950 | // the form |
| 2951 | // |
| 2952 | // T* operator+(T*); |
| 2953 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 2954 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 2955 | QualType ParamTy = *Ptr; |
| 2956 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 2957 | } |
| 2958 | |
| 2959 | // Fall through |
| 2960 | |
| 2961 | UnaryMinus: |
| 2962 | // C++ [over.built]p9: |
| 2963 | // For every promoted arithmetic type T, there exist candidate |
| 2964 | // operator functions of the form |
| 2965 | // |
| 2966 | // T operator+(T); |
| 2967 | // T operator-(T); |
| 2968 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 2969 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 2970 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 2971 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 2972 | } |
| 2973 | break; |
| 2974 | |
| 2975 | case OO_Tilde: |
| 2976 | // C++ [over.built]p10: |
| 2977 | // For every promoted integral type T, there exist candidate |
| 2978 | // operator functions of the form |
| 2979 | // |
| 2980 | // T operator~(T); |
| 2981 | for (unsigned Int = FirstPromotedIntegralType; |
| 2982 | Int < LastPromotedIntegralType; ++Int) { |
| 2983 | QualType IntTy = ArithmeticTypes[Int]; |
| 2984 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 2985 | } |
| 2986 | break; |
| 2987 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2988 | case OO_New: |
| 2989 | case OO_Delete: |
| 2990 | case OO_Array_New: |
| 2991 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2992 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2993 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2994 | break; |
| 2995 | |
| 2996 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2997 | UnaryAmp: |
| 2998 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2999 | // C++ [over.match.oper]p3: |
| 3000 | // -- For the operator ',', the unary operator '&', or the |
| 3001 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3002 | break; |
| 3003 | |
| 3004 | case OO_Less: |
| 3005 | case OO_Greater: |
| 3006 | case OO_LessEqual: |
| 3007 | case OO_GreaterEqual: |
| 3008 | case OO_EqualEqual: |
| 3009 | case OO_ExclaimEqual: |
| 3010 | // C++ [over.built]p15: |
| 3011 | // |
| 3012 | // For every pointer or enumeration type T, there exist |
| 3013 | // candidate operator functions of the form |
| 3014 | // |
| 3015 | // bool operator<(T, T); |
| 3016 | // bool operator>(T, T); |
| 3017 | // bool operator<=(T, T); |
| 3018 | // bool operator>=(T, T); |
| 3019 | // bool operator==(T, T); |
| 3020 | // bool operator!=(T, T); |
| 3021 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3022 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3023 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3024 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3025 | } |
| 3026 | for (BuiltinCandidateTypeSet::iterator Enum |
| 3027 | = CandidateTypes.enumeration_begin(); |
| 3028 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3029 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3030 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3031 | } |
| 3032 | |
| 3033 | // Fall through. |
| 3034 | isComparison = true; |
| 3035 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3036 | BinaryPlus: |
| 3037 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3038 | if (!isComparison) { |
| 3039 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3040 | |
| 3041 | // C++ [over.built]p13: |
| 3042 | // |
| 3043 | // For every cv-qualified or cv-unqualified object type T |
| 3044 | // there exist candidate operator functions of the form |
| 3045 | // |
| 3046 | // T* operator+(T*, ptrdiff_t); |
| 3047 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3048 | // T* operator-(T*, ptrdiff_t); |
| 3049 | // T* operator+(ptrdiff_t, T*); |
| 3050 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3051 | // |
| 3052 | // C++ [over.built]p14: |
| 3053 | // |
| 3054 | // For every T, where T is a pointer to object type, there |
| 3055 | // exist candidate operator functions of the form |
| 3056 | // |
| 3057 | // ptrdiff_t operator-(T, T); |
| 3058 | for (BuiltinCandidateTypeSet::iterator Ptr |
| 3059 | = CandidateTypes.pointer_begin(); |
| 3060 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3061 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3062 | |
| 3063 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3064 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3065 | |
| 3066 | if (Op == OO_Plus) { |
| 3067 | // T* operator+(ptrdiff_t, T*); |
| 3068 | ParamTypes[0] = ParamTypes[1]; |
| 3069 | ParamTypes[1] = *Ptr; |
| 3070 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3071 | } else { |
| 3072 | // ptrdiff_t operator-(T, T); |
| 3073 | ParamTypes[1] = *Ptr; |
| 3074 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3075 | Args, 2, CandidateSet); |
| 3076 | } |
| 3077 | } |
| 3078 | } |
| 3079 | // Fall through |
| 3080 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3081 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3082 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3083 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3084 | // C++ [over.built]p12: |
| 3085 | // |
| 3086 | // For every pair of promoted arithmetic types L and R, there |
| 3087 | // exist candidate operator functions of the form |
| 3088 | // |
| 3089 | // LR operator*(L, R); |
| 3090 | // LR operator/(L, R); |
| 3091 | // LR operator+(L, R); |
| 3092 | // LR operator-(L, R); |
| 3093 | // bool operator<(L, R); |
| 3094 | // bool operator>(L, R); |
| 3095 | // bool operator<=(L, R); |
| 3096 | // bool operator>=(L, R); |
| 3097 | // bool operator==(L, R); |
| 3098 | // bool operator!=(L, R); |
| 3099 | // |
| 3100 | // where LR is the result of the usual arithmetic conversions |
| 3101 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3102 | // |
| 3103 | // C++ [over.built]p24: |
| 3104 | // |
| 3105 | // For every pair of promoted arithmetic types L and R, there exist |
| 3106 | // candidate operator functions of the form |
| 3107 | // |
| 3108 | // LR operator?(bool, L, R); |
| 3109 | // |
| 3110 | // where LR is the result of the usual arithmetic conversions |
| 3111 | // between types L and R. |
| 3112 | // Our candidates ignore the first parameter. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3113 | for (unsigned Left = FirstPromotedArithmeticType; |
| 3114 | Left < LastPromotedArithmeticType; ++Left) { |
| 3115 | for (unsigned Right = FirstPromotedArithmeticType; |
| 3116 | Right < LastPromotedArithmeticType; ++Right) { |
| 3117 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3118 | QualType Result |
| 3119 | = isComparison? Context.BoolTy |
| 3120 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 3121 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3122 | } |
| 3123 | } |
| 3124 | break; |
| 3125 | |
| 3126 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3127 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3128 | case OO_Caret: |
| 3129 | case OO_Pipe: |
| 3130 | case OO_LessLess: |
| 3131 | case OO_GreaterGreater: |
| 3132 | // C++ [over.built]p17: |
| 3133 | // |
| 3134 | // For every pair of promoted integral types L and R, there |
| 3135 | // exist candidate operator functions of the form |
| 3136 | // |
| 3137 | // LR operator%(L, R); |
| 3138 | // LR operator&(L, R); |
| 3139 | // LR operator^(L, R); |
| 3140 | // LR operator|(L, R); |
| 3141 | // L operator<<(L, R); |
| 3142 | // L operator>>(L, R); |
| 3143 | // |
| 3144 | // where LR is the result of the usual arithmetic conversions |
| 3145 | // between types L and R. |
| 3146 | for (unsigned Left = FirstPromotedIntegralType; |
| 3147 | Left < LastPromotedIntegralType; ++Left) { |
| 3148 | for (unsigned Right = FirstPromotedIntegralType; |
| 3149 | Right < LastPromotedIntegralType; ++Right) { |
| 3150 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3151 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3152 | ? LandR[0] |
| 3153 | : UsualArithmeticConversionsType(LandR[0], LandR[1]); |
| 3154 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3155 | } |
| 3156 | } |
| 3157 | break; |
| 3158 | |
| 3159 | case OO_Equal: |
| 3160 | // C++ [over.built]p20: |
| 3161 | // |
| 3162 | // For every pair (T, VQ), where T is an enumeration or |
| 3163 | // (FIXME:) pointer to member type and VQ is either volatile or |
| 3164 | // empty, there exist candidate operator functions of the form |
| 3165 | // |
| 3166 | // VQ T& operator=(VQ T&, T); |
| 3167 | for (BuiltinCandidateTypeSet::iterator Enum |
| 3168 | = CandidateTypes.enumeration_begin(); |
| 3169 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3170 | QualType ParamTypes[2]; |
| 3171 | |
| 3172 | // T& operator=(T&, T) |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3173 | ParamTypes[0] = Context.getLValueReferenceType(*Enum); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3174 | ParamTypes[1] = *Enum; |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3175 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3176 | /*IsAssignmentOperator=*/false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3178 | if (!Context.getCanonicalType(*Enum).isVolatileQualified()) { |
| 3179 | // volatile T& operator=(volatile T&, T) |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3180 | ParamTypes[0] = Context.getLValueReferenceType((*Enum).withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3181 | ParamTypes[1] = *Enum; |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3182 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3183 | /*IsAssignmentOperator=*/false); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3184 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3185 | } |
| 3186 | // Fall through. |
| 3187 | |
| 3188 | case OO_PlusEqual: |
| 3189 | case OO_MinusEqual: |
| 3190 | // C++ [over.built]p19: |
| 3191 | // |
| 3192 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3193 | // volatile or empty, there exist candidate operator functions |
| 3194 | // of the form |
| 3195 | // |
| 3196 | // T*VQ& operator=(T*VQ&, T*); |
| 3197 | // |
| 3198 | // C++ [over.built]p21: |
| 3199 | // |
| 3200 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3201 | // cv-unqualified object type and VQ is either volatile or |
| 3202 | // empty, there exist candidate operator functions of the form |
| 3203 | // |
| 3204 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3205 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3206 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3207 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3208 | QualType ParamTypes[2]; |
| 3209 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3210 | |
| 3211 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3212 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3213 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3214 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3215 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3216 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3217 | // volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3218 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3219 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3220 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3221 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3222 | } |
| 3223 | // Fall through. |
| 3224 | |
| 3225 | case OO_StarEqual: |
| 3226 | case OO_SlashEqual: |
| 3227 | // C++ [over.built]p18: |
| 3228 | // |
| 3229 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3230 | // VQ is either volatile or empty, and R is a promoted |
| 3231 | // arithmetic type, there exist candidate operator functions of |
| 3232 | // the form |
| 3233 | // |
| 3234 | // VQ L& operator=(VQ L&, R); |
| 3235 | // VQ L& operator*=(VQ L&, R); |
| 3236 | // VQ L& operator/=(VQ L&, R); |
| 3237 | // VQ L& operator+=(VQ L&, R); |
| 3238 | // VQ L& operator-=(VQ L&, R); |
| 3239 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 3240 | for (unsigned Right = FirstPromotedArithmeticType; |
| 3241 | Right < LastPromotedArithmeticType; ++Right) { |
| 3242 | QualType ParamTypes[2]; |
| 3243 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3244 | |
| 3245 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3246 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3247 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3248 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3251 | ParamTypes[0] = ArithmeticTypes[Left].withVolatile(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3252 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3253 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3254 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3255 | } |
| 3256 | } |
| 3257 | break; |
| 3258 | |
| 3259 | case OO_PercentEqual: |
| 3260 | case OO_LessLessEqual: |
| 3261 | case OO_GreaterGreaterEqual: |
| 3262 | case OO_AmpEqual: |
| 3263 | case OO_CaretEqual: |
| 3264 | case OO_PipeEqual: |
| 3265 | // C++ [over.built]p22: |
| 3266 | // |
| 3267 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3268 | // is either volatile or empty, and R is a promoted integral |
| 3269 | // type, there exist candidate operator functions of the form |
| 3270 | // |
| 3271 | // VQ L& operator%=(VQ L&, R); |
| 3272 | // VQ L& operator<<=(VQ L&, R); |
| 3273 | // VQ L& operator>>=(VQ L&, R); |
| 3274 | // VQ L& operator&=(VQ L&, R); |
| 3275 | // VQ L& operator^=(VQ L&, R); |
| 3276 | // VQ L& operator|=(VQ L&, R); |
| 3277 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 3278 | for (unsigned Right = FirstPromotedIntegralType; |
| 3279 | Right < LastPromotedIntegralType; ++Right) { |
| 3280 | QualType ParamTypes[2]; |
| 3281 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3282 | |
| 3283 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3284 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3285 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3286 | |
| 3287 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3288 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3289 | ParamTypes[0].addVolatile(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3290 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3291 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3292 | } |
| 3293 | } |
| 3294 | break; |
| 3295 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3296 | case OO_Exclaim: { |
| 3297 | // C++ [over.operator]p23: |
| 3298 | // |
| 3299 | // There also exist candidate operator functions of the form |
| 3300 | // |
| 3301 | // bool operator!(bool); |
| 3302 | // bool operator&&(bool, bool); [BELOW] |
| 3303 | // bool operator||(bool, bool); [BELOW] |
| 3304 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3305 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3306 | /*IsAssignmentOperator=*/false, |
| 3307 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3308 | break; |
| 3309 | } |
| 3310 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3311 | case OO_AmpAmp: |
| 3312 | case OO_PipePipe: { |
| 3313 | // C++ [over.operator]p23: |
| 3314 | // |
| 3315 | // There also exist candidate operator functions of the form |
| 3316 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3317 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3318 | // bool operator&&(bool, bool); |
| 3319 | // bool operator||(bool, bool); |
| 3320 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3321 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3322 | /*IsAssignmentOperator=*/false, |
| 3323 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3324 | break; |
| 3325 | } |
| 3326 | |
| 3327 | case OO_Subscript: |
| 3328 | // C++ [over.built]p13: |
| 3329 | // |
| 3330 | // For every cv-qualified or cv-unqualified object type T there |
| 3331 | // exist candidate operator functions of the form |
| 3332 | // |
| 3333 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3334 | // T& operator[](T*, ptrdiff_t); |
| 3335 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3336 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3337 | // T& operator[](ptrdiff_t, T*); |
| 3338 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3339 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3340 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3341 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3342 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3343 | |
| 3344 | // T& operator[](T*, ptrdiff_t) |
| 3345 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3346 | |
| 3347 | // T& operator[](ptrdiff_t, T*); |
| 3348 | ParamTypes[0] = ParamTypes[1]; |
| 3349 | ParamTypes[1] = *Ptr; |
| 3350 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3351 | } |
| 3352 | break; |
| 3353 | |
| 3354 | case OO_ArrowStar: |
| 3355 | // FIXME: No support for pointer-to-members yet. |
| 3356 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3357 | |
| 3358 | case OO_Conditional: |
| 3359 | // Note that we don't consider the first argument, since it has been |
| 3360 | // contextually converted to bool long ago. The candidates below are |
| 3361 | // therefore added as binary. |
| 3362 | // |
| 3363 | // C++ [over.built]p24: |
| 3364 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3365 | // there exist candidate operator functions of the form |
| 3366 | // |
| 3367 | // T operator?(bool, T, T); |
| 3368 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3369 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3370 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3371 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3372 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3373 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3374 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3375 | CandidateTypes.member_pointer_begin(), |
| 3376 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3377 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3378 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3379 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3380 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3384 | /// \brief Add function candidates found via argument-dependent lookup |
| 3385 | /// to the set of overloading candidates. |
| 3386 | /// |
| 3387 | /// This routine performs argument-dependent name lookup based on the |
| 3388 | /// given function name (which may also be an operator name) and adds |
| 3389 | /// all of the overload candidates found by ADL to the overload |
| 3390 | /// candidate set (C++ [basic.lookup.argdep]). |
| 3391 | void |
| 3392 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3393 | Expr **Args, unsigned NumArgs, |
| 3394 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3395 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3397 | // Record all of the function candidates that we've already |
| 3398 | // added to the overload set, so that we don't add those same |
| 3399 | // candidates a second time. |
| 3400 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3401 | CandEnd = CandidateSet.end(); |
| 3402 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3403 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3404 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3405 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3406 | Functions.insert(FunTmpl); |
| 3407 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3409 | ArgumentDependentLookup(Name, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3410 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3411 | // Erase all of the candidates we already knew about. |
| 3412 | // FIXME: This is suboptimal. Is there a better way? |
| 3413 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3414 | CandEnd = CandidateSet.end(); |
| 3415 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3416 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3417 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3418 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3419 | Functions.erase(FunTmpl); |
| 3420 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3421 | |
| 3422 | // For each of the ADL candidates we found, add it to the overload |
| 3423 | // set. |
| 3424 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3425 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3426 | Func != FuncEnd; ++Func) { |
| 3427 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) |
| 3428 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet); |
| 3429 | else |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3430 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
| 3431 | /*FIXME: explicit args */false, 0, 0, |
| 3432 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3433 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3436 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3437 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
| 3438 | bool |
| 3439 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
| 3440 | const OverloadCandidate& Cand2) |
| 3441 | { |
| 3442 | // Define viable functions to be better candidates than non-viable |
| 3443 | // functions. |
| 3444 | if (!Cand2.Viable) |
| 3445 | return Cand1.Viable; |
| 3446 | else if (!Cand1.Viable) |
| 3447 | return false; |
| 3448 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3449 | // C++ [over.match.best]p1: |
| 3450 | // |
| 3451 | // -- if F is a static member function, ICS1(F) is defined such |
| 3452 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3453 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3454 | // better nor worse than ICS1(F). |
| 3455 | unsigned StartArg = 0; |
| 3456 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3457 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3459 | // C++ [over.match.best]p1: |
| 3460 | // A viable function F1 is defined to be a better function than another |
| 3461 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
| 3462 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3463 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3464 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3465 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3466 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3467 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3468 | Cand2.Conversions[ArgIdx])) { |
| 3469 | case ImplicitConversionSequence::Better: |
| 3470 | // Cand1 has a better conversion sequence. |
| 3471 | HasBetterConversion = true; |
| 3472 | break; |
| 3473 | |
| 3474 | case ImplicitConversionSequence::Worse: |
| 3475 | // Cand1 can't be better than Cand2. |
| 3476 | return false; |
| 3477 | |
| 3478 | case ImplicitConversionSequence::Indistinguishable: |
| 3479 | // Do nothing. |
| 3480 | break; |
| 3481 | } |
| 3482 | } |
| 3483 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3484 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
| 3485 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3486 | if (HasBetterConversion) |
| 3487 | return true; |
| 3488 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3489 | // - F1 is a non-template function and F2 is a function template |
| 3490 | // specialization, or, if not that, |
| 3491 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 3492 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 3493 | return true; |
| 3494 | |
| 3495 | // -- F1 and F2 are function template specializations, and the function |
| 3496 | // template for F1 is more specialized than the template for F2 |
| 3497 | // according to the partial ordering rules described in 14.5.5.2, or, |
| 3498 | // if not that, |
| 3499 | |
| 3500 | // FIXME: Implement partial ordering of function templates. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3502 | // -- the context is an initialization by user-defined conversion |
| 3503 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 3504 | // from the return type of F1 to the destination type (i.e., |
| 3505 | // the type of the entity being initialized) is a better |
| 3506 | // conversion sequence than the standard conversion sequence |
| 3507 | // from the return type of F2 to the destination type. |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3508 | if (Cand1.Function && Cand2.Function && |
| 3509 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3510 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 3511 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 3512 | Cand2.FinalConversion)) { |
| 3513 | case ImplicitConversionSequence::Better: |
| 3514 | // Cand1 has a better conversion sequence. |
| 3515 | return true; |
| 3516 | |
| 3517 | case ImplicitConversionSequence::Worse: |
| 3518 | // Cand1 can't be better than Cand2. |
| 3519 | return false; |
| 3520 | |
| 3521 | case ImplicitConversionSequence::Indistinguishable: |
| 3522 | // Do nothing |
| 3523 | break; |
| 3524 | } |
| 3525 | } |
| 3526 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3527 | return false; |
| 3528 | } |
| 3529 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3530 | /// \brief Computes the best viable function (C++ 13.3.3) |
| 3531 | /// within an overload candidate set. |
| 3532 | /// |
| 3533 | /// \param CandidateSet the set of candidate functions. |
| 3534 | /// |
| 3535 | /// \param Loc the location of the function name (or operator symbol) for |
| 3536 | /// which overload resolution occurs. |
| 3537 | /// |
| 3538 | /// \param Best f overload resolution was successful or found a deleted |
| 3539 | /// function, Best points to the candidate function found. |
| 3540 | /// |
| 3541 | /// \returns The result of overload resolution. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3542 | Sema::OverloadingResult |
| 3543 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3544 | SourceLocation Loc, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3545 | OverloadCandidateSet::iterator& Best) |
| 3546 | { |
| 3547 | // Find the best viable function. |
| 3548 | Best = CandidateSet.end(); |
| 3549 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3550 | Cand != CandidateSet.end(); ++Cand) { |
| 3551 | if (Cand->Viable) { |
| 3552 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 3553 | Best = Cand; |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | // If we didn't find any viable functions, abort. |
| 3558 | if (Best == CandidateSet.end()) |
| 3559 | return OR_No_Viable_Function; |
| 3560 | |
| 3561 | // Make sure that this function is better than every other viable |
| 3562 | // function. If not, we have an ambiguity. |
| 3563 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3564 | Cand != CandidateSet.end(); ++Cand) { |
| 3565 | if (Cand->Viable && |
| 3566 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3567 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 3568 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3569 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3570 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3574 | if (Best->Function && |
| 3575 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3576 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3577 | return OR_Deleted; |
| 3578 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3579 | // C++ [basic.def.odr]p2: |
| 3580 | // An overloaded function is used if it is selected by overload resolution |
| 3581 | // when referred to from a potentially-evaluated expression. [Note: this |
| 3582 | // covers calls to named functions (5.2.2), operator overloading |
| 3583 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 3584 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 3585 | if (Best->Function) |
| 3586 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3587 | return OR_Success; |
| 3588 | } |
| 3589 | |
| 3590 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 3591 | /// diagnostic messages containing the candidates in the candidate |
| 3592 | /// set. If OnlyViable is true, only viable candidates will be printed. |
| 3593 | void |
| 3594 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
| 3595 | bool OnlyViable) |
| 3596 | { |
| 3597 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3598 | LastCand = CandidateSet.end(); |
| 3599 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3600 | if (Cand->Viable || !OnlyViable) { |
| 3601 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3602 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3603 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3604 | // Deleted or "unavailable" function. |
| 3605 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 3606 | << Cand->Function->isDeleted(); |
| 3607 | } else { |
| 3608 | // Normal function |
| 3609 | // FIXME: Give a better reason! |
| 3610 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
| 3611 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3612 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3613 | // Desugar the type of the surrogate down to a function type, |
| 3614 | // retaining as many typedefs as possible while still showing |
| 3615 | // the function type (and, therefore, its parameter types). |
| 3616 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3617 | bool isLValueReference = false; |
| 3618 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3619 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3620 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3621 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3622 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3623 | isLValueReference = true; |
| 3624 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3625 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3626 | FnType = FnTypeRef->getPointeeType(); |
| 3627 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3628 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3629 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3630 | FnType = FnTypePtr->getPointeeType(); |
| 3631 | isPointer = true; |
| 3632 | } |
| 3633 | // Desugar down to a function type. |
| 3634 | FnType = QualType(FnType->getAsFunctionType(), 0); |
| 3635 | // Reconstruct the pointer/reference as appropriate. |
| 3636 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3637 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 3638 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3639 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3640 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3641 | << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3642 | } else { |
| 3643 | // FIXME: We need to get the identifier in here |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3644 | // FIXME: Do we want the error message to point at the operator? |
| 3645 | // (built-ins won't have a location) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3646 | QualType FnType |
| 3647 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 3648 | Cand->BuiltinTypes.ParamTypes, |
| 3649 | Cand->Conversions.size(), |
| 3650 | false, 0); |
| 3651 | |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3652 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3653 | } |
| 3654 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3655 | } |
| 3656 | } |
| 3657 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3658 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 3659 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 3660 | /// expression with overloaded function type and @p ToType is the type |
| 3661 | /// we're trying to resolve to. For example: |
| 3662 | /// |
| 3663 | /// @code |
| 3664 | /// int f(double); |
| 3665 | /// int f(int); |
| 3666 | /// |
| 3667 | /// int (*pfd)(double) = f; // selects f(double) |
| 3668 | /// @endcode |
| 3669 | /// |
| 3670 | /// This routine returns the resulting FunctionDecl if it could be |
| 3671 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 3672 | /// routine will emit diagnostics if there is an error. |
| 3673 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3674 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3675 | bool Complain) { |
| 3676 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3677 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3678 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3679 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3680 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 3681 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3682 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3683 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3684 | FunctionType = MemTypePtr->getPointeeType(); |
| 3685 | IsMember = true; |
| 3686 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3687 | |
| 3688 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 3689 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3690 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3691 | return 0; |
| 3692 | |
| 3693 | // Find the actual overloaded function declaration. |
| 3694 | OverloadedFunctionDecl *Ovl = 0; |
| 3695 | |
| 3696 | // C++ [over.over]p1: |
| 3697 | // [...] [Note: any redundant set of parentheses surrounding the |
| 3698 | // overloaded function name is ignored (5.1). ] |
| 3699 | Expr *OvlExpr = From->IgnoreParens(); |
| 3700 | |
| 3701 | // C++ [over.over]p1: |
| 3702 | // [...] The overloaded function name can be preceded by the & |
| 3703 | // operator. |
| 3704 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 3705 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 3706 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 3707 | } |
| 3708 | |
| 3709 | // Try to dig out the overloaded function. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3710 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 3711 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3712 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3713 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
| 3714 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3715 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3716 | // If there's no overloaded function declaration or function template, |
| 3717 | // we're done. |
| 3718 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3719 | return 0; |
| 3720 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3721 | OverloadIterator Fun; |
| 3722 | if (Ovl) |
| 3723 | Fun = Ovl; |
| 3724 | else |
| 3725 | Fun = FunctionTemplate; |
| 3726 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3727 | // Look through all of the overloaded functions, searching for one |
| 3728 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3729 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
| 3730 | |
| 3731 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3732 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3733 | // C++ [over.over]p3: |
| 3734 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 3735 | // targets of type "pointer-to-function" or "reference-to-function." |
| 3736 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3737 | // type "pointer-to-member-function." |
| 3738 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3739 | |
| 3740 | if (FunctionTemplateDecl *FunctionTemplate |
| 3741 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3742 | if (CXXMethodDecl *Method |
| 3743 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 3744 | // Skip non-static function templates when converting to pointer, and |
| 3745 | // static when converting to member pointer. |
| 3746 | if (Method->isStatic() == IsMember) |
| 3747 | continue; |
| 3748 | } else if (IsMember) |
| 3749 | continue; |
| 3750 | |
| 3751 | // C++ [over.over]p2: |
| 3752 | // If the name is a function template, template argument deduction is |
| 3753 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 3754 | // resulting template argument list is used to generate a single |
| 3755 | // function template specialization, which is added to the set of |
| 3756 | // overloaded functions considered. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3757 | FunctionDecl *Specialization = 0; |
| 3758 | TemplateDeductionInfo Info(Context); |
| 3759 | if (TemplateDeductionResult Result |
| 3760 | = DeduceTemplateArguments(FunctionTemplate, /*FIXME*/false, |
| 3761 | /*FIXME:*/0, /*FIXME:*/0, |
| 3762 | FunctionType, Specialization, Info)) { |
| 3763 | // FIXME: make a note of the failed deduction for diagnostics. |
| 3764 | (void)Result; |
| 3765 | } else { |
| 3766 | assert(FunctionType |
| 3767 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3768 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3769 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3770 | } |
| 3771 | } |
| 3772 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3773 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 3774 | // Skip non-static functions when converting to pointer, and static |
| 3775 | // when converting to member pointer. |
| 3776 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3777 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3778 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3779 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3780 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3781 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3782 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3783 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3784 | FoundNonTemplateFunction = true; |
| 3785 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3786 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3789 | // If there were 0 or 1 matches, we're done. |
| 3790 | if (Matches.empty()) |
| 3791 | return 0; |
| 3792 | else if (Matches.size() == 1) |
| 3793 | return *Matches.begin(); |
| 3794 | |
| 3795 | // C++ [over.over]p4: |
| 3796 | // If more than one function is selected, [...] |
| 3797 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 3798 | if (FoundNonTemplateFunction) { |
| 3799 | // [...] any function template specializations in the set are eliminated |
| 3800 | // if the set also contains a non-template function, [...] |
| 3801 | for (llvm::SmallPtrSet<FunctionDecl *, 4>::iterator M = Matches.begin(), |
| 3802 | MEnd = Matches.end(); |
| 3803 | M != MEnd; ++M) |
| 3804 | if ((*M)->getPrimaryTemplate() == 0) |
| 3805 | RemainingMatches.push_back(*M); |
| 3806 | } else { |
| 3807 | // [...] and any given function template specialization F1 is eliminated |
| 3808 | // if the set contains a second function template specialization whose |
| 3809 | // function template is more specialized than the function template of F1 |
| 3810 | // according to the partial ordering rules of 14.5.5.2. |
| 3811 | // FIXME: Implement this! |
| 3812 | RemainingMatches.append(Matches.begin(), Matches.end()); |
| 3813 | } |
| 3814 | |
| 3815 | // [...] After such eliminations, if any, there shall remain exactly one |
| 3816 | // selected function. |
| 3817 | if (RemainingMatches.size() == 1) |
| 3818 | return RemainingMatches.front(); |
| 3819 | |
| 3820 | // FIXME: We should probably return the same thing that BestViableFunction |
| 3821 | // returns (even if we issue the diagnostics here). |
| 3822 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 3823 | << RemainingMatches[0]->getDeclName(); |
| 3824 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 3825 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3826 | return 0; |
| 3827 | } |
| 3828 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3829 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3830 | /// (which eventually refers to the declaration Func) and the call |
| 3831 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 3832 | /// to a specific function. If overload resolution succeeds, returns |
| 3833 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3834 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3835 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3836 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3837 | DeclarationName UnqualifiedName, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3838 | bool HasExplicitTemplateArgs, |
| 3839 | const TemplateArgument *ExplicitTemplateArgs, |
| 3840 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3841 | SourceLocation LParenLoc, |
| 3842 | Expr **Args, unsigned NumArgs, |
| 3843 | SourceLocation *CommaLocs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3844 | SourceLocation RParenLoc, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3845 | bool &ArgumentDependentLookup) { |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3846 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3847 | |
| 3848 | // Add the functions denoted by Callee to the set of candidate |
| 3849 | // functions. While we're doing so, track whether argument-dependent |
| 3850 | // lookup still applies, per: |
| 3851 | // |
| 3852 | // C++0x [basic.lookup.argdep]p3: |
| 3853 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 3854 | // and let Y be the lookup set produced by argument dependent |
| 3855 | // lookup (defined as follows). If X contains |
| 3856 | // |
| 3857 | // -- a declaration of a class member, or |
| 3858 | // |
| 3859 | // -- a block-scope function declaration that is not a |
| 3860 | // using-declaration, or |
| 3861 | // |
| 3862 | // -- a declaration that is neither a function or a function |
| 3863 | // template |
| 3864 | // |
| 3865 | // then Y is empty. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3866 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3867 | = dyn_cast_or_null<OverloadedFunctionDecl>(Callee)) { |
| 3868 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 3869 | FuncEnd = Ovl->function_end(); |
| 3870 | Func != FuncEnd; ++Func) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3871 | DeclContext *Ctx = 0; |
| 3872 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Func)) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3873 | if (HasExplicitTemplateArgs) |
| 3874 | continue; |
| 3875 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3876 | AddOverloadCandidate(FunDecl, Args, NumArgs, CandidateSet); |
| 3877 | Ctx = FunDecl->getDeclContext(); |
| 3878 | } else { |
| 3879 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*Func); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3880 | AddTemplateOverloadCandidate(FunTmpl, HasExplicitTemplateArgs, |
| 3881 | ExplicitTemplateArgs, |
| 3882 | NumExplicitTemplateArgs, |
| 3883 | Args, NumArgs, CandidateSet); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3884 | Ctx = FunTmpl->getDeclContext(); |
| 3885 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3887 | |
| 3888 | if (Ctx->isRecord() || Ctx->isFunctionOrMethod()) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3889 | ArgumentDependentLookup = false; |
| 3890 | } |
| 3891 | } else if (FunctionDecl *Func = dyn_cast_or_null<FunctionDecl>(Callee)) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3892 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3893 | AddOverloadCandidate(Func, Args, NumArgs, CandidateSet); |
| 3894 | |
| 3895 | if (Func->getDeclContext()->isRecord() || |
| 3896 | Func->getDeclContext()->isFunctionOrMethod()) |
| 3897 | ArgumentDependentLookup = false; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3898 | } else if (FunctionTemplateDecl *FuncTemplate |
| 3899 | = dyn_cast_or_null<FunctionTemplateDecl>(Callee)) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3900 | AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 3901 | ExplicitTemplateArgs, |
| 3902 | NumExplicitTemplateArgs, |
| 3903 | Args, NumArgs, CandidateSet); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3904 | |
| 3905 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 3906 | ArgumentDependentLookup = false; |
| 3907 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3908 | |
| 3909 | if (Callee) |
| 3910 | UnqualifiedName = Callee->getDeclName(); |
| 3911 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3912 | // FIXME: Pass explicit template arguments through for ADL |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3913 | if (ArgumentDependentLookup) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3914 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3915 | CandidateSet); |
| 3916 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3917 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3918 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 3919 | case OR_Success: |
| 3920 | return Best->Function; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3921 | |
| 3922 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3923 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3924 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3925 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3926 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 3927 | break; |
| 3928 | |
| 3929 | case OR_Ambiguous: |
| 3930 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3931 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3932 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3933 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3934 | |
| 3935 | case OR_Deleted: |
| 3936 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 3937 | << Best->Function->isDeleted() |
| 3938 | << UnqualifiedName |
| 3939 | << Fn->getSourceRange(); |
| 3940 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3941 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | // Overload resolution failed. Destroy all of the subexpressions and |
| 3945 | // return NULL. |
| 3946 | Fn->Destroy(Context); |
| 3947 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 3948 | Args[Arg]->Destroy(Context); |
| 3949 | return 0; |
| 3950 | } |
| 3951 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 3952 | /// \brief Create a unary operation that may resolve to an overloaded |
| 3953 | /// operator. |
| 3954 | /// |
| 3955 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 3956 | /// |
| 3957 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 3958 | /// operator. |
| 3959 | /// |
| 3960 | /// \param Functions The set of non-member functions that will be |
| 3961 | /// considered by overload resolution. The caller needs to build this |
| 3962 | /// set based on the context using, e.g., |
| 3963 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 3964 | /// set should not contain any member functions; those will be added |
| 3965 | /// by CreateOverloadedUnaryOp(). |
| 3966 | /// |
| 3967 | /// \param input The input argument. |
| 3968 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 3969 | unsigned OpcIn, |
| 3970 | FunctionSet &Functions, |
| 3971 | ExprArg input) { |
| 3972 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 3973 | Expr *Input = (Expr *)input.get(); |
| 3974 | |
| 3975 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 3976 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 3977 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3978 | |
| 3979 | Expr *Args[2] = { Input, 0 }; |
| 3980 | unsigned NumArgs = 1; |
| 3981 | |
| 3982 | // For post-increment and post-decrement, add the implicit '0' as |
| 3983 | // the second argument, so that we know this is a post-increment or |
| 3984 | // post-decrement. |
| 3985 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 3986 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
| 3987 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
| 3988 | SourceLocation()); |
| 3989 | NumArgs = 2; |
| 3990 | } |
| 3991 | |
| 3992 | if (Input->isTypeDependent()) { |
| 3993 | OverloadedFunctionDecl *Overloads |
| 3994 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
| 3995 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3996 | FuncEnd = Functions.end(); |
| 3997 | Func != FuncEnd; ++Func) |
| 3998 | Overloads->addOverload(*Func); |
| 3999 | |
| 4000 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4001 | OpLoc, false, false); |
| 4002 | |
| 4003 | input.release(); |
| 4004 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4005 | &Args[0], NumArgs, |
| 4006 | Context.DependentTy, |
| 4007 | OpLoc)); |
| 4008 | } |
| 4009 | |
| 4010 | // Build an empty overload set. |
| 4011 | OverloadCandidateSet CandidateSet; |
| 4012 | |
| 4013 | // Add the candidates from the given function set. |
| 4014 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4015 | |
| 4016 | // Add operator candidates that are member functions. |
| 4017 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4018 | |
| 4019 | // Add builtin operator candidates. |
| 4020 | AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet); |
| 4021 | |
| 4022 | // Perform overload resolution. |
| 4023 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4024 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4025 | case OR_Success: { |
| 4026 | // We found a built-in operator or an overloaded operator. |
| 4027 | FunctionDecl *FnDecl = Best->Function; |
| 4028 | |
| 4029 | if (FnDecl) { |
| 4030 | // We matched an overloaded operator. Build a call to that |
| 4031 | // operator. |
| 4032 | |
| 4033 | // Convert the arguments. |
| 4034 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4035 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4036 | return ExprError(); |
| 4037 | } else { |
| 4038 | // Convert the arguments. |
| 4039 | if (PerformCopyInitialization(Input, |
| 4040 | FnDecl->getParamDecl(0)->getType(), |
| 4041 | "passing")) |
| 4042 | return ExprError(); |
| 4043 | } |
| 4044 | |
| 4045 | // Determine the result type |
| 4046 | QualType ResultTy |
| 4047 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4048 | ResultTy = ResultTy.getNonReferenceType(); |
| 4049 | |
| 4050 | // Build the actual expression node. |
| 4051 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4052 | SourceLocation()); |
| 4053 | UsualUnaryConversions(FnExpr); |
| 4054 | |
| 4055 | input.release(); |
| 4056 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 4057 | &Input, 1, ResultTy, |
| 4058 | OpLoc)); |
| 4059 | } else { |
| 4060 | // We matched a built-in operator. Convert the arguments, then |
| 4061 | // break out so that we will build the appropriate built-in |
| 4062 | // operator node. |
| 4063 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4064 | Best->Conversions[0], "passing")) |
| 4065 | return ExprError(); |
| 4066 | |
| 4067 | break; |
| 4068 | } |
| 4069 | } |
| 4070 | |
| 4071 | case OR_No_Viable_Function: |
| 4072 | // No viable function; fall through to handling this as a |
| 4073 | // built-in operator, which will produce an error message for us. |
| 4074 | break; |
| 4075 | |
| 4076 | case OR_Ambiguous: |
| 4077 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4078 | << UnaryOperator::getOpcodeStr(Opc) |
| 4079 | << Input->getSourceRange(); |
| 4080 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4081 | return ExprError(); |
| 4082 | |
| 4083 | case OR_Deleted: |
| 4084 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4085 | << Best->Function->isDeleted() |
| 4086 | << UnaryOperator::getOpcodeStr(Opc) |
| 4087 | << Input->getSourceRange(); |
| 4088 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4089 | return ExprError(); |
| 4090 | } |
| 4091 | |
| 4092 | // Either we found no viable overloaded operator or we matched a |
| 4093 | // built-in operator. In either case, fall through to trying to |
| 4094 | // build a built-in operation. |
| 4095 | input.release(); |
| 4096 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4097 | } |
| 4098 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4099 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4100 | /// operator. |
| 4101 | /// |
| 4102 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4103 | /// |
| 4104 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4105 | /// operator. |
| 4106 | /// |
| 4107 | /// \param Functions The set of non-member functions that will be |
| 4108 | /// considered by overload resolution. The caller needs to build this |
| 4109 | /// set based on the context using, e.g., |
| 4110 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4111 | /// set should not contain any member functions; those will be added |
| 4112 | /// by CreateOverloadedBinOp(). |
| 4113 | /// |
| 4114 | /// \param LHS Left-hand argument. |
| 4115 | /// \param RHS Right-hand argument. |
| 4116 | Sema::OwningExprResult |
| 4117 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
| 4118 | unsigned OpcIn, |
| 4119 | FunctionSet &Functions, |
| 4120 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4121 | Expr *Args[2] = { LHS, RHS }; |
| 4122 | |
| 4123 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4124 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4125 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4126 | |
| 4127 | // If either side is type-dependent, create an appropriate dependent |
| 4128 | // expression. |
| 4129 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) { |
| 4130 | // .* cannot be overloaded. |
| 4131 | if (Opc == BinaryOperator::PtrMemD) |
| 4132 | return Owned(new (Context) BinaryOperator(LHS, RHS, Opc, |
| 4133 | Context.DependentTy, OpLoc)); |
| 4134 | |
| 4135 | OverloadedFunctionDecl *Overloads |
| 4136 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
| 4137 | for (FunctionSet::iterator Func = Functions.begin(), |
| 4138 | FuncEnd = Functions.end(); |
| 4139 | Func != FuncEnd; ++Func) |
| 4140 | Overloads->addOverload(*Func); |
| 4141 | |
| 4142 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4143 | OpLoc, false, false); |
| 4144 | |
| 4145 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4146 | Args, 2, |
| 4147 | Context.DependentTy, |
| 4148 | OpLoc)); |
| 4149 | } |
| 4150 | |
| 4151 | // If this is the .* operator, which is not overloadable, just |
| 4152 | // create a built-in binary operator. |
| 4153 | if (Opc == BinaryOperator::PtrMemD) |
| 4154 | return CreateBuiltinBinOp(OpLoc, Opc, LHS, RHS); |
| 4155 | |
| 4156 | // If this is one of the assignment operators, we only perform |
| 4157 | // overload resolution if the left-hand side is a class or |
| 4158 | // enumeration type (C++ [expr.ass]p3). |
| 4159 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
| 4160 | !LHS->getType()->isOverloadableType()) |
| 4161 | return CreateBuiltinBinOp(OpLoc, Opc, LHS, RHS); |
| 4162 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4163 | // Build an empty overload set. |
| 4164 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4165 | |
| 4166 | // Add the candidates from the given function set. |
| 4167 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4168 | |
| 4169 | // Add operator candidates that are member functions. |
| 4170 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4171 | |
| 4172 | // Add builtin operator candidates. |
| 4173 | AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet); |
| 4174 | |
| 4175 | // Perform overload resolution. |
| 4176 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4177 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4178 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4179 | // We found a built-in operator or an overloaded operator. |
| 4180 | FunctionDecl *FnDecl = Best->Function; |
| 4181 | |
| 4182 | if (FnDecl) { |
| 4183 | // We matched an overloaded operator. Build a call to that |
| 4184 | // operator. |
| 4185 | |
| 4186 | // Convert the arguments. |
| 4187 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4188 | if (PerformObjectArgumentInitialization(LHS, Method) || |
| 4189 | PerformCopyInitialization(RHS, FnDecl->getParamDecl(0)->getType(), |
| 4190 | "passing")) |
| 4191 | return ExprError(); |
| 4192 | } else { |
| 4193 | // Convert the arguments. |
| 4194 | if (PerformCopyInitialization(LHS, FnDecl->getParamDecl(0)->getType(), |
| 4195 | "passing") || |
| 4196 | PerformCopyInitialization(RHS, FnDecl->getParamDecl(1)->getType(), |
| 4197 | "passing")) |
| 4198 | return ExprError(); |
| 4199 | } |
| 4200 | |
| 4201 | // Determine the result type |
| 4202 | QualType ResultTy |
| 4203 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4204 | ResultTy = ResultTy.getNonReferenceType(); |
| 4205 | |
| 4206 | // Build the actual expression node. |
| 4207 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4208 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4209 | UsualUnaryConversions(FnExpr); |
| 4210 | |
| 4211 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 4212 | Args, 2, ResultTy, |
| 4213 | OpLoc)); |
| 4214 | } else { |
| 4215 | // We matched a built-in operator. Convert the arguments, then |
| 4216 | // break out so that we will build the appropriate built-in |
| 4217 | // operator node. |
| 4218 | if (PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], |
| 4219 | Best->Conversions[0], "passing") || |
| 4220 | PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], |
| 4221 | Best->Conversions[1], "passing")) |
| 4222 | return ExprError(); |
| 4223 | |
| 4224 | break; |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | case OR_No_Viable_Function: |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4229 | // For class as left operand for assignment or compound assigment operator |
| 4230 | // do not fall through to handling in built-in, but report that no overloaded |
| 4231 | // assignment operator found |
| 4232 | if (LHS->getType()->isRecordType() && Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
| 4233 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4234 | << BinaryOperator::getOpcodeStr(Opc) |
| 4235 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4236 | return ExprError(); |
| 4237 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4238 | // No viable function; fall through to handling this as a |
| 4239 | // built-in operator, which will produce an error message for us. |
| 4240 | break; |
| 4241 | |
| 4242 | case OR_Ambiguous: |
| 4243 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4244 | << BinaryOperator::getOpcodeStr(Opc) |
| 4245 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4246 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4247 | return ExprError(); |
| 4248 | |
| 4249 | case OR_Deleted: |
| 4250 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4251 | << Best->Function->isDeleted() |
| 4252 | << BinaryOperator::getOpcodeStr(Opc) |
| 4253 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4254 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4255 | return ExprError(); |
| 4256 | } |
| 4257 | |
| 4258 | // Either we found no viable overloaded operator or we matched a |
| 4259 | // built-in operator. In either case, try to build a built-in |
| 4260 | // operation. |
| 4261 | return CreateBuiltinBinOp(OpLoc, Opc, LHS, RHS); |
| 4262 | } |
| 4263 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4264 | /// BuildCallToMemberFunction - Build a call to a member |
| 4265 | /// function. MemExpr is the expression that refers to the member |
| 4266 | /// function (and includes the object parameter), Args/NumArgs are the |
| 4267 | /// arguments to the function call (not including the object |
| 4268 | /// parameter). The caller needs to validate that the member |
| 4269 | /// expression refers to a member function or an overloaded member |
| 4270 | /// function. |
| 4271 | Sema::ExprResult |
| 4272 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 4273 | SourceLocation LParenLoc, Expr **Args, |
| 4274 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 4275 | SourceLocation RParenLoc) { |
| 4276 | // Dig out the member expression. This holds both the object |
| 4277 | // argument and the member function we're referring to. |
| 4278 | MemberExpr *MemExpr = 0; |
| 4279 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 4280 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 4281 | else |
| 4282 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 4283 | assert(MemExpr && "Building member call without member expression"); |
| 4284 | |
| 4285 | // Extract the object argument. |
| 4286 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4287 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4288 | CXXMethodDecl *Method = 0; |
| 4289 | if (OverloadedFunctionDecl *Ovl |
| 4290 | = dyn_cast<OverloadedFunctionDecl>(MemExpr->getMemberDecl())) { |
| 4291 | // Add overload candidates |
| 4292 | OverloadCandidateSet CandidateSet; |
| 4293 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4294 | FuncEnd = Ovl->function_end(); |
| 4295 | Func != FuncEnd; ++Func) { |
| 4296 | assert(isa<CXXMethodDecl>(*Func) && "Function is not a method"); |
| 4297 | Method = cast<CXXMethodDecl>(*Func); |
| 4298 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
| 4299 | /*SuppressUserConversions=*/false); |
| 4300 | } |
| 4301 | |
| 4302 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4303 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4304 | case OR_Success: |
| 4305 | Method = cast<CXXMethodDecl>(Best->Function); |
| 4306 | break; |
| 4307 | |
| 4308 | case OR_No_Viable_Function: |
| 4309 | Diag(MemExpr->getSourceRange().getBegin(), |
| 4310 | diag::err_ovl_no_viable_member_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4311 | << Ovl->getDeclName() << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4312 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4313 | // FIXME: Leaking incoming expressions! |
| 4314 | return true; |
| 4315 | |
| 4316 | case OR_Ambiguous: |
| 4317 | Diag(MemExpr->getSourceRange().getBegin(), |
| 4318 | diag::err_ovl_ambiguous_member_call) |
| 4319 | << Ovl->getDeclName() << MemExprE->getSourceRange(); |
| 4320 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4321 | // FIXME: Leaking incoming expressions! |
| 4322 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4323 | |
| 4324 | case OR_Deleted: |
| 4325 | Diag(MemExpr->getSourceRange().getBegin(), |
| 4326 | diag::err_ovl_deleted_member_call) |
| 4327 | << Best->Function->isDeleted() |
| 4328 | << Ovl->getDeclName() << MemExprE->getSourceRange(); |
| 4329 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4330 | // FIXME: Leaking incoming expressions! |
| 4331 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | FixOverloadedFunctionReference(MemExpr, Method); |
| 4335 | } else { |
| 4336 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 4337 | } |
| 4338 | |
| 4339 | assert(Method && "Member call to something that isn't a method?"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4340 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4341 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
| 4342 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4343 | Method->getResultType().getNonReferenceType(), |
| 4344 | RParenLoc)); |
| 4345 | |
| 4346 | // Convert the object argument (for a non-static member function call). |
| 4347 | if (!Method->isStatic() && |
| 4348 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 4349 | return true; |
| 4350 | MemExpr->setBase(ObjectArg); |
| 4351 | |
| 4352 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4353 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4354 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
| 4355 | RParenLoc)) |
| 4356 | return true; |
| 4357 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4358 | return CheckFunctionCall(Method, TheCall.take()).release(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4361 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 4362 | /// type (C++ [over.call.object]), which can end up invoking an |
| 4363 | /// overloaded function call operator (@c operator()) or performing a |
| 4364 | /// user-defined conversion on the object argument. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4365 | Sema::ExprResult |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 4366 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
| 4367 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4368 | Expr **Args, unsigned NumArgs, |
| 4369 | SourceLocation *CommaLocs, |
| 4370 | SourceLocation RParenLoc) { |
| 4371 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4372 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4373 | |
| 4374 | // C++ [over.call.object]p1: |
| 4375 | // If the primary-expression E in the function call syntax |
| 4376 | // evaluates to a class object of type “cv T”, then the set of |
| 4377 | // candidate functions includes at least the function call |
| 4378 | // operators of T. The function call operators of T are obtained by |
| 4379 | // ordinary lookup of the name operator() in the context of |
| 4380 | // (E).operator(). |
| 4381 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4382 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4383 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4384 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4385 | Oper != OperEnd; ++Oper) |
| 4386 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
| 4387 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4388 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4389 | // C++ [over.call.object]p2: |
| 4390 | // In addition, for each conversion function declared in T of the |
| 4391 | // form |
| 4392 | // |
| 4393 | // operator conversion-type-id () cv-qualifier; |
| 4394 | // |
| 4395 | // where cv-qualifier is the same cv-qualification as, or a |
| 4396 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 4397 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 4398 | // R", or the type "reference to pointer to function of |
| 4399 | // (P1,...,Pn) returning R", or the type "reference to function |
| 4400 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4401 | // is also considered as a candidate function. Similarly, |
| 4402 | // surrogate call functions are added to the set of candidate |
| 4403 | // functions for each conversion function declared in an |
| 4404 | // accessible base class provided the function is not hidden |
| 4405 | // within T by another intervening declaration. |
| 4406 | // |
| 4407 | // FIXME: Look in base classes for more conversion operators! |
| 4408 | OverloadedFunctionDecl *Conversions |
| 4409 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4410 | for (OverloadedFunctionDecl::function_iterator |
| 4411 | Func = Conversions->function_begin(), |
| 4412 | FuncEnd = Conversions->function_end(); |
| 4413 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4414 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*Func); |
| 4415 | |
| 4416 | // Strip the reference type (if any) and then the pointer type (if |
| 4417 | // any) to get down to what might be a function type. |
| 4418 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4419 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4420 | ConvType = ConvPtrType->getPointeeType(); |
| 4421 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4422 | if (const FunctionProtoType *Proto = ConvType->getAsFunctionProtoType()) |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4423 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 4424 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4425 | |
| 4426 | // Perform overload resolution. |
| 4427 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4428 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4429 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4430 | // Overload resolution succeeded; we'll build the appropriate call |
| 4431 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4432 | break; |
| 4433 | |
| 4434 | case OR_No_Viable_Function: |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4435 | Diag(Object->getSourceRange().getBegin(), |
| 4436 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4437 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4438 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4439 | break; |
| 4440 | |
| 4441 | case OR_Ambiguous: |
| 4442 | Diag(Object->getSourceRange().getBegin(), |
| 4443 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4444 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4445 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4446 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4447 | |
| 4448 | case OR_Deleted: |
| 4449 | Diag(Object->getSourceRange().getBegin(), |
| 4450 | diag::err_ovl_deleted_object_call) |
| 4451 | << Best->Function->isDeleted() |
| 4452 | << Object->getType() << Object->getSourceRange(); |
| 4453 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4454 | break; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4457 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4458 | // We had an error; delete all of the subexpressions and return |
| 4459 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4460 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4461 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4462 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4463 | return true; |
| 4464 | } |
| 4465 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4466 | if (Best->Function == 0) { |
| 4467 | // Since there is no function declaration, this is one of the |
| 4468 | // surrogate candidates. Dig out the conversion function. |
| 4469 | CXXConversionDecl *Conv |
| 4470 | = cast<CXXConversionDecl>( |
| 4471 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 4472 | |
| 4473 | // We selected one of the surrogate functions that converts the |
| 4474 | // object parameter to a function pointer. Perform the conversion |
| 4475 | // on the object argument, then let ActOnCallExpr finish the job. |
| 4476 | // FIXME: Represent the user-defined conversion in the AST! |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4477 | ImpCastExprToType(Object, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4478 | Conv->getConversionType().getNonReferenceType(), |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 4479 | CastExpr::CK_Unknown, |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4480 | Conv->getConversionType()->isLValueReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4481 | return ActOnCallExpr(S, ExprArg(*this, Object), LParenLoc, |
| 4482 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 4483 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 4487 | // that calls this method, using Object for the implicit object |
| 4488 | // parameter and passing along the remaining arguments. |
| 4489 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4490 | const FunctionProtoType *Proto = Method->getType()->getAsFunctionProtoType(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4491 | |
| 4492 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4493 | unsigned NumArgsToCheck = NumArgs; |
| 4494 | |
| 4495 | // Build the full argument list for the method call (the |
| 4496 | // implicit object parameter is placed at the beginning of the |
| 4497 | // list). |
| 4498 | Expr **MethodArgs; |
| 4499 | if (NumArgs < NumArgsInProto) { |
| 4500 | NumArgsToCheck = NumArgsInProto; |
| 4501 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 4502 | } else { |
| 4503 | MethodArgs = new Expr*[NumArgs + 1]; |
| 4504 | } |
| 4505 | MethodArgs[0] = Object; |
| 4506 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4507 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
| 4508 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4509 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
| 4510 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4511 | UsualUnaryConversions(NewFn); |
| 4512 | |
| 4513 | // Once we've built TheCall, all of the expressions are properly |
| 4514 | // owned. |
| 4515 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4516 | ExprOwningPtr<CXXOperatorCallExpr> |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4517 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
| 4518 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4519 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4520 | delete [] MethodArgs; |
| 4521 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4522 | // We may have default arguments. If so, we need to allocate more |
| 4523 | // slots in the call for them. |
| 4524 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4525 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4526 | else if (NumArgs > NumArgsInProto) |
| 4527 | NumArgsToCheck = NumArgsInProto; |
| 4528 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4529 | bool IsError = false; |
| 4530 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4531 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4532 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4533 | TheCall->setArg(0, Object); |
| 4534 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4535 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4536 | // Check the argument types. |
| 4537 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4538 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4539 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4540 | Arg = Args[i]; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4541 | |
| 4542 | // Pass the argument. |
| 4543 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4544 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4545 | } else { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4546 | Arg = new (Context) CXXDefaultArgExpr(Method->getParamDecl(i)); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4547 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4548 | |
| 4549 | TheCall->setArg(i + 1, Arg); |
| 4550 | } |
| 4551 | |
| 4552 | // If this is a variadic call, handle args passed through "...". |
| 4553 | if (Proto->isVariadic()) { |
| 4554 | // Promote the arguments (C99 6.5.2.2p7). |
| 4555 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 4556 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4557 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4558 | TheCall->setArg(i + 1, Arg); |
| 4559 | } |
| 4560 | } |
| 4561 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4562 | if (IsError) return true; |
| 4563 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4564 | return CheckFunctionCall(Method, TheCall.take()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4567 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
| 4568 | /// (if one exists), where @c Base is an expression of class type and |
| 4569 | /// @c Member is the name of the member we're trying to find. |
| 4570 | Action::ExprResult |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4571 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4572 | SourceLocation MemberLoc, |
| 4573 | IdentifierInfo &Member) { |
| 4574 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
| 4575 | |
| 4576 | // C++ [over.ref]p1: |
| 4577 | // |
| 4578 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 4579 | // for a class object x of type T if T::operator->() exists and if |
| 4580 | // the operator is selected as the best match function by the |
| 4581 | // overload resolution mechanism (13.3). |
| 4582 | // FIXME: look in base classes. |
| 4583 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 4584 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4585 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4586 | |
| 4587 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4588 | for (llvm::tie(Oper, OperEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4589 | = BaseRecord->getDecl()->lookup(OpName); Oper != OperEnd; ++Oper) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4590 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4591 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4592 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4593 | ExprOwningPtr<Expr> BasePtr(this, Base); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4594 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4595 | // Perform overload resolution. |
| 4596 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4597 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4598 | case OR_Success: |
| 4599 | // Overload resolution succeeded; we'll build the call below. |
| 4600 | break; |
| 4601 | |
| 4602 | case OR_No_Viable_Function: |
| 4603 | if (CandidateSet.empty()) |
| 4604 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4605 | << BasePtr->getType() << BasePtr->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4606 | else |
| 4607 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4608 | << "operator->" << BasePtr->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4609 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4610 | return true; |
| 4611 | |
| 4612 | case OR_Ambiguous: |
| 4613 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4614 | << "operator->" << BasePtr->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4615 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4616 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4617 | |
| 4618 | case OR_Deleted: |
| 4619 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4620 | << Best->Function->isDeleted() |
| 4621 | << "operator->" << BasePtr->getSourceRange(); |
| 4622 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4623 | return true; |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | // Convert the object parameter. |
| 4627 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4628 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4629 | return true; |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4630 | |
| 4631 | // No concerns about early exits now. |
| 4632 | BasePtr.take(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4633 | |
| 4634 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4635 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 4636 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4637 | UsualUnaryConversions(FnExpr); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4638 | Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4639 | Method->getResultType().getNonReferenceType(), |
| 4640 | OpLoc); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4641 | return ActOnMemberReferenceExpr(S, ExprArg(*this, Base), OpLoc, tok::arrow, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4642 | MemberLoc, Member, DeclPtrTy()).release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4645 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 4646 | /// a C++ overloaded function (possibly with some parentheses and |
| 4647 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 4648 | /// to the function declaration Fn, so patch up the expression E to |
| 4649 | /// refer (possibly indirectly) to Fn. |
| 4650 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 4651 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 4652 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 4653 | E->setType(PE->getSubExpr()->getType()); |
| 4654 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
| 4655 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
| 4656 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4657 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4658 | if (Method->isStatic()) { |
| 4659 | // Do nothing: static member functions aren't any different |
| 4660 | // from non-member functions. |
| 4661 | } |
| 4662 | else if (QualifiedDeclRefExpr *DRE |
| 4663 | = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) { |
| 4664 | // We have taken the address of a pointer to member |
| 4665 | // function. Perform the computation here so that we get the |
| 4666 | // appropriate pointer to member type. |
| 4667 | DRE->setDecl(Fn); |
| 4668 | DRE->setType(Fn->getType()); |
| 4669 | QualType ClassType |
| 4670 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 4671 | E->setType(Context.getMemberPointerType(Fn->getType(), |
| 4672 | ClassType.getTypePtr())); |
| 4673 | return; |
| 4674 | } |
| 4675 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4676 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4677 | E->setType(Context.getPointerType(UnOp->getSubExpr()->getType())); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4678 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4679 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
| 4680 | isa<FunctionTemplateDecl>(DR->getDecl())) && |
| 4681 | "Expected overloaded function or function template"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4682 | DR->setDecl(Fn); |
| 4683 | E->setType(Fn->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4684 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 4685 | MemExpr->setMemberDecl(Fn); |
| 4686 | E->setType(Fn->getType()); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4687 | } else { |
| 4688 | assert(false && "Invalid reference to overloaded function"); |
| 4689 | } |
| 4690 | } |
| 4691 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4692 | } // end namespace clang |