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