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