Douglas Gregor | 5251f1b | 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 | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include <algorithm> |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | /// GetConversionCategory - Retrieve the implicit conversion |
| 32 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 35 | static const ImplicitConversionCategory |
| 36 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 37 | ICC_Identity, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Lvalue_Transformation, |
| 40 | ICC_Lvalue_Transformation, |
| 41 | ICC_Qualification_Adjustment, |
| 42 | ICC_Promotion, |
| 43 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 44 | ICC_Promotion, |
| 45 | ICC_Conversion, |
| 46 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
| 50 | ICC_Conversion, |
| 51 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 52 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 54 | ICC_Conversion |
| 55 | }; |
| 56 | return Category[(int)Kind]; |
| 57 | } |
| 58 | |
| 59 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 60 | /// corresponding to the given implicit conversion kind. |
| 61 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 62 | static const ImplicitConversionRank |
| 63 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 64 | ICR_Exact_Match, |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Promotion, |
| 70 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Conversion, |
| 73 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 79 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 80 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 81 | ICR_Conversion |
| 82 | }; |
| 83 | return Rank[(int)Kind]; |
| 84 | } |
| 85 | |
| 86 | /// GetImplicitConversionName - Return the name of this kind of |
| 87 | /// implicit conversion. |
| 88 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 89 | static const char* Name[(int)ICK_Num_Conversion_Kinds] = { |
| 90 | "No conversion", |
| 91 | "Lvalue-to-rvalue", |
| 92 | "Array-to-pointer", |
| 93 | "Function-to-pointer", |
| 94 | "Qualification", |
| 95 | "Integral promotion", |
| 96 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 97 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | "Integral conversion", |
| 99 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Floating-integral conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 102 | "Complex-real conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 103 | "Pointer conversion", |
| 104 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 106 | "Compatible-types conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 107 | "Derived-to-base conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | }; |
| 109 | return Name[Kind]; |
| 110 | } |
| 111 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 112 | /// StandardConversionSequence - Set the standard conversion |
| 113 | /// sequence to the identity conversion. |
| 114 | void StandardConversionSequence::setAsIdentityConversion() { |
| 115 | First = ICK_Identity; |
| 116 | Second = ICK_Identity; |
| 117 | Third = ICK_Identity; |
| 118 | Deprecated = false; |
| 119 | ReferenceBinding = false; |
| 120 | DirectBinding = false; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 121 | RRefBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 122 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 125 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 126 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 127 | /// implicit conversions. |
| 128 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 129 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 130 | if (GetConversionRank(First) > Rank) |
| 131 | Rank = GetConversionRank(First); |
| 132 | if (GetConversionRank(Second) > Rank) |
| 133 | Rank = GetConversionRank(Second); |
| 134 | if (GetConversionRank(Third) > Rank) |
| 135 | Rank = GetConversionRank(Third); |
| 136 | return Rank; |
| 137 | } |
| 138 | |
| 139 | /// isPointerConversionToBool - Determines whether this conversion is |
| 140 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 144 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 145 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 146 | |
| 147 | // Note that FromType has not necessarily been transformed by the |
| 148 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 149 | // check for their presence as well as checking whether FromType is |
| 150 | // a pointer. |
| 151 | if (ToType->isBooleanType() && |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 152 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 159 | /// isPointerConversionToVoidPointer - Determines whether this |
| 160 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 161 | /// used as part of the ranking of standard conversion sequences (C++ |
| 162 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 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 | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 5251f1b | 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 | 2fe9883 | 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 | 5251f1b | 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 | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 227 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 5251f1b | 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 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | // FunctionDecl that New cannot be overloaded with. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 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", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 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 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 287 | OverloadedFunctionDecl::function_iterator& MatchedDecl) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 288 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 289 | // Is this new function an overload of every function in the |
| 290 | // overload set? |
| 291 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 292 | FuncEnd = Ovl->function_end(); |
| 293 | for (; Func != FuncEnd; ++Func) { |
| 294 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 295 | MatchedDecl = Func; |
| 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // This function overloads every function in the overload set. |
| 301 | return true; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 302 | } else if (FunctionTemplateDecl *Old = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 303 | return IsOverload(New, Old->getTemplatedDecl(), MatchedDecl); |
| 304 | else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 305 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 307 | |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 308 | // C++ [temp.fct]p2: |
| 309 | // A function template can be overloaded with other function templates |
| 310 | // and with normal (non-template) functions. |
| 311 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 312 | return true; |
| 313 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 314 | // Is the function New an overload of the function Old? |
| 315 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 316 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 317 | |
| 318 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 319 | // determine whether they are overloads. If we find any mismatch |
| 320 | // in the signature, they are overloads. |
| 321 | |
| 322 | // If either of these functions is a K&R-style function (no |
| 323 | // prototype), then we consider them to have matching signatures. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 324 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 325 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 326 | return false; |
| 327 | |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 328 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 329 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 330 | |
| 331 | // The signature of a function includes the types of its |
| 332 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 333 | // of the ellipsis; see C++ DR 357). |
| 334 | if (OldQType != NewQType && |
| 335 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 336 | OldType->isVariadic() != NewType->isVariadic() || |
| 337 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 338 | NewType->arg_type_begin()))) |
| 339 | return true; |
| 340 | |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 341 | // C++ [temp.over.link]p4: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | // The signature of a function template consists of its function |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 343 | // signature, its return type and its template parameter list. The names |
| 344 | // of the template parameters are significant only for establishing the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | // relationship between the template parameters and the rest of the |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 346 | // signature. |
| 347 | // |
| 348 | // We check the return type and template parameter lists for function |
| 349 | // templates first; the remaining checks follow. |
| 350 | if (NewTemplate && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 352 | OldTemplate->getTemplateParameters(), |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 353 | false, false, SourceLocation()) || |
| 354 | OldType->getResultType() != NewType->getResultType())) |
| 355 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 357 | // If the function is a class member, its signature includes the |
| 358 | // cv-qualifiers (if any) on the function itself. |
| 359 | // |
| 360 | // As part of this, also check whether one of the member functions |
| 361 | // is static, in which case they are not overloads (C++ |
| 362 | // 13.1p2). While not part of the definition of the signature, |
| 363 | // this check is important to determine whether these functions |
| 364 | // can be overloaded. |
| 365 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 366 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | if (OldMethod && NewMethod && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 368 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | b81897c | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 369 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 370 | return true; |
| 371 | |
| 372 | // The signatures match; this is not an overload. |
| 373 | return false; |
| 374 | } else { |
| 375 | // (C++ 13p1): |
| 376 | // Only function declarations can be overloaded; object and type |
| 377 | // declarations cannot be overloaded. |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 382 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 383 | /// from the given expression (Expr) to the given type (ToType). This |
| 384 | /// function returns an implicit conversion sequence that can be used |
| 385 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 386 | /// |
| 387 | /// void f(float f); |
| 388 | /// void g(int i) { f(i); } |
| 389 | /// |
| 390 | /// this routine would produce an implicit conversion sequence to |
| 391 | /// describe the initialization of f from i, which will be a standard |
| 392 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 393 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 394 | // |
| 395 | /// Note that this routine only determines how the conversion can be |
| 396 | /// performed; it does not actually perform the conversion. As such, |
| 397 | /// it will not produce any diagnostics if no conversion is available, |
| 398 | /// but will instead return an implicit conversion sequence of kind |
| 399 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 400 | /// |
| 401 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 402 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 403 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 404 | /// permitted. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 405 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 406 | /// no matter its actual lvalueness. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 407 | ImplicitConversionSequence |
Anders Carlsson | 5ec4abf | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 408 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 409 | bool SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 410 | bool AllowExplicit, bool ForceRValue, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | bool InOverloadResolution) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | ImplicitConversionSequence ICS; |
Fariborz Jahanian | 19c7328 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 413 | OverloadCandidateSet Conversions; |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 414 | OverloadingResult UserDefResult = OR_Success; |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 415 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 416 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 417 | else if (getLangOptions().CPlusPlus && |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 418 | (UserDefResult = IsUserDefinedConversion(From, ToType, |
| 419 | ICS.UserDefined, |
Fariborz Jahanian | 19c7328 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 420 | Conversions, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 421 | !SuppressUserConversions, AllowExplicit, |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 422 | ForceRValue)) == OR_Success) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 423 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 424 | // C++ [over.ics.user]p4: |
| 425 | // A conversion of an expression of class type to the same class |
| 426 | // type is given Exact Match rank, and a conversion of an |
| 427 | // expression of class type to a base class of that type is |
| 428 | // given Conversion rank, in spite of the fact that a copy |
| 429 | // constructor (i.e., a user-defined conversion function) is |
| 430 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 432 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | QualType FromCanon |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 434 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 435 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 436 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 437 | // Turn this into a "standard" conversion sequence, so that it |
| 438 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 439 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 440 | ICS.Standard.setAsIdentityConversion(); |
| 441 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 442 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 443 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 444 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 445 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 446 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 447 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 448 | |
| 449 | // C++ [over.best.ics]p4: |
| 450 | // However, when considering the argument of a user-defined |
| 451 | // conversion function that is a candidate by 13.3.1.3 when |
| 452 | // invoked for the copying of the temporary in the second step |
| 453 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 454 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 455 | // ellipsis conversion sequences are allowed. |
| 456 | if (SuppressUserConversions && |
| 457 | ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) |
| 458 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 459 | } else { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 460 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 461 | if (UserDefResult == OR_Ambiguous) { |
| 462 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 463 | Cand != Conversions.end(); ++Cand) |
| 464 | ICS.ConversionFunctionSet.push_back(Cand->Function); |
| 465 | } |
| 466 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 467 | |
| 468 | return ICS; |
| 469 | } |
| 470 | |
| 471 | /// IsStandardConversion - Determines whether there is a standard |
| 472 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 473 | /// expression From to the type ToType. Standard conversion sequences |
| 474 | /// only consider non-class types; for conversions that involve class |
| 475 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 476 | /// contain the standard conversion sequence required to perform this |
| 477 | /// conversion and this routine will return true. Otherwise, this |
| 478 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | bool |
| 480 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 481 | bool InOverloadResolution, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | StandardConversionSequence &SCS) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | QualType FromType = From->getType(); |
| 484 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 485 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 486 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 487 | SCS.Deprecated = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 488 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 489 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 490 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 492 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 494 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 495 | if (getLangOptions().CPlusPlus) |
| 496 | return false; |
| 497 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 501 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 502 | // array-to-pointer conversion, or function-to-pointer conversion |
| 503 | // (C++ 4p1). |
| 504 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 507 | // converted to an rvalue. |
| 508 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 510 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 511 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 512 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 513 | |
| 514 | // If T is a non-class type, the type of the rvalue is the |
| 515 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 516 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 517 | // just strip the qualifiers because they don't matter. |
| 518 | |
| 519 | // FIXME: Doesn't see through to qualifiers behind a typedef! |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 520 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 521 | } else if (FromType->isArrayType()) { |
| 522 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 523 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 524 | |
| 525 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 526 | // bound of T" can be converted to an rvalue of type "pointer to |
| 527 | // T" (C++ 4.2p1). |
| 528 | FromType = Context.getArrayDecayedType(FromType); |
| 529 | |
| 530 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 531 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 532 | SCS.Deprecated = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 533 | |
| 534 | // For the purpose of ranking in overload resolution |
| 535 | // (13.3.3.1.1), this conversion is considered an |
| 536 | // array-to-pointer conversion followed by a qualification |
| 537 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 538 | SCS.Second = ICK_Identity; |
| 539 | SCS.Third = ICK_Qualification; |
| 540 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 541 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 542 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 543 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 544 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 545 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 546 | |
| 547 | // An lvalue of function type T can be converted to an rvalue of |
| 548 | // type "pointer to T." The result is a pointer to the |
| 549 | // function. (C++ 4.3p1). |
| 550 | FromType = Context.getPointerType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | } else if (FunctionDecl *Fn |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 552 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 553 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 554 | SCS.First = ICK_Function_To_Pointer; |
| 555 | |
| 556 | // We were able to resolve the address of the overloaded function, |
| 557 | // so we can convert to the type of that function. |
| 558 | FromType = Fn->getType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 559 | if (ToType->isLValueReferenceType()) |
| 560 | FromType = Context.getLValueReferenceType(FromType); |
| 561 | else if (ToType->isRValueReferenceType()) |
| 562 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 563 | else if (ToType->isMemberPointerType()) { |
| 564 | // Resolve address only succeeds if both sides are member pointers, |
| 565 | // but it doesn't have to be the same class. See DR 247. |
| 566 | // Note that this means that the type of &Derived::fn can be |
| 567 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 568 | // base class, even if it was brought into the derived class via a |
| 569 | // using declaration. The standard isn't clear on this issue at all. |
| 570 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 571 | FromType = Context.getMemberPointerType(FromType, |
| 572 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 573 | } else |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 574 | FromType = Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 575 | } else { |
| 576 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 577 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // The second conversion can be an integral promotion, floating |
| 581 | // point promotion, integral conversion, floating point conversion, |
| 582 | // floating-integral conversion, pointer conversion, |
| 583 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 584 | // For overloading in C, this can also be a "compatible-type" |
| 585 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 586 | bool IncompatibleObjC = false; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 587 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 588 | // The unqualified versions of the types are the same: there's no |
| 589 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 590 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 591 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 593 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 594 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 595 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 596 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 597 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 599 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 600 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 601 | SCS.Second = ICK_Complex_Promotion; |
| 602 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 603 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 604 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 605 | // Integral conversions (C++ 4.7). |
| 606 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 607 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 608 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 609 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 610 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 611 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 612 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 613 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 614 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 615 | SCS.Second = ICK_Complex_Conversion; |
| 616 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 617 | } else if ((FromType->isFloatingType() && |
| 618 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 619 | !ToType->isEnumeralType())) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 621 | ToType->isFloatingType())) { |
| 622 | // Floating-integral conversions (C++ 4.9). |
| 623 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 624 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 625 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 626 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 627 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 628 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 629 | SCS.Second = ICK_Complex_Real; |
| 630 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 631 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 632 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 633 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 634 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 635 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 636 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 637 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 638 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 639 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 640 | } else if (ToType->isBooleanType() && |
| 641 | (FromType->isArithmeticType() || |
| 642 | FromType->isEnumeralType() || |
| 643 | FromType->isPointerType() || |
| 644 | FromType->isBlockPointerType() || |
| 645 | FromType->isMemberPointerType() || |
| 646 | FromType->isNullPtrType())) { |
| 647 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 648 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 649 | FromType = Context.BoolTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 651 | Context.typesAreCompatible(ToType, FromType)) { |
| 652 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 653 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 654 | } else { |
| 655 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 656 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 659 | QualType CanonFrom; |
| 660 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 661 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 662 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 663 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 664 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 665 | CanonFrom = Context.getCanonicalType(FromType); |
| 666 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 667 | } else { |
| 668 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 669 | SCS.Third = ICK_Identity; |
| 670 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 672 | // [...] Any difference in top-level cv-qualification is |
| 673 | // subsumed by the initialization itself and does not constitute |
| 674 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 675 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 677 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 678 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 679 | FromType = ToType; |
| 680 | CanonFrom = CanonTo; |
| 681 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | // If we have not converted the argument type to the parameter type, |
| 685 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 686 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 687 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 689 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 690 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 694 | /// expression From (whose potentially-adjusted type is FromType) to |
| 695 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 696 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 698 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 699 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 700 | if (!To) { |
| 701 | return false; |
| 702 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 703 | |
| 704 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 705 | // unsigned short int can be converted to an rvalue of type int if |
| 706 | // int can represent all the values of the source type; otherwise, |
| 707 | // the source rvalue can be converted to an rvalue of type unsigned |
| 708 | // int (C++ 4.5p1). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 709 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 710 | if (// We can promote any signed, promotable integer type to an int |
| 711 | (FromType->isSignedIntegerType() || |
| 712 | // We can promote any unsigned integer type whose size is |
| 713 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 715 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 716 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 719 | return To->getKind() == BuiltinType::UInt; |
| 720 | } |
| 721 | |
| 722 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 723 | // can be converted to an rvalue of the first of the following types |
| 724 | // that can represent all the values of its underlying type: int, |
| 725 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 726 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 727 | && ToType->isIntegerType()) { |
| 728 | // Determine whether the type we're converting from is signed or |
| 729 | // unsigned. |
| 730 | bool FromIsSigned; |
| 731 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 732 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 733 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 734 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 735 | } else { |
| 736 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 737 | FromIsSigned = true; |
| 738 | } |
| 739 | |
| 740 | // The types we'll try to promote to, in the appropriate |
| 741 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | QualType PromoteTypes[6] = { |
| 743 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 744 | Context.LongTy, Context.UnsignedLongTy , |
| 745 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 746 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 747 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 748 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 749 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 751 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 752 | // We found the type that we can promote to. If this is the |
| 753 | // type we wanted, we have a promotion. Otherwise, no |
| 754 | // promotion. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 755 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 756 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 762 | // rvalue of type int if int can represent all the values of the |
| 763 | // bit-field; otherwise, it can be converted to unsigned int if |
| 764 | // unsigned int can represent all the values of the bit-field. If |
| 765 | // the bit-field is larger yet, no integral promotion applies to |
| 766 | // it. If the bit-field has an enumerated type, it is treated as any |
| 767 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 768 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 769 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 770 | using llvm::APSInt; |
| 771 | if (From) |
| 772 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 773 | APSInt BitWidth; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 774 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 775 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 776 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 777 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 779 | // Are we promoting to an int from a bitfield that fits in an int? |
| 780 | if (BitWidth < ToSize || |
| 781 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 782 | return To->getKind() == BuiltinType::Int; |
| 783 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 785 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 786 | // that fits into an unsigned int? |
| 787 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 788 | return To->getKind() == BuiltinType::UInt; |
| 789 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 791 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 792 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 793 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 795 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 796 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 797 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 798 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 799 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 800 | |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 805 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 806 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 808 | /// An rvalue of type float can be converted to an rvalue of type |
| 809 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 810 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 811 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 812 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 813 | ToBuiltin->getKind() == BuiltinType::Double) |
| 814 | return true; |
| 815 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 816 | // C99 6.3.1.5p1: |
| 817 | // When a float is promoted to double or long double, or a |
| 818 | // double is promoted to long double [...]. |
| 819 | if (!getLangOptions().CPlusPlus && |
| 820 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 821 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 822 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 823 | return true; |
| 824 | } |
| 825 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 826 | return false; |
| 827 | } |
| 828 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 829 | /// \brief Determine if a conversion is a complex promotion. |
| 830 | /// |
| 831 | /// A complex promotion is defined as a complex -> complex conversion |
| 832 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 833 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 834 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 835 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 836 | if (!FromComplex) |
| 837 | return false; |
| 838 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 839 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 840 | if (!ToComplex) |
| 841 | return false; |
| 842 | |
| 843 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 844 | ToComplex->getElementType()) || |
| 845 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 846 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 849 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 850 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 851 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 852 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 853 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | static QualType |
| 855 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 856 | QualType ToPointee, QualType ToType, |
| 857 | ASTContext &Context) { |
| 858 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 859 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 860 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
| 862 | // Exact qualifier match -> return the pointer type we're converting to. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 863 | if (CanonToPointee.getQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 864 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 865 | if (!ToType.isNull()) |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 866 | return ToType; |
| 867 | |
| 868 | // Build a pointer to ToPointee. It has the right qualifiers |
| 869 | // already. |
| 870 | return Context.getPointerType(ToPointee); |
| 871 | } |
| 872 | |
| 873 | // Just build a canonical type that has the right qualifiers. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 874 | return Context.getPointerType( |
| 875 | Context.getQualifiedType(CanonToPointee.getUnqualifiedType(), Quals)); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 879 | bool InOverloadResolution, |
| 880 | ASTContext &Context) { |
| 881 | // Handle value-dependent integral null pointer constants correctly. |
| 882 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 883 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 884 | Expr->getType()->isIntegralType()) |
| 885 | return !InOverloadResolution; |
| 886 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 887 | return Expr->isNullPointerConstant(Context, |
| 888 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 889 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 890 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 892 | /// IsPointerConversion - Determines whether the conversion of the |
| 893 | /// expression From, which has the (possibly adjusted) type FromType, |
| 894 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 895 | /// 4.10). If so, returns true and places the converted type (that |
| 896 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 897 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 898 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 899 | /// This routine also supports conversions to and from block pointers |
| 900 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 901 | /// pointers to interfaces. FIXME: Once we've determined the |
| 902 | /// appropriate overloading rules for Objective-C, we may want to |
| 903 | /// split the Objective-C checks into a different routine; however, |
| 904 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 905 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 906 | /// set if the conversion is an allowed Objective-C conversion that |
| 907 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 908 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 909 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 910 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 912 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 913 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 914 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 915 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 917 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 918 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 919 | ConvertedType = ToType; |
| 920 | return true; |
| 921 | } |
| 922 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 923 | // Blocks: Block pointers can be converted to void*. |
| 924 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 925 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 926 | ConvertedType = ToType; |
| 927 | return true; |
| 928 | } |
| 929 | // Blocks: A null pointer constant can be converted to a block |
| 930 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 932 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 933 | ConvertedType = ToType; |
| 934 | return true; |
| 935 | } |
| 936 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 937 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 938 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 940 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 941 | ConvertedType = ToType; |
| 942 | return true; |
| 943 | } |
| 944 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 945 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 946 | if (!ToTypePtr) |
| 947 | return false; |
| 948 | |
| 949 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 950 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 951 | ConvertedType = ToType; |
| 952 | return true; |
| 953 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 955 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 956 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 957 | if (!FromTypePtr) |
| 958 | return false; |
| 959 | |
| 960 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 961 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 962 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 963 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 964 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 965 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 966 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 968 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 969 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 970 | return true; |
| 971 | } |
| 972 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 973 | // When we're overloading in C, we allow a special kind of pointer |
| 974 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 976 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 978 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 980 | return true; |
| 981 | } |
| 982 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 983 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 985 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 986 | // can be converted to an rvalue of type "pointer to cv B," where |
| 987 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 988 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 989 | // necessitates this conversion is ill-formed. The result of the |
| 990 | // conversion is a pointer to the base class sub-object of the |
| 991 | // derived class object. The null pointer value is converted to |
| 992 | // the null pointer value of the destination type. |
| 993 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 994 | // Note that we do not check for ambiguity or inaccessibility |
| 995 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 996 | if (getLangOptions().CPlusPlus && |
| 997 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 998 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1000 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1001 | ToType, Context); |
| 1002 | return true; |
| 1003 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1004 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
| 1007 | |
| 1008 | /// isObjCPointerConversion - Determines whether this is an |
| 1009 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1010 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1012 | QualType& ConvertedType, |
| 1013 | bool &IncompatibleObjC) { |
| 1014 | if (!getLangOptions().ObjC1) |
| 1015 | return false; |
| 1016 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1017 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1018 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1020 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1021 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1022 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1023 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1024 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1025 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1026 | ConvertedType = ToType; |
| 1027 | return true; |
| 1028 | } |
| 1029 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1031 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1033 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1034 | ConvertedType = ToType; |
| 1035 | return true; |
| 1036 | } |
| 1037 | // Objective C++: We're able to convert from a pointer to an |
| 1038 | // interface to a pointer to a different interface. |
| 1039 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1040 | ConvertedType = ToType; |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
| 1044 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1045 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1046 | // interfaces, which is permitted. However, we're going to |
| 1047 | // complain about it. |
| 1048 | IncompatibleObjC = true; |
| 1049 | ConvertedType = FromType; |
| 1050 | return true; |
| 1051 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1053 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1054 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1055 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1056 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1057 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1058 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1059 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1060 | return false; |
| 1061 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1062 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1063 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1064 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1065 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1066 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1067 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1068 | return false; |
| 1069 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1070 | // If we have pointers to pointers, recursively check whether this |
| 1071 | // is an Objective-C conversion. |
| 1072 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1073 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1074 | IncompatibleObjC)) { |
| 1075 | // We always complain about this conversion. |
| 1076 | IncompatibleObjC = true; |
| 1077 | ConvertedType = ToType; |
| 1078 | return true; |
| 1079 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1080 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1081 | // differences in the argument and result types are in Objective-C |
| 1082 | // pointer conversions. If so, we permit the conversion (but |
| 1083 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1085 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1086 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1087 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1088 | if (FromFunctionType && ToFunctionType) { |
| 1089 | // If the function types are exactly the same, this isn't an |
| 1090 | // Objective-C pointer conversion. |
| 1091 | if (Context.getCanonicalType(FromPointeeType) |
| 1092 | == Context.getCanonicalType(ToPointeeType)) |
| 1093 | return false; |
| 1094 | |
| 1095 | // Perform the quick checks that will tell us whether these |
| 1096 | // function types are obviously different. |
| 1097 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1098 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1099 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1100 | return false; |
| 1101 | |
| 1102 | bool HasObjCConversion = false; |
| 1103 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1104 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1105 | // Okay, the types match exactly. Nothing to do. |
| 1106 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1107 | ToFunctionType->getResultType(), |
| 1108 | ConvertedType, IncompatibleObjC)) { |
| 1109 | // Okay, we have an Objective-C pointer conversion. |
| 1110 | HasObjCConversion = true; |
| 1111 | } else { |
| 1112 | // Function types are too different. Abort. |
| 1113 | return false; |
| 1114 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1116 | // Check argument types. |
| 1117 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1118 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1119 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1120 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1121 | if (Context.getCanonicalType(FromArgType) |
| 1122 | == Context.getCanonicalType(ToArgType)) { |
| 1123 | // Okay, the types match exactly. Nothing to do. |
| 1124 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1125 | ConvertedType, IncompatibleObjC)) { |
| 1126 | // Okay, we have an Objective-C pointer conversion. |
| 1127 | HasObjCConversion = true; |
| 1128 | } else { |
| 1129 | // Argument types are too different. Abort. |
| 1130 | return false; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | if (HasObjCConversion) { |
| 1135 | // We had an Objective-C conversion. Allow this pointer |
| 1136 | // conversion, but complain about it. |
| 1137 | ConvertedType = ToType; |
| 1138 | IncompatibleObjC = true; |
| 1139 | return true; |
| 1140 | } |
| 1141 | } |
| 1142 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1143 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1146 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1147 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1148 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1149 | /// conversions for which IsPointerConversion has already returned |
| 1150 | /// true. It returns true and produces a diagnostic if there was an |
| 1151 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1152 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
| 1153 | CastExpr::CastKind &Kind) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1154 | QualType FromType = From->getType(); |
| 1155 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1156 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1157 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1158 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1159 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1161 | if (FromPointeeType->isRecordType() && |
| 1162 | ToPointeeType->isRecordType()) { |
| 1163 | // We must have a derived-to-base conversion. Check an |
| 1164 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1165 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1166 | From->getExprLoc(), |
| 1167 | From->getSourceRange())) |
| 1168 | return true; |
| 1169 | |
| 1170 | // The conversion was successful. |
| 1171 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1172 | } |
| 1173 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1175 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1177 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1178 | // Objective-C++ conversions are always okay. |
| 1179 | // FIXME: We should have a different class of conversions for the |
| 1180 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1181 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1182 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1183 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1184 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1185 | return false; |
| 1186 | } |
| 1187 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1188 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1189 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1190 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1191 | /// If so, returns true and places the converted type (that might differ from |
| 1192 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1193 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1194 | QualType ToType, |
| 1195 | bool InOverloadResolution, |
| 1196 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1197 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1198 | if (!ToTypePtr) |
| 1199 | return false; |
| 1200 | |
| 1201 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1202 | if (From->isNullPointerConstant(Context, |
| 1203 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1204 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1205 | ConvertedType = ToType; |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1210 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1211 | if (!FromTypePtr) |
| 1212 | return false; |
| 1213 | |
| 1214 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1215 | // where D is derived from B (C++ 4.11p2). |
| 1216 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1217 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1218 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1219 | |
| 1220 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1221 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1222 | ToClass.getTypePtr()); |
| 1223 | return true; |
| 1224 | } |
| 1225 | |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
| 1229 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1230 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1231 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1232 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1233 | /// true and produces a diagnostic if there was an error, or returns false |
| 1234 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1236 | CastExpr::CastKind &Kind) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1237 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1238 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1239 | if (!FromPtrType) { |
| 1240 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1241 | assert(From->isNullPointerConstant(Context, |
| 1242 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1243 | "Expr must be null pointer constant!"); |
| 1244 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1245 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1246 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1247 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1248 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1249 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1250 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1251 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1252 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1253 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1254 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1255 | // FIXME: What about dependent types? |
| 1256 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1257 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1258 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1259 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1260 | /*DetectVirtual=*/true); |
| 1261 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1262 | assert(DerivationOkay && |
| 1263 | "Should not have been called if derivation isn't OK."); |
| 1264 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1265 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1266 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1267 | getUnqualifiedType())) { |
| 1268 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1269 | Paths.clear(); |
| 1270 | Paths.setRecordingPaths(true); |
| 1271 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1272 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1273 | (void)StillOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1274 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1275 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1276 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1277 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1278 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1279 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1280 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1281 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1282 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1283 | << FromClass << ToClass << QualType(VBase, 0) |
| 1284 | << From->getSourceRange(); |
| 1285 | return true; |
| 1286 | } |
| 1287 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1288 | // Must be a base to derived member conversion. |
| 1289 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1290 | return false; |
| 1291 | } |
| 1292 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1293 | /// IsQualificationConversion - Determines whether the conversion from |
| 1294 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1295 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | bool |
| 1297 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1298 | FromType = Context.getCanonicalType(FromType); |
| 1299 | ToType = Context.getCanonicalType(ToType); |
| 1300 | |
| 1301 | // If FromType and ToType are the same type, this is not a |
| 1302 | // qualification conversion. |
| 1303 | if (FromType == ToType) |
| 1304 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1306 | // (C++ 4.4p4): |
| 1307 | // A conversion can add cv-qualifiers at levels other than the first |
| 1308 | // in multi-level pointers, subject to the following rules: [...] |
| 1309 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1310 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1311 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1312 | // Within each iteration of the loop, we check the qualifiers to |
| 1313 | // determine if this still looks like a qualification |
| 1314 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1315 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1316 | // until there are no more pointers or pointers-to-members left to |
| 1317 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1318 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1319 | |
| 1320 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1321 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1322 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1323 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1325 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1326 | // every cv for 0 < k < j. |
| 1327 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1328 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1329 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1331 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1332 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1334 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1335 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1336 | |
| 1337 | // We are left with FromType and ToType being the pointee types |
| 1338 | // after unwrapping the original FromType and ToType the same number |
| 1339 | // of types. If we unwrapped any pointers, and if FromType and |
| 1340 | // ToType have the same unqualified type (since we checked |
| 1341 | // qualifiers above), then this is a qualification conversion. |
| 1342 | return UnwrappedAnyPointer && |
| 1343 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1344 | } |
| 1345 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1346 | /// \brief Given a function template or function, extract the function template |
| 1347 | /// declaration (if any) and the underlying function declaration. |
| 1348 | template<typename T> |
| 1349 | static void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function, |
| 1350 | FunctionTemplateDecl *&FunctionTemplate) { |
| 1351 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig); |
| 1352 | if (FunctionTemplate) |
| 1353 | Function = cast<T>(FunctionTemplate->getTemplatedDecl()); |
| 1354 | else |
| 1355 | Function = cast<T>(Orig); |
| 1356 | } |
| 1357 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1358 | /// Determines whether there is a user-defined conversion sequence |
| 1359 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1360 | /// ToType. If such a conversion exists, User will contain the |
| 1361 | /// user-defined conversion sequence that performs such a conversion |
| 1362 | /// and this routine will return true. Otherwise, this routine returns |
| 1363 | /// false and User is unspecified. |
| 1364 | /// |
| 1365 | /// \param AllowConversionFunctions true if the conversion should |
| 1366 | /// consider conversion functions at all. If false, only constructors |
| 1367 | /// will be considered. |
| 1368 | /// |
| 1369 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1370 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1371 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1372 | /// |
| 1373 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1374 | /// for overload resolution. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1375 | Sema::OverloadingResult Sema::IsUserDefinedConversion( |
| 1376 | Expr *From, QualType ToType, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1377 | UserDefinedConversionSequence& User, |
Fariborz Jahanian | 19c7328 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 1378 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1379 | bool AllowConversionFunctions, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | bool AllowExplicit, bool ForceRValue) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1381 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | if (CXXRecordDecl *ToRecordDecl |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1383 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1384 | // C++ [over.match.ctor]p1: |
| 1385 | // When objects of class type are direct-initialized (8.5), or |
| 1386 | // copy-initialized from an expression of the same or a |
| 1387 | // derived class type (8.5), overload resolution selects the |
| 1388 | // constructor. [...] For copy-initialization, the candidate |
| 1389 | // functions are all the converting constructors (12.3.1) of |
| 1390 | // that class. The argument list is the expression-list within |
| 1391 | // the parentheses of the initializer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1393 | = Context.DeclarationNames.getCXXConstructorName( |
| 1394 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1395 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1396 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1397 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1398 | Con != ConEnd; ++Con) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1399 | // Find the constructor (which may be a template). |
| 1400 | CXXConstructorDecl *Constructor = 0; |
| 1401 | FunctionTemplateDecl *ConstructorTmpl |
| 1402 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1403 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1405 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1406 | else |
| 1407 | Constructor = cast<CXXConstructorDecl>(*Con); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1409 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1410 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1411 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1413 | 1, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | /*SuppressUserConversions=*/true, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1415 | ForceRValue); |
| 1416 | else |
| 1417 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 1418 | /*SuppressUserConversions=*/true, ForceRValue); |
| 1419 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1420 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1424 | if (!AllowConversionFunctions) { |
| 1425 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1427 | PDiag(0) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1428 | << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1429 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | } else if (const RecordType *FromRecordType |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1431 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1433 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1434 | // Add all of the conversion functions as candidates. |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1435 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1436 | = FromRecordDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1437 | for (OverloadedFunctionDecl::function_iterator Func |
| 1438 | = Conversions->function_begin(); |
| 1439 | Func != Conversions->function_end(); ++Func) { |
| 1440 | CXXConversionDecl *Conv; |
| 1441 | FunctionTemplateDecl *ConvTemplate; |
| 1442 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 1443 | if (ConvTemplate) |
| 1444 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1445 | else |
| 1446 | Conv = dyn_cast<CXXConversionDecl>(*Func); |
| 1447 | |
| 1448 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1449 | if (ConvTemplate) |
| 1450 | AddTemplateConversionCandidate(ConvTemplate, From, ToType, |
| 1451 | CandidateSet); |
| 1452 | else |
| 1453 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1454 | } |
| 1455 | } |
| 1456 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1457 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1458 | |
| 1459 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1460 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1461 | case OR_Success: |
| 1462 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1464 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1465 | // C++ [over.ics.user]p1: |
| 1466 | // If the user-defined conversion is specified by a |
| 1467 | // constructor (12.3.1), the initial standard conversion |
| 1468 | // sequence converts the source type to the type required by |
| 1469 | // the argument of the constructor. |
| 1470 | // |
| 1471 | // FIXME: What about ellipsis conversions? |
| 1472 | QualType ThisType = Constructor->getThisType(Context); |
| 1473 | User.Before = Best->Conversions[0].Standard; |
| 1474 | User.ConversionFunction = Constructor; |
| 1475 | User.After.setAsIdentityConversion(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | User.After.FromTypePtr |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1477 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1478 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1479 | return OR_Success; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1480 | } else if (CXXConversionDecl *Conversion |
| 1481 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1482 | // C++ [over.ics.user]p1: |
| 1483 | // |
| 1484 | // [...] If the user-defined conversion is specified by a |
| 1485 | // conversion function (12.3.2), the initial standard |
| 1486 | // conversion sequence converts the source type to the |
| 1487 | // implicit object parameter of the conversion function. |
| 1488 | User.Before = Best->Conversions[0].Standard; |
| 1489 | User.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
| 1491 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1492 | // The second standard conversion sequence converts the |
| 1493 | // result of the user-defined conversion to the target type |
| 1494 | // for the sequence. Since an implicit conversion sequence |
| 1495 | // is an initialization, the special rules for |
| 1496 | // initialization by user-defined conversion apply when |
| 1497 | // selecting the best user-defined conversion for a |
| 1498 | // user-defined conversion sequence (see 13.3.3 and |
| 1499 | // 13.3.3.1). |
| 1500 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1501 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1502 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1503 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1504 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1505 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1507 | case OR_No_Viable_Function: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1508 | return OR_No_Viable_Function; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1509 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1510 | // No conversion here! We're done. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1511 | return OR_Deleted; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1512 | |
| 1513 | case OR_Ambiguous: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1514 | return OR_Ambiguous; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1517 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1518 | } |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1519 | |
| 1520 | bool |
| 1521 | Sema::DiagnoseAmbiguousUserDefinedConversion(Expr *From, QualType ToType) { |
| 1522 | ImplicitConversionSequence ICS; |
| 1523 | OverloadCandidateSet CandidateSet; |
| 1524 | OverloadingResult OvResult = |
| 1525 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1526 | CandidateSet, true, false, false); |
| 1527 | if (OvResult != OR_Ambiguous) |
| 1528 | return false; |
| 1529 | Diag(From->getSourceRange().getBegin(), |
| 1530 | diag::err_typecheck_ambiguous_condition) |
| 1531 | << From->getType() << ToType << From->getSourceRange(); |
| 1532 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 1533 | return true; |
| 1534 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1535 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1536 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1537 | /// conversion sequences to determine whether one is better than the |
| 1538 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1540 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1541 | const ImplicitConversionSequence& ICS2) |
| 1542 | { |
| 1543 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1544 | // conversion sequences (as defined in 13.3.3.1) |
| 1545 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1546 | // conversion sequence than a user-defined conversion sequence or |
| 1547 | // an ellipsis conversion sequence, and |
| 1548 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1549 | // conversion sequence than an ellipsis conversion sequence |
| 1550 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | // |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1552 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1553 | return ImplicitConversionSequence::Better; |
| 1554 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1555 | return ImplicitConversionSequence::Worse; |
| 1556 | |
| 1557 | // Two implicit conversion sequences of the same form are |
| 1558 | // indistinguishable conversion sequences unless one of the |
| 1559 | // following rules apply: (C++ 13.3.3.2p3): |
| 1560 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1561 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | else if (ICS1.ConversionKind == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1563 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1564 | // User-defined conversion sequence U1 is a better conversion |
| 1565 | // sequence than another user-defined conversion sequence U2 if |
| 1566 | // they contain the same user-defined conversion function or |
| 1567 | // constructor and if the second standard conversion sequence of |
| 1568 | // U1 is better than the second standard conversion sequence of |
| 1569 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1571 | ICS2.UserDefined.ConversionFunction) |
| 1572 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1573 | ICS2.UserDefined.After); |
| 1574 | } |
| 1575 | |
| 1576 | return ImplicitConversionSequence::Indistinguishable; |
| 1577 | } |
| 1578 | |
| 1579 | /// CompareStandardConversionSequences - Compare two standard |
| 1580 | /// conversion sequences to determine whether one is better than the |
| 1581 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1583 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1584 | const StandardConversionSequence& SCS2) |
| 1585 | { |
| 1586 | // Standard conversion sequence S1 is a better conversion sequence |
| 1587 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1588 | |
| 1589 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1590 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1591 | // excluding any Lvalue Transformation; the identity conversion |
| 1592 | // sequence is considered to be a subsequence of any |
| 1593 | // non-identity conversion sequence) or, if not that, |
| 1594 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1595 | // Neither is a proper subsequence of the other. Do nothing. |
| 1596 | ; |
| 1597 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1598 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1600 | SCS1.Third == ICK_Identity)) |
| 1601 | // SCS1 is a proper subsequence of SCS2. |
| 1602 | return ImplicitConversionSequence::Better; |
| 1603 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1604 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1606 | SCS2.Third == ICK_Identity)) |
| 1607 | // SCS2 is a proper subsequence of SCS1. |
| 1608 | return ImplicitConversionSequence::Worse; |
| 1609 | |
| 1610 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1611 | // defined below), or, if not that, |
| 1612 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1613 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1614 | if (Rank1 < Rank2) |
| 1615 | return ImplicitConversionSequence::Better; |
| 1616 | else if (Rank2 < Rank1) |
| 1617 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1618 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1619 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1620 | // are indistinguishable unless one of the following rules |
| 1621 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1623 | // A conversion that is not a conversion of a pointer, or |
| 1624 | // pointer to member, to bool is better than another conversion |
| 1625 | // that is such a conversion. |
| 1626 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1627 | return SCS2.isPointerConversionToBool() |
| 1628 | ? ImplicitConversionSequence::Better |
| 1629 | : ImplicitConversionSequence::Worse; |
| 1630 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1631 | // C++ [over.ics.rank]p4b2: |
| 1632 | // |
| 1633 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1634 | // conversion of B* to A* is better than conversion of B* to |
| 1635 | // void*, and conversion of A* to void* is better than conversion |
| 1636 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1638 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1640 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1641 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1642 | // Exactly one of the conversion sequences is a conversion to |
| 1643 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1644 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1645 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1646 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1647 | // Neither conversion sequence converts to a void pointer; compare |
| 1648 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1649 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1650 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1651 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1652 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1653 | // Both conversion sequences are conversions to void |
| 1654 | // pointers. Compare the source types to determine if there's an |
| 1655 | // inheritance relationship in their sources. |
| 1656 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1657 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1658 | |
| 1659 | // Adjust the types we're converting from via the array-to-pointer |
| 1660 | // conversion, if we need to. |
| 1661 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1662 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1663 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1664 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1665 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1667 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1668 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1669 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1670 | |
| 1671 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1672 | return ImplicitConversionSequence::Better; |
| 1673 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1674 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Objective-C++: If one interface is more specific than the |
| 1677 | // other, it is the better one. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1678 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1679 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1680 | if (FromIface1 && FromIface1) { |
| 1681 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1682 | return ImplicitConversionSequence::Better; |
| 1683 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1684 | return ImplicitConversionSequence::Worse; |
| 1685 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1686 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1689 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1691 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1692 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1693 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1694 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1695 | // C++0x [over.ics.rank]p3b4: |
| 1696 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1697 | // implicit object parameter of a non-static member function declared |
| 1698 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1699 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1700 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1701 | // or if the member function in this case has a ref qualifier. |
| 1702 | // (Of course, we don't have ref qualifiers yet.) |
| 1703 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1704 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1705 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1706 | |
| 1707 | // C++ [over.ics.rank]p3b4: |
| 1708 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1709 | // which the references refer are the same type except for |
| 1710 | // top-level cv-qualifiers, and the type to which the reference |
| 1711 | // initialized by S2 refers is more cv-qualified than the type |
| 1712 | // to which the reference initialized by S1 refers. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1713 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1714 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1715 | T1 = Context.getCanonicalType(T1); |
| 1716 | T2 = Context.getCanonicalType(T2); |
| 1717 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1718 | if (T2.isMoreQualifiedThan(T1)) |
| 1719 | return ImplicitConversionSequence::Better; |
| 1720 | else if (T1.isMoreQualifiedThan(T2)) |
| 1721 | return ImplicitConversionSequence::Worse; |
| 1722 | } |
| 1723 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1724 | |
| 1725 | return ImplicitConversionSequence::Indistinguishable; |
| 1726 | } |
| 1727 | |
| 1728 | /// CompareQualificationConversions - Compares two standard conversion |
| 1729 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1730 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1731 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1732 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1734 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1735 | // -- S1 and S2 differ only in their qualification conversion and |
| 1736 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1737 | // cv-qualification signature of type T1 is a proper subset of |
| 1738 | // the cv-qualification signature of type T2, and S1 is not the |
| 1739 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1740 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1741 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1742 | return ImplicitConversionSequence::Indistinguishable; |
| 1743 | |
| 1744 | // FIXME: the example in the standard doesn't use a qualification |
| 1745 | // conversion (!) |
| 1746 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1747 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1748 | T1 = Context.getCanonicalType(T1); |
| 1749 | T2 = Context.getCanonicalType(T2); |
| 1750 | |
| 1751 | // If the types are the same, we won't learn anything by unwrapped |
| 1752 | // them. |
| 1753 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1754 | return ImplicitConversionSequence::Indistinguishable; |
| 1755 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1757 | = ImplicitConversionSequence::Indistinguishable; |
| 1758 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1759 | // Within each iteration of the loop, we check the qualifiers to |
| 1760 | // determine if this still looks like a qualification |
| 1761 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1762 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1763 | // until there are no more pointers or pointers-to-members left |
| 1764 | // to unwrap. This essentially mimics what |
| 1765 | // IsQualificationConversion does, but here we're checking for a |
| 1766 | // strict subset of qualifiers. |
| 1767 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1768 | // The qualifiers are the same, so this doesn't tell us anything |
| 1769 | // about how the sequences rank. |
| 1770 | ; |
| 1771 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1772 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1773 | if (Result == ImplicitConversionSequence::Worse) |
| 1774 | // Neither has qualifiers that are a subset of the other's |
| 1775 | // qualifiers. |
| 1776 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1778 | Result = ImplicitConversionSequence::Better; |
| 1779 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1780 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1781 | if (Result == ImplicitConversionSequence::Better) |
| 1782 | // Neither has qualifiers that are a subset of the other's |
| 1783 | // qualifiers. |
| 1784 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1786 | Result = ImplicitConversionSequence::Worse; |
| 1787 | } else { |
| 1788 | // Qualifiers are disjoint. |
| 1789 | return ImplicitConversionSequence::Indistinguishable; |
| 1790 | } |
| 1791 | |
| 1792 | // If the types after this point are equivalent, we're done. |
| 1793 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1794 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1797 | // Check that the winning standard conversion sequence isn't using |
| 1798 | // the deprecated string literal array to pointer conversion. |
| 1799 | switch (Result) { |
| 1800 | case ImplicitConversionSequence::Better: |
| 1801 | if (SCS1.Deprecated) |
| 1802 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1803 | break; |
| 1804 | |
| 1805 | case ImplicitConversionSequence::Indistinguishable: |
| 1806 | break; |
| 1807 | |
| 1808 | case ImplicitConversionSequence::Worse: |
| 1809 | if (SCS2.Deprecated) |
| 1810 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1811 | break; |
| 1812 | } |
| 1813 | |
| 1814 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1817 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1818 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1819 | /// various kinds of derived-to-base conversions (C++ |
| 1820 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1821 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1822 | ImplicitConversionSequence::CompareKind |
| 1823 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1824 | const StandardConversionSequence& SCS2) { |
| 1825 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1826 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1827 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1828 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1829 | |
| 1830 | // Adjust the types we're converting from via the array-to-pointer |
| 1831 | // conversion, if we need to. |
| 1832 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1833 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1834 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1835 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1836 | |
| 1837 | // Canonicalize all of the types. |
| 1838 | FromType1 = Context.getCanonicalType(FromType1); |
| 1839 | ToType1 = Context.getCanonicalType(ToType1); |
| 1840 | FromType2 = Context.getCanonicalType(FromType2); |
| 1841 | ToType2 = Context.getCanonicalType(ToType2); |
| 1842 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1843 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1844 | // |
| 1845 | // If class B is derived directly or indirectly from class A and |
| 1846 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1847 | // |
| 1848 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1849 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1853 | SCS2.Second == ICK_Pointer_Conversion && |
| 1854 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1855 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1856 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1858 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1860 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1861 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1862 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1863 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1864 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1865 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1866 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1867 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1868 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 1869 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1871 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1872 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1873 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1874 | return ImplicitConversionSequence::Better; |
| 1875 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1876 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1877 | |
| 1878 | if (ToIface1 && ToIface2) { |
| 1879 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1880 | return ImplicitConversionSequence::Better; |
| 1881 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1882 | return ImplicitConversionSequence::Worse; |
| 1883 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1884 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1885 | |
| 1886 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1887 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1888 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1889 | return ImplicitConversionSequence::Better; |
| 1890 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1891 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1893 | if (FromIface1 && FromIface2) { |
| 1894 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1895 | return ImplicitConversionSequence::Better; |
| 1896 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1897 | return ImplicitConversionSequence::Worse; |
| 1898 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1899 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1902 | // Compare based on reference bindings. |
| 1903 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1904 | SCS1.Second == ICK_Derived_To_Base) { |
| 1905 | // -- binding of an expression of type C to a reference of type |
| 1906 | // B& is better than binding an expression of type C to a |
| 1907 | // reference of type A&, |
| 1908 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1909 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1910 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1911 | return ImplicitConversionSequence::Better; |
| 1912 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1913 | return ImplicitConversionSequence::Worse; |
| 1914 | } |
| 1915 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1916 | // -- binding of an expression of type B to a reference of type |
| 1917 | // A& is better than binding an expression of type C to a |
| 1918 | // reference of type A&, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1919 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1920 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1921 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1922 | return ImplicitConversionSequence::Better; |
| 1923 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1924 | return ImplicitConversionSequence::Worse; |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | |
| 1929 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1930 | // A::* to C::*, |
| 1931 | |
| 1932 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1933 | // A::* to C::*, and |
| 1934 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1935 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1936 | SCS1.Second == ICK_Derived_To_Base) { |
| 1937 | // -- conversion of C to B is better than conversion of C to A, |
| 1938 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1939 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1940 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1941 | return ImplicitConversionSequence::Better; |
| 1942 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1943 | return ImplicitConversionSequence::Worse; |
| 1944 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1946 | // -- conversion of B to A is better than conversion of C to A. |
| 1947 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1948 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1949 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1950 | return ImplicitConversionSequence::Better; |
| 1951 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1952 | return ImplicitConversionSequence::Worse; |
| 1953 | } |
| 1954 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1956 | return ImplicitConversionSequence::Indistinguishable; |
| 1957 | } |
| 1958 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1959 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1960 | /// ToType from the expression From. Return the implicit conversion |
| 1961 | /// sequence required to pass this argument, which may be a bad |
| 1962 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1963 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1964 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 1965 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | ImplicitConversionSequence |
| 1967 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 1968 | bool SuppressUserConversions, bool ForceRValue, |
| 1969 | bool InOverloadResolution) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1970 | if (ToType->isReferenceType()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1971 | ImplicitConversionSequence ICS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1973 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1974 | SuppressUserConversions, |
| 1975 | /*AllowExplicit=*/false, |
| 1976 | ForceRValue, |
| 1977 | &ICS); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1978 | return ICS; |
| 1979 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1981 | SuppressUserConversions, |
| 1982 | /*AllowExplicit=*/false, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1983 | ForceRValue, |
| 1984 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1988 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 1989 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 1990 | /// an error, returns false if the initialization succeeded. Elidable should |
| 1991 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 1992 | /// differently in C++0x for this case. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1994 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1995 | if (!getLangOptions().CPlusPlus) { |
| 1996 | // In C, argument passing is the same as performing an assignment. |
| 1997 | QualType FromType = From->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1999 | AssignConvertType ConvTy = |
| 2000 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2001 | if (ConvTy != Compatible && |
| 2002 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2003 | ConvTy = Compatible; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2005 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 2006 | FromType, From, Flavor); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2007 | } |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2008 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2009 | if (ToType->isReferenceType()) |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2010 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2011 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2012 | /*SuppressUserConversions=*/false, |
| 2013 | /*AllowExplicit=*/false, |
| 2014 | /*ForceRValue=*/false); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2015 | |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2016 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 2017 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2018 | return false; |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2019 | if (!DiagnoseAmbiguousUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2020 | return Diag(From->getSourceRange().getBegin(), |
| 2021 | diag::err_typecheck_convert_incompatible) |
| 2022 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2023 | return true; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2026 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2027 | /// parameter of the given member function (@c Method) from the |
| 2028 | /// expression @p From. |
| 2029 | ImplicitConversionSequence |
| 2030 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 2031 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2032 | QualType ImplicitParamType |
| 2033 | = Context.getCVRQualifiedType(ClassType, Method->getTypeQualifiers()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2034 | |
| 2035 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2036 | // to exit early. |
| 2037 | ImplicitConversionSequence ICS; |
| 2038 | ICS.Standard.setAsIdentityConversion(); |
| 2039 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2040 | |
| 2041 | // We need to have an object of class type. |
| 2042 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2043 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2044 | FromType = PT->getPointeeType(); |
| 2045 | |
| 2046 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2047 | |
| 2048 | // The implicit object parmeter is has the type "reference to cv X", |
| 2049 | // where X is the class of which the function is a member |
| 2050 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2051 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2053 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2054 | // reference binding here, that allows class rvalues to bind to |
| 2055 | // non-constant references. |
| 2056 | |
| 2057 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2058 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2059 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 2060 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 2061 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 2062 | return ICS; |
| 2063 | |
| 2064 | // Check that we have either the same type or a derived type. It |
| 2065 | // affects the conversion rank. |
| 2066 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 2067 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 2068 | ICS.Standard.Second = ICK_Identity; |
| 2069 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2070 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2071 | else |
| 2072 | return ICS; |
| 2073 | |
| 2074 | // Success. Mark this as a reference binding. |
| 2075 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2076 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2077 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2078 | ICS.Standard.ReferenceBinding = true; |
| 2079 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2080 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2081 | return ICS; |
| 2082 | } |
| 2083 | |
| 2084 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2085 | /// the implicit object parameter for the given Method with the given |
| 2086 | /// expression. |
| 2087 | bool |
| 2088 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2089 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2091 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2093 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2094 | FromRecordType = PT->getPointeeType(); |
| 2095 | DestType = Method->getThisType(Context); |
| 2096 | } else { |
| 2097 | FromRecordType = From->getType(); |
| 2098 | DestType = ImplicitParamRecordType; |
| 2099 | } |
| 2100 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | ImplicitConversionSequence ICS |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2102 | = TryObjectArgumentInitialization(From, Method); |
| 2103 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2104 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2105 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2106 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2108 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2109 | CheckDerivedToBaseConversion(FromRecordType, |
| 2110 | ImplicitParamRecordType, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2111 | From->getSourceRange().getBegin(), |
| 2112 | From->getSourceRange())) |
| 2113 | return true; |
| 2114 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 4f4aab2 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2116 | /*isLvalue=*/true); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2117 | return false; |
| 2118 | } |
| 2119 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2120 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2121 | /// expression From to bool (C++0x [conv]p3). |
| 2122 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2124 | // FIXME: Are these flags correct? |
| 2125 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2127 | /*ForceRValue=*/false, |
| 2128 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2132 | /// of the expression From to bool (C++0x [conv]p3). |
| 2133 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2134 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2135 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2136 | return false; |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2137 | |
| 2138 | if (!DiagnoseAmbiguousUserDefinedConversion(From, Context.BoolTy)) |
| 2139 | return Diag(From->getSourceRange().getBegin(), |
| 2140 | diag::err_typecheck_bool_condition) |
| 2141 | << From->getType() << From->getSourceRange(); |
| 2142 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2145 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2146 | /// candidate functions, using the given function call arguments. If |
| 2147 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2148 | /// conversions via constructors or conversion operators. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2149 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2150 | /// hacky way to implement the overloading rules for elidable copy |
| 2151 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2152 | /// |
| 2153 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2154 | /// based on an incomplete set of function arguments. This feature is used by |
| 2155 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | void |
| 2157 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2158 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2159 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2160 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2161 | bool ForceRValue, |
| 2162 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2164 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2165 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2167 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2169 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2171 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2172 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2173 | // If we get here, it's because we're calling a member function |
| 2174 | // that is named without a member access expression (e.g., |
| 2175 | // "this->f") that was either written explicitly or created |
| 2176 | // implicitly. This can happen with a qualified call to a member |
| 2177 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2178 | // object argument (C++ [over.call.func]p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2180 | SuppressUserConversions, ForceRValue); |
| 2181 | return; |
| 2182 | } |
| 2183 | // We treat a constructor like a non-member function, since its object |
| 2184 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2187 | if (!CandidateSet.isNewCandidate(Function)) |
| 2188 | return; |
| 2189 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2190 | // Add this candidate |
| 2191 | CandidateSet.push_back(OverloadCandidate()); |
| 2192 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2193 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2194 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2195 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2196 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2197 | |
| 2198 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2199 | |
| 2200 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2201 | // parameters is viable only if it has an ellipsis in its parameter |
| 2202 | // list (8.3.5). |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2203 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2204 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2205 | Candidate.Viable = false; |
| 2206 | return; |
| 2207 | } |
| 2208 | |
| 2209 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2210 | // is viable only if the (m+1)st parameter has a default argument |
| 2211 | // (8.3.6). For the purposes of overload resolution, the |
| 2212 | // parameter list is truncated on the right, so that there are |
| 2213 | // exactly m parameters. |
| 2214 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2215 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2216 | // Not enough arguments. |
| 2217 | Candidate.Viable = false; |
| 2218 | return; |
| 2219 | } |
| 2220 | |
| 2221 | // Determine the implicit conversion sequences for each of the |
| 2222 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2223 | Candidate.Conversions.resize(NumArgs); |
| 2224 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2225 | if (ArgIdx < NumArgsInProto) { |
| 2226 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2227 | // exist for each argument an implicit conversion sequence |
| 2228 | // (13.3.3.1) that converts that argument to the corresponding |
| 2229 | // parameter of F. |
| 2230 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | Candidate.Conversions[ArgIdx] |
| 2232 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2233 | SuppressUserConversions, ForceRValue, |
| 2234 | /*InOverloadResolution=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2236 | == ImplicitConversionSequence::BadConversion) { |
Fariborz Jahanian | c9c3917 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2237 | // 13.3.3.1-p10 If several different sequences of conversions exist that |
| 2238 | // each convert the argument to the parameter type, the implicit conversion |
| 2239 | // sequence associated with the parameter is defined to be the unique conversion |
| 2240 | // sequence designated the ambiguous conversion sequence. For the purpose of |
| 2241 | // ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous |
| 2242 | // conversion sequence is treated as a user-defined sequence that is |
| 2243 | // indistinguishable from any other user-defined conversion sequence |
Fariborz Jahanian | 91ae9fd | 2009-09-29 17:31:54 +0000 | [diff] [blame^] | 2244 | if (!Candidate.Conversions[ArgIdx].ConversionFunctionSet.empty()) { |
Fariborz Jahanian | c9c3917 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2245 | Candidate.Conversions[ArgIdx].ConversionKind = |
| 2246 | ImplicitConversionSequence::UserDefinedConversion; |
Fariborz Jahanian | 91ae9fd | 2009-09-29 17:31:54 +0000 | [diff] [blame^] | 2247 | // Set the conversion function to one of them. As due to ambiguity, |
| 2248 | // they carry the same weight and is needed for overload resolution |
| 2249 | // later. |
| 2250 | Candidate.Conversions[ArgIdx].UserDefined.ConversionFunction = |
| 2251 | Candidate.Conversions[ArgIdx].ConversionFunctionSet[0]; |
| 2252 | } |
Fariborz Jahanian | c9c3917 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2253 | else { |
| 2254 | Candidate.Viable = false; |
| 2255 | break; |
| 2256 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2257 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2258 | } else { |
| 2259 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2260 | // argument for which there is no corresponding parameter is |
| 2261 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2263 | = ImplicitConversionSequence::EllipsisConversion; |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2268 | /// \brief Add all of the function declarations in the given function set to |
| 2269 | /// the overload canddiate set. |
| 2270 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2271 | Expr **Args, unsigned NumArgs, |
| 2272 | OverloadCandidateSet& CandidateSet, |
| 2273 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2274 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2275 | FEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2276 | F != FEnd; ++F) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2277 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2278 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2279 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
| 2280 | Args[0], Args + 1, NumArgs - 1, |
| 2281 | CandidateSet, SuppressUserConversions); |
| 2282 | else |
| 2283 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2284 | SuppressUserConversions); |
| 2285 | } else { |
| 2286 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2287 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2288 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2289 | AddMethodTemplateCandidate(FunTmpl, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2290 | /*FIXME: explicit args */false, 0, 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2291 | Args[0], Args + 1, NumArgs - 1, |
| 2292 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2293 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2294 | else |
| 2295 | AddTemplateOverloadCandidate(FunTmpl, |
| 2296 | /*FIXME: explicit args */false, 0, 0, |
| 2297 | Args, NumArgs, CandidateSet, |
| 2298 | SuppressUserConversions); |
| 2299 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2300 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2303 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2304 | /// of candidate functions, using the given function call arguments |
| 2305 | /// and the object argument (@c Object). For example, in a call |
| 2306 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2307 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2308 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2309 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2310 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2311 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | void |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2313 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2314 | Expr **Args, unsigned NumArgs, |
| 2315 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | bool SuppressUserConversions, bool ForceRValue) { |
| 2317 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2318 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2319 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2320 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2321 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2322 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2323 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2325 | if (!CandidateSet.isNewCandidate(Method)) |
| 2326 | return; |
| 2327 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2328 | // Add this candidate |
| 2329 | CandidateSet.push_back(OverloadCandidate()); |
| 2330 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2331 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2332 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2333 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2334 | |
| 2335 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2336 | |
| 2337 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2338 | // parameters is viable only if it has an ellipsis in its parameter |
| 2339 | // list (8.3.5). |
| 2340 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2341 | Candidate.Viable = false; |
| 2342 | return; |
| 2343 | } |
| 2344 | |
| 2345 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2346 | // is viable only if the (m+1)st parameter has a default argument |
| 2347 | // (8.3.6). For the purposes of overload resolution, the |
| 2348 | // parameter list is truncated on the right, so that there are |
| 2349 | // exactly m parameters. |
| 2350 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2351 | if (NumArgs < MinRequiredArgs) { |
| 2352 | // Not enough arguments. |
| 2353 | Candidate.Viable = false; |
| 2354 | return; |
| 2355 | } |
| 2356 | |
| 2357 | Candidate.Viable = true; |
| 2358 | Candidate.Conversions.resize(NumArgs + 1); |
| 2359 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2360 | if (Method->isStatic() || !Object) |
| 2361 | // The implicit object argument is ignored. |
| 2362 | Candidate.IgnoreObjectArgument = true; |
| 2363 | else { |
| 2364 | // Determine the implicit conversion sequence for the object |
| 2365 | // parameter. |
| 2366 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2368 | == ImplicitConversionSequence::BadConversion) { |
| 2369 | Candidate.Viable = false; |
| 2370 | return; |
| 2371 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | // Determine the implicit conversion sequences for each of the |
| 2375 | // arguments. |
| 2376 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2377 | if (ArgIdx < NumArgsInProto) { |
| 2378 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2379 | // exist for each argument an implicit conversion sequence |
| 2380 | // (13.3.3.1) that converts that argument to the corresponding |
| 2381 | // parameter of F. |
| 2382 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | Candidate.Conversions[ArgIdx + 1] |
| 2384 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2385 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2386 | /*InOverloadResolution=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2388 | == ImplicitConversionSequence::BadConversion) { |
| 2389 | Candidate.Viable = false; |
| 2390 | break; |
| 2391 | } |
| 2392 | } else { |
| 2393 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2394 | // argument for which there is no corresponding parameter is |
| 2395 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2397 | = ImplicitConversionSequence::EllipsisConversion; |
| 2398 | } |
| 2399 | } |
| 2400 | } |
| 2401 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2402 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2403 | /// set, using template argument deduction to produce an appropriate member |
| 2404 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2406 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2407 | bool HasExplicitTemplateArgs, |
| 2408 | const TemplateArgument *ExplicitTemplateArgs, |
| 2409 | unsigned NumExplicitTemplateArgs, |
| 2410 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2411 | OverloadCandidateSet& CandidateSet, |
| 2412 | bool SuppressUserConversions, |
| 2413 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2414 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2415 | return; |
| 2416 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2417 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2419 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2421 | // candidate functions in the usual way.113) A given name can refer to one |
| 2422 | // or more function templates and also to a set of overloaded non-template |
| 2423 | // functions. In such a case, the candidate functions generated from each |
| 2424 | // function template are combined with the set of non-template candidate |
| 2425 | // functions. |
| 2426 | TemplateDeductionInfo Info(Context); |
| 2427 | FunctionDecl *Specialization = 0; |
| 2428 | if (TemplateDeductionResult Result |
| 2429 | = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs, |
| 2430 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2431 | Args, NumArgs, Specialization, Info)) { |
| 2432 | // FIXME: Record what happened with template argument deduction, so |
| 2433 | // that we can give the user a beautiful diagnostic. |
| 2434 | (void)Result; |
| 2435 | return; |
| 2436 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2438 | // Add the function template specialization produced by template argument |
| 2439 | // deduction as a candidate. |
| 2440 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2442 | "Specialization is not a member function?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2444 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2445 | } |
| 2446 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2447 | /// \brief Add a C++ function template specialization as a candidate |
| 2448 | /// in the candidate set, using template argument deduction to produce |
| 2449 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2451 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2452 | bool HasExplicitTemplateArgs, |
| 2453 | const TemplateArgument *ExplicitTemplateArgs, |
| 2454 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2455 | Expr **Args, unsigned NumArgs, |
| 2456 | OverloadCandidateSet& CandidateSet, |
| 2457 | bool SuppressUserConversions, |
| 2458 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2459 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2460 | return; |
| 2461 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2462 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2464 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2466 | // candidate functions in the usual way.113) A given name can refer to one |
| 2467 | // or more function templates and also to a set of overloaded non-template |
| 2468 | // functions. In such a case, the candidate functions generated from each |
| 2469 | // function template are combined with the set of non-template candidate |
| 2470 | // functions. |
| 2471 | TemplateDeductionInfo Info(Context); |
| 2472 | FunctionDecl *Specialization = 0; |
| 2473 | if (TemplateDeductionResult Result |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2474 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2475 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2476 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2477 | // FIXME: Record what happened with template argument deduction, so |
| 2478 | // that we can give the user a beautiful diagnostic. |
| 2479 | (void)Result; |
| 2480 | return; |
| 2481 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2483 | // Add the function template specialization produced by template argument |
| 2484 | // deduction as a candidate. |
| 2485 | assert(Specialization && "Missing function template specialization?"); |
| 2486 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2487 | SuppressUserConversions, ForceRValue); |
| 2488 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2490 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2491 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2492 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2494 | /// (which may or may not be the same type as the type that the |
| 2495 | /// conversion function produces). |
| 2496 | void |
| 2497 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2498 | Expr *From, QualType ToType, |
| 2499 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2500 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2501 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2502 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2503 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2504 | return; |
| 2505 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2506 | // Add this candidate |
| 2507 | CandidateSet.push_back(OverloadCandidate()); |
| 2508 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2509 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2510 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2511 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2512 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2514 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2515 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2516 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2517 | // Determine the implicit conversion sequence for the implicit |
| 2518 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2519 | Candidate.Viable = true; |
| 2520 | Candidate.Conversions.resize(1); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2521 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2522 | // Conversion functions to a different type in the base class is visible in |
| 2523 | // the derived class. So, a derived to base conversion should not participate |
| 2524 | // in overload resolution. |
| 2525 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2526 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2528 | == ImplicitConversionSequence::BadConversion) { |
| 2529 | Candidate.Viable = false; |
| 2530 | return; |
| 2531 | } |
| 2532 | |
| 2533 | // To determine what the conversion from the result of calling the |
| 2534 | // conversion function to the type we're eventually trying to |
| 2535 | // convert to (ToType), we need to synthesize a call to the |
| 2536 | // conversion function and attempt copy initialization from it. This |
| 2537 | // makes sure that we get the right semantics with respect to |
| 2538 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2539 | // call on the stack and we don't need its arguments to be |
| 2540 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2542 | SourceLocation()); |
| 2543 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2544 | CastExpr::CK_Unknown, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2545 | &ConversionRef, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | |
| 2547 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2548 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2549 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2551 | Conversion->getConversionType().getNonReferenceType(), |
| 2552 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | ImplicitConversionSequence ICS = |
| 2554 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2555 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2556 | /*ForceRValue=*/false, |
| 2557 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2559 | switch (ICS.ConversionKind) { |
| 2560 | case ImplicitConversionSequence::StandardConversion: |
| 2561 | Candidate.FinalConversion = ICS.Standard; |
| 2562 | break; |
| 2563 | |
| 2564 | case ImplicitConversionSequence::BadConversion: |
| 2565 | Candidate.Viable = false; |
| 2566 | break; |
| 2567 | |
| 2568 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2570 | "Can only end up with a standard conversion sequence or failure"); |
| 2571 | } |
| 2572 | } |
| 2573 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2574 | /// \brief Adds a conversion function template specialization |
| 2575 | /// candidate to the overload set, using template argument deduction |
| 2576 | /// to deduce the template arguments of the conversion function |
| 2577 | /// template from the type that we are converting to (C++ |
| 2578 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2580 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2581 | Expr *From, QualType ToType, |
| 2582 | OverloadCandidateSet &CandidateSet) { |
| 2583 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2584 | "Only conversion function templates permitted here"); |
| 2585 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2586 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2587 | return; |
| 2588 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2589 | TemplateDeductionInfo Info(Context); |
| 2590 | CXXConversionDecl *Specialization = 0; |
| 2591 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2593 | Specialization, Info)) { |
| 2594 | // FIXME: Record what happened with template argument deduction, so |
| 2595 | // that we can give the user a beautiful diagnostic. |
| 2596 | (void)Result; |
| 2597 | return; |
| 2598 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2600 | // Add the conversion function template specialization produced by |
| 2601 | // template argument deduction as a candidate. |
| 2602 | assert(Specialization && "Missing function template specialization?"); |
| 2603 | AddConversionCandidate(Specialization, From, ToType, CandidateSet); |
| 2604 | } |
| 2605 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2606 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2607 | /// converts the given @c Object to a function pointer via the |
| 2608 | /// conversion function @c Conversion, and then attempts to call it |
| 2609 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2610 | /// the type of function that we'll eventually be calling. |
| 2611 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2612 | const FunctionProtoType *Proto, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2613 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2614 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2615 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2616 | return; |
| 2617 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2618 | CandidateSet.push_back(OverloadCandidate()); |
| 2619 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2620 | Candidate.Function = 0; |
| 2621 | Candidate.Surrogate = Conversion; |
| 2622 | Candidate.Viable = true; |
| 2623 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2624 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2625 | Candidate.Conversions.resize(NumArgs + 1); |
| 2626 | |
| 2627 | // Determine the implicit conversion sequence for the implicit |
| 2628 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | ImplicitConversionSequence ObjectInit |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2630 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2631 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2632 | Candidate.Viable = false; |
| 2633 | return; |
| 2634 | } |
| 2635 | |
| 2636 | // The first conversion is actually a user-defined conversion whose |
| 2637 | // first conversion is ObjectInit's standard conversion (which is |
| 2638 | // effectively a reference binding). Record it as such. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2640 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2641 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2642 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2644 | = Candidate.Conversions[0].UserDefined.Before; |
| 2645 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2646 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2648 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2649 | |
| 2650 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2651 | // parameters is viable only if it has an ellipsis in its parameter |
| 2652 | // list (8.3.5). |
| 2653 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2654 | Candidate.Viable = false; |
| 2655 | return; |
| 2656 | } |
| 2657 | |
| 2658 | // Function types don't have any default arguments, so just check if |
| 2659 | // we have enough arguments. |
| 2660 | if (NumArgs < NumArgsInProto) { |
| 2661 | // Not enough arguments. |
| 2662 | Candidate.Viable = false; |
| 2663 | return; |
| 2664 | } |
| 2665 | |
| 2666 | // Determine the implicit conversion sequences for each of the |
| 2667 | // arguments. |
| 2668 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2669 | if (ArgIdx < NumArgsInProto) { |
| 2670 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2671 | // exist for each argument an implicit conversion sequence |
| 2672 | // (13.3.3.1) that converts that argument to the corresponding |
| 2673 | // parameter of F. |
| 2674 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | Candidate.Conversions[ArgIdx + 1] |
| 2676 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2677 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2678 | /*ForceRValue=*/false, |
| 2679 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2681 | == ImplicitConversionSequence::BadConversion) { |
| 2682 | Candidate.Viable = false; |
| 2683 | break; |
| 2684 | } |
| 2685 | } else { |
| 2686 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2687 | // argument for which there is no corresponding parameter is |
| 2688 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2690 | = ImplicitConversionSequence::EllipsisConversion; |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2695 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2696 | // operator overloading logic over to the scheme used by binary operators, which |
| 2697 | // works for template instantiation. |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2698 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2699 | SourceLocation OpLoc, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2700 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2701 | OverloadCandidateSet& CandidateSet, |
| 2702 | SourceRange OpRange) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2703 | FunctionSet Functions; |
| 2704 | |
| 2705 | QualType T1 = Args[0]->getType(); |
| 2706 | QualType T2; |
| 2707 | if (NumArgs > 1) |
| 2708 | T2 = Args[1]->getType(); |
| 2709 | |
| 2710 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2711 | if (S) |
| 2712 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2713 | ArgumentDependentLookup(OpName, Args, NumArgs, Functions); |
| 2714 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2715 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 2716 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
| 2717 | } |
| 2718 | |
| 2719 | /// \brief Add overload candidates for overloaded operators that are |
| 2720 | /// member functions. |
| 2721 | /// |
| 2722 | /// Add the overloaded operator candidates that are member functions |
| 2723 | /// for the operator Op that was used in an operator expression such |
| 2724 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2725 | /// CandidateSet will store the added overload candidates. (C++ |
| 2726 | /// [over.match.oper]). |
| 2727 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2728 | SourceLocation OpLoc, |
| 2729 | Expr **Args, unsigned NumArgs, |
| 2730 | OverloadCandidateSet& CandidateSet, |
| 2731 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2732 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2733 | |
| 2734 | // C++ [over.match.oper]p3: |
| 2735 | // For a unary operator @ with an operand of a type whose |
| 2736 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2737 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2738 | // a right operand of a type whose cv-unqualified version is T2, |
| 2739 | // three sets of candidate functions, designated member |
| 2740 | // candidates, non-member candidates and built-in candidates, are |
| 2741 | // constructed as follows: |
| 2742 | QualType T1 = Args[0]->getType(); |
| 2743 | QualType T2; |
| 2744 | if (NumArgs > 1) |
| 2745 | T2 = Args[1]->getType(); |
| 2746 | |
| 2747 | // -- If T1 is a class type, the set of member candidates is the |
| 2748 | // result of the qualified lookup of T1::operator@ |
| 2749 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2750 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2751 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2752 | // Complete the type if it can be completed. Otherwise, we're done. |
| 2753 | if (RequireCompleteType(OpLoc, T1, PartialDiagnostic(0))) |
| 2754 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
| 2756 | LookupResult Operators = LookupQualifiedName(T1Rec->getDecl(), OpName, |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2757 | LookupOrdinaryName, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2759 | OperEnd = Operators.end(); |
| 2760 | Oper != OperEnd; |
| 2761 | ++Oper) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2763 | Args+1, NumArgs - 1, CandidateSet, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2764 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2765 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2768 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2769 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2770 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2771 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2772 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2773 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2774 | /// (at the beginning of the argument list) that will be contextually |
| 2775 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2777 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2778 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2779 | bool IsAssignmentOperator, |
| 2780 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2781 | // Add this candidate |
| 2782 | CandidateSet.push_back(OverloadCandidate()); |
| 2783 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2784 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2785 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2786 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2787 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2788 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2789 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2790 | |
| 2791 | // Determine the implicit conversion sequences for each of the |
| 2792 | // arguments. |
| 2793 | Candidate.Viable = true; |
| 2794 | Candidate.Conversions.resize(NumArgs); |
| 2795 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2796 | // C++ [over.match.oper]p4: |
| 2797 | // For the built-in assignment operators, conversions of the |
| 2798 | // left operand are restricted as follows: |
| 2799 | // -- no temporaries are introduced to hold the left operand, and |
| 2800 | // -- no user-defined conversions are applied to the left |
| 2801 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2803 | // |
| 2804 | // We block these conversions by turning off user-defined |
| 2805 | // conversions, since that is the only way that initialization of |
| 2806 | // a reference to a non-class type can occur from something that |
| 2807 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2808 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2810 | "Contextual conversion to bool requires bool type"); |
| 2811 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2812 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | Candidate.Conversions[ArgIdx] |
| 2814 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2815 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2816 | /*ForceRValue=*/false, |
| 2817 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2818 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2820 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2821 | Candidate.Viable = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2822 | break; |
| 2823 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2828 | /// candidate operator functions for built-in operators (C++ |
| 2829 | /// [over.built]). The types are separated into pointer types and |
| 2830 | /// enumeration types. |
| 2831 | class BuiltinCandidateTypeSet { |
| 2832 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2833 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2834 | |
| 2835 | /// PointerTypes - The set of pointer types that will be used in the |
| 2836 | /// built-in candidates. |
| 2837 | TypeSet PointerTypes; |
| 2838 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2839 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2840 | /// used in the built-in candidates. |
| 2841 | TypeSet MemberPointerTypes; |
| 2842 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2843 | /// EnumerationTypes - The set of enumeration types that will be |
| 2844 | /// used in the built-in candidates. |
| 2845 | TypeSet EnumerationTypes; |
| 2846 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2847 | /// Sema - The semantic analysis instance where we are building the |
| 2848 | /// candidate type set. |
| 2849 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2851 | /// Context - The AST context in which we will build the type sets. |
| 2852 | ASTContext &Context; |
| 2853 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2854 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| 2855 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2856 | |
| 2857 | public: |
| 2858 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2859 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2860 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2862 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2864 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions, |
| 2865 | bool AllowExplicitConversions); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2866 | |
| 2867 | /// pointer_begin - First pointer type found; |
| 2868 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2869 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2870 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2871 | iterator pointer_end() { return PointerTypes.end(); } |
| 2872 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2873 | /// member_pointer_begin - First member pointer type found; |
| 2874 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2875 | |
| 2876 | /// member_pointer_end - Past the last member pointer type found; |
| 2877 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2878 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2879 | /// enumeration_begin - First enumeration type found; |
| 2880 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2881 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2882 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2883 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2884 | }; |
| 2885 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2886 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2887 | /// the set of pointer types along with any more-qualified variants of |
| 2888 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2889 | /// will add "int const *", "int const volatile *", "int const |
| 2890 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2891 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2892 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2893 | /// |
| 2894 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2895 | bool |
| 2896 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2898 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2899 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2900 | return false; |
| 2901 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2902 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 2903 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2904 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2905 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2906 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 2907 | |
| 2908 | // Iterate through all strict supersets of BaseCVR. |
| 2909 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 2910 | if ((CVR | BaseCVR) != CVR) continue; |
| 2911 | |
| 2912 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 2913 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | return true; |
| 2917 | } |
| 2918 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2919 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 2920 | /// to the set of pointer types along with any more-qualified variants of |
| 2921 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2922 | /// will add "int const *", "int const volatile *", "int const |
| 2923 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2924 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2925 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2926 | /// |
| 2927 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2928 | bool |
| 2929 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 2930 | QualType Ty) { |
| 2931 | // Insert this type. |
| 2932 | if (!MemberPointerTypes.insert(Ty)) |
| 2933 | return false; |
| 2934 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2935 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 2936 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2937 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2938 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2939 | const Type *ClassTy = PointerTy->getClass(); |
| 2940 | |
| 2941 | // Iterate through all strict supersets of the pointee type's CVR |
| 2942 | // qualifiers. |
| 2943 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 2944 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 2945 | if ((CVR | BaseCVR) != CVR) continue; |
| 2946 | |
| 2947 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 2948 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | return true; |
| 2952 | } |
| 2953 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2954 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2955 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2956 | /// primarily interested in pointer types and enumeration types. We also |
| 2957 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2958 | /// AllowUserConversions is true if we should look at the conversion |
| 2959 | /// functions of a class type, and AllowExplicitConversions if we |
| 2960 | /// should also include the explicit conversion functions of a class |
| 2961 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2963 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2964 | bool AllowUserConversions, |
| 2965 | bool AllowExplicitConversions) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2966 | // Only deal with canonical types. |
| 2967 | Ty = Context.getCanonicalType(Ty); |
| 2968 | |
| 2969 | // Look through reference types; they aren't part of the type of an |
| 2970 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2971 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2972 | Ty = RefTy->getPointeeType(); |
| 2973 | |
| 2974 | // We don't care about qualifiers on the type. |
| 2975 | Ty = Ty.getUnqualifiedType(); |
| 2976 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2977 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2978 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2979 | |
| 2980 | // Insert our type, and its more-qualified variants, into the set |
| 2981 | // of types. |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2982 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2983 | return; |
| 2984 | |
| 2985 | // Add 'cv void*' to our set of types. |
| 2986 | if (!Ty->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | QualType QualVoid |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2988 | = Context.getCVRQualifiedType(Context.VoidTy, |
| 2989 | PointeeTy.getCVRQualifiers()); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2990 | AddPointerWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | // If this is a pointer to a class type, add pointers to its bases |
| 2994 | // (with the same level of cv-qualification as the original |
| 2995 | // derived class, of course). |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2996 | if (const RecordType *PointeeRec = PointeeTy->getAs<RecordType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2997 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 2998 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2999 | Base != ClassDecl->bases_end(); ++Base) { |
| 3000 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3001 | BaseTy = Context.getCVRQualifiedType(BaseTy.getUnqualifiedType(), |
| 3002 | PointeeTy.getCVRQualifiers()); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3003 | |
| 3004 | // Add the pointer type, recursively, so that we get all of |
| 3005 | // the indirect base classes, too. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3006 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3007 | } |
| 3008 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3009 | } else if (Ty->isMemberPointerType()) { |
| 3010 | // Member pointers are far easier, since the pointee can't be converted. |
| 3011 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3012 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3013 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3014 | EnumerationTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3015 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3016 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3017 | if (SemaRef.RequireCompleteType(SourceLocation(), Ty, 0)) { |
| 3018 | // No conversion functions in incomplete types. |
| 3019 | return; |
| 3020 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3022 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 3023 | // FIXME: Visit conversion functions in the base classes, too. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3024 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3025 | = ClassDecl->getConversionFunctions(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3027 | = Conversions->function_begin(); |
| 3028 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3029 | CXXConversionDecl *Conv; |
| 3030 | FunctionTemplateDecl *ConvTemplate; |
| 3031 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 3032 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3034 | // about which builtin types we can convert to. |
| 3035 | if (ConvTemplate) |
| 3036 | continue; |
| 3037 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3038 | if (AllowExplicitConversions || !Conv->isExplicit()) |
| 3039 | AddTypesConvertedFrom(Conv->getConversionType(), false, false); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3040 | } |
| 3041 | } |
| 3042 | } |
| 3043 | } |
| 3044 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3045 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3046 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3047 | /// given type to the candidate set. |
| 3048 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3049 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3050 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3051 | unsigned NumArgs, |
| 3052 | OverloadCandidateSet &CandidateSet) { |
| 3053 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3055 | // T& operator=(T&, T) |
| 3056 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3057 | ParamTypes[1] = T; |
| 3058 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3059 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3061 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3062 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3063 | ParamTypes[0] |
| 3064 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3065 | ParamTypes[1] = T; |
| 3066 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3068 | } |
| 3069 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3071 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3072 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3073 | /// on the operator @p Op and the arguments given. For example, if the |
| 3074 | /// operator is a binary '+', this routine might add "int |
| 3075 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3076 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3078 | Expr **Args, unsigned NumArgs, |
| 3079 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3080 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3081 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3082 | // that the first few of these types are the promoted integral |
| 3083 | // types; these types need to be first. |
| 3084 | // FIXME: What about complex? |
| 3085 | const unsigned FirstIntegralType = 0; |
| 3086 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3088 | LastPromotedIntegralType = 13; |
| 3089 | const unsigned FirstPromotedArithmeticType = 7, |
| 3090 | LastPromotedArithmeticType = 16; |
| 3091 | const unsigned NumArithmeticTypes = 16; |
| 3092 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3093 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3094 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3095 | Context.SignedCharTy, Context.ShortTy, |
| 3096 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3097 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3098 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3099 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3100 | }; |
| 3101 | |
| 3102 | // Find all of the types that the arguments can convert to, but only |
| 3103 | // if the operator we're looking at has built-in operator candidates |
| 3104 | // that make use of these types. |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3105 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3106 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3107 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3108 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3109 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3110 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3111 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3112 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3113 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 3114 | true, |
| 3115 | (Op == OO_Exclaim || |
| 3116 | Op == OO_AmpAmp || |
| 3117 | Op == OO_PipePipe)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | bool isComparison = false; |
| 3121 | switch (Op) { |
| 3122 | case OO_None: |
| 3123 | case NUM_OVERLOADED_OPERATORS: |
| 3124 | assert(false && "Expected an overloaded operator"); |
| 3125 | break; |
| 3126 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3127 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3129 | goto UnaryStar; |
| 3130 | else |
| 3131 | goto BinaryStar; |
| 3132 | break; |
| 3133 | |
| 3134 | case OO_Plus: // '+' is either unary or binary |
| 3135 | if (NumArgs == 1) |
| 3136 | goto UnaryPlus; |
| 3137 | else |
| 3138 | goto BinaryPlus; |
| 3139 | break; |
| 3140 | |
| 3141 | case OO_Minus: // '-' is either unary or binary |
| 3142 | if (NumArgs == 1) |
| 3143 | goto UnaryMinus; |
| 3144 | else |
| 3145 | goto BinaryMinus; |
| 3146 | break; |
| 3147 | |
| 3148 | case OO_Amp: // '&' is either unary or binary |
| 3149 | if (NumArgs == 1) |
| 3150 | goto UnaryAmp; |
| 3151 | else |
| 3152 | goto BinaryAmp; |
| 3153 | |
| 3154 | case OO_PlusPlus: |
| 3155 | case OO_MinusMinus: |
| 3156 | // C++ [over.built]p3: |
| 3157 | // |
| 3158 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3159 | // is either volatile or empty, there exist candidate operator |
| 3160 | // functions of the form |
| 3161 | // |
| 3162 | // VQ T& operator++(VQ T&); |
| 3163 | // T operator++(VQ T&, int); |
| 3164 | // |
| 3165 | // C++ [over.built]p4: |
| 3166 | // |
| 3167 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3168 | // than bool, and VQ is either volatile or empty, there exist |
| 3169 | // candidate operator functions of the form |
| 3170 | // |
| 3171 | // VQ T& operator--(VQ T&); |
| 3172 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3174 | Arith < NumArithmeticTypes; ++Arith) { |
| 3175 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3177 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3178 | |
| 3179 | // Non-volatile version. |
| 3180 | if (NumArgs == 1) |
| 3181 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3182 | else |
| 3183 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3184 | |
| 3185 | // Volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3186 | ParamTypes[0] |
| 3187 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3188 | if (NumArgs == 1) |
| 3189 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3190 | else |
| 3191 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3192 | } |
| 3193 | |
| 3194 | // C++ [over.built]p5: |
| 3195 | // |
| 3196 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3197 | // cv-unqualified object type, and VQ is either volatile or |
| 3198 | // empty, there exist candidate operator functions of the form |
| 3199 | // |
| 3200 | // T*VQ& operator++(T*VQ&); |
| 3201 | // T*VQ& operator--(T*VQ&); |
| 3202 | // T* operator++(T*VQ&, int); |
| 3203 | // T* operator--(T*VQ&, int); |
| 3204 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3205 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3206 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3207 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3208 | continue; |
| 3209 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | QualType ParamTypes[2] = { |
| 3211 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3212 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3214 | // Without volatile |
| 3215 | if (NumArgs == 1) |
| 3216 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3217 | else |
| 3218 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3219 | |
| 3220 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3221 | // With volatile |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3222 | ParamTypes[0] |
| 3223 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3224 | if (NumArgs == 1) |
| 3225 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3226 | else |
| 3227 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3228 | } |
| 3229 | } |
| 3230 | break; |
| 3231 | |
| 3232 | UnaryStar: |
| 3233 | // C++ [over.built]p6: |
| 3234 | // For every cv-qualified or cv-unqualified object type T, there |
| 3235 | // exist candidate operator functions of the form |
| 3236 | // |
| 3237 | // T& operator*(T*); |
| 3238 | // |
| 3239 | // C++ [over.built]p7: |
| 3240 | // For every function type T, there exist candidate operator |
| 3241 | // functions of the form |
| 3242 | // T& operator*(T*); |
| 3243 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3244 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3245 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3246 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3248 | &ParamTy, Args, 1, CandidateSet); |
| 3249 | } |
| 3250 | break; |
| 3251 | |
| 3252 | UnaryPlus: |
| 3253 | // C++ [over.built]p8: |
| 3254 | // For every type T, there exist candidate operator functions of |
| 3255 | // the form |
| 3256 | // |
| 3257 | // T* operator+(T*); |
| 3258 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3259 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3260 | QualType ParamTy = *Ptr; |
| 3261 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3262 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3264 | // Fall through |
| 3265 | |
| 3266 | UnaryMinus: |
| 3267 | // C++ [over.built]p9: |
| 3268 | // For every promoted arithmetic type T, there exist candidate |
| 3269 | // operator functions of the form |
| 3270 | // |
| 3271 | // T operator+(T); |
| 3272 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3274 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3275 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3276 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3277 | } |
| 3278 | break; |
| 3279 | |
| 3280 | case OO_Tilde: |
| 3281 | // C++ [over.built]p10: |
| 3282 | // For every promoted integral type T, there exist candidate |
| 3283 | // operator functions of the form |
| 3284 | // |
| 3285 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3287 | Int < LastPromotedIntegralType; ++Int) { |
| 3288 | QualType IntTy = ArithmeticTypes[Int]; |
| 3289 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3290 | } |
| 3291 | break; |
| 3292 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3293 | case OO_New: |
| 3294 | case OO_Delete: |
| 3295 | case OO_Array_New: |
| 3296 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3297 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3298 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3299 | break; |
| 3300 | |
| 3301 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3302 | UnaryAmp: |
| 3303 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3304 | // C++ [over.match.oper]p3: |
| 3305 | // -- For the operator ',', the unary operator '&', or the |
| 3306 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3307 | break; |
| 3308 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3309 | case OO_EqualEqual: |
| 3310 | case OO_ExclaimEqual: |
| 3311 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | // For every pointer to member type T, there exist candidate operator |
| 3313 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3314 | // |
| 3315 | // bool operator==(T,T); |
| 3316 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3318 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3319 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3320 | MemPtr != MemPtrEnd; |
| 3321 | ++MemPtr) { |
| 3322 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3323 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3324 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3326 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3328 | case OO_Less: |
| 3329 | case OO_Greater: |
| 3330 | case OO_LessEqual: |
| 3331 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3332 | // C++ [over.built]p15: |
| 3333 | // |
| 3334 | // For every pointer or enumeration type T, there exist |
| 3335 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3337 | // bool operator<(T, T); |
| 3338 | // bool operator>(T, T); |
| 3339 | // bool operator<=(T, T); |
| 3340 | // bool operator>=(T, T); |
| 3341 | // bool operator==(T, T); |
| 3342 | // bool operator!=(T, T); |
| 3343 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3344 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3345 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3346 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3347 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3349 | = CandidateTypes.enumeration_begin(); |
| 3350 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3351 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3352 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3353 | } |
| 3354 | |
| 3355 | // Fall through. |
| 3356 | isComparison = true; |
| 3357 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3358 | BinaryPlus: |
| 3359 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3360 | if (!isComparison) { |
| 3361 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3362 | |
| 3363 | // C++ [over.built]p13: |
| 3364 | // |
| 3365 | // For every cv-qualified or cv-unqualified object type T |
| 3366 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3368 | // T* operator+(T*, ptrdiff_t); |
| 3369 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3370 | // T* operator-(T*, ptrdiff_t); |
| 3371 | // T* operator+(ptrdiff_t, T*); |
| 3372 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3373 | // |
| 3374 | // C++ [over.built]p14: |
| 3375 | // |
| 3376 | // For every T, where T is a pointer to object type, there |
| 3377 | // exist candidate operator functions of the form |
| 3378 | // |
| 3379 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3380 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3381 | = CandidateTypes.pointer_begin(); |
| 3382 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3383 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3384 | |
| 3385 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3386 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3387 | |
| 3388 | if (Op == OO_Plus) { |
| 3389 | // T* operator+(ptrdiff_t, T*); |
| 3390 | ParamTypes[0] = ParamTypes[1]; |
| 3391 | ParamTypes[1] = *Ptr; |
| 3392 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3393 | } else { |
| 3394 | // ptrdiff_t operator-(T, T); |
| 3395 | ParamTypes[1] = *Ptr; |
| 3396 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3397 | Args, 2, CandidateSet); |
| 3398 | } |
| 3399 | } |
| 3400 | } |
| 3401 | // Fall through |
| 3402 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3403 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3404 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3405 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3406 | // C++ [over.built]p12: |
| 3407 | // |
| 3408 | // For every pair of promoted arithmetic types L and R, there |
| 3409 | // exist candidate operator functions of the form |
| 3410 | // |
| 3411 | // LR operator*(L, R); |
| 3412 | // LR operator/(L, R); |
| 3413 | // LR operator+(L, R); |
| 3414 | // LR operator-(L, R); |
| 3415 | // bool operator<(L, R); |
| 3416 | // bool operator>(L, R); |
| 3417 | // bool operator<=(L, R); |
| 3418 | // bool operator>=(L, R); |
| 3419 | // bool operator==(L, R); |
| 3420 | // bool operator!=(L, R); |
| 3421 | // |
| 3422 | // where LR is the result of the usual arithmetic conversions |
| 3423 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3424 | // |
| 3425 | // C++ [over.built]p24: |
| 3426 | // |
| 3427 | // For every pair of promoted arithmetic types L and R, there exist |
| 3428 | // candidate operator functions of the form |
| 3429 | // |
| 3430 | // LR operator?(bool, L, R); |
| 3431 | // |
| 3432 | // where LR is the result of the usual arithmetic conversions |
| 3433 | // between types L and R. |
| 3434 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3436 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3438 | Right < LastPromotedArithmeticType; ++Right) { |
| 3439 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3440 | QualType Result |
| 3441 | = isComparison |
| 3442 | ? Context.BoolTy |
| 3443 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3444 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3445 | } |
| 3446 | } |
| 3447 | break; |
| 3448 | |
| 3449 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3450 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3451 | case OO_Caret: |
| 3452 | case OO_Pipe: |
| 3453 | case OO_LessLess: |
| 3454 | case OO_GreaterGreater: |
| 3455 | // C++ [over.built]p17: |
| 3456 | // |
| 3457 | // For every pair of promoted integral types L and R, there |
| 3458 | // exist candidate operator functions of the form |
| 3459 | // |
| 3460 | // LR operator%(L, R); |
| 3461 | // LR operator&(L, R); |
| 3462 | // LR operator^(L, R); |
| 3463 | // LR operator|(L, R); |
| 3464 | // L operator<<(L, R); |
| 3465 | // L operator>>(L, R); |
| 3466 | // |
| 3467 | // where LR is the result of the usual arithmetic conversions |
| 3468 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3470 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3472 | Right < LastPromotedIntegralType; ++Right) { |
| 3473 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3474 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3475 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3476 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3477 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3478 | } |
| 3479 | } |
| 3480 | break; |
| 3481 | |
| 3482 | case OO_Equal: |
| 3483 | // C++ [over.built]p20: |
| 3484 | // |
| 3485 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3486 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3487 | // empty, there exist candidate operator functions of the form |
| 3488 | // |
| 3489 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3490 | for (BuiltinCandidateTypeSet::iterator |
| 3491 | Enum = CandidateTypes.enumeration_begin(), |
| 3492 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3493 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3494 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3495 | CandidateSet); |
| 3496 | for (BuiltinCandidateTypeSet::iterator |
| 3497 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3498 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3499 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3500 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3501 | CandidateSet); |
| 3502 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3503 | |
| 3504 | case OO_PlusEqual: |
| 3505 | case OO_MinusEqual: |
| 3506 | // C++ [over.built]p19: |
| 3507 | // |
| 3508 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3509 | // volatile or empty, there exist candidate operator functions |
| 3510 | // of the form |
| 3511 | // |
| 3512 | // T*VQ& operator=(T*VQ&, T*); |
| 3513 | // |
| 3514 | // C++ [over.built]p21: |
| 3515 | // |
| 3516 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3517 | // cv-unqualified object type and VQ is either volatile or |
| 3518 | // empty, there exist candidate operator functions of the form |
| 3519 | // |
| 3520 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3521 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3522 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3523 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3524 | QualType ParamTypes[2]; |
| 3525 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3526 | |
| 3527 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3528 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3529 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3530 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3531 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3532 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3533 | // volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3534 | ParamTypes[0] |
| 3535 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3536 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3537 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3538 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3539 | } |
| 3540 | // Fall through. |
| 3541 | |
| 3542 | case OO_StarEqual: |
| 3543 | case OO_SlashEqual: |
| 3544 | // C++ [over.built]p18: |
| 3545 | // |
| 3546 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3547 | // VQ is either volatile or empty, and R is a promoted |
| 3548 | // arithmetic type, there exist candidate operator functions of |
| 3549 | // the form |
| 3550 | // |
| 3551 | // VQ L& operator=(VQ L&, R); |
| 3552 | // VQ L& operator*=(VQ L&, R); |
| 3553 | // VQ L& operator/=(VQ L&, R); |
| 3554 | // VQ L& operator+=(VQ L&, R); |
| 3555 | // VQ L& operator-=(VQ L&, R); |
| 3556 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3558 | Right < LastPromotedArithmeticType; ++Right) { |
| 3559 | QualType ParamTypes[2]; |
| 3560 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3561 | |
| 3562 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3563 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3564 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3565 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3566 | |
| 3567 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3568 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3569 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3570 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3571 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3572 | } |
| 3573 | } |
| 3574 | break; |
| 3575 | |
| 3576 | case OO_PercentEqual: |
| 3577 | case OO_LessLessEqual: |
| 3578 | case OO_GreaterGreaterEqual: |
| 3579 | case OO_AmpEqual: |
| 3580 | case OO_CaretEqual: |
| 3581 | case OO_PipeEqual: |
| 3582 | // C++ [over.built]p22: |
| 3583 | // |
| 3584 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3585 | // is either volatile or empty, and R is a promoted integral |
| 3586 | // type, there exist candidate operator functions of the form |
| 3587 | // |
| 3588 | // VQ L& operator%=(VQ L&, R); |
| 3589 | // VQ L& operator<<=(VQ L&, R); |
| 3590 | // VQ L& operator>>=(VQ L&, R); |
| 3591 | // VQ L& operator&=(VQ L&, R); |
| 3592 | // VQ L& operator^=(VQ L&, R); |
| 3593 | // VQ L& operator|=(VQ L&, R); |
| 3594 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3596 | Right < LastPromotedIntegralType; ++Right) { |
| 3597 | QualType ParamTypes[2]; |
| 3598 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3599 | |
| 3600 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3601 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3602 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3603 | |
| 3604 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3605 | ParamTypes[0] = ArithmeticTypes[Left]; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3606 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3607 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3608 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3609 | } |
| 3610 | } |
| 3611 | break; |
| 3612 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3613 | case OO_Exclaim: { |
| 3614 | // C++ [over.operator]p23: |
| 3615 | // |
| 3616 | // There also exist candidate operator functions of the form |
| 3617 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3619 | // bool operator&&(bool, bool); [BELOW] |
| 3620 | // bool operator||(bool, bool); [BELOW] |
| 3621 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3622 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3623 | /*IsAssignmentOperator=*/false, |
| 3624 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3625 | break; |
| 3626 | } |
| 3627 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3628 | case OO_AmpAmp: |
| 3629 | case OO_PipePipe: { |
| 3630 | // C++ [over.operator]p23: |
| 3631 | // |
| 3632 | // There also exist candidate operator functions of the form |
| 3633 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3634 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3635 | // bool operator&&(bool, bool); |
| 3636 | // bool operator||(bool, bool); |
| 3637 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3638 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3639 | /*IsAssignmentOperator=*/false, |
| 3640 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3641 | break; |
| 3642 | } |
| 3643 | |
| 3644 | case OO_Subscript: |
| 3645 | // C++ [over.built]p13: |
| 3646 | // |
| 3647 | // For every cv-qualified or cv-unqualified object type T there |
| 3648 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3650 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3651 | // T& operator[](T*, ptrdiff_t); |
| 3652 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3653 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3654 | // T& operator[](ptrdiff_t, T*); |
| 3655 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3656 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3657 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3658 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3659 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3660 | |
| 3661 | // T& operator[](T*, ptrdiff_t) |
| 3662 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3663 | |
| 3664 | // T& operator[](ptrdiff_t, T*); |
| 3665 | ParamTypes[0] = ParamTypes[1]; |
| 3666 | ParamTypes[1] = *Ptr; |
| 3667 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3668 | } |
| 3669 | break; |
| 3670 | |
| 3671 | case OO_ArrowStar: |
| 3672 | // FIXME: No support for pointer-to-members yet. |
| 3673 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3674 | |
| 3675 | case OO_Conditional: |
| 3676 | // Note that we don't consider the first argument, since it has been |
| 3677 | // contextually converted to bool long ago. The candidates below are |
| 3678 | // therefore added as binary. |
| 3679 | // |
| 3680 | // C++ [over.built]p24: |
| 3681 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3682 | // there exist candidate operator functions of the form |
| 3683 | // |
| 3684 | // T operator?(bool, T, T); |
| 3685 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3686 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3687 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3688 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3689 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3690 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3691 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3692 | CandidateTypes.member_pointer_begin(), |
| 3693 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3694 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3695 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3696 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3697 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3698 | } |
| 3699 | } |
| 3700 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3701 | /// \brief Add function candidates found via argument-dependent lookup |
| 3702 | /// to the set of overloading candidates. |
| 3703 | /// |
| 3704 | /// This routine performs argument-dependent name lookup based on the |
| 3705 | /// given function name (which may also be an operator name) and adds |
| 3706 | /// all of the overload candidates found by ADL to the overload |
| 3707 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3709 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3710 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3711 | bool HasExplicitTemplateArgs, |
| 3712 | const TemplateArgument *ExplicitTemplateArgs, |
| 3713 | unsigned NumExplicitTemplateArgs, |
| 3714 | OverloadCandidateSet& CandidateSet, |
| 3715 | bool PartialOverloading) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3716 | FunctionSet Functions; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3717 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3718 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 3719 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3720 | // Record all of the function candidates that we've already |
| 3721 | // added to the overload set, so that we don't add those same |
| 3722 | // candidates a second time. |
| 3723 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3724 | CandEnd = CandidateSet.end(); |
| 3725 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3726 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3727 | Functions.insert(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3728 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3729 | Functions.insert(FunTmpl); |
| 3730 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3731 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3732 | // FIXME: Pass in the explicit template arguments? |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3733 | ArgumentDependentLookup(Name, Args, NumArgs, Functions); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3734 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3735 | // Erase all of the candidates we already knew about. |
| 3736 | // FIXME: This is suboptimal. Is there a better way? |
| 3737 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3738 | CandEnd = CandidateSet.end(); |
| 3739 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3740 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3741 | Functions.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3742 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3743 | Functions.erase(FunTmpl); |
| 3744 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3745 | |
| 3746 | // For each of the ADL candidates we found, add it to the overload |
| 3747 | // set. |
| 3748 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3749 | FuncEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3750 | Func != FuncEnd; ++Func) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3751 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
| 3752 | if (HasExplicitTemplateArgs) |
| 3753 | continue; |
| 3754 | |
| 3755 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 3756 | false, false, PartialOverloading); |
| 3757 | } else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3759 | HasExplicitTemplateArgs, |
| 3760 | ExplicitTemplateArgs, |
| 3761 | NumExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3762 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3763 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3766 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3767 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3769 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3771 | // Define viable functions to be better candidates than non-viable |
| 3772 | // functions. |
| 3773 | if (!Cand2.Viable) |
| 3774 | return Cand1.Viable; |
| 3775 | else if (!Cand1.Viable) |
| 3776 | return false; |
| 3777 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3778 | // C++ [over.match.best]p1: |
| 3779 | // |
| 3780 | // -- if F is a static member function, ICS1(F) is defined such |
| 3781 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3782 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3783 | // better nor worse than ICS1(F). |
| 3784 | unsigned StartArg = 0; |
| 3785 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3786 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3787 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3788 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | // A viable function F1 is defined to be a better function than another |
| 3790 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3791 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3792 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3793 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3794 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3795 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3796 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3797 | Cand2.Conversions[ArgIdx])) { |
| 3798 | case ImplicitConversionSequence::Better: |
| 3799 | // Cand1 has a better conversion sequence. |
| 3800 | HasBetterConversion = true; |
| 3801 | break; |
| 3802 | |
| 3803 | case ImplicitConversionSequence::Worse: |
| 3804 | // Cand1 can't be better than Cand2. |
| 3805 | return false; |
| 3806 | |
| 3807 | case ImplicitConversionSequence::Indistinguishable: |
| 3808 | // Do nothing. |
| 3809 | break; |
| 3810 | } |
| 3811 | } |
| 3812 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3814 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3815 | if (HasBetterConversion) |
| 3816 | return true; |
| 3817 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3818 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3819 | // specialization, or, if not that, |
| 3820 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 3821 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 3822 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3823 | |
| 3824 | // -- F1 and F2 are function template specializations, and the function |
| 3825 | // template for F1 is more specialized than the template for F2 |
| 3826 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3827 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 3828 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 3829 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3830 | if (FunctionTemplateDecl *BetterTemplate |
| 3831 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 3832 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 6010da0 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 3833 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 3834 | : TPOC_Call)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3835 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3836 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3837 | // -- the context is an initialization by user-defined conversion |
| 3838 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 3839 | // from the return type of F1 to the destination type (i.e., |
| 3840 | // the type of the entity being initialized) is a better |
| 3841 | // conversion sequence than the standard conversion sequence |
| 3842 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3843 | if (Cand1.Function && Cand2.Function && |
| 3844 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3845 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 3846 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 3847 | Cand2.FinalConversion)) { |
| 3848 | case ImplicitConversionSequence::Better: |
| 3849 | // Cand1 has a better conversion sequence. |
| 3850 | return true; |
| 3851 | |
| 3852 | case ImplicitConversionSequence::Worse: |
| 3853 | // Cand1 can't be better than Cand2. |
| 3854 | return false; |
| 3855 | |
| 3856 | case ImplicitConversionSequence::Indistinguishable: |
| 3857 | // Do nothing |
| 3858 | break; |
| 3859 | } |
| 3860 | } |
| 3861 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3862 | return false; |
| 3863 | } |
| 3864 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3865 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3866 | /// within an overload candidate set. |
| 3867 | /// |
| 3868 | /// \param CandidateSet the set of candidate functions. |
| 3869 | /// |
| 3870 | /// \param Loc the location of the function name (or operator symbol) for |
| 3871 | /// which overload resolution occurs. |
| 3872 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3874 | /// function, Best points to the candidate function found. |
| 3875 | /// |
| 3876 | /// \returns The result of overload resolution. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | Sema::OverloadingResult |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3878 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3879 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3881 | // Find the best viable function. |
| 3882 | Best = CandidateSet.end(); |
| 3883 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3884 | Cand != CandidateSet.end(); ++Cand) { |
| 3885 | if (Cand->Viable) { |
| 3886 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 3887 | Best = Cand; |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | // If we didn't find any viable functions, abort. |
| 3892 | if (Best == CandidateSet.end()) |
| 3893 | return OR_No_Viable_Function; |
| 3894 | |
| 3895 | // Make sure that this function is better than every other viable |
| 3896 | // function. If not, we have an ambiguity. |
| 3897 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3898 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3900 | Cand != Best && |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3901 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 3902 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3903 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3904 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3905 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3907 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3908 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3910 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3911 | return OR_Deleted; |
| 3912 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3913 | // C++ [basic.def.odr]p2: |
| 3914 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3915 | // when referred to from a potentially-evaluated expression. [Note: this |
| 3916 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3917 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 3918 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 3919 | if (Best->Function) |
| 3920 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3921 | return OR_Success; |
| 3922 | } |
| 3923 | |
| 3924 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 3925 | /// diagnostic messages containing the candidates in the candidate |
| 3926 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3928 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | bool OnlyViable) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3930 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3931 | LastCand = CandidateSet.end(); |
| 3932 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3933 | if (Cand->Viable || !OnlyViable) { |
| 3934 | if (Cand->Function) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3935 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3936 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3937 | // Deleted or "unavailable" function. |
| 3938 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 3939 | << Cand->Function->isDeleted(); |
Douglas Gregor | 4fb9cde8e | 2009-09-15 20:11:42 +0000 | [diff] [blame] | 3940 | } else if (FunctionTemplateDecl *FunTmpl |
| 3941 | = Cand->Function->getPrimaryTemplate()) { |
| 3942 | // Function template specialization |
| 3943 | // FIXME: Give a better reason! |
| 3944 | Diag(Cand->Function->getLocation(), diag::err_ovl_template_candidate) |
| 3945 | << getTemplateArgumentBindingsText(FunTmpl->getTemplateParameters(), |
| 3946 | *Cand->Function->getTemplateSpecializationArgs()); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3947 | } else { |
| 3948 | // Normal function |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 3949 | bool errReported = false; |
| 3950 | if (!Cand->Viable && Cand->Conversions.size() > 0) { |
| 3951 | for (int i = Cand->Conversions.size()-1; i >= 0; i--) { |
| 3952 | const ImplicitConversionSequence &Conversion = |
| 3953 | Cand->Conversions[i]; |
| 3954 | if ((Conversion.ConversionKind != |
| 3955 | ImplicitConversionSequence::BadConversion) || |
| 3956 | Conversion.ConversionFunctionSet.size() == 0) |
| 3957 | continue; |
| 3958 | Diag(Cand->Function->getLocation(), |
| 3959 | diag::err_ovl_candidate_not_viable) << (i+1); |
| 3960 | errReported = true; |
| 3961 | for (int j = Conversion.ConversionFunctionSet.size()-1; |
| 3962 | j >= 0; j--) { |
| 3963 | FunctionDecl *Func = Conversion.ConversionFunctionSet[j]; |
| 3964 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 3965 | } |
| 3966 | } |
| 3967 | } |
| 3968 | if (!errReported) |
| 3969 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3970 | } |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3971 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3972 | // Desugar the type of the surrogate down to a function type, |
| 3973 | // retaining as many typedefs as possible while still showing |
| 3974 | // the function type (and, therefore, its parameter types). |
| 3975 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3976 | bool isLValueReference = false; |
| 3977 | bool isRValueReference = false; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3978 | bool isPointer = false; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3979 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3980 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3981 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3982 | isLValueReference = true; |
| 3983 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3984 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3985 | FnType = FnTypeRef->getPointeeType(); |
| 3986 | isRValueReference = true; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3987 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3988 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3989 | FnType = FnTypePtr->getPointeeType(); |
| 3990 | isPointer = true; |
| 3991 | } |
| 3992 | // Desugar down to a function type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3993 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3994 | // Reconstruct the pointer/reference as appropriate. |
| 3995 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3996 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 3997 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3998 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3999 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4000 | << FnType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4001 | } else { |
| 4002 | // FIXME: We need to get the identifier in here |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4003 | // FIXME: Do we want the error message to point at the operator? |
| 4004 | // (built-ins won't have a location) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | QualType FnType |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4006 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 4007 | Cand->BuiltinTypes.ParamTypes, |
| 4008 | Cand->Conversions.size(), |
| 4009 | false, 0); |
| 4010 | |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4011 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4012 | } |
| 4013 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4017 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4018 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4019 | /// expression with overloaded function type and @p ToType is the type |
| 4020 | /// we're trying to resolve to. For example: |
| 4021 | /// |
| 4022 | /// @code |
| 4023 | /// int f(double); |
| 4024 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4026 | /// int (*pfd)(double) = f; // selects f(double) |
| 4027 | /// @endcode |
| 4028 | /// |
| 4029 | /// This routine returns the resulting FunctionDecl if it could be |
| 4030 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4031 | /// routine will emit diagnostics if there is an error. |
| 4032 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4033 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4034 | bool Complain) { |
| 4035 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4036 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4037 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4038 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4039 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4040 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4041 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4042 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4043 | FunctionType = MemTypePtr->getPointeeType(); |
| 4044 | IsMember = true; |
| 4045 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4046 | |
| 4047 | // We only look at pointers or references to functions. |
Douglas Gregor | 6b6ba8b | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4048 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4049 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4050 | return 0; |
| 4051 | |
| 4052 | // Find the actual overloaded function declaration. |
| 4053 | OverloadedFunctionDecl *Ovl = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4054 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4055 | // C++ [over.over]p1: |
| 4056 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4057 | // overloaded function name is ignored (5.1). ] |
| 4058 | Expr *OvlExpr = From->IgnoreParens(); |
| 4059 | |
| 4060 | // C++ [over.over]p1: |
| 4061 | // [...] The overloaded function name can be preceded by the & |
| 4062 | // operator. |
| 4063 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4064 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4065 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4066 | } |
| 4067 | |
| 4068 | // Try to dig out the overloaded function. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4069 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 4070 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4071 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4072 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
| 4073 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4074 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | // If there's no overloaded function declaration or function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4076 | // we're done. |
| 4077 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4078 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4079 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4080 | OverloadIterator Fun; |
| 4081 | if (Ovl) |
| 4082 | Fun = Ovl; |
| 4083 | else |
| 4084 | Fun = FunctionTemplate; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4085 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4086 | // Look through all of the overloaded functions, searching for one |
| 4087 | // whose type matches exactly. |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4088 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4089 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4090 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4091 | // C++ [over.over]p3: |
| 4092 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4093 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4094 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4095 | // type "pointer-to-member-function." |
| 4096 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4097 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4098 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4099 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4100 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4101 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4103 | // static when converting to member pointer. |
| 4104 | if (Method->isStatic() == IsMember) |
| 4105 | continue; |
| 4106 | } else if (IsMember) |
| 4107 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4109 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4110 | // If the name is a function template, template argument deduction is |
| 4111 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4112 | // resulting template argument list is used to generate a single |
| 4113 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4114 | // overloaded functions considered. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4115 | // FIXME: We don't really want to build the specialization here, do we? |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4116 | FunctionDecl *Specialization = 0; |
| 4117 | TemplateDeductionInfo Info(Context); |
| 4118 | if (TemplateDeductionResult Result |
| 4119 | = DeduceTemplateArguments(FunctionTemplate, /*FIXME*/false, |
| 4120 | /*FIXME:*/0, /*FIXME:*/0, |
| 4121 | FunctionType, Specialization, Info)) { |
| 4122 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4123 | (void)Result; |
| 4124 | } else { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4125 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4126 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4128 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4129 | Matches.insert( |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4130 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4131 | } |
| 4132 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4134 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 4135 | // Skip non-static functions when converting to pointer, and static |
| 4136 | // when converting to member pointer. |
| 4137 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4138 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4139 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4140 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4141 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4142 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4143 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4144 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4145 | FoundNonTemplateFunction = true; |
| 4146 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4150 | // If there were 0 or 1 matches, we're done. |
| 4151 | if (Matches.empty()) |
| 4152 | return 0; |
| 4153 | else if (Matches.size() == 1) |
| 4154 | return *Matches.begin(); |
| 4155 | |
| 4156 | // C++ [over.over]p4: |
| 4157 | // If more than one function is selected, [...] |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4158 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4159 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4160 | // [...] and any given function template specialization F1 is |
| 4161 | // eliminated if the set contains a second function template |
| 4162 | // specialization whose function template is more specialized |
| 4163 | // than the function template of F1 according to the partial |
| 4164 | // ordering rules of 14.5.5.2. |
| 4165 | |
| 4166 | // The algorithm specified above is quadratic. We instead use a |
| 4167 | // two-pass algorithm (similar to the one used to identify the |
| 4168 | // best viable function in an overload set) that identifies the |
| 4169 | // best function template (if it exists). |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4170 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
| 4171 | Matches.end()); |
| 4172 | return getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4173 | TPOC_Other, From->getLocStart(), |
| 4174 | PartialDiagnostic(0), |
| 4175 | PartialDiagnostic(diag::err_addr_ovl_ambiguous) |
| 4176 | << TemplateMatches[0]->getDeclName(), |
| 4177 | PartialDiagnostic(diag::err_ovl_template_candidate)); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4178 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4179 | |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4180 | // [...] any function template specializations in the set are |
| 4181 | // eliminated if the set also contains a non-template function, [...] |
| 4182 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4183 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4184 | if ((*M)->getPrimaryTemplate() == 0) |
| 4185 | RemainingMatches.push_back(*M); |
| 4186 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4187 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4188 | // selected function. |
| 4189 | if (RemainingMatches.size() == 1) |
| 4190 | return RemainingMatches.front(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4192 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4193 | // returns (even if we issue the diagnostics here). |
| 4194 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4195 | << RemainingMatches[0]->getDeclName(); |
| 4196 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 4197 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4198 | return 0; |
| 4199 | } |
| 4200 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4201 | /// \brief Add a single candidate to the overload set. |
| 4202 | static void AddOverloadedCallCandidate(Sema &S, |
| 4203 | AnyFunctionDecl Callee, |
| 4204 | bool &ArgumentDependentLookup, |
| 4205 | bool HasExplicitTemplateArgs, |
| 4206 | const TemplateArgument *ExplicitTemplateArgs, |
| 4207 | unsigned NumExplicitTemplateArgs, |
| 4208 | Expr **Args, unsigned NumArgs, |
| 4209 | OverloadCandidateSet &CandidateSet, |
| 4210 | bool PartialOverloading) { |
| 4211 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
| 4212 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
| 4213 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 4214 | PartialOverloading); |
| 4215 | |
| 4216 | if (Func->getDeclContext()->isRecord() || |
| 4217 | Func->getDeclContext()->isFunctionOrMethod()) |
| 4218 | ArgumentDependentLookup = false; |
| 4219 | return; |
| 4220 | } |
| 4221 | |
| 4222 | FunctionTemplateDecl *FuncTemplate = cast<FunctionTemplateDecl>(Callee); |
| 4223 | S.AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 4224 | ExplicitTemplateArgs, |
| 4225 | NumExplicitTemplateArgs, |
| 4226 | Args, NumArgs, CandidateSet); |
| 4227 | |
| 4228 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 4229 | ArgumentDependentLookup = false; |
| 4230 | } |
| 4231 | |
| 4232 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 4233 | /// dependent lookup to the given overload set. |
| 4234 | void Sema::AddOverloadedCallCandidates(NamedDecl *Callee, |
| 4235 | DeclarationName &UnqualifiedName, |
| 4236 | bool &ArgumentDependentLookup, |
| 4237 | bool HasExplicitTemplateArgs, |
| 4238 | const TemplateArgument *ExplicitTemplateArgs, |
| 4239 | unsigned NumExplicitTemplateArgs, |
| 4240 | Expr **Args, unsigned NumArgs, |
| 4241 | OverloadCandidateSet &CandidateSet, |
| 4242 | bool PartialOverloading) { |
| 4243 | // Add the functions denoted by Callee to the set of candidate |
| 4244 | // functions. While we're doing so, track whether argument-dependent |
| 4245 | // lookup still applies, per: |
| 4246 | // |
| 4247 | // C++0x [basic.lookup.argdep]p3: |
| 4248 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4249 | // and let Y be the lookup set produced by argument dependent |
| 4250 | // lookup (defined as follows). If X contains |
| 4251 | // |
| 4252 | // -- a declaration of a class member, or |
| 4253 | // |
| 4254 | // -- a block-scope function declaration that is not a |
| 4255 | // using-declaration (FIXME: check for using declaration), or |
| 4256 | // |
| 4257 | // -- a declaration that is neither a function or a function |
| 4258 | // template |
| 4259 | // |
| 4260 | // then Y is empty. |
| 4261 | if (!Callee) { |
| 4262 | // Nothing to do. |
| 4263 | } else if (OverloadedFunctionDecl *Ovl |
| 4264 | = dyn_cast<OverloadedFunctionDecl>(Callee)) { |
| 4265 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4266 | FuncEnd = Ovl->function_end(); |
| 4267 | Func != FuncEnd; ++Func) |
| 4268 | AddOverloadedCallCandidate(*this, *Func, ArgumentDependentLookup, |
| 4269 | HasExplicitTemplateArgs, |
| 4270 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4271 | Args, NumArgs, CandidateSet, |
| 4272 | PartialOverloading); |
| 4273 | } else if (isa<FunctionDecl>(Callee) || isa<FunctionTemplateDecl>(Callee)) |
| 4274 | AddOverloadedCallCandidate(*this, |
| 4275 | AnyFunctionDecl::getFromNamedDecl(Callee), |
| 4276 | ArgumentDependentLookup, |
| 4277 | HasExplicitTemplateArgs, |
| 4278 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4279 | Args, NumArgs, CandidateSet, |
| 4280 | PartialOverloading); |
| 4281 | // FIXME: assert isa<FunctionDecl> || isa<FunctionTemplateDecl> rather than |
| 4282 | // checking dynamically. |
| 4283 | |
| 4284 | if (Callee) |
| 4285 | UnqualifiedName = Callee->getDeclName(); |
| 4286 | |
| 4287 | if (ArgumentDependentLookup) |
| 4288 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
| 4289 | HasExplicitTemplateArgs, |
| 4290 | ExplicitTemplateArgs, |
| 4291 | NumExplicitTemplateArgs, |
| 4292 | CandidateSet, |
| 4293 | PartialOverloading); |
| 4294 | } |
| 4295 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4296 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4297 | /// (which eventually refers to the declaration Func) and the call |
| 4298 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4299 | /// to a specific function. If overload resolution succeeds, returns |
| 4300 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4301 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4302 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4303 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4304 | DeclarationName UnqualifiedName, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4305 | bool HasExplicitTemplateArgs, |
| 4306 | const TemplateArgument *ExplicitTemplateArgs, |
| 4307 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4308 | SourceLocation LParenLoc, |
| 4309 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4310 | SourceLocation *CommaLocs, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4311 | SourceLocation RParenLoc, |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4312 | bool &ArgumentDependentLookup) { |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4313 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4314 | |
| 4315 | // Add the functions denoted by Callee to the set of candidate |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4316 | // functions. |
| 4317 | AddOverloadedCallCandidates(Callee, UnqualifiedName, ArgumentDependentLookup, |
| 4318 | HasExplicitTemplateArgs, ExplicitTemplateArgs, |
| 4319 | NumExplicitTemplateArgs, Args, NumArgs, |
| 4320 | CandidateSet); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4321 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4322 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4323 | case OR_Success: |
| 4324 | return Best->Function; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4325 | |
| 4326 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4327 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4328 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4329 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4330 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4331 | break; |
| 4332 | |
| 4333 | case OR_Ambiguous: |
| 4334 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4335 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4336 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4337 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4338 | |
| 4339 | case OR_Deleted: |
| 4340 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4341 | << Best->Function->isDeleted() |
| 4342 | << UnqualifiedName |
| 4343 | << Fn->getSourceRange(); |
| 4344 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4345 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4349 | // return NULL. |
| 4350 | Fn->Destroy(Context); |
| 4351 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4352 | Args[Arg]->Destroy(Context); |
| 4353 | return 0; |
| 4354 | } |
| 4355 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4356 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4357 | /// operator. |
| 4358 | /// |
| 4359 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4360 | /// |
| 4361 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4362 | /// operator. |
| 4363 | /// |
| 4364 | /// \param Functions The set of non-member functions that will be |
| 4365 | /// considered by overload resolution. The caller needs to build this |
| 4366 | /// set based on the context using, e.g., |
| 4367 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4368 | /// set should not contain any member functions; those will be added |
| 4369 | /// by CreateOverloadedUnaryOp(). |
| 4370 | /// |
| 4371 | /// \param input The input argument. |
| 4372 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4373 | unsigned OpcIn, |
| 4374 | FunctionSet &Functions, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4375 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4376 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4377 | Expr *Input = (Expr *)input.get(); |
| 4378 | |
| 4379 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 4380 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 4381 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4382 | |
| 4383 | Expr *Args[2] = { Input, 0 }; |
| 4384 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4385 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4386 | // For post-increment and post-decrement, add the implicit '0' as |
| 4387 | // the second argument, so that we know this is a post-increment or |
| 4388 | // post-decrement. |
| 4389 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 4390 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4391 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4392 | SourceLocation()); |
| 4393 | NumArgs = 2; |
| 4394 | } |
| 4395 | |
| 4396 | if (Input->isTypeDependent()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4398 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4399 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4400 | FuncEnd = Functions.end(); |
| 4401 | Func != FuncEnd; ++Func) |
| 4402 | Overloads->addOverload(*Func); |
| 4403 | |
| 4404 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4405 | OpLoc, false, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4406 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4407 | input.release(); |
| 4408 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4409 | &Args[0], NumArgs, |
| 4410 | Context.DependentTy, |
| 4411 | OpLoc)); |
| 4412 | } |
| 4413 | |
| 4414 | // Build an empty overload set. |
| 4415 | OverloadCandidateSet CandidateSet; |
| 4416 | |
| 4417 | // Add the candidates from the given function set. |
| 4418 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4419 | |
| 4420 | // Add operator candidates that are member functions. |
| 4421 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4422 | |
| 4423 | // Add builtin operator candidates. |
| 4424 | AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet); |
| 4425 | |
| 4426 | // Perform overload resolution. |
| 4427 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4428 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4429 | case OR_Success: { |
| 4430 | // We found a built-in operator or an overloaded operator. |
| 4431 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4433 | if (FnDecl) { |
| 4434 | // We matched an overloaded operator. Build a call to that |
| 4435 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4436 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4437 | // Convert the arguments. |
| 4438 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4439 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4440 | return ExprError(); |
| 4441 | } else { |
| 4442 | // Convert the arguments. |
| 4443 | if (PerformCopyInitialization(Input, |
| 4444 | FnDecl->getParamDecl(0)->getType(), |
| 4445 | "passing")) |
| 4446 | return ExprError(); |
| 4447 | } |
| 4448 | |
| 4449 | // Determine the result type |
| 4450 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4451 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4452 | ResultTy = ResultTy.getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4453 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4454 | // Build the actual expression node. |
| 4455 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4456 | SourceLocation()); |
| 4457 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4458 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4459 | input.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4460 | |
| 4461 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | e80ccac | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4462 | &Input, 1, ResultTy, OpLoc); |
| 4463 | return MaybeBindToTemporary(CE); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4464 | } else { |
| 4465 | // We matched a built-in operator. Convert the arguments, then |
| 4466 | // break out so that we will build the appropriate built-in |
| 4467 | // operator node. |
| 4468 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4469 | Best->Conversions[0], "passing")) |
| 4470 | return ExprError(); |
| 4471 | |
| 4472 | break; |
| 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | case OR_No_Viable_Function: |
| 4477 | // No viable function; fall through to handling this as a |
| 4478 | // built-in operator, which will produce an error message for us. |
| 4479 | break; |
| 4480 | |
| 4481 | case OR_Ambiguous: |
| 4482 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4483 | << UnaryOperator::getOpcodeStr(Opc) |
| 4484 | << Input->getSourceRange(); |
| 4485 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4486 | return ExprError(); |
| 4487 | |
| 4488 | case OR_Deleted: |
| 4489 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4490 | << Best->Function->isDeleted() |
| 4491 | << UnaryOperator::getOpcodeStr(Opc) |
| 4492 | << Input->getSourceRange(); |
| 4493 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4494 | return ExprError(); |
| 4495 | } |
| 4496 | |
| 4497 | // Either we found no viable overloaded operator or we matched a |
| 4498 | // built-in operator. In either case, fall through to trying to |
| 4499 | // build a built-in operation. |
| 4500 | input.release(); |
| 4501 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4502 | } |
| 4503 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4504 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4505 | /// operator. |
| 4506 | /// |
| 4507 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4508 | /// |
| 4509 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4510 | /// operator. |
| 4511 | /// |
| 4512 | /// \param Functions The set of non-member functions that will be |
| 4513 | /// considered by overload resolution. The caller needs to build this |
| 4514 | /// set based on the context using, e.g., |
| 4515 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4516 | /// set should not contain any member functions; those will be added |
| 4517 | /// by CreateOverloadedBinOp(). |
| 4518 | /// |
| 4519 | /// \param LHS Left-hand argument. |
| 4520 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4522 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | unsigned OpcIn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4524 | FunctionSet &Functions, |
| 4525 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4526 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4527 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4528 | |
| 4529 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4530 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4531 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4532 | |
| 4533 | // If either side is type-dependent, create an appropriate dependent |
| 4534 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4535 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4536 | // .* cannot be overloaded. |
| 4537 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4538 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4539 | Context.DependentTy, OpLoc)); |
| 4540 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4541 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4542 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4543 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4544 | FuncEnd = Functions.end(); |
| 4545 | Func != FuncEnd; ++Func) |
| 4546 | Overloads->addOverload(*Func); |
| 4547 | |
| 4548 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4549 | OpLoc, false, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4551 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4552 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4553 | Context.DependentTy, |
| 4554 | OpLoc)); |
| 4555 | } |
| 4556 | |
| 4557 | // If this is the .* operator, which is not overloadable, just |
| 4558 | // create a built-in binary operator. |
| 4559 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4560 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4561 | |
| 4562 | // If this is one of the assignment operators, we only perform |
| 4563 | // overload resolution if the left-hand side is a class or |
| 4564 | // enumeration type (C++ [expr.ass]p3). |
| 4565 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4566 | !Args[0]->getType()->isOverloadableType()) |
| 4567 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4569 | // Build an empty overload set. |
| 4570 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4571 | |
| 4572 | // Add the candidates from the given function set. |
| 4573 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4574 | |
| 4575 | // Add operator candidates that are member functions. |
| 4576 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4577 | |
| 4578 | // Add builtin operator candidates. |
| 4579 | AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet); |
| 4580 | |
| 4581 | // Perform overload resolution. |
| 4582 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4583 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4584 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4585 | // We found a built-in operator or an overloaded operator. |
| 4586 | FunctionDecl *FnDecl = Best->Function; |
| 4587 | |
| 4588 | if (FnDecl) { |
| 4589 | // We matched an overloaded operator. Build a call to that |
| 4590 | // operator. |
| 4591 | |
| 4592 | // Convert the arguments. |
| 4593 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4594 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 4595 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4596 | "passing")) |
| 4597 | return ExprError(); |
| 4598 | } else { |
| 4599 | // Convert the arguments. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4600 | if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4601 | "passing") || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4602 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4603 | "passing")) |
| 4604 | return ExprError(); |
| 4605 | } |
| 4606 | |
| 4607 | // Determine the result type |
| 4608 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4609 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4610 | ResultTy = ResultTy.getNonReferenceType(); |
| 4611 | |
| 4612 | // Build the actual expression node. |
| 4613 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4614 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4615 | UsualUnaryConversions(FnExpr); |
| 4616 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | e80ccac | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4618 | Args, 2, ResultTy, OpLoc); |
| 4619 | return MaybeBindToTemporary(CE); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4620 | } else { |
| 4621 | // We matched a built-in operator. Convert the arguments, then |
| 4622 | // break out so that we will build the appropriate built-in |
| 4623 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4624 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4625 | Best->Conversions[0], "passing") || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4626 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4627 | Best->Conversions[1], "passing")) |
| 4628 | return ExprError(); |
| 4629 | |
| 4630 | break; |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | case OR_No_Viable_Function: |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4635 | // For class as left operand for assignment or compound assigment operator |
| 4636 | // do not fall through to handling in built-in, but report that no overloaded |
| 4637 | // assignment operator found |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4638 | if (Args[0]->getType()->isRecordType() && Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4639 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4640 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4641 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4642 | return ExprError(); |
| 4643 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4644 | // No viable function; fall through to handling this as a |
| 4645 | // built-in operator, which will produce an error message for us. |
| 4646 | break; |
| 4647 | |
| 4648 | case OR_Ambiguous: |
| 4649 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4650 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4651 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4652 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4653 | return ExprError(); |
| 4654 | |
| 4655 | case OR_Deleted: |
| 4656 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4657 | << Best->Function->isDeleted() |
| 4658 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4659 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4660 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4661 | return ExprError(); |
| 4662 | } |
| 4663 | |
| 4664 | // Either we found no viable overloaded operator or we matched a |
| 4665 | // built-in operator. In either case, try to build a built-in |
| 4666 | // operation. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4667 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4670 | /// BuildCallToMemberFunction - Build a call to a member |
| 4671 | /// function. MemExpr is the expression that refers to the member |
| 4672 | /// function (and includes the object parameter), Args/NumArgs are the |
| 4673 | /// arguments to the function call (not including the object |
| 4674 | /// parameter). The caller needs to validate that the member |
| 4675 | /// expression refers to a member function or an overloaded member |
| 4676 | /// function. |
| 4677 | Sema::ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4678 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 4679 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4680 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 4681 | SourceLocation RParenLoc) { |
| 4682 | // Dig out the member expression. This holds both the object |
| 4683 | // argument and the member function we're referring to. |
| 4684 | MemberExpr *MemExpr = 0; |
| 4685 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 4686 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 4687 | else |
| 4688 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 4689 | assert(MemExpr && "Building member call without member expression"); |
| 4690 | |
| 4691 | // Extract the object argument. |
| 4692 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4693 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4694 | CXXMethodDecl *Method = 0; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4695 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 4696 | isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4697 | // Add overload candidates |
| 4698 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4699 | DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4701 | for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd; |
| 4702 | Func != FuncEnd; ++Func) { |
| 4703 | if ((Method = dyn_cast<CXXMethodDecl>(*Func))) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4705 | /*SuppressUserConversions=*/false); |
| 4706 | else |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 4707 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func), |
| 4708 | MemExpr->hasExplicitTemplateArgumentList(), |
| 4709 | MemExpr->getTemplateArgs(), |
| 4710 | MemExpr->getNumTemplateArgs(), |
| 4711 | ObjectArg, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4712 | CandidateSet, |
| 4713 | /*SuppressUsedConversions=*/false); |
| 4714 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4716 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4717 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4718 | case OR_Success: |
| 4719 | Method = cast<CXXMethodDecl>(Best->Function); |
| 4720 | break; |
| 4721 | |
| 4722 | case OR_No_Viable_Function: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4723 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4724 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4725 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4726 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4727 | // FIXME: Leaking incoming expressions! |
| 4728 | return true; |
| 4729 | |
| 4730 | case OR_Ambiguous: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4731 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4732 | diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4733 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4734 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4735 | // FIXME: Leaking incoming expressions! |
| 4736 | return true; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4737 | |
| 4738 | case OR_Deleted: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4739 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4740 | diag::err_ovl_deleted_member_call) |
| 4741 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4742 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4743 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4744 | // FIXME: Leaking incoming expressions! |
| 4745 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | FixOverloadedFunctionReference(MemExpr, Method); |
| 4749 | } else { |
| 4750 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 4751 | } |
| 4752 | |
| 4753 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4755 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4756 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4757 | Method->getResultType().getNonReferenceType(), |
| 4758 | RParenLoc)); |
| 4759 | |
| 4760 | // Convert the object argument (for a non-static member function call). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | if (!Method->isStatic() && |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4762 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 4763 | return true; |
| 4764 | MemExpr->setBase(ObjectArg); |
| 4765 | |
| 4766 | // Convert the rest of the arguments |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4767 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4769 | RParenLoc)) |
| 4770 | return true; |
| 4771 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4772 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4773 | return true; |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 4774 | |
| 4775 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4778 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 4779 | /// type (C++ [over.call.object]), which can end up invoking an |
| 4780 | /// overloaded function call operator (@c operator()) or performing a |
| 4781 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4782 | Sema::ExprResult |
| 4783 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 4784 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4785 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4786 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4787 | SourceLocation RParenLoc) { |
| 4788 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4789 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4790 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4791 | // C++ [over.call.object]p1: |
| 4792 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4793 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4794 | // candidate functions includes at least the function call |
| 4795 | // operators of T. The function call operators of T are obtained by |
| 4796 | // ordinary lookup of the name operator() in the context of |
| 4797 | // (E).operator(). |
| 4798 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4799 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4800 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4801 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4802 | Oper != OperEnd; ++Oper) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4804 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4805 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4806 | // C++ [over.call.object]p2: |
| 4807 | // In addition, for each conversion function declared in T of the |
| 4808 | // form |
| 4809 | // |
| 4810 | // operator conversion-type-id () cv-qualifier; |
| 4811 | // |
| 4812 | // where cv-qualifier is the same cv-qualification as, or a |
| 4813 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 4814 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 4815 | // R", or the type "reference to pointer to function of |
| 4816 | // (P1,...,Pn) returning R", or the type "reference to function |
| 4817 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4818 | // is also considered as a candidate function. Similarly, |
| 4819 | // surrogate call functions are added to the set of candidate |
| 4820 | // functions for each conversion function declared in an |
| 4821 | // accessible base class provided the function is not hidden |
| 4822 | // within T by another intervening declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4824 | if (!RequireCompleteType(SourceLocation(), Object->getType(), 0)) { |
| 4825 | // FIXME: Look in base classes for more conversion operators! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4827 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4828 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4829 | Func = Conversions->function_begin(), |
| 4830 | FuncEnd = Conversions->function_end(); |
| 4831 | Func != FuncEnd; ++Func) { |
| 4832 | CXXConversionDecl *Conv; |
| 4833 | FunctionTemplateDecl *ConvTemplate; |
| 4834 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4835 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4836 | // Skip over templated conversion functions; they aren't |
| 4837 | // surrogates. |
| 4838 | if (ConvTemplate) |
| 4839 | continue; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4840 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4841 | // Strip the reference type (if any) and then the pointer type (if |
| 4842 | // any) to get down to what might be a function type. |
| 4843 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 4844 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4845 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4846 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4847 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4848 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 4849 | } |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4850 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4852 | // Perform overload resolution. |
| 4853 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4854 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4855 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4856 | // Overload resolution succeeded; we'll build the appropriate call |
| 4857 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4858 | break; |
| 4859 | |
| 4860 | case OR_No_Viable_Function: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4861 | Diag(Object->getSourceRange().getBegin(), |
Sebastian Redl | 15b02d2 | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4862 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4863 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | 15b02d2 | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4864 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4865 | break; |
| 4866 | |
| 4867 | case OR_Ambiguous: |
| 4868 | Diag(Object->getSourceRange().getBegin(), |
| 4869 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4870 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4871 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4872 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4873 | |
| 4874 | case OR_Deleted: |
| 4875 | Diag(Object->getSourceRange().getBegin(), |
| 4876 | diag::err_ovl_deleted_object_call) |
| 4877 | << Best->Function->isDeleted() |
| 4878 | << Object->getType() << Object->getSourceRange(); |
| 4879 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4880 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4881 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4882 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4883 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4884 | // We had an error; delete all of the subexpressions and return |
| 4885 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4886 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4887 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4888 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4889 | return true; |
| 4890 | } |
| 4891 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4892 | if (Best->Function == 0) { |
| 4893 | // Since there is no function declaration, this is one of the |
| 4894 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4896 | = cast<CXXConversionDecl>( |
| 4897 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 4898 | |
| 4899 | // We selected one of the surrogate functions that converts the |
| 4900 | // object parameter to a function pointer. Perform the conversion |
| 4901 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 4902 | |
| 4903 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4904 | // and then call it. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 4905 | CXXMemberCallExpr *CE = |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4906 | BuildCXXMemberCallExpr(Object, Conv); |
| 4907 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 4908 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4909 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 4910 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 4914 | // that calls this method, using Object for the implicit object |
| 4915 | // parameter and passing along the remaining arguments. |
| 4916 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4917 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4918 | |
| 4919 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4920 | unsigned NumArgsToCheck = NumArgs; |
| 4921 | |
| 4922 | // Build the full argument list for the method call (the |
| 4923 | // implicit object parameter is placed at the beginning of the |
| 4924 | // list). |
| 4925 | Expr **MethodArgs; |
| 4926 | if (NumArgs < NumArgsInProto) { |
| 4927 | NumArgsToCheck = NumArgsInProto; |
| 4928 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 4929 | } else { |
| 4930 | MethodArgs = new Expr*[NumArgs + 1]; |
| 4931 | } |
| 4932 | MethodArgs[0] = Object; |
| 4933 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4934 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4935 | |
| 4936 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4937 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4938 | UsualUnaryConversions(NewFn); |
| 4939 | |
| 4940 | // Once we've built TheCall, all of the expressions are properly |
| 4941 | // owned. |
| 4942 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | ExprOwningPtr<CXXOperatorCallExpr> |
| 4944 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4945 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4946 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4947 | delete [] MethodArgs; |
| 4948 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4949 | // We may have default arguments. If so, we need to allocate more |
| 4950 | // slots in the call for them. |
| 4951 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4952 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4953 | else if (NumArgs > NumArgsInProto) |
| 4954 | NumArgsToCheck = NumArgsInProto; |
| 4955 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4956 | bool IsError = false; |
| 4957 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4958 | // Initialize the implicit object parameter. |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4959 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4960 | TheCall->setArg(0, Object); |
| 4961 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4962 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4963 | // Check the argument types. |
| 4964 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4965 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4966 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4967 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4968 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4969 | // Pass the argument. |
| 4970 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4971 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4972 | } else { |
Anders Carlsson | e827123 | 2009-08-14 18:30:22 +0000 | [diff] [blame] | 4973 | Arg = CXXDefaultArgExpr::Create(Context, Method->getParamDecl(i)); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4974 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4975 | |
| 4976 | TheCall->setArg(i + 1, Arg); |
| 4977 | } |
| 4978 | |
| 4979 | // If this is a variadic call, handle args passed through "...". |
| 4980 | if (Proto->isVariadic()) { |
| 4981 | // Promote the arguments (C99 6.5.2.2p7). |
| 4982 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 4983 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4984 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4985 | TheCall->setArg(i + 1, Arg); |
| 4986 | } |
| 4987 | } |
| 4988 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4989 | if (IsError) return true; |
| 4990 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4991 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4992 | return true; |
| 4993 | |
Anders Carlsson | 1c83deb | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 4994 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4997 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4998 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4999 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5000 | Sema::OwningExprResult |
| 5001 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 5002 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5003 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5004 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5005 | // C++ [over.ref]p1: |
| 5006 | // |
| 5007 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 5008 | // for a class object x of type T if T::operator->() exists and if |
| 5009 | // the operator is selected as the best match function by the |
| 5010 | // overload resolution mechanism (13.3). |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5011 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 5012 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5013 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5014 | |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5015 | LookupResult R = LookupQualifiedName(BaseRecord->getDecl(), OpName, |
| 5016 | LookupOrdinaryName); |
| 5017 | |
| 5018 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 5019 | Oper != OperEnd; ++Oper) |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5020 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5021 | /*SuppressUserConversions=*/false); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5022 | |
| 5023 | // Perform overload resolution. |
| 5024 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5025 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5026 | case OR_Success: |
| 5027 | // Overload resolution succeeded; we'll build the call below. |
| 5028 | break; |
| 5029 | |
| 5030 | case OR_No_Viable_Function: |
| 5031 | if (CandidateSet.empty()) |
| 5032 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5033 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5034 | else |
| 5035 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5036 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5037 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5038 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5039 | |
| 5040 | case OR_Ambiguous: |
| 5041 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5042 | << "->" << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5043 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5044 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5045 | |
| 5046 | case OR_Deleted: |
| 5047 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5048 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5049 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5050 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5051 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | // Convert the object parameter. |
| 5055 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5056 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5057 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5058 | |
| 5059 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5060 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5061 | |
| 5062 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5063 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 5064 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5065 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5066 | Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5067 | Method->getResultType().getNonReferenceType(), |
| 5068 | OpLoc); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5069 | return Owned(Base); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5072 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 5073 | /// a C++ overloaded function (possibly with some parentheses and |
| 5074 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 5075 | /// to the function declaration Fn, so patch up the expression E to |
| 5076 | /// refer (possibly indirectly) to Fn. |
| 5077 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 5078 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 5079 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 5080 | E->setType(PE->getSubExpr()->getType()); |
| 5081 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5082 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5083 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5084 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5085 | if (Method->isStatic()) { |
| 5086 | // Do nothing: static member functions aren't any different |
| 5087 | // from non-member functions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5088 | } else if (QualifiedDeclRefExpr *DRE |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5089 | = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) { |
| 5090 | // We have taken the address of a pointer to member |
| 5091 | // function. Perform the computation here so that we get the |
| 5092 | // appropriate pointer to member type. |
| 5093 | DRE->setDecl(Fn); |
| 5094 | DRE->setType(Fn->getType()); |
| 5095 | QualType ClassType |
| 5096 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5097 | E->setType(Context.getMemberPointerType(Fn->getType(), |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5098 | ClassType.getTypePtr())); |
| 5099 | return; |
| 5100 | } |
| 5101 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5102 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5103 | E->setType(Context.getPointerType(UnOp->getSubExpr()->getType())); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5104 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5105 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5106 | isa<FunctionTemplateDecl>(DR->getDecl())) && |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5107 | "Expected overloaded function or function template"); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5108 | DR->setDecl(Fn); |
| 5109 | E->setType(Fn->getType()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5110 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 5111 | MemExpr->setMemberDecl(Fn); |
| 5112 | E->setType(Fn->getType()); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5113 | } else { |
| 5114 | assert(false && "Invalid reference to overloaded function"); |
| 5115 | } |
| 5116 | } |
| 5117 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5118 | } // end namespace clang |