Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include <algorithm> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 44 | ICC_Promotion, |
| 45 | ICC_Conversion, |
| 46 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 52 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Conversion, |
| 73 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 79 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 80 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 97 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | "Integral conversion", |
| 99 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Floating-integral conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 102 | "Complex-real conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 103 | "Pointer conversion", |
| 104 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 106 | "Compatible-types conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 107 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | }; |
| 109 | return Name[Kind]; |
| 110 | } |
| 111 | |
Douglas Gregor | 60d62c2 | 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 | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 121 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 122 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 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 | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 152 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 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 | bc0805a | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
Douglas Gregor | bc0805a | 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 | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
| 185 | bool PrintedSomething = false; |
| 186 | if (First != ICK_Identity) { |
| 187 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 188 | PrintedSomething = true; |
| 189 | } |
| 190 | |
| 191 | if (Second != ICK_Identity) { |
| 192 | if (PrintedSomething) { |
| 193 | fprintf(stderr, " -> "); |
| 194 | } |
| 195 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 196 | |
| 197 | if (CopyConstructor) { |
| 198 | fprintf(stderr, " (by copy constructor)"); |
| 199 | } else if (DirectBinding) { |
| 200 | fprintf(stderr, " (direct reference binding)"); |
| 201 | } else if (ReferenceBinding) { |
| 202 | fprintf(stderr, " (reference binding)"); |
| 203 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | PrintedSomething = true; |
| 205 | } |
| 206 | |
| 207 | if (Third != ICK_Identity) { |
| 208 | if (PrintedSomething) { |
| 209 | fprintf(stderr, " -> "); |
| 210 | } |
| 211 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 212 | PrintedSomething = true; |
| 213 | } |
| 214 | |
| 215 | if (!PrintedSomething) { |
| 216 | fprintf(stderr, "No conversions required"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 221 | /// error. Useful for debugging overloading issues. |
| 222 | void UserDefinedConversionSequence::DebugPrint() const { |
| 223 | if (Before.First || Before.Second || Before.Third) { |
| 224 | Before.DebugPrint(); |
| 225 | fprintf(stderr, " -> "); |
| 226 | } |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 227 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | if (After.First || After.Second || After.Third) { |
| 229 | fprintf(stderr, " -> "); |
| 230 | After.DebugPrint(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 235 | /// error. Useful for debugging overloading issues. |
| 236 | void ImplicitConversionSequence::DebugPrint() const { |
| 237 | switch (ConversionKind) { |
| 238 | case StandardConversion: |
| 239 | fprintf(stderr, "Standard conversion: "); |
| 240 | Standard.DebugPrint(); |
| 241 | break; |
| 242 | case UserDefinedConversion: |
| 243 | fprintf(stderr, "User-defined conversion: "); |
| 244 | UserDefined.DebugPrint(); |
| 245 | break; |
| 246 | case EllipsisConversion: |
| 247 | fprintf(stderr, "Ellipsis conversion"); |
| 248 | break; |
| 249 | case BadConversion: |
| 250 | fprintf(stderr, "Bad conversion"); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | fprintf(stderr, "\n"); |
| 255 | } |
| 256 | |
| 257 | // IsOverload - Determine whether the given New declaration is an |
| 258 | // overload of the Old declaration. This routine returns false if New |
| 259 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 260 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 261 | // function (or overload set). When it does return false and Old is an |
| 262 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | // FunctionDecl that New cannot be overloaded with. |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 287 | OverloadedFunctionDecl::function_iterator& MatchedDecl) { |
Douglas Gregor | 8e9bebd | 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 | e53060f | 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 | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 305 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 307 | |
Douglas Gregor | 34d1dc9 | 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 | 8e9bebd | 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 | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 324 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 325 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 326 | return false; |
| 327 | |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 328 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 329 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
Douglas Gregor | 8e9bebd | 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 | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 341 | // C++ [temp.over.link]p4: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | // The signature of a function template consists of its function |
Douglas Gregor | 34d1dc9 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | // relationship between the template parameters and the rest of the |
Douglas Gregor | 34d1dc9 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 352 | OldTemplate->getTemplateParameters(), |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 353 | false, false, SourceLocation()) || |
| 354 | OldType->getResultType() != NewType->getResultType())) |
| 355 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | if (OldMethod && NewMethod && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 368 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 369 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | 8e9bebd | 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 | 27c8dc0 | 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 | 8e9bebd | 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 | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 400 | /// |
| 401 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 402 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 403 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 404 | /// permitted. |
Sebastian Redl | e2b6833 | 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. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 407 | /// If @p UserCast, the implicit conversion is being done for a user-specified |
| 408 | /// cast. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 409 | ImplicitConversionSequence |
Anders Carlsson | 2974b5c | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 410 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 411 | bool SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 412 | bool AllowExplicit, bool ForceRValue, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 413 | bool InOverloadResolution, |
| 414 | bool UserCast) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 415 | ImplicitConversionSequence ICS; |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 416 | OverloadCandidateSet Conversions; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 417 | OverloadingResult UserDefResult = OR_Success; |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 418 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 419 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 420 | else if (getLangOptions().CPlusPlus && |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 421 | (UserDefResult = IsUserDefinedConversion(From, ToType, |
| 422 | ICS.UserDefined, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 423 | Conversions, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 424 | !SuppressUserConversions, AllowExplicit, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 425 | ForceRValue, UserCast)) == OR_Success) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 426 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 427 | // C++ [over.ics.user]p4: |
| 428 | // A conversion of an expression of class type to the same class |
| 429 | // type is given Exact Match rank, and a conversion of an |
| 430 | // expression of class type to a base class of that type is |
| 431 | // given Conversion rank, in spite of the fact that a copy |
| 432 | // constructor (i.e., a user-defined conversion function) is |
| 433 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 435 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 437 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 438 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 439 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 440 | // Turn this into a "standard" conversion sequence, so that it |
| 441 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 442 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 443 | ICS.Standard.setAsIdentityConversion(); |
| 444 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 445 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 446 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 447 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 448 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 449 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 450 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 451 | |
| 452 | // C++ [over.best.ics]p4: |
| 453 | // However, when considering the argument of a user-defined |
| 454 | // conversion function that is a candidate by 13.3.1.3 when |
| 455 | // invoked for the copying of the temporary in the second step |
| 456 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 457 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 458 | // ellipsis conversion sequences are allowed. |
| 459 | if (SuppressUserConversions && |
| 460 | ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) |
| 461 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 462 | } else { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 463 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 464 | if (UserDefResult == OR_Ambiguous) { |
| 465 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 466 | Cand != Conversions.end(); ++Cand) |
| 467 | ICS.ConversionFunctionSet.push_back(Cand->Function); |
| 468 | } |
| 469 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 470 | |
| 471 | return ICS; |
| 472 | } |
| 473 | |
| 474 | /// IsStandardConversion - Determines whether there is a standard |
| 475 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 476 | /// expression From to the type ToType. Standard conversion sequences |
| 477 | /// only consider non-class types; for conversions that involve class |
| 478 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 479 | /// contain the standard conversion sequence required to perform this |
| 480 | /// conversion and this routine will return true. Otherwise, this |
| 481 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | bool |
| 483 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 484 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 486 | QualType FromType = From->getType(); |
| 487 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 488 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 489 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 490 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 491 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 492 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 493 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 495 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 497 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 498 | if (getLangOptions().CPlusPlus) |
| 499 | return false; |
| 500 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 504 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 505 | // array-to-pointer conversion, or function-to-pointer conversion |
| 506 | // (C++ 4p1). |
| 507 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 509 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 510 | // converted to an rvalue. |
| 511 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 513 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 514 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 515 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 516 | |
| 517 | // If T is a non-class type, the type of the rvalue is the |
| 518 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 519 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 520 | // just strip the qualifiers because they don't matter. |
| 521 | |
| 522 | // FIXME: Doesn't see through to qualifiers behind a typedef! |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 523 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 524 | } else if (FromType->isArrayType()) { |
| 525 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 526 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 527 | |
| 528 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 529 | // bound of T" can be converted to an rvalue of type "pointer to |
| 530 | // T" (C++ 4.2p1). |
| 531 | FromType = Context.getArrayDecayedType(FromType); |
| 532 | |
| 533 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 534 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 535 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 536 | |
| 537 | // For the purpose of ranking in overload resolution |
| 538 | // (13.3.3.1.1), this conversion is considered an |
| 539 | // array-to-pointer conversion followed by a qualification |
| 540 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 541 | SCS.Second = ICK_Identity; |
| 542 | SCS.Third = ICK_Qualification; |
| 543 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 544 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 545 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 546 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 547 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 548 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 549 | |
| 550 | // An lvalue of function type T can be converted to an rvalue of |
| 551 | // type "pointer to T." The result is a pointer to the |
| 552 | // function. (C++ 4.3p1). |
| 553 | FromType = Context.getPointerType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | } else if (FunctionDecl *Fn |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 555 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 556 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 557 | SCS.First = ICK_Function_To_Pointer; |
| 558 | |
| 559 | // We were able to resolve the address of the overloaded function, |
| 560 | // so we can convert to the type of that function. |
| 561 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 562 | if (ToType->isLValueReferenceType()) |
| 563 | FromType = Context.getLValueReferenceType(FromType); |
| 564 | else if (ToType->isRValueReferenceType()) |
| 565 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 566 | else if (ToType->isMemberPointerType()) { |
| 567 | // Resolve address only succeeds if both sides are member pointers, |
| 568 | // but it doesn't have to be the same class. See DR 247. |
| 569 | // Note that this means that the type of &Derived::fn can be |
| 570 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 571 | // base class, even if it was brought into the derived class via a |
| 572 | // using declaration. The standard isn't clear on this issue at all. |
| 573 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 574 | FromType = Context.getMemberPointerType(FromType, |
| 575 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 576 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 577 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 578 | } else { |
| 579 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 580 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // The second conversion can be an integral promotion, floating |
| 584 | // point promotion, integral conversion, floating point conversion, |
| 585 | // floating-integral conversion, pointer conversion, |
| 586 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 587 | // For overloading in C, this can also be a "compatible-type" |
| 588 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 589 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 590 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 591 | // The unqualified versions of the types are the same: there's no |
| 592 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 593 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 594 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 596 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 597 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 598 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 599 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 600 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 601 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 602 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 603 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 604 | SCS.Second = ICK_Complex_Promotion; |
| 605 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 606 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 607 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 608 | // Integral conversions (C++ 4.7). |
| 609 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 610 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 611 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 612 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 613 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 614 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 615 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 616 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 617 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 618 | SCS.Second = ICK_Complex_Conversion; |
| 619 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 620 | } else if ((FromType->isFloatingType() && |
| 621 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 622 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 624 | ToType->isFloatingType())) { |
| 625 | // Floating-integral conversions (C++ 4.9). |
| 626 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 627 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 628 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 629 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 630 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 631 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 632 | SCS.Second = ICK_Complex_Real; |
| 633 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 634 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 635 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 636 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 637 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 638 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 639 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 640 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 641 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 642 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 643 | } else if (ToType->isBooleanType() && |
| 644 | (FromType->isArithmeticType() || |
| 645 | FromType->isEnumeralType() || |
| 646 | FromType->isPointerType() || |
| 647 | FromType->isBlockPointerType() || |
| 648 | FromType->isMemberPointerType() || |
| 649 | FromType->isNullPtrType())) { |
| 650 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 651 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 652 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 654 | Context.typesAreCompatible(ToType, FromType)) { |
| 655 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 656 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 657 | } else { |
| 658 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 659 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 662 | QualType CanonFrom; |
| 663 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 664 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 665 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 666 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 667 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 668 | CanonFrom = Context.getCanonicalType(FromType); |
| 669 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 670 | } else { |
| 671 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 672 | SCS.Third = ICK_Identity; |
| 673 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 675 | // [...] Any difference in top-level cv-qualification is |
| 676 | // subsumed by the initialization itself and does not constitute |
| 677 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 678 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 680 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 681 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 682 | FromType = ToType; |
| 683 | CanonFrom = CanonTo; |
| 684 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // If we have not converted the argument type to the parameter type, |
| 688 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 689 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 690 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 691 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 692 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 693 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 697 | /// expression From (whose potentially-adjusted type is FromType) to |
| 698 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 699 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 701 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 702 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 703 | if (!To) { |
| 704 | return false; |
| 705 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 706 | |
| 707 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 708 | // unsigned short int can be converted to an rvalue of type int if |
| 709 | // int can represent all the values of the source type; otherwise, |
| 710 | // the source rvalue can be converted to an rvalue of type unsigned |
| 711 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 712 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 713 | if (// We can promote any signed, promotable integer type to an int |
| 714 | (FromType->isSignedIntegerType() || |
| 715 | // We can promote any unsigned integer type whose size is |
| 716 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 718 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 719 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 722 | return To->getKind() == BuiltinType::UInt; |
| 723 | } |
| 724 | |
| 725 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 726 | // can be converted to an rvalue of the first of the following types |
| 727 | // that can represent all the values of its underlying type: int, |
| 728 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 729 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 730 | && ToType->isIntegerType()) { |
| 731 | // Determine whether the type we're converting from is signed or |
| 732 | // unsigned. |
| 733 | bool FromIsSigned; |
| 734 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 735 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 736 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 737 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 738 | } else { |
| 739 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 740 | FromIsSigned = true; |
| 741 | } |
| 742 | |
| 743 | // The types we'll try to promote to, in the appropriate |
| 744 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | QualType PromoteTypes[6] = { |
| 746 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 747 | Context.LongTy, Context.UnsignedLongTy , |
| 748 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 749 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 750 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 751 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 752 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 754 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 755 | // We found the type that we can promote to. If this is the |
| 756 | // type we wanted, we have a promotion. Otherwise, no |
| 757 | // promotion. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 758 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 759 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 765 | // rvalue of type int if int can represent all the values of the |
| 766 | // bit-field; otherwise, it can be converted to unsigned int if |
| 767 | // unsigned int can represent all the values of the bit-field. If |
| 768 | // the bit-field is larger yet, no integral promotion applies to |
| 769 | // it. If the bit-field has an enumerated type, it is treated as any |
| 770 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 771 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 772 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 773 | using llvm::APSInt; |
| 774 | if (From) |
| 775 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 776 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 777 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 778 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 779 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 780 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 782 | // Are we promoting to an int from a bitfield that fits in an int? |
| 783 | if (BitWidth < ToSize || |
| 784 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 785 | return To->getKind() == BuiltinType::Int; |
| 786 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 788 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 789 | // that fits into an unsigned int? |
| 790 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 791 | return To->getKind() == BuiltinType::UInt; |
| 792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 794 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 795 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 796 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 798 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 799 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 800 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 801 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 802 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 803 | |
| 804 | return false; |
| 805 | } |
| 806 | |
| 807 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 808 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 809 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 811 | /// An rvalue of type float can be converted to an rvalue of type |
| 812 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 813 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 814 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 815 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 816 | ToBuiltin->getKind() == BuiltinType::Double) |
| 817 | return true; |
| 818 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 819 | // C99 6.3.1.5p1: |
| 820 | // When a float is promoted to double or long double, or a |
| 821 | // double is promoted to long double [...]. |
| 822 | if (!getLangOptions().CPlusPlus && |
| 823 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 824 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 825 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 826 | return true; |
| 827 | } |
| 828 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 829 | return false; |
| 830 | } |
| 831 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 832 | /// \brief Determine if a conversion is a complex promotion. |
| 833 | /// |
| 834 | /// A complex promotion is defined as a complex -> complex conversion |
| 835 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 836 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 837 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 838 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 839 | if (!FromComplex) |
| 840 | return false; |
| 841 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 842 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 843 | if (!ToComplex) |
| 844 | return false; |
| 845 | |
| 846 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 847 | ToComplex->getElementType()) || |
| 848 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 849 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 852 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 853 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 854 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 855 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 856 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | static QualType |
| 858 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 859 | QualType ToPointee, QualType ToType, |
| 860 | ASTContext &Context) { |
| 861 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 862 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 863 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
| 865 | // Exact qualifier match -> return the pointer type we're converting to. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 866 | if (CanonToPointee.getQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 867 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 868 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 869 | return ToType; |
| 870 | |
| 871 | // Build a pointer to ToPointee. It has the right qualifiers |
| 872 | // already. |
| 873 | return Context.getPointerType(ToPointee); |
| 874 | } |
| 875 | |
| 876 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 877 | return Context.getPointerType( |
| 878 | Context.getQualifiedType(CanonToPointee.getUnqualifiedType(), Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 882 | bool InOverloadResolution, |
| 883 | ASTContext &Context) { |
| 884 | // Handle value-dependent integral null pointer constants correctly. |
| 885 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 886 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 887 | Expr->getType()->isIntegralType()) |
| 888 | return !InOverloadResolution; |
| 889 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 890 | return Expr->isNullPointerConstant(Context, |
| 891 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 892 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 895 | /// IsPointerConversion - Determines whether the conversion of the |
| 896 | /// expression From, which has the (possibly adjusted) type FromType, |
| 897 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 898 | /// 4.10). If so, returns true and places the converted type (that |
| 899 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 900 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 901 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 902 | /// This routine also supports conversions to and from block pointers |
| 903 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 904 | /// pointers to interfaces. FIXME: Once we've determined the |
| 905 | /// appropriate overloading rules for Objective-C, we may want to |
| 906 | /// split the Objective-C checks into a different routine; however, |
| 907 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 908 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 909 | /// set if the conversion is an allowed Objective-C conversion that |
| 910 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 911 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 912 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 913 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 915 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 916 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 917 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 918 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 920 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 921 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 922 | ConvertedType = ToType; |
| 923 | return true; |
| 924 | } |
| 925 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 926 | // Blocks: Block pointers can be converted to void*. |
| 927 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 928 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 929 | ConvertedType = ToType; |
| 930 | return true; |
| 931 | } |
| 932 | // Blocks: A null pointer constant can be converted to a block |
| 933 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 935 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 936 | ConvertedType = ToType; |
| 937 | return true; |
| 938 | } |
| 939 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 940 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 941 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 943 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 944 | ConvertedType = ToType; |
| 945 | return true; |
| 946 | } |
| 947 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 948 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 949 | if (!ToTypePtr) |
| 950 | return false; |
| 951 | |
| 952 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 953 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 954 | ConvertedType = ToType; |
| 955 | return true; |
| 956 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 958 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 959 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 960 | if (!FromTypePtr) |
| 961 | return false; |
| 962 | |
| 963 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 964 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 965 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 966 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 967 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 968 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 969 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 971 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 972 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 976 | // When we're overloading in C, we allow a special kind of pointer |
| 977 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 979 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 981 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 983 | return true; |
| 984 | } |
| 985 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 986 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 988 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 989 | // can be converted to an rvalue of type "pointer to cv B," where |
| 990 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 991 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 992 | // necessitates this conversion is ill-formed. The result of the |
| 993 | // conversion is a pointer to the base class sub-object of the |
| 994 | // derived class object. The null pointer value is converted to |
| 995 | // the null pointer value of the destination type. |
| 996 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 997 | // Note that we do not check for ambiguity or inaccessibility |
| 998 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 999 | if (getLangOptions().CPlusPlus && |
| 1000 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1001 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1003 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1004 | ToType, Context); |
| 1005 | return true; |
| 1006 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1007 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | /// isObjCPointerConversion - Determines whether this is an |
| 1012 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1013 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1015 | QualType& ConvertedType, |
| 1016 | bool &IncompatibleObjC) { |
| 1017 | if (!getLangOptions().ObjC1) |
| 1018 | return false; |
| 1019 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1020 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1021 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1023 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1024 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1025 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1026 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1027 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1028 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1029 | ConvertedType = ToType; |
| 1030 | return true; |
| 1031 | } |
| 1032 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1034 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1036 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1037 | ConvertedType = ToType; |
| 1038 | return true; |
| 1039 | } |
| 1040 | // Objective C++: We're able to convert from a pointer to an |
| 1041 | // interface to a pointer to a different interface. |
| 1042 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1043 | ConvertedType = ToType; |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
| 1047 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1048 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1049 | // interfaces, which is permitted. However, we're going to |
| 1050 | // complain about it. |
| 1051 | IncompatibleObjC = true; |
| 1052 | ConvertedType = FromType; |
| 1053 | return true; |
| 1054 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1056 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1057 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1058 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1059 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1060 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1061 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1062 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1063 | return false; |
| 1064 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1065 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1066 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1067 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1068 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1069 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1070 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1071 | return false; |
| 1072 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1073 | // If we have pointers to pointers, recursively check whether this |
| 1074 | // is an Objective-C conversion. |
| 1075 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1076 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1077 | IncompatibleObjC)) { |
| 1078 | // We always complain about this conversion. |
| 1079 | IncompatibleObjC = true; |
| 1080 | ConvertedType = ToType; |
| 1081 | return true; |
| 1082 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1083 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1084 | // differences in the argument and result types are in Objective-C |
| 1085 | // pointer conversions. If so, we permit the conversion (but |
| 1086 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1088 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1089 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1090 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1091 | if (FromFunctionType && ToFunctionType) { |
| 1092 | // If the function types are exactly the same, this isn't an |
| 1093 | // Objective-C pointer conversion. |
| 1094 | if (Context.getCanonicalType(FromPointeeType) |
| 1095 | == Context.getCanonicalType(ToPointeeType)) |
| 1096 | return false; |
| 1097 | |
| 1098 | // Perform the quick checks that will tell us whether these |
| 1099 | // function types are obviously different. |
| 1100 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1101 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1102 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1103 | return false; |
| 1104 | |
| 1105 | bool HasObjCConversion = false; |
| 1106 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1107 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1108 | // Okay, the types match exactly. Nothing to do. |
| 1109 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1110 | ToFunctionType->getResultType(), |
| 1111 | ConvertedType, IncompatibleObjC)) { |
| 1112 | // Okay, we have an Objective-C pointer conversion. |
| 1113 | HasObjCConversion = true; |
| 1114 | } else { |
| 1115 | // Function types are too different. Abort. |
| 1116 | return false; |
| 1117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1119 | // Check argument types. |
| 1120 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1121 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1122 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1123 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1124 | if (Context.getCanonicalType(FromArgType) |
| 1125 | == Context.getCanonicalType(ToArgType)) { |
| 1126 | // Okay, the types match exactly. Nothing to do. |
| 1127 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1128 | ConvertedType, IncompatibleObjC)) { |
| 1129 | // Okay, we have an Objective-C pointer conversion. |
| 1130 | HasObjCConversion = true; |
| 1131 | } else { |
| 1132 | // Argument types are too different. Abort. |
| 1133 | return false; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (HasObjCConversion) { |
| 1138 | // We had an Objective-C conversion. Allow this pointer |
| 1139 | // conversion, but complain about it. |
| 1140 | ConvertedType = ToType; |
| 1141 | IncompatibleObjC = true; |
| 1142 | return true; |
| 1143 | } |
| 1144 | } |
| 1145 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1146 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1149 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1150 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1151 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1152 | /// conversions for which IsPointerConversion has already returned |
| 1153 | /// true. It returns true and produces a diagnostic if there was an |
| 1154 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1155 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
| 1156 | CastExpr::CastKind &Kind) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1157 | QualType FromType = From->getType(); |
| 1158 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1159 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1160 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1161 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1162 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1163 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1164 | if (FromPointeeType->isRecordType() && |
| 1165 | ToPointeeType->isRecordType()) { |
| 1166 | // We must have a derived-to-base conversion. Check an |
| 1167 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1168 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1169 | From->getExprLoc(), |
| 1170 | From->getSourceRange())) |
| 1171 | return true; |
| 1172 | |
| 1173 | // The conversion was successful. |
| 1174 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1175 | } |
| 1176 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1178 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1180 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1181 | // Objective-C++ conversions are always okay. |
| 1182 | // FIXME: We should have a different class of conversions for the |
| 1183 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1184 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1185 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1186 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1187 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1188 | return false; |
| 1189 | } |
| 1190 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1191 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1192 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1193 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1194 | /// If so, returns true and places the converted type (that might differ from |
| 1195 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1196 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1197 | QualType ToType, |
| 1198 | bool InOverloadResolution, |
| 1199 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1200 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1201 | if (!ToTypePtr) |
| 1202 | return false; |
| 1203 | |
| 1204 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1205 | if (From->isNullPointerConstant(Context, |
| 1206 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1207 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1208 | ConvertedType = ToType; |
| 1209 | return true; |
| 1210 | } |
| 1211 | |
| 1212 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1213 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1214 | if (!FromTypePtr) |
| 1215 | return false; |
| 1216 | |
| 1217 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1218 | // where D is derived from B (C++ 4.11p2). |
| 1219 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1220 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1221 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1222 | |
| 1223 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1224 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1225 | ToClass.getTypePtr()); |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | return false; |
| 1230 | } |
| 1231 | |
| 1232 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1233 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1234 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1235 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1236 | /// true and produces a diagnostic if there was an error, or returns false |
| 1237 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1239 | CastExpr::CastKind &Kind) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1240 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1241 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1242 | if (!FromPtrType) { |
| 1243 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1244 | assert(From->isNullPointerConstant(Context, |
| 1245 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1246 | "Expr must be null pointer constant!"); |
| 1247 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1248 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1249 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1250 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1251 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1252 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1253 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1254 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1255 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1256 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1257 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1258 | // FIXME: What about dependent types? |
| 1259 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1260 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1262 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1263 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1264 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1265 | assert(DerivationOkay && |
| 1266 | "Should not have been called if derivation isn't OK."); |
| 1267 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1268 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1269 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1270 | getUnqualifiedType())) { |
| 1271 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1272 | Paths.clear(); |
| 1273 | Paths.setRecordingPaths(true); |
| 1274 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1275 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1276 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1277 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1278 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1279 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1280 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1281 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1282 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1283 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1284 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1285 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1286 | << FromClass << ToClass << QualType(VBase, 0) |
| 1287 | << From->getSourceRange(); |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1291 | // Must be a base to derived member conversion. |
| 1292 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1293 | return false; |
| 1294 | } |
| 1295 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1296 | /// IsQualificationConversion - Determines whether the conversion from |
| 1297 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1298 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | bool |
| 1300 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1301 | FromType = Context.getCanonicalType(FromType); |
| 1302 | ToType = Context.getCanonicalType(ToType); |
| 1303 | |
| 1304 | // If FromType and ToType are the same type, this is not a |
| 1305 | // qualification conversion. |
| 1306 | if (FromType == ToType) |
| 1307 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1308 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1309 | // (C++ 4.4p4): |
| 1310 | // A conversion can add cv-qualifiers at levels other than the first |
| 1311 | // in multi-level pointers, subject to the following rules: [...] |
| 1312 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1313 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1314 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1315 | // Within each iteration of the loop, we check the qualifiers to |
| 1316 | // determine if this still looks like a qualification |
| 1317 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1318 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1319 | // until there are no more pointers or pointers-to-members left to |
| 1320 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1321 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1322 | |
| 1323 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1324 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1325 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1326 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1328 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1329 | // every cv for 0 < k < j. |
| 1330 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1331 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1332 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1334 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1335 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1337 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1338 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1339 | |
| 1340 | // We are left with FromType and ToType being the pointee types |
| 1341 | // after unwrapping the original FromType and ToType the same number |
| 1342 | // of types. If we unwrapped any pointers, and if FromType and |
| 1343 | // ToType have the same unqualified type (since we checked |
| 1344 | // qualifiers above), then this is a qualification conversion. |
| 1345 | return UnwrappedAnyPointer && |
| 1346 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1347 | } |
| 1348 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1349 | /// \brief Given a function template or function, extract the function template |
| 1350 | /// declaration (if any) and the underlying function declaration. |
| 1351 | template<typename T> |
| 1352 | static void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function, |
| 1353 | FunctionTemplateDecl *&FunctionTemplate) { |
| 1354 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig); |
| 1355 | if (FunctionTemplate) |
| 1356 | Function = cast<T>(FunctionTemplate->getTemplatedDecl()); |
| 1357 | else |
| 1358 | Function = cast<T>(Orig); |
| 1359 | } |
| 1360 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1361 | /// Determines whether there is a user-defined conversion sequence |
| 1362 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1363 | /// ToType. If such a conversion exists, User will contain the |
| 1364 | /// user-defined conversion sequence that performs such a conversion |
| 1365 | /// and this routine will return true. Otherwise, this routine returns |
| 1366 | /// false and User is unspecified. |
| 1367 | /// |
| 1368 | /// \param AllowConversionFunctions true if the conversion should |
| 1369 | /// consider conversion functions at all. If false, only constructors |
| 1370 | /// will be considered. |
| 1371 | /// |
| 1372 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1373 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1374 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1375 | /// |
| 1376 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1377 | /// for overload resolution. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1378 | /// \param UserCast true if looking for user defined conversion for a static |
| 1379 | /// cast. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1380 | Sema::OverloadingResult Sema::IsUserDefinedConversion( |
| 1381 | Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1382 | UserDefinedConversionSequence& User, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 1383 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1384 | bool AllowConversionFunctions, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1385 | bool AllowExplicit, bool ForceRValue, |
| 1386 | bool UserCast) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1387 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | if (CXXRecordDecl *ToRecordDecl |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1389 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1390 | // C++ [over.match.ctor]p1: |
| 1391 | // When objects of class type are direct-initialized (8.5), or |
| 1392 | // copy-initialized from an expression of the same or a |
| 1393 | // derived class type (8.5), overload resolution selects the |
| 1394 | // constructor. [...] For copy-initialization, the candidate |
| 1395 | // functions are all the converting constructors (12.3.1) of |
| 1396 | // that class. The argument list is the expression-list within |
| 1397 | // the parentheses of the initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1399 | = Context.DeclarationNames.getCXXConstructorName( |
| 1400 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1401 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1403 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1404 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1405 | // Find the constructor (which may be a template). |
| 1406 | CXXConstructorDecl *Constructor = 0; |
| 1407 | FunctionTemplateDecl *ConstructorTmpl |
| 1408 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1409 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1411 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1412 | else |
| 1413 | Constructor = cast<CXXConstructorDecl>(*Con); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1415 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1416 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1417 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1419 | 1, CandidateSet, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1420 | /*SuppressUserConversions=*/!UserCast, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1421 | ForceRValue); |
| 1422 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1423 | // Allow one user-defined conversion when user specifies a |
| 1424 | // From->ToType conversion via an static cast (c-style, etc). |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1425 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1426 | /*SuppressUserConversions=*/!UserCast, |
| 1427 | ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1428 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1429 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1433 | if (!AllowConversionFunctions) { |
| 1434 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1436 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1437 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1438 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1440 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1442 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1443 | // Add all of the conversion functions as candidates. |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1444 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1445 | = FromRecordDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1446 | for (OverloadedFunctionDecl::function_iterator Func |
| 1447 | = Conversions->function_begin(); |
| 1448 | Func != Conversions->function_end(); ++Func) { |
| 1449 | CXXConversionDecl *Conv; |
| 1450 | FunctionTemplateDecl *ConvTemplate; |
| 1451 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 1452 | if (ConvTemplate) |
| 1453 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1454 | else |
| 1455 | Conv = dyn_cast<CXXConversionDecl>(*Func); |
| 1456 | |
| 1457 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1458 | if (ConvTemplate) |
| 1459 | AddTemplateConversionCandidate(ConvTemplate, From, ToType, |
| 1460 | CandidateSet); |
| 1461 | else |
| 1462 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1466 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1467 | |
| 1468 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1469 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1470 | case OR_Success: |
| 1471 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1473 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1474 | // C++ [over.ics.user]p1: |
| 1475 | // If the user-defined conversion is specified by a |
| 1476 | // constructor (12.3.1), the initial standard conversion |
| 1477 | // sequence converts the source type to the type required by |
| 1478 | // the argument of the constructor. |
| 1479 | // |
| 1480 | // FIXME: What about ellipsis conversions? |
| 1481 | QualType ThisType = Constructor->getThisType(Context); |
| 1482 | User.Before = Best->Conversions[0].Standard; |
| 1483 | User.ConversionFunction = Constructor; |
| 1484 | User.After.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | User.After.FromTypePtr |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1486 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1487 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1488 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1489 | } else if (CXXConversionDecl *Conversion |
| 1490 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1491 | // C++ [over.ics.user]p1: |
| 1492 | // |
| 1493 | // [...] If the user-defined conversion is specified by a |
| 1494 | // conversion function (12.3.2), the initial standard |
| 1495 | // conversion sequence converts the source type to the |
| 1496 | // implicit object parameter of the conversion function. |
| 1497 | User.Before = Best->Conversions[0].Standard; |
| 1498 | User.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
| 1500 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1501 | // The second standard conversion sequence converts the |
| 1502 | // result of the user-defined conversion to the target type |
| 1503 | // for the sequence. Since an implicit conversion sequence |
| 1504 | // is an initialization, the special rules for |
| 1505 | // initialization by user-defined conversion apply when |
| 1506 | // selecting the best user-defined conversion for a |
| 1507 | // user-defined conversion sequence (see 13.3.3 and |
| 1508 | // 13.3.3.1). |
| 1509 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1510 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1511 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1512 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1513 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1514 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1516 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1517 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1518 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1519 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1520 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1521 | |
| 1522 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1523 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1526 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1527 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1528 | |
| 1529 | bool |
| 1530 | Sema::DiagnoseAmbiguousUserDefinedConversion(Expr *From, QualType ToType) { |
| 1531 | ImplicitConversionSequence ICS; |
| 1532 | OverloadCandidateSet CandidateSet; |
| 1533 | OverloadingResult OvResult = |
| 1534 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1535 | CandidateSet, true, false, false); |
| 1536 | if (OvResult != OR_Ambiguous) |
| 1537 | return false; |
| 1538 | Diag(From->getSourceRange().getBegin(), |
| 1539 | diag::err_typecheck_ambiguous_condition) |
| 1540 | << From->getType() << ToType << From->getSourceRange(); |
| 1541 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 1542 | return true; |
| 1543 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1544 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1545 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1546 | /// conversion sequences to determine whether one is better than the |
| 1547 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1549 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1550 | const ImplicitConversionSequence& ICS2) |
| 1551 | { |
| 1552 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1553 | // conversion sequences (as defined in 13.3.3.1) |
| 1554 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1555 | // conversion sequence than a user-defined conversion sequence or |
| 1556 | // an ellipsis conversion sequence, and |
| 1557 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1558 | // conversion sequence than an ellipsis conversion sequence |
| 1559 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | // |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1561 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1562 | return ImplicitConversionSequence::Better; |
| 1563 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1564 | return ImplicitConversionSequence::Worse; |
| 1565 | |
| 1566 | // Two implicit conversion sequences of the same form are |
| 1567 | // indistinguishable conversion sequences unless one of the |
| 1568 | // following rules apply: (C++ 13.3.3.2p3): |
| 1569 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1570 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | else if (ICS1.ConversionKind == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1572 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1573 | // User-defined conversion sequence U1 is a better conversion |
| 1574 | // sequence than another user-defined conversion sequence U2 if |
| 1575 | // they contain the same user-defined conversion function or |
| 1576 | // constructor and if the second standard conversion sequence of |
| 1577 | // U1 is better than the second standard conversion sequence of |
| 1578 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1580 | ICS2.UserDefined.ConversionFunction) |
| 1581 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1582 | ICS2.UserDefined.After); |
| 1583 | } |
| 1584 | |
| 1585 | return ImplicitConversionSequence::Indistinguishable; |
| 1586 | } |
| 1587 | |
| 1588 | /// CompareStandardConversionSequences - Compare two standard |
| 1589 | /// conversion sequences to determine whether one is better than the |
| 1590 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1592 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1593 | const StandardConversionSequence& SCS2) |
| 1594 | { |
| 1595 | // Standard conversion sequence S1 is a better conversion sequence |
| 1596 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1597 | |
| 1598 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1599 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1600 | // excluding any Lvalue Transformation; the identity conversion |
| 1601 | // sequence is considered to be a subsequence of any |
| 1602 | // non-identity conversion sequence) or, if not that, |
| 1603 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1604 | // Neither is a proper subsequence of the other. Do nothing. |
| 1605 | ; |
| 1606 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1607 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1609 | SCS1.Third == ICK_Identity)) |
| 1610 | // SCS1 is a proper subsequence of SCS2. |
| 1611 | return ImplicitConversionSequence::Better; |
| 1612 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1613 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1615 | SCS2.Third == ICK_Identity)) |
| 1616 | // SCS2 is a proper subsequence of SCS1. |
| 1617 | return ImplicitConversionSequence::Worse; |
| 1618 | |
| 1619 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1620 | // defined below), or, if not that, |
| 1621 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1622 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1623 | if (Rank1 < Rank2) |
| 1624 | return ImplicitConversionSequence::Better; |
| 1625 | else if (Rank2 < Rank1) |
| 1626 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1627 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1628 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1629 | // are indistinguishable unless one of the following rules |
| 1630 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1632 | // A conversion that is not a conversion of a pointer, or |
| 1633 | // pointer to member, to bool is better than another conversion |
| 1634 | // that is such a conversion. |
| 1635 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1636 | return SCS2.isPointerConversionToBool() |
| 1637 | ? ImplicitConversionSequence::Better |
| 1638 | : ImplicitConversionSequence::Worse; |
| 1639 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1640 | // C++ [over.ics.rank]p4b2: |
| 1641 | // |
| 1642 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1643 | // conversion of B* to A* is better than conversion of B* to |
| 1644 | // void*, and conversion of A* to void* is better than conversion |
| 1645 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1647 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1649 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1650 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1651 | // Exactly one of the conversion sequences is a conversion to |
| 1652 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1653 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1654 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1655 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1656 | // Neither conversion sequence converts to a void pointer; compare |
| 1657 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1658 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1659 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1660 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1661 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1662 | // Both conversion sequences are conversions to void |
| 1663 | // pointers. Compare the source types to determine if there's an |
| 1664 | // inheritance relationship in their sources. |
| 1665 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1666 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1667 | |
| 1668 | // Adjust the types we're converting from via the array-to-pointer |
| 1669 | // conversion, if we need to. |
| 1670 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1671 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1672 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1673 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1674 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1676 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1677 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1678 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1679 | |
| 1680 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1681 | return ImplicitConversionSequence::Better; |
| 1682 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1683 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1684 | |
| 1685 | // Objective-C++: If one interface is more specific than the |
| 1686 | // other, it is the better one. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1687 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1688 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1689 | if (FromIface1 && FromIface1) { |
| 1690 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1691 | return ImplicitConversionSequence::Better; |
| 1692 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1693 | return ImplicitConversionSequence::Worse; |
| 1694 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1695 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1698 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1700 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1701 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1702 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1703 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1704 | // C++0x [over.ics.rank]p3b4: |
| 1705 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1706 | // implicit object parameter of a non-static member function declared |
| 1707 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1708 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1709 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1710 | // or if the member function in this case has a ref qualifier. |
| 1711 | // (Of course, we don't have ref qualifiers yet.) |
| 1712 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1713 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1714 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1715 | |
| 1716 | // C++ [over.ics.rank]p3b4: |
| 1717 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1718 | // which the references refer are the same type except for |
| 1719 | // top-level cv-qualifiers, and the type to which the reference |
| 1720 | // initialized by S2 refers is more cv-qualified than the type |
| 1721 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1722 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1723 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1724 | T1 = Context.getCanonicalType(T1); |
| 1725 | T2 = Context.getCanonicalType(T2); |
| 1726 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1727 | if (T2.isMoreQualifiedThan(T1)) |
| 1728 | return ImplicitConversionSequence::Better; |
| 1729 | else if (T1.isMoreQualifiedThan(T2)) |
| 1730 | return ImplicitConversionSequence::Worse; |
| 1731 | } |
| 1732 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1733 | |
| 1734 | return ImplicitConversionSequence::Indistinguishable; |
| 1735 | } |
| 1736 | |
| 1737 | /// CompareQualificationConversions - Compares two standard conversion |
| 1738 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1740 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1741 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1743 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1744 | // -- S1 and S2 differ only in their qualification conversion and |
| 1745 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1746 | // cv-qualification signature of type T1 is a proper subset of |
| 1747 | // the cv-qualification signature of type T2, and S1 is not the |
| 1748 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1749 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1750 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1751 | return ImplicitConversionSequence::Indistinguishable; |
| 1752 | |
| 1753 | // FIXME: the example in the standard doesn't use a qualification |
| 1754 | // conversion (!) |
| 1755 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1756 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1757 | T1 = Context.getCanonicalType(T1); |
| 1758 | T2 = Context.getCanonicalType(T2); |
| 1759 | |
| 1760 | // If the types are the same, we won't learn anything by unwrapped |
| 1761 | // them. |
| 1762 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1763 | return ImplicitConversionSequence::Indistinguishable; |
| 1764 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1766 | = ImplicitConversionSequence::Indistinguishable; |
| 1767 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1768 | // Within each iteration of the loop, we check the qualifiers to |
| 1769 | // determine if this still looks like a qualification |
| 1770 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1771 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1772 | // until there are no more pointers or pointers-to-members left |
| 1773 | // to unwrap. This essentially mimics what |
| 1774 | // IsQualificationConversion does, but here we're checking for a |
| 1775 | // strict subset of qualifiers. |
| 1776 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1777 | // The qualifiers are the same, so this doesn't tell us anything |
| 1778 | // about how the sequences rank. |
| 1779 | ; |
| 1780 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1781 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1782 | if (Result == ImplicitConversionSequence::Worse) |
| 1783 | // Neither has qualifiers that are a subset of the other's |
| 1784 | // qualifiers. |
| 1785 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1787 | Result = ImplicitConversionSequence::Better; |
| 1788 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1789 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1790 | if (Result == ImplicitConversionSequence::Better) |
| 1791 | // Neither has qualifiers that are a subset of the other's |
| 1792 | // qualifiers. |
| 1793 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1795 | Result = ImplicitConversionSequence::Worse; |
| 1796 | } else { |
| 1797 | // Qualifiers are disjoint. |
| 1798 | return ImplicitConversionSequence::Indistinguishable; |
| 1799 | } |
| 1800 | |
| 1801 | // If the types after this point are equivalent, we're done. |
| 1802 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1803 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1806 | // Check that the winning standard conversion sequence isn't using |
| 1807 | // the deprecated string literal array to pointer conversion. |
| 1808 | switch (Result) { |
| 1809 | case ImplicitConversionSequence::Better: |
| 1810 | if (SCS1.Deprecated) |
| 1811 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1812 | break; |
| 1813 | |
| 1814 | case ImplicitConversionSequence::Indistinguishable: |
| 1815 | break; |
| 1816 | |
| 1817 | case ImplicitConversionSequence::Worse: |
| 1818 | if (SCS2.Deprecated) |
| 1819 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1820 | break; |
| 1821 | } |
| 1822 | |
| 1823 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1826 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1827 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1828 | /// various kinds of derived-to-base conversions (C++ |
| 1829 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1830 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1831 | ImplicitConversionSequence::CompareKind |
| 1832 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1833 | const StandardConversionSequence& SCS2) { |
| 1834 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1835 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1836 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1837 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1838 | |
| 1839 | // Adjust the types we're converting from via the array-to-pointer |
| 1840 | // conversion, if we need to. |
| 1841 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1842 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1843 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1844 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1845 | |
| 1846 | // Canonicalize all of the types. |
| 1847 | FromType1 = Context.getCanonicalType(FromType1); |
| 1848 | ToType1 = Context.getCanonicalType(ToType1); |
| 1849 | FromType2 = Context.getCanonicalType(FromType2); |
| 1850 | ToType2 = Context.getCanonicalType(ToType2); |
| 1851 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1852 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1853 | // |
| 1854 | // If class B is derived directly or indirectly from class A and |
| 1855 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1856 | // |
| 1857 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1858 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1859 | |
| 1860 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1862 | SCS2.Second == ICK_Pointer_Conversion && |
| 1863 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1864 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1865 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1867 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1869 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1870 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1871 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1872 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1873 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1874 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1875 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1876 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1877 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 1878 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1879 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1880 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1881 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1882 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1883 | return ImplicitConversionSequence::Better; |
| 1884 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1885 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1886 | |
| 1887 | if (ToIface1 && ToIface2) { |
| 1888 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1889 | return ImplicitConversionSequence::Better; |
| 1890 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1891 | return ImplicitConversionSequence::Worse; |
| 1892 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1893 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1894 | |
| 1895 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1896 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1897 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1898 | return ImplicitConversionSequence::Better; |
| 1899 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1900 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1902 | if (FromIface1 && FromIface2) { |
| 1903 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1904 | return ImplicitConversionSequence::Better; |
| 1905 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1906 | return ImplicitConversionSequence::Worse; |
| 1907 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1908 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1911 | // Compare based on reference bindings. |
| 1912 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1913 | SCS1.Second == ICK_Derived_To_Base) { |
| 1914 | // -- binding of an expression of type C to a reference of type |
| 1915 | // B& is better than binding an expression of type C to a |
| 1916 | // reference of type A&, |
| 1917 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1918 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1919 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1920 | return ImplicitConversionSequence::Better; |
| 1921 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1922 | return ImplicitConversionSequence::Worse; |
| 1923 | } |
| 1924 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1925 | // -- binding of an expression of type B to a reference of type |
| 1926 | // A& is better than binding an expression of type C to a |
| 1927 | // reference of type A&, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1928 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1929 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1930 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1931 | return ImplicitConversionSequence::Better; |
| 1932 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1933 | return ImplicitConversionSequence::Worse; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | |
| 1938 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1939 | // A::* to C::*, |
| 1940 | |
| 1941 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1942 | // A::* to C::*, and |
| 1943 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1944 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1945 | SCS1.Second == ICK_Derived_To_Base) { |
| 1946 | // -- conversion of C to B is better than conversion of C to A, |
| 1947 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1948 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1949 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1950 | return ImplicitConversionSequence::Better; |
| 1951 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1952 | return ImplicitConversionSequence::Worse; |
| 1953 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1954 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1955 | // -- conversion of B to A is better than conversion of C to A. |
| 1956 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1957 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1958 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1959 | return ImplicitConversionSequence::Better; |
| 1960 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1961 | return ImplicitConversionSequence::Worse; |
| 1962 | } |
| 1963 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1965 | return ImplicitConversionSequence::Indistinguishable; |
| 1966 | } |
| 1967 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1968 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1969 | /// ToType from the expression From. Return the implicit conversion |
| 1970 | /// sequence required to pass this argument, which may be a bad |
| 1971 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1972 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1973 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 1974 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | ImplicitConversionSequence |
| 1976 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 1977 | bool SuppressUserConversions, bool ForceRValue, |
| 1978 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1979 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1980 | ImplicitConversionSequence ICS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1982 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1983 | SuppressUserConversions, |
| 1984 | /*AllowExplicit=*/false, |
| 1985 | ForceRValue, |
| 1986 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1987 | return ICS; |
| 1988 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1990 | SuppressUserConversions, |
| 1991 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1992 | ForceRValue, |
| 1993 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1994 | } |
| 1995 | } |
| 1996 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1997 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 1998 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 1999 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2000 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2001 | /// differently in C++0x for this case. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2003 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2004 | if (!getLangOptions().CPlusPlus) { |
| 2005 | // In C, argument passing is the same as performing an assignment. |
| 2006 | QualType FromType = From->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2008 | AssignConvertType ConvTy = |
| 2009 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2010 | if (ConvTy != Compatible && |
| 2011 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2012 | ConvTy = Compatible; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2014 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 2015 | FromType, From, Flavor); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2016 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2018 | if (ToType->isReferenceType()) |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2019 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2020 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2021 | /*SuppressUserConversions=*/false, |
| 2022 | /*AllowExplicit=*/false, |
| 2023 | /*ForceRValue=*/false); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2024 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2025 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 2026 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2027 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2028 | if (!DiagnoseAmbiguousUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2029 | return Diag(From->getSourceRange().getBegin(), |
| 2030 | diag::err_typecheck_convert_incompatible) |
| 2031 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2032 | return true; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2035 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2036 | /// parameter of the given member function (@c Method) from the |
| 2037 | /// expression @p From. |
| 2038 | ImplicitConversionSequence |
| 2039 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 2040 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2041 | QualType ImplicitParamType |
| 2042 | = Context.getCVRQualifiedType(ClassType, Method->getTypeQualifiers()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2043 | |
| 2044 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2045 | // to exit early. |
| 2046 | ImplicitConversionSequence ICS; |
| 2047 | ICS.Standard.setAsIdentityConversion(); |
| 2048 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2049 | |
| 2050 | // We need to have an object of class type. |
| 2051 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2052 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2053 | FromType = PT->getPointeeType(); |
| 2054 | |
| 2055 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2056 | |
| 2057 | // The implicit object parmeter is has the type "reference to cv X", |
| 2058 | // where X is the class of which the function is a member |
| 2059 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2060 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2062 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2063 | // reference binding here, that allows class rvalues to bind to |
| 2064 | // non-constant references. |
| 2065 | |
| 2066 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2067 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2068 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 2069 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 2070 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 2071 | return ICS; |
| 2072 | |
| 2073 | // Check that we have either the same type or a derived type. It |
| 2074 | // affects the conversion rank. |
| 2075 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 2076 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 2077 | ICS.Standard.Second = ICK_Identity; |
| 2078 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2079 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2080 | else |
| 2081 | return ICS; |
| 2082 | |
| 2083 | // Success. Mark this as a reference binding. |
| 2084 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2085 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2086 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2087 | ICS.Standard.ReferenceBinding = true; |
| 2088 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2089 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2090 | return ICS; |
| 2091 | } |
| 2092 | |
| 2093 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2094 | /// the implicit object parameter for the given Method with the given |
| 2095 | /// expression. |
| 2096 | bool |
| 2097 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2098 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2100 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2102 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2103 | FromRecordType = PT->getPointeeType(); |
| 2104 | DestType = Method->getThisType(Context); |
| 2105 | } else { |
| 2106 | FromRecordType = From->getType(); |
| 2107 | DestType = ImplicitParamRecordType; |
| 2108 | } |
| 2109 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | ImplicitConversionSequence ICS |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2111 | = TryObjectArgumentInitialization(From, Method); |
| 2112 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2113 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2114 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2115 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2117 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2118 | CheckDerivedToBaseConversion(FromRecordType, |
| 2119 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2120 | From->getSourceRange().getBegin(), |
| 2121 | From->getSourceRange())) |
| 2122 | return true; |
| 2123 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 116b7d9 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2125 | /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2126 | return false; |
| 2127 | } |
| 2128 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2129 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2130 | /// expression From to bool (C++0x [conv]p3). |
| 2131 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2133 | // FIXME: Are these flags correct? |
| 2134 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2136 | /*ForceRValue=*/false, |
| 2137 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2141 | /// of the expression From to bool (C++0x [conv]p3). |
| 2142 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2143 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2144 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2145 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2146 | |
| 2147 | if (!DiagnoseAmbiguousUserDefinedConversion(From, Context.BoolTy)) |
| 2148 | return Diag(From->getSourceRange().getBegin(), |
| 2149 | diag::err_typecheck_bool_condition) |
| 2150 | << From->getType() << From->getSourceRange(); |
| 2151 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2154 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2155 | /// candidate functions, using the given function call arguments. If |
| 2156 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2157 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2158 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2159 | /// hacky way to implement the overloading rules for elidable copy |
| 2160 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2161 | /// |
| 2162 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2163 | /// based on an incomplete set of function arguments. This feature is used by |
| 2164 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | void |
| 2166 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2167 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2168 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2169 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2170 | bool ForceRValue, |
| 2171 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2173 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2174 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2176 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2178 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2180 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2181 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2182 | // If we get here, it's because we're calling a member function |
| 2183 | // that is named without a member access expression (e.g., |
| 2184 | // "this->f") that was either written explicitly or created |
| 2185 | // implicitly. This can happen with a qualified call to a member |
| 2186 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2187 | // object argument (C++ [over.call.func]p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2189 | SuppressUserConversions, ForceRValue); |
| 2190 | return; |
| 2191 | } |
| 2192 | // We treat a constructor like a non-member function, since its object |
| 2193 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2196 | if (!CandidateSet.isNewCandidate(Function)) |
| 2197 | return; |
| 2198 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2199 | // Add this candidate |
| 2200 | CandidateSet.push_back(OverloadCandidate()); |
| 2201 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2202 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2203 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2204 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2205 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2206 | |
| 2207 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2208 | |
| 2209 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2210 | // parameters is viable only if it has an ellipsis in its parameter |
| 2211 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2212 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2213 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2214 | Candidate.Viable = false; |
| 2215 | return; |
| 2216 | } |
| 2217 | |
| 2218 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2219 | // is viable only if the (m+1)st parameter has a default argument |
| 2220 | // (8.3.6). For the purposes of overload resolution, the |
| 2221 | // parameter list is truncated on the right, so that there are |
| 2222 | // exactly m parameters. |
| 2223 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2224 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2225 | // Not enough arguments. |
| 2226 | Candidate.Viable = false; |
| 2227 | return; |
| 2228 | } |
| 2229 | |
| 2230 | // Determine the implicit conversion sequences for each of the |
| 2231 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2232 | Candidate.Conversions.resize(NumArgs); |
| 2233 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2234 | if (ArgIdx < NumArgsInProto) { |
| 2235 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2236 | // exist for each argument an implicit conversion sequence |
| 2237 | // (13.3.3.1) that converts that argument to the corresponding |
| 2238 | // parameter of F. |
| 2239 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | Candidate.Conversions[ArgIdx] |
| 2241 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2242 | SuppressUserConversions, ForceRValue, |
| 2243 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2245 | == ImplicitConversionSequence::BadConversion) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2246 | // 13.3.3.1-p10 If several different sequences of conversions exist that |
| 2247 | // each convert the argument to the parameter type, the implicit conversion |
| 2248 | // sequence associated with the parameter is defined to be the unique conversion |
| 2249 | // sequence designated the ambiguous conversion sequence. For the purpose of |
| 2250 | // ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous |
| 2251 | // conversion sequence is treated as a user-defined sequence that is |
| 2252 | // indistinguishable from any other user-defined conversion sequence |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2253 | if (!Candidate.Conversions[ArgIdx].ConversionFunctionSet.empty()) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2254 | Candidate.Conversions[ArgIdx].ConversionKind = |
| 2255 | ImplicitConversionSequence::UserDefinedConversion; |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2256 | // Set the conversion function to one of them. As due to ambiguity, |
| 2257 | // they carry the same weight and is needed for overload resolution |
| 2258 | // later. |
| 2259 | Candidate.Conversions[ArgIdx].UserDefined.ConversionFunction = |
| 2260 | Candidate.Conversions[ArgIdx].ConversionFunctionSet[0]; |
| 2261 | } |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2262 | else { |
| 2263 | Candidate.Viable = false; |
| 2264 | break; |
| 2265 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2266 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2267 | } else { |
| 2268 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2269 | // argument for which there is no corresponding parameter is |
| 2270 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2271 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2272 | = ImplicitConversionSequence::EllipsisConversion; |
| 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2277 | /// \brief Add all of the function declarations in the given function set to |
| 2278 | /// the overload canddiate set. |
| 2279 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2280 | Expr **Args, unsigned NumArgs, |
| 2281 | OverloadCandidateSet& CandidateSet, |
| 2282 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2284 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2285 | F != FEnd; ++F) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2286 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2287 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2288 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
| 2289 | Args[0], Args + 1, NumArgs - 1, |
| 2290 | CandidateSet, SuppressUserConversions); |
| 2291 | else |
| 2292 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2293 | SuppressUserConversions); |
| 2294 | } else { |
| 2295 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2296 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2297 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2298 | AddMethodTemplateCandidate(FunTmpl, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2299 | /*FIXME: explicit args */false, 0, 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2300 | Args[0], Args + 1, NumArgs - 1, |
| 2301 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2302 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2303 | else |
| 2304 | AddTemplateOverloadCandidate(FunTmpl, |
| 2305 | /*FIXME: explicit args */false, 0, 0, |
| 2306 | Args, NumArgs, CandidateSet, |
| 2307 | SuppressUserConversions); |
| 2308 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2309 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2312 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2313 | /// of candidate functions, using the given function call arguments |
| 2314 | /// and the object argument (@c Object). For example, in a call |
| 2315 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2316 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2317 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2318 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2319 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2320 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | void |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2322 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2323 | Expr **Args, unsigned NumArgs, |
| 2324 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | bool SuppressUserConversions, bool ForceRValue) { |
| 2326 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2327 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2328 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2329 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2330 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2331 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2332 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2333 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2334 | if (!CandidateSet.isNewCandidate(Method)) |
| 2335 | return; |
| 2336 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2337 | // Add this candidate |
| 2338 | CandidateSet.push_back(OverloadCandidate()); |
| 2339 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2340 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2341 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2342 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2343 | |
| 2344 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2345 | |
| 2346 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2347 | // parameters is viable only if it has an ellipsis in its parameter |
| 2348 | // list (8.3.5). |
| 2349 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2350 | Candidate.Viable = false; |
| 2351 | return; |
| 2352 | } |
| 2353 | |
| 2354 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2355 | // is viable only if the (m+1)st parameter has a default argument |
| 2356 | // (8.3.6). For the purposes of overload resolution, the |
| 2357 | // parameter list is truncated on the right, so that there are |
| 2358 | // exactly m parameters. |
| 2359 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2360 | if (NumArgs < MinRequiredArgs) { |
| 2361 | // Not enough arguments. |
| 2362 | Candidate.Viable = false; |
| 2363 | return; |
| 2364 | } |
| 2365 | |
| 2366 | Candidate.Viable = true; |
| 2367 | Candidate.Conversions.resize(NumArgs + 1); |
| 2368 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2369 | if (Method->isStatic() || !Object) |
| 2370 | // The implicit object argument is ignored. |
| 2371 | Candidate.IgnoreObjectArgument = true; |
| 2372 | else { |
| 2373 | // Determine the implicit conversion sequence for the object |
| 2374 | // parameter. |
| 2375 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2377 | == ImplicitConversionSequence::BadConversion) { |
| 2378 | Candidate.Viable = false; |
| 2379 | return; |
| 2380 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | // Determine the implicit conversion sequences for each of the |
| 2384 | // arguments. |
| 2385 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2386 | if (ArgIdx < NumArgsInProto) { |
| 2387 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2388 | // exist for each argument an implicit conversion sequence |
| 2389 | // (13.3.3.1) that converts that argument to the corresponding |
| 2390 | // parameter of F. |
| 2391 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | Candidate.Conversions[ArgIdx + 1] |
| 2393 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2394 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2395 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2397 | == ImplicitConversionSequence::BadConversion) { |
| 2398 | Candidate.Viable = false; |
| 2399 | break; |
| 2400 | } |
| 2401 | } else { |
| 2402 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2403 | // argument for which there is no corresponding parameter is |
| 2404 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2406 | = ImplicitConversionSequence::EllipsisConversion; |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2411 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2412 | /// set, using template argument deduction to produce an appropriate member |
| 2413 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2414 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2415 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2416 | bool HasExplicitTemplateArgs, |
| 2417 | const TemplateArgument *ExplicitTemplateArgs, |
| 2418 | unsigned NumExplicitTemplateArgs, |
| 2419 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2420 | OverloadCandidateSet& CandidateSet, |
| 2421 | bool SuppressUserConversions, |
| 2422 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2423 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2424 | return; |
| 2425 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2426 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2427 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2428 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2430 | // candidate functions in the usual way.113) A given name can refer to one |
| 2431 | // or more function templates and also to a set of overloaded non-template |
| 2432 | // functions. In such a case, the candidate functions generated from each |
| 2433 | // function template are combined with the set of non-template candidate |
| 2434 | // functions. |
| 2435 | TemplateDeductionInfo Info(Context); |
| 2436 | FunctionDecl *Specialization = 0; |
| 2437 | if (TemplateDeductionResult Result |
| 2438 | = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs, |
| 2439 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2440 | Args, NumArgs, Specialization, Info)) { |
| 2441 | // FIXME: Record what happened with template argument deduction, so |
| 2442 | // that we can give the user a beautiful diagnostic. |
| 2443 | (void)Result; |
| 2444 | return; |
| 2445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2447 | // Add the function template specialization produced by template argument |
| 2448 | // deduction as a candidate. |
| 2449 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2451 | "Specialization is not a member function?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2453 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2454 | } |
| 2455 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2456 | /// \brief Add a C++ function template specialization as a candidate |
| 2457 | /// in the candidate set, using template argument deduction to produce |
| 2458 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2460 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2461 | bool HasExplicitTemplateArgs, |
| 2462 | const TemplateArgument *ExplicitTemplateArgs, |
| 2463 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2464 | Expr **Args, unsigned NumArgs, |
| 2465 | OverloadCandidateSet& CandidateSet, |
| 2466 | bool SuppressUserConversions, |
| 2467 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2468 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2469 | return; |
| 2470 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2471 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2473 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2475 | // candidate functions in the usual way.113) A given name can refer to one |
| 2476 | // or more function templates and also to a set of overloaded non-template |
| 2477 | // functions. In such a case, the candidate functions generated from each |
| 2478 | // function template are combined with the set of non-template candidate |
| 2479 | // functions. |
| 2480 | TemplateDeductionInfo Info(Context); |
| 2481 | FunctionDecl *Specialization = 0; |
| 2482 | if (TemplateDeductionResult Result |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2483 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2484 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2485 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2486 | // FIXME: Record what happened with template argument deduction, so |
| 2487 | // that we can give the user a beautiful diagnostic. |
| 2488 | (void)Result; |
| 2489 | return; |
| 2490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2491 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2492 | // Add the function template specialization produced by template argument |
| 2493 | // deduction as a candidate. |
| 2494 | assert(Specialization && "Missing function template specialization?"); |
| 2495 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2496 | SuppressUserConversions, ForceRValue); |
| 2497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2499 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2501 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2502 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2503 | /// (which may or may not be the same type as the type that the |
| 2504 | /// conversion function produces). |
| 2505 | void |
| 2506 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2507 | Expr *From, QualType ToType, |
| 2508 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2509 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2510 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2511 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2512 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2513 | return; |
| 2514 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2515 | // Add this candidate |
| 2516 | CandidateSet.push_back(OverloadCandidate()); |
| 2517 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2518 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2519 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2520 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2521 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2522 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2523 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2524 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2525 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2526 | // Determine the implicit conversion sequence for the implicit |
| 2527 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2528 | Candidate.Viable = true; |
| 2529 | Candidate.Conversions.resize(1); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2530 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2531 | // Conversion functions to a different type in the base class is visible in |
| 2532 | // the derived class. So, a derived to base conversion should not participate |
| 2533 | // in overload resolution. |
| 2534 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2535 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2536 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2537 | == ImplicitConversionSequence::BadConversion) { |
| 2538 | Candidate.Viable = false; |
| 2539 | return; |
| 2540 | } |
| 2541 | |
| 2542 | // To determine what the conversion from the result of calling the |
| 2543 | // conversion function to the type we're eventually trying to |
| 2544 | // convert to (ToType), we need to synthesize a call to the |
| 2545 | // conversion function and attempt copy initialization from it. This |
| 2546 | // makes sure that we get the right semantics with respect to |
| 2547 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2548 | // call on the stack and we don't need its arguments to be |
| 2549 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2551 | SourceLocation()); |
| 2552 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2553 | CastExpr::CK_Unknown, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2554 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2557 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2558 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2560 | Conversion->getConversionType().getNonReferenceType(), |
| 2561 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | ImplicitConversionSequence ICS = |
| 2563 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2564 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2565 | /*ForceRValue=*/false, |
| 2566 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2568 | switch (ICS.ConversionKind) { |
| 2569 | case ImplicitConversionSequence::StandardConversion: |
| 2570 | Candidate.FinalConversion = ICS.Standard; |
| 2571 | break; |
| 2572 | |
| 2573 | case ImplicitConversionSequence::BadConversion: |
| 2574 | Candidate.Viable = false; |
| 2575 | break; |
| 2576 | |
| 2577 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2579 | "Can only end up with a standard conversion sequence or failure"); |
| 2580 | } |
| 2581 | } |
| 2582 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2583 | /// \brief Adds a conversion function template specialization |
| 2584 | /// candidate to the overload set, using template argument deduction |
| 2585 | /// to deduce the template arguments of the conversion function |
| 2586 | /// template from the type that we are converting to (C++ |
| 2587 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2589 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2590 | Expr *From, QualType ToType, |
| 2591 | OverloadCandidateSet &CandidateSet) { |
| 2592 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2593 | "Only conversion function templates permitted here"); |
| 2594 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2595 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2596 | return; |
| 2597 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2598 | TemplateDeductionInfo Info(Context); |
| 2599 | CXXConversionDecl *Specialization = 0; |
| 2600 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2602 | Specialization, Info)) { |
| 2603 | // FIXME: Record what happened with template argument deduction, so |
| 2604 | // that we can give the user a beautiful diagnostic. |
| 2605 | (void)Result; |
| 2606 | return; |
| 2607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2609 | // Add the conversion function template specialization produced by |
| 2610 | // template argument deduction as a candidate. |
| 2611 | assert(Specialization && "Missing function template specialization?"); |
| 2612 | AddConversionCandidate(Specialization, From, ToType, CandidateSet); |
| 2613 | } |
| 2614 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2615 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2616 | /// converts the given @c Object to a function pointer via the |
| 2617 | /// conversion function @c Conversion, and then attempts to call it |
| 2618 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2619 | /// the type of function that we'll eventually be calling. |
| 2620 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2621 | const FunctionProtoType *Proto, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2622 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2623 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2624 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2625 | return; |
| 2626 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2627 | CandidateSet.push_back(OverloadCandidate()); |
| 2628 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2629 | Candidate.Function = 0; |
| 2630 | Candidate.Surrogate = Conversion; |
| 2631 | Candidate.Viable = true; |
| 2632 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2633 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2634 | Candidate.Conversions.resize(NumArgs + 1); |
| 2635 | |
| 2636 | // Determine the implicit conversion sequence for the implicit |
| 2637 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | ImplicitConversionSequence ObjectInit |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2639 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2640 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2641 | Candidate.Viable = false; |
| 2642 | return; |
| 2643 | } |
| 2644 | |
| 2645 | // The first conversion is actually a user-defined conversion whose |
| 2646 | // first conversion is ObjectInit's standard conversion (which is |
| 2647 | // effectively a reference binding). Record it as such. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2649 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2650 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2651 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2653 | = Candidate.Conversions[0].UserDefined.Before; |
| 2654 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2655 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2657 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2658 | |
| 2659 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2660 | // parameters is viable only if it has an ellipsis in its parameter |
| 2661 | // list (8.3.5). |
| 2662 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2663 | Candidate.Viable = false; |
| 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | // Function types don't have any default arguments, so just check if |
| 2668 | // we have enough arguments. |
| 2669 | if (NumArgs < NumArgsInProto) { |
| 2670 | // Not enough arguments. |
| 2671 | Candidate.Viable = false; |
| 2672 | return; |
| 2673 | } |
| 2674 | |
| 2675 | // Determine the implicit conversion sequences for each of the |
| 2676 | // arguments. |
| 2677 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2678 | if (ArgIdx < NumArgsInProto) { |
| 2679 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2680 | // exist for each argument an implicit conversion sequence |
| 2681 | // (13.3.3.1) that converts that argument to the corresponding |
| 2682 | // parameter of F. |
| 2683 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | Candidate.Conversions[ArgIdx + 1] |
| 2685 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2686 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2687 | /*ForceRValue=*/false, |
| 2688 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2690 | == ImplicitConversionSequence::BadConversion) { |
| 2691 | Candidate.Viable = false; |
| 2692 | break; |
| 2693 | } |
| 2694 | } else { |
| 2695 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2696 | // argument for which there is no corresponding parameter is |
| 2697 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2699 | = ImplicitConversionSequence::EllipsisConversion; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2704 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2705 | // operator overloading logic over to the scheme used by binary operators, which |
| 2706 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2707 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2708 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2709 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2710 | OverloadCandidateSet& CandidateSet, |
| 2711 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2712 | FunctionSet Functions; |
| 2713 | |
| 2714 | QualType T1 = Args[0]->getType(); |
| 2715 | QualType T2; |
| 2716 | if (NumArgs > 1) |
| 2717 | T2 = Args[1]->getType(); |
| 2718 | |
| 2719 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2720 | if (S) |
| 2721 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2722 | ArgumentDependentLookup(OpName, Args, NumArgs, Functions); |
| 2723 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2724 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 2725 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
| 2726 | } |
| 2727 | |
| 2728 | /// \brief Add overload candidates for overloaded operators that are |
| 2729 | /// member functions. |
| 2730 | /// |
| 2731 | /// Add the overloaded operator candidates that are member functions |
| 2732 | /// for the operator Op that was used in an operator expression such |
| 2733 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2734 | /// CandidateSet will store the added overload candidates. (C++ |
| 2735 | /// [over.match.oper]). |
| 2736 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2737 | SourceLocation OpLoc, |
| 2738 | Expr **Args, unsigned NumArgs, |
| 2739 | OverloadCandidateSet& CandidateSet, |
| 2740 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2741 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2742 | |
| 2743 | // C++ [over.match.oper]p3: |
| 2744 | // For a unary operator @ with an operand of a type whose |
| 2745 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2746 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2747 | // a right operand of a type whose cv-unqualified version is T2, |
| 2748 | // three sets of candidate functions, designated member |
| 2749 | // candidates, non-member candidates and built-in candidates, are |
| 2750 | // constructed as follows: |
| 2751 | QualType T1 = Args[0]->getType(); |
| 2752 | QualType T2; |
| 2753 | if (NumArgs > 1) |
| 2754 | T2 = Args[1]->getType(); |
| 2755 | |
| 2756 | // -- If T1 is a class type, the set of member candidates is the |
| 2757 | // result of the qualified lookup of T1::operator@ |
| 2758 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2759 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2760 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2761 | // Complete the type if it can be completed. Otherwise, we're done. |
| 2762 | if (RequireCompleteType(OpLoc, T1, PartialDiagnostic(0))) |
| 2763 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
| 2765 | LookupResult Operators = LookupQualifiedName(T1Rec->getDecl(), OpName, |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2766 | LookupOrdinaryName, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2768 | OperEnd = Operators.end(); |
| 2769 | Oper != OperEnd; |
| 2770 | ++Oper) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2772 | Args+1, NumArgs - 1, CandidateSet, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2773 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2774 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2777 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2778 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2779 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2780 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2781 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2782 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2783 | /// (at the beginning of the argument list) that will be contextually |
| 2784 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2785 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2786 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2787 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2788 | bool IsAssignmentOperator, |
| 2789 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2790 | // Add this candidate |
| 2791 | CandidateSet.push_back(OverloadCandidate()); |
| 2792 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2793 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2794 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2795 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2796 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2797 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2798 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2799 | |
| 2800 | // Determine the implicit conversion sequences for each of the |
| 2801 | // arguments. |
| 2802 | Candidate.Viable = true; |
| 2803 | Candidate.Conversions.resize(NumArgs); |
| 2804 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2805 | // C++ [over.match.oper]p4: |
| 2806 | // For the built-in assignment operators, conversions of the |
| 2807 | // left operand are restricted as follows: |
| 2808 | // -- no temporaries are introduced to hold the left operand, and |
| 2809 | // -- no user-defined conversions are applied to the left |
| 2810 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2812 | // |
| 2813 | // We block these conversions by turning off user-defined |
| 2814 | // conversions, since that is the only way that initialization of |
| 2815 | // a reference to a non-class type can occur from something that |
| 2816 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2817 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2819 | "Contextual conversion to bool requires bool type"); |
| 2820 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2821 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | Candidate.Conversions[ArgIdx] |
| 2823 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2824 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2825 | /*ForceRValue=*/false, |
| 2826 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2827 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2829 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2830 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2831 | break; |
| 2832 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2837 | /// candidate operator functions for built-in operators (C++ |
| 2838 | /// [over.built]). The types are separated into pointer types and |
| 2839 | /// enumeration types. |
| 2840 | class BuiltinCandidateTypeSet { |
| 2841 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2842 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2843 | |
| 2844 | /// PointerTypes - The set of pointer types that will be used in the |
| 2845 | /// built-in candidates. |
| 2846 | TypeSet PointerTypes; |
| 2847 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2848 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2849 | /// used in the built-in candidates. |
| 2850 | TypeSet MemberPointerTypes; |
| 2851 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2852 | /// EnumerationTypes - The set of enumeration types that will be |
| 2853 | /// used in the built-in candidates. |
| 2854 | TypeSet EnumerationTypes; |
| 2855 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2856 | /// Sema - The semantic analysis instance where we are building the |
| 2857 | /// candidate type set. |
| 2858 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2860 | /// Context - The AST context in which we will build the type sets. |
| 2861 | ASTContext &Context; |
| 2862 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2863 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| 2864 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2865 | |
| 2866 | public: |
| 2867 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2868 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2869 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2871 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2872 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2873 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions, |
| 2874 | bool AllowExplicitConversions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2875 | |
| 2876 | /// pointer_begin - First pointer type found; |
| 2877 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2878 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2879 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2880 | iterator pointer_end() { return PointerTypes.end(); } |
| 2881 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2882 | /// member_pointer_begin - First member pointer type found; |
| 2883 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2884 | |
| 2885 | /// member_pointer_end - Past the last member pointer type found; |
| 2886 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2887 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2888 | /// enumeration_begin - First enumeration type found; |
| 2889 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2890 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2891 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2892 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2893 | }; |
| 2894 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2895 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2896 | /// the set of pointer types along with any more-qualified variants of |
| 2897 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2898 | /// will add "int const *", "int const volatile *", "int const |
| 2899 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2900 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2901 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2902 | /// |
| 2903 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2904 | bool |
| 2905 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2906 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2907 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2908 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2909 | return false; |
| 2910 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2911 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 2912 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2913 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2914 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2915 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 2916 | |
| 2917 | // Iterate through all strict supersets of BaseCVR. |
| 2918 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 2919 | if ((CVR | BaseCVR) != CVR) continue; |
| 2920 | |
| 2921 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 2922 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | return true; |
| 2926 | } |
| 2927 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2928 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 2929 | /// to the set of pointer types along with any more-qualified variants of |
| 2930 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2931 | /// will add "int const *", "int const volatile *", "int const |
| 2932 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2933 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2934 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2935 | /// |
| 2936 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2937 | bool |
| 2938 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 2939 | QualType Ty) { |
| 2940 | // Insert this type. |
| 2941 | if (!MemberPointerTypes.insert(Ty)) |
| 2942 | return false; |
| 2943 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2944 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 2945 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2946 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2947 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2948 | const Type *ClassTy = PointerTy->getClass(); |
| 2949 | |
| 2950 | // Iterate through all strict supersets of the pointee type's CVR |
| 2951 | // qualifiers. |
| 2952 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 2953 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 2954 | if ((CVR | BaseCVR) != CVR) continue; |
| 2955 | |
| 2956 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 2957 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | return true; |
| 2961 | } |
| 2962 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2963 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2964 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2965 | /// primarily interested in pointer types and enumeration types. We also |
| 2966 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2967 | /// AllowUserConversions is true if we should look at the conversion |
| 2968 | /// functions of a class type, and AllowExplicitConversions if we |
| 2969 | /// should also include the explicit conversion functions of a class |
| 2970 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2972 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2973 | bool AllowUserConversions, |
| 2974 | bool AllowExplicitConversions) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2975 | // Only deal with canonical types. |
| 2976 | Ty = Context.getCanonicalType(Ty); |
| 2977 | |
| 2978 | // Look through reference types; they aren't part of the type of an |
| 2979 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2980 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2981 | Ty = RefTy->getPointeeType(); |
| 2982 | |
| 2983 | // We don't care about qualifiers on the type. |
| 2984 | Ty = Ty.getUnqualifiedType(); |
| 2985 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2986 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2987 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2988 | |
| 2989 | // Insert our type, and its more-qualified variants, into the set |
| 2990 | // of types. |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2991 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2992 | return; |
| 2993 | |
| 2994 | // Add 'cv void*' to our set of types. |
| 2995 | if (!Ty->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2996 | QualType QualVoid |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2997 | = Context.getCVRQualifiedType(Context.VoidTy, |
| 2998 | PointeeTy.getCVRQualifiers()); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2999 | AddPointerWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | // If this is a pointer to a class type, add pointers to its bases |
| 3003 | // (with the same level of cv-qualification as the original |
| 3004 | // derived class, of course). |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3005 | if (const RecordType *PointeeRec = PointeeTy->getAs<RecordType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3006 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 3007 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 3008 | Base != ClassDecl->bases_end(); ++Base) { |
| 3009 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3010 | BaseTy = Context.getCVRQualifiedType(BaseTy.getUnqualifiedType(), |
| 3011 | PointeeTy.getCVRQualifiers()); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3012 | |
| 3013 | // Add the pointer type, recursively, so that we get all of |
| 3014 | // the indirect base classes, too. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3015 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3016 | } |
| 3017 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3018 | } else if (Ty->isMemberPointerType()) { |
| 3019 | // Member pointers are far easier, since the pointee can't be converted. |
| 3020 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3021 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3022 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3023 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3024 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3025 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3026 | if (SemaRef.RequireCompleteType(SourceLocation(), Ty, 0)) { |
| 3027 | // No conversion functions in incomplete types. |
| 3028 | return; |
| 3029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3030 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3031 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3033 | = ClassDecl->getVisibleConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3035 | = Conversions->function_begin(); |
| 3036 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3037 | CXXConversionDecl *Conv; |
| 3038 | FunctionTemplateDecl *ConvTemplate; |
| 3039 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 3040 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3041 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3042 | // about which builtin types we can convert to. |
| 3043 | if (ConvTemplate) |
| 3044 | continue; |
| 3045 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3046 | if (AllowExplicitConversions || !Conv->isExplicit()) |
| 3047 | AddTypesConvertedFrom(Conv->getConversionType(), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3053 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3054 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3055 | /// given type to the candidate set. |
| 3056 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3057 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3059 | unsigned NumArgs, |
| 3060 | OverloadCandidateSet &CandidateSet) { |
| 3061 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3063 | // T& operator=(T&, T) |
| 3064 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3065 | ParamTypes[1] = T; |
| 3066 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3067 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3069 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3070 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3071 | ParamTypes[0] |
| 3072 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3073 | ParamTypes[1] = T; |
| 3074 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3076 | } |
| 3077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3079 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3080 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3081 | /// on the operator @p Op and the arguments given. For example, if the |
| 3082 | /// operator is a binary '+', this routine might add "int |
| 3083 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3084 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3086 | Expr **Args, unsigned NumArgs, |
| 3087 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3088 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3089 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3090 | // that the first few of these types are the promoted integral |
| 3091 | // types; these types need to be first. |
| 3092 | // FIXME: What about complex? |
| 3093 | const unsigned FirstIntegralType = 0; |
| 3094 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3096 | LastPromotedIntegralType = 13; |
| 3097 | const unsigned FirstPromotedArithmeticType = 7, |
| 3098 | LastPromotedArithmeticType = 16; |
| 3099 | const unsigned NumArithmeticTypes = 16; |
| 3100 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3102 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3103 | Context.SignedCharTy, Context.ShortTy, |
| 3104 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3105 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3106 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3107 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3108 | }; |
| 3109 | |
| 3110 | // Find all of the types that the arguments can convert to, but only |
| 3111 | // if the operator we're looking at has built-in operator candidates |
| 3112 | // that make use of these types. |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3113 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3114 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3115 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3116 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3117 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3118 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3119 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3120 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3121 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 3122 | true, |
| 3123 | (Op == OO_Exclaim || |
| 3124 | Op == OO_AmpAmp || |
| 3125 | Op == OO_PipePipe)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | bool isComparison = false; |
| 3129 | switch (Op) { |
| 3130 | case OO_None: |
| 3131 | case NUM_OVERLOADED_OPERATORS: |
| 3132 | assert(false && "Expected an overloaded operator"); |
| 3133 | break; |
| 3134 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3135 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3137 | goto UnaryStar; |
| 3138 | else |
| 3139 | goto BinaryStar; |
| 3140 | break; |
| 3141 | |
| 3142 | case OO_Plus: // '+' is either unary or binary |
| 3143 | if (NumArgs == 1) |
| 3144 | goto UnaryPlus; |
| 3145 | else |
| 3146 | goto BinaryPlus; |
| 3147 | break; |
| 3148 | |
| 3149 | case OO_Minus: // '-' is either unary or binary |
| 3150 | if (NumArgs == 1) |
| 3151 | goto UnaryMinus; |
| 3152 | else |
| 3153 | goto BinaryMinus; |
| 3154 | break; |
| 3155 | |
| 3156 | case OO_Amp: // '&' is either unary or binary |
| 3157 | if (NumArgs == 1) |
| 3158 | goto UnaryAmp; |
| 3159 | else |
| 3160 | goto BinaryAmp; |
| 3161 | |
| 3162 | case OO_PlusPlus: |
| 3163 | case OO_MinusMinus: |
| 3164 | // C++ [over.built]p3: |
| 3165 | // |
| 3166 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3167 | // is either volatile or empty, there exist candidate operator |
| 3168 | // functions of the form |
| 3169 | // |
| 3170 | // VQ T& operator++(VQ T&); |
| 3171 | // T operator++(VQ T&, int); |
| 3172 | // |
| 3173 | // C++ [over.built]p4: |
| 3174 | // |
| 3175 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3176 | // than bool, and VQ is either volatile or empty, there exist |
| 3177 | // candidate operator functions of the form |
| 3178 | // |
| 3179 | // VQ T& operator--(VQ T&); |
| 3180 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3182 | Arith < NumArithmeticTypes; ++Arith) { |
| 3183 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3185 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3186 | |
| 3187 | // Non-volatile version. |
| 3188 | if (NumArgs == 1) |
| 3189 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3190 | else |
| 3191 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3192 | |
| 3193 | // Volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3194 | ParamTypes[0] |
| 3195 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3196 | if (NumArgs == 1) |
| 3197 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3198 | else |
| 3199 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3200 | } |
| 3201 | |
| 3202 | // C++ [over.built]p5: |
| 3203 | // |
| 3204 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3205 | // cv-unqualified object type, and VQ is either volatile or |
| 3206 | // empty, there exist candidate operator functions of the form |
| 3207 | // |
| 3208 | // T*VQ& operator++(T*VQ&); |
| 3209 | // T*VQ& operator--(T*VQ&); |
| 3210 | // T* operator++(T*VQ&, int); |
| 3211 | // T* operator--(T*VQ&, int); |
| 3212 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3213 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3214 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3215 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3216 | continue; |
| 3217 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | QualType ParamTypes[2] = { |
| 3219 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3220 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3222 | // Without volatile |
| 3223 | if (NumArgs == 1) |
| 3224 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3225 | else |
| 3226 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3227 | |
| 3228 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3229 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3230 | ParamTypes[0] |
| 3231 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3232 | if (NumArgs == 1) |
| 3233 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3234 | else |
| 3235 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3236 | } |
| 3237 | } |
| 3238 | break; |
| 3239 | |
| 3240 | UnaryStar: |
| 3241 | // C++ [over.built]p6: |
| 3242 | // For every cv-qualified or cv-unqualified object type T, there |
| 3243 | // exist candidate operator functions of the form |
| 3244 | // |
| 3245 | // T& operator*(T*); |
| 3246 | // |
| 3247 | // C++ [over.built]p7: |
| 3248 | // For every function type T, there exist candidate operator |
| 3249 | // functions of the form |
| 3250 | // T& operator*(T*); |
| 3251 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3252 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3253 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3254 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3256 | &ParamTy, Args, 1, CandidateSet); |
| 3257 | } |
| 3258 | break; |
| 3259 | |
| 3260 | UnaryPlus: |
| 3261 | // C++ [over.built]p8: |
| 3262 | // For every type T, there exist candidate operator functions of |
| 3263 | // the form |
| 3264 | // |
| 3265 | // T* operator+(T*); |
| 3266 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3267 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3268 | QualType ParamTy = *Ptr; |
| 3269 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3272 | // Fall through |
| 3273 | |
| 3274 | UnaryMinus: |
| 3275 | // C++ [over.built]p9: |
| 3276 | // For every promoted arithmetic type T, there exist candidate |
| 3277 | // operator functions of the form |
| 3278 | // |
| 3279 | // T operator+(T); |
| 3280 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3282 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3283 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3284 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3285 | } |
| 3286 | break; |
| 3287 | |
| 3288 | case OO_Tilde: |
| 3289 | // C++ [over.built]p10: |
| 3290 | // For every promoted integral type T, there exist candidate |
| 3291 | // operator functions of the form |
| 3292 | // |
| 3293 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3294 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3295 | Int < LastPromotedIntegralType; ++Int) { |
| 3296 | QualType IntTy = ArithmeticTypes[Int]; |
| 3297 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3298 | } |
| 3299 | break; |
| 3300 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3301 | case OO_New: |
| 3302 | case OO_Delete: |
| 3303 | case OO_Array_New: |
| 3304 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3305 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3306 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3307 | break; |
| 3308 | |
| 3309 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3310 | UnaryAmp: |
| 3311 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3312 | // C++ [over.match.oper]p3: |
| 3313 | // -- For the operator ',', the unary operator '&', or the |
| 3314 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3315 | break; |
| 3316 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3317 | case OO_EqualEqual: |
| 3318 | case OO_ExclaimEqual: |
| 3319 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | // For every pointer to member type T, there exist candidate operator |
| 3321 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3322 | // |
| 3323 | // bool operator==(T,T); |
| 3324 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3326 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3327 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3328 | MemPtr != MemPtrEnd; |
| 3329 | ++MemPtr) { |
| 3330 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3331 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3334 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3336 | case OO_Less: |
| 3337 | case OO_Greater: |
| 3338 | case OO_LessEqual: |
| 3339 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3340 | // C++ [over.built]p15: |
| 3341 | // |
| 3342 | // For every pointer or enumeration type T, there exist |
| 3343 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3344 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3345 | // bool operator<(T, T); |
| 3346 | // bool operator>(T, T); |
| 3347 | // bool operator<=(T, T); |
| 3348 | // bool operator>=(T, T); |
| 3349 | // bool operator==(T, T); |
| 3350 | // bool operator!=(T, T); |
| 3351 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3352 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3353 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3354 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3355 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3357 | = CandidateTypes.enumeration_begin(); |
| 3358 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3359 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3360 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3361 | } |
| 3362 | |
| 3363 | // Fall through. |
| 3364 | isComparison = true; |
| 3365 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3366 | BinaryPlus: |
| 3367 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3368 | if (!isComparison) { |
| 3369 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3370 | |
| 3371 | // C++ [over.built]p13: |
| 3372 | // |
| 3373 | // For every cv-qualified or cv-unqualified object type T |
| 3374 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3375 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3376 | // T* operator+(T*, ptrdiff_t); |
| 3377 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3378 | // T* operator-(T*, ptrdiff_t); |
| 3379 | // T* operator+(ptrdiff_t, T*); |
| 3380 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3381 | // |
| 3382 | // C++ [over.built]p14: |
| 3383 | // |
| 3384 | // For every T, where T is a pointer to object type, there |
| 3385 | // exist candidate operator functions of the form |
| 3386 | // |
| 3387 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3389 | = CandidateTypes.pointer_begin(); |
| 3390 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3391 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3392 | |
| 3393 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3394 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3395 | |
| 3396 | if (Op == OO_Plus) { |
| 3397 | // T* operator+(ptrdiff_t, T*); |
| 3398 | ParamTypes[0] = ParamTypes[1]; |
| 3399 | ParamTypes[1] = *Ptr; |
| 3400 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3401 | } else { |
| 3402 | // ptrdiff_t operator-(T, T); |
| 3403 | ParamTypes[1] = *Ptr; |
| 3404 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3405 | Args, 2, CandidateSet); |
| 3406 | } |
| 3407 | } |
| 3408 | } |
| 3409 | // Fall through |
| 3410 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3411 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3412 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3413 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3414 | // C++ [over.built]p12: |
| 3415 | // |
| 3416 | // For every pair of promoted arithmetic types L and R, there |
| 3417 | // exist candidate operator functions of the form |
| 3418 | // |
| 3419 | // LR operator*(L, R); |
| 3420 | // LR operator/(L, R); |
| 3421 | // LR operator+(L, R); |
| 3422 | // LR operator-(L, R); |
| 3423 | // bool operator<(L, R); |
| 3424 | // bool operator>(L, R); |
| 3425 | // bool operator<=(L, R); |
| 3426 | // bool operator>=(L, R); |
| 3427 | // bool operator==(L, R); |
| 3428 | // bool operator!=(L, R); |
| 3429 | // |
| 3430 | // where LR is the result of the usual arithmetic conversions |
| 3431 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3432 | // |
| 3433 | // C++ [over.built]p24: |
| 3434 | // |
| 3435 | // For every pair of promoted arithmetic types L and R, there exist |
| 3436 | // candidate operator functions of the form |
| 3437 | // |
| 3438 | // LR operator?(bool, L, R); |
| 3439 | // |
| 3440 | // where LR is the result of the usual arithmetic conversions |
| 3441 | // between types L and R. |
| 3442 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3444 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3445 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3446 | Right < LastPromotedArithmeticType; ++Right) { |
| 3447 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3448 | QualType Result |
| 3449 | = isComparison |
| 3450 | ? Context.BoolTy |
| 3451 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3452 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3453 | } |
| 3454 | } |
| 3455 | break; |
| 3456 | |
| 3457 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3458 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3459 | case OO_Caret: |
| 3460 | case OO_Pipe: |
| 3461 | case OO_LessLess: |
| 3462 | case OO_GreaterGreater: |
| 3463 | // C++ [over.built]p17: |
| 3464 | // |
| 3465 | // For every pair of promoted integral types L and R, there |
| 3466 | // exist candidate operator functions of the form |
| 3467 | // |
| 3468 | // LR operator%(L, R); |
| 3469 | // LR operator&(L, R); |
| 3470 | // LR operator^(L, R); |
| 3471 | // LR operator|(L, R); |
| 3472 | // L operator<<(L, R); |
| 3473 | // L operator>>(L, R); |
| 3474 | // |
| 3475 | // where LR is the result of the usual arithmetic conversions |
| 3476 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3478 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3480 | Right < LastPromotedIntegralType; ++Right) { |
| 3481 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3482 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3483 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3484 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3485 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3486 | } |
| 3487 | } |
| 3488 | break; |
| 3489 | |
| 3490 | case OO_Equal: |
| 3491 | // C++ [over.built]p20: |
| 3492 | // |
| 3493 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3494 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3495 | // empty, there exist candidate operator functions of the form |
| 3496 | // |
| 3497 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3498 | for (BuiltinCandidateTypeSet::iterator |
| 3499 | Enum = CandidateTypes.enumeration_begin(), |
| 3500 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3501 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3502 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3503 | CandidateSet); |
| 3504 | for (BuiltinCandidateTypeSet::iterator |
| 3505 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3506 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3507 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3509 | CandidateSet); |
| 3510 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3511 | |
| 3512 | case OO_PlusEqual: |
| 3513 | case OO_MinusEqual: |
| 3514 | // C++ [over.built]p19: |
| 3515 | // |
| 3516 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3517 | // volatile or empty, there exist candidate operator functions |
| 3518 | // of the form |
| 3519 | // |
| 3520 | // T*VQ& operator=(T*VQ&, T*); |
| 3521 | // |
| 3522 | // C++ [over.built]p21: |
| 3523 | // |
| 3524 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3525 | // cv-unqualified object type and VQ is either volatile or |
| 3526 | // empty, there exist candidate operator functions of the form |
| 3527 | // |
| 3528 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3529 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3530 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3531 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3532 | QualType ParamTypes[2]; |
| 3533 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3534 | |
| 3535 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3536 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3537 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3538 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3539 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3540 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3541 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3542 | ParamTypes[0] |
| 3543 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3544 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3545 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3546 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3547 | } |
| 3548 | // Fall through. |
| 3549 | |
| 3550 | case OO_StarEqual: |
| 3551 | case OO_SlashEqual: |
| 3552 | // C++ [over.built]p18: |
| 3553 | // |
| 3554 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3555 | // VQ is either volatile or empty, and R is a promoted |
| 3556 | // arithmetic type, there exist candidate operator functions of |
| 3557 | // the form |
| 3558 | // |
| 3559 | // VQ L& operator=(VQ L&, R); |
| 3560 | // VQ L& operator*=(VQ L&, R); |
| 3561 | // VQ L& operator/=(VQ L&, R); |
| 3562 | // VQ L& operator+=(VQ L&, R); |
| 3563 | // VQ L& operator-=(VQ L&, R); |
| 3564 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3565 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3566 | Right < LastPromotedArithmeticType; ++Right) { |
| 3567 | QualType ParamTypes[2]; |
| 3568 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3569 | |
| 3570 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3571 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3572 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3573 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3574 | |
| 3575 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3576 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3577 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3578 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3579 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3580 | } |
| 3581 | } |
| 3582 | break; |
| 3583 | |
| 3584 | case OO_PercentEqual: |
| 3585 | case OO_LessLessEqual: |
| 3586 | case OO_GreaterGreaterEqual: |
| 3587 | case OO_AmpEqual: |
| 3588 | case OO_CaretEqual: |
| 3589 | case OO_PipeEqual: |
| 3590 | // C++ [over.built]p22: |
| 3591 | // |
| 3592 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3593 | // is either volatile or empty, and R is a promoted integral |
| 3594 | // type, there exist candidate operator functions of the form |
| 3595 | // |
| 3596 | // VQ L& operator%=(VQ L&, R); |
| 3597 | // VQ L& operator<<=(VQ L&, R); |
| 3598 | // VQ L& operator>>=(VQ L&, R); |
| 3599 | // VQ L& operator&=(VQ L&, R); |
| 3600 | // VQ L& operator^=(VQ L&, R); |
| 3601 | // VQ L& operator|=(VQ L&, R); |
| 3602 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3604 | Right < LastPromotedIntegralType; ++Right) { |
| 3605 | QualType ParamTypes[2]; |
| 3606 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3607 | |
| 3608 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3609 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3610 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3611 | |
| 3612 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3613 | ParamTypes[0] = ArithmeticTypes[Left]; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3614 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3615 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3616 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3617 | } |
| 3618 | } |
| 3619 | break; |
| 3620 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3621 | case OO_Exclaim: { |
| 3622 | // C++ [over.operator]p23: |
| 3623 | // |
| 3624 | // There also exist candidate operator functions of the form |
| 3625 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3627 | // bool operator&&(bool, bool); [BELOW] |
| 3628 | // bool operator||(bool, bool); [BELOW] |
| 3629 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3630 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3631 | /*IsAssignmentOperator=*/false, |
| 3632 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3633 | break; |
| 3634 | } |
| 3635 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3636 | case OO_AmpAmp: |
| 3637 | case OO_PipePipe: { |
| 3638 | // C++ [over.operator]p23: |
| 3639 | // |
| 3640 | // There also exist candidate operator functions of the form |
| 3641 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3642 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3643 | // bool operator&&(bool, bool); |
| 3644 | // bool operator||(bool, bool); |
| 3645 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3646 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3647 | /*IsAssignmentOperator=*/false, |
| 3648 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3649 | break; |
| 3650 | } |
| 3651 | |
| 3652 | case OO_Subscript: |
| 3653 | // C++ [over.built]p13: |
| 3654 | // |
| 3655 | // For every cv-qualified or cv-unqualified object type T there |
| 3656 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3658 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3659 | // T& operator[](T*, ptrdiff_t); |
| 3660 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3661 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3662 | // T& operator[](ptrdiff_t, T*); |
| 3663 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3664 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3665 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3666 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3667 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3668 | |
| 3669 | // T& operator[](T*, ptrdiff_t) |
| 3670 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3671 | |
| 3672 | // T& operator[](ptrdiff_t, T*); |
| 3673 | ParamTypes[0] = ParamTypes[1]; |
| 3674 | ParamTypes[1] = *Ptr; |
| 3675 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3676 | } |
| 3677 | break; |
| 3678 | |
| 3679 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3680 | // C++ [over.built]p11: |
| 3681 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 3682 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 3683 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 3684 | // there exist candidate operator functions of the form |
| 3685 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 3686 | // where CV12 is the union of CV1 and CV2. |
| 3687 | { |
| 3688 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3689 | CandidateTypes.pointer_begin(); |
| 3690 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3691 | QualType C1Ty = (*Ptr); |
| 3692 | QualType C1; |
| 3693 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
| 3694 | C1 = PointerTy->getPointeeType(); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 3695 | C1 = C1.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3696 | if (!isa<RecordType>(C1)) |
| 3697 | continue; |
| 3698 | } |
| 3699 | for (BuiltinCandidateTypeSet::iterator |
| 3700 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3701 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3702 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 3703 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 3704 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 3705 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3706 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 3707 | break; |
| 3708 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 3709 | // build CV12 T& |
| 3710 | QualType T = mptr->getPointeeType(); |
| 3711 | unsigned CV1 = (*Ptr).getCVRQualifiers(); |
| 3712 | unsigned CV2 = T.getCVRQualifiers(); |
| 3713 | T = Context.getCVRQualifiedType(T, (CV1 | CV2)); |
| 3714 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 3715 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3716 | } |
| 3717 | } |
| 3718 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3719 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3720 | |
| 3721 | case OO_Conditional: |
| 3722 | // Note that we don't consider the first argument, since it has been |
| 3723 | // contextually converted to bool long ago. The candidates below are |
| 3724 | // therefore added as binary. |
| 3725 | // |
| 3726 | // C++ [over.built]p24: |
| 3727 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3728 | // there exist candidate operator functions of the form |
| 3729 | // |
| 3730 | // T operator?(bool, T, T); |
| 3731 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3732 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3733 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3734 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3735 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3736 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3737 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3738 | CandidateTypes.member_pointer_begin(), |
| 3739 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3740 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3741 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3742 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3743 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3747 | /// \brief Add function candidates found via argument-dependent lookup |
| 3748 | /// to the set of overloading candidates. |
| 3749 | /// |
| 3750 | /// This routine performs argument-dependent name lookup based on the |
| 3751 | /// given function name (which may also be an operator name) and adds |
| 3752 | /// all of the overload candidates found by ADL to the overload |
| 3753 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3755 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3756 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3757 | bool HasExplicitTemplateArgs, |
| 3758 | const TemplateArgument *ExplicitTemplateArgs, |
| 3759 | unsigned NumExplicitTemplateArgs, |
| 3760 | OverloadCandidateSet& CandidateSet, |
| 3761 | bool PartialOverloading) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3762 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3763 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3764 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 3765 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3766 | // Record all of the function candidates that we've already |
| 3767 | // added to the overload set, so that we don't add those same |
| 3768 | // candidates a second time. |
| 3769 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3770 | CandEnd = CandidateSet.end(); |
| 3771 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3772 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3773 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3774 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3775 | Functions.insert(FunTmpl); |
| 3776 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3777 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3778 | // FIXME: Pass in the explicit template arguments? |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3779 | ArgumentDependentLookup(Name, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3780 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3781 | // Erase all of the candidates we already knew about. |
| 3782 | // FIXME: This is suboptimal. Is there a better way? |
| 3783 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3784 | CandEnd = CandidateSet.end(); |
| 3785 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3786 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3787 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3788 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3789 | Functions.erase(FunTmpl); |
| 3790 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3791 | |
| 3792 | // For each of the ADL candidates we found, add it to the overload |
| 3793 | // set. |
| 3794 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3795 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3796 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3797 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
| 3798 | if (HasExplicitTemplateArgs) |
| 3799 | continue; |
| 3800 | |
| 3801 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 3802 | false, false, PartialOverloading); |
| 3803 | } else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3805 | HasExplicitTemplateArgs, |
| 3806 | ExplicitTemplateArgs, |
| 3807 | NumExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3808 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3809 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3812 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3813 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3815 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3817 | // Define viable functions to be better candidates than non-viable |
| 3818 | // functions. |
| 3819 | if (!Cand2.Viable) |
| 3820 | return Cand1.Viable; |
| 3821 | else if (!Cand1.Viable) |
| 3822 | return false; |
| 3823 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3824 | // C++ [over.match.best]p1: |
| 3825 | // |
| 3826 | // -- if F is a static member function, ICS1(F) is defined such |
| 3827 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3828 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3829 | // better nor worse than ICS1(F). |
| 3830 | unsigned StartArg = 0; |
| 3831 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3832 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3834 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | // A viable function F1 is defined to be a better function than another |
| 3836 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3837 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3838 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3839 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3840 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3841 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3842 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3843 | Cand2.Conversions[ArgIdx])) { |
| 3844 | case ImplicitConversionSequence::Better: |
| 3845 | // Cand1 has a better conversion sequence. |
| 3846 | HasBetterConversion = true; |
| 3847 | break; |
| 3848 | |
| 3849 | case ImplicitConversionSequence::Worse: |
| 3850 | // Cand1 can't be better than Cand2. |
| 3851 | return false; |
| 3852 | |
| 3853 | case ImplicitConversionSequence::Indistinguishable: |
| 3854 | // Do nothing. |
| 3855 | break; |
| 3856 | } |
| 3857 | } |
| 3858 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3860 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3861 | if (HasBetterConversion) |
| 3862 | return true; |
| 3863 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3865 | // specialization, or, if not that, |
| 3866 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 3867 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 3868 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3869 | |
| 3870 | // -- F1 and F2 are function template specializations, and the function |
| 3871 | // template for F1 is more specialized than the template for F2 |
| 3872 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3873 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 3874 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 3875 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3876 | if (FunctionTemplateDecl *BetterTemplate |
| 3877 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 3878 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 3879 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 3880 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3881 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3882 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3883 | // -- the context is an initialization by user-defined conversion |
| 3884 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 3885 | // from the return type of F1 to the destination type (i.e., |
| 3886 | // the type of the entity being initialized) is a better |
| 3887 | // conversion sequence than the standard conversion sequence |
| 3888 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | if (Cand1.Function && Cand2.Function && |
| 3890 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3891 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 3892 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 3893 | Cand2.FinalConversion)) { |
| 3894 | case ImplicitConversionSequence::Better: |
| 3895 | // Cand1 has a better conversion sequence. |
| 3896 | return true; |
| 3897 | |
| 3898 | case ImplicitConversionSequence::Worse: |
| 3899 | // Cand1 can't be better than Cand2. |
| 3900 | return false; |
| 3901 | |
| 3902 | case ImplicitConversionSequence::Indistinguishable: |
| 3903 | // Do nothing |
| 3904 | break; |
| 3905 | } |
| 3906 | } |
| 3907 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3908 | return false; |
| 3909 | } |
| 3910 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3912 | /// within an overload candidate set. |
| 3913 | /// |
| 3914 | /// \param CandidateSet the set of candidate functions. |
| 3915 | /// |
| 3916 | /// \param Loc the location of the function name (or operator symbol) for |
| 3917 | /// which overload resolution occurs. |
| 3918 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3920 | /// function, Best points to the candidate function found. |
| 3921 | /// |
| 3922 | /// \returns The result of overload resolution. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | Sema::OverloadingResult |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3924 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3925 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3927 | // Find the best viable function. |
| 3928 | Best = CandidateSet.end(); |
| 3929 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3930 | Cand != CandidateSet.end(); ++Cand) { |
| 3931 | if (Cand->Viable) { |
| 3932 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 3933 | Best = Cand; |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | // If we didn't find any viable functions, abort. |
| 3938 | if (Best == CandidateSet.end()) |
| 3939 | return OR_No_Viable_Function; |
| 3940 | |
| 3941 | // Make sure that this function is better than every other viable |
| 3942 | // function. If not, we have an ambiguity. |
| 3943 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3944 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3945 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3946 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3947 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 3948 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3949 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3950 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3951 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3952 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3953 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3954 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3956 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3957 | return OR_Deleted; |
| 3958 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3959 | // C++ [basic.def.odr]p2: |
| 3960 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | // when referred to from a potentially-evaluated expression. [Note: this |
| 3962 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3963 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 3964 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 3965 | if (Best->Function) |
| 3966 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3967 | return OR_Success; |
| 3968 | } |
| 3969 | |
| 3970 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 3971 | /// diagnostic messages containing the candidates in the candidate |
| 3972 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3974 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | bool OnlyViable) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3976 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3977 | LastCand = CandidateSet.end(); |
| 3978 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3979 | if (Cand->Viable || !OnlyViable) { |
| 3980 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3981 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3982 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3983 | // Deleted or "unavailable" function. |
| 3984 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 3985 | << Cand->Function->isDeleted(); |
Douglas Gregor | 1fdd89b | 2009-09-15 20:11:42 +0000 | [diff] [blame] | 3986 | } else if (FunctionTemplateDecl *FunTmpl |
| 3987 | = Cand->Function->getPrimaryTemplate()) { |
| 3988 | // Function template specialization |
| 3989 | // FIXME: Give a better reason! |
| 3990 | Diag(Cand->Function->getLocation(), diag::err_ovl_template_candidate) |
| 3991 | << getTemplateArgumentBindingsText(FunTmpl->getTemplateParameters(), |
| 3992 | *Cand->Function->getTemplateSpecializationArgs()); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3993 | } else { |
| 3994 | // Normal function |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 3995 | bool errReported = false; |
| 3996 | if (!Cand->Viable && Cand->Conversions.size() > 0) { |
| 3997 | for (int i = Cand->Conversions.size()-1; i >= 0; i--) { |
| 3998 | const ImplicitConversionSequence &Conversion = |
| 3999 | Cand->Conversions[i]; |
| 4000 | if ((Conversion.ConversionKind != |
| 4001 | ImplicitConversionSequence::BadConversion) || |
| 4002 | Conversion.ConversionFunctionSet.size() == 0) |
| 4003 | continue; |
| 4004 | Diag(Cand->Function->getLocation(), |
| 4005 | diag::err_ovl_candidate_not_viable) << (i+1); |
| 4006 | errReported = true; |
| 4007 | for (int j = Conversion.ConversionFunctionSet.size()-1; |
| 4008 | j >= 0; j--) { |
| 4009 | FunctionDecl *Func = Conversion.ConversionFunctionSet[j]; |
| 4010 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 4011 | } |
| 4012 | } |
| 4013 | } |
| 4014 | if (!errReported) |
| 4015 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4016 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4017 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4018 | // Desugar the type of the surrogate down to a function type, |
| 4019 | // retaining as many typedefs as possible while still showing |
| 4020 | // the function type (and, therefore, its parameter types). |
| 4021 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4022 | bool isLValueReference = false; |
| 4023 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4024 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4025 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4026 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4027 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4028 | isLValueReference = true; |
| 4029 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4030 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4031 | FnType = FnTypeRef->getPointeeType(); |
| 4032 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4033 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4034 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4035 | FnType = FnTypePtr->getPointeeType(); |
| 4036 | isPointer = true; |
| 4037 | } |
| 4038 | // Desugar down to a function type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4039 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4040 | // Reconstruct the pointer/reference as appropriate. |
| 4041 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4042 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 4043 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4045 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4046 | << FnType; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4047 | } else if (OnlyViable) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4048 | // FIXME: We need to get the identifier in here |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4049 | // FIXME: Do we want the error message to point at the operator? |
| 4050 | // (built-ins won't have a location) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4051 | // FIXME: can we get some kind of stable location for this? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4052 | QualType FnType |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4053 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 4054 | Cand->BuiltinTypes.ParamTypes, |
| 4055 | Cand->Conversions.size(), |
| 4056 | false, 0); |
| 4057 | |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4058 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4061 | } |
| 4062 | } |
| 4063 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4064 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4065 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4066 | /// expression with overloaded function type and @p ToType is the type |
| 4067 | /// we're trying to resolve to. For example: |
| 4068 | /// |
| 4069 | /// @code |
| 4070 | /// int f(double); |
| 4071 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4072 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4073 | /// int (*pfd)(double) = f; // selects f(double) |
| 4074 | /// @endcode |
| 4075 | /// |
| 4076 | /// This routine returns the resulting FunctionDecl if it could be |
| 4077 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4078 | /// routine will emit diagnostics if there is an error. |
| 4079 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4080 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4081 | bool Complain) { |
| 4082 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4083 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4084 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4085 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4086 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4087 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4088 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4089 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4090 | FunctionType = MemTypePtr->getPointeeType(); |
| 4091 | IsMember = true; |
| 4092 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4093 | |
| 4094 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4095 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4096 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4097 | return 0; |
| 4098 | |
| 4099 | // Find the actual overloaded function declaration. |
| 4100 | OverloadedFunctionDecl *Ovl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4102 | // C++ [over.over]p1: |
| 4103 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4104 | // overloaded function name is ignored (5.1). ] |
| 4105 | Expr *OvlExpr = From->IgnoreParens(); |
| 4106 | |
| 4107 | // C++ [over.over]p1: |
| 4108 | // [...] The overloaded function name can be preceded by the & |
| 4109 | // operator. |
| 4110 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4111 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4112 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4113 | } |
| 4114 | |
| 4115 | // Try to dig out the overloaded function. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4116 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 4117 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4118 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4119 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
Anders Carlsson | 6e8f550 | 2009-10-07 22:26:29 +0000 | [diff] [blame^] | 4120 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(OvlExpr)) { |
| 4121 | Ovl = dyn_cast<OverloadedFunctionDecl>(ME->getMemberDecl()); |
| 4122 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(ME->getMemberDecl()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4123 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4124 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4125 | // If there's no overloaded function declaration or function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4126 | // we're done. |
| 4127 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4128 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4129 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4130 | OverloadIterator Fun; |
| 4131 | if (Ovl) |
| 4132 | Fun = Ovl; |
| 4133 | else |
| 4134 | Fun = FunctionTemplate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4136 | // Look through all of the overloaded functions, searching for one |
| 4137 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4138 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4139 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4140 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4141 | // C++ [over.over]p3: |
| 4142 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4143 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4144 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4145 | // type "pointer-to-member-function." |
| 4146 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4147 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4148 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4149 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4151 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4153 | // static when converting to member pointer. |
| 4154 | if (Method->isStatic() == IsMember) |
| 4155 | continue; |
| 4156 | } else if (IsMember) |
| 4157 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4158 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4159 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | // If the name is a function template, template argument deduction is |
| 4161 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4162 | // resulting template argument list is used to generate a single |
| 4163 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4164 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4165 | // FIXME: We don't really want to build the specialization here, do we? |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4166 | FunctionDecl *Specialization = 0; |
| 4167 | TemplateDeductionInfo Info(Context); |
| 4168 | if (TemplateDeductionResult Result |
| 4169 | = DeduceTemplateArguments(FunctionTemplate, /*FIXME*/false, |
| 4170 | /*FIXME:*/0, /*FIXME:*/0, |
| 4171 | FunctionType, Specialization, Info)) { |
| 4172 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4173 | (void)Result; |
| 4174 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4175 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4176 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4177 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4178 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4179 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4180 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4181 | } |
| 4182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4184 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 4185 | // Skip non-static functions when converting to pointer, and static |
| 4186 | // when converting to member pointer. |
| 4187 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4188 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4189 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4190 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4191 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4192 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4193 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4194 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4195 | FoundNonTemplateFunction = true; |
| 4196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4200 | // If there were 0 or 1 matches, we're done. |
| 4201 | if (Matches.empty()) |
| 4202 | return 0; |
| 4203 | else if (Matches.size() == 1) |
| 4204 | return *Matches.begin(); |
| 4205 | |
| 4206 | // C++ [over.over]p4: |
| 4207 | // If more than one function is selected, [...] |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4208 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4209 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4210 | // [...] and any given function template specialization F1 is |
| 4211 | // eliminated if the set contains a second function template |
| 4212 | // specialization whose function template is more specialized |
| 4213 | // than the function template of F1 according to the partial |
| 4214 | // ordering rules of 14.5.5.2. |
| 4215 | |
| 4216 | // The algorithm specified above is quadratic. We instead use a |
| 4217 | // two-pass algorithm (similar to the one used to identify the |
| 4218 | // best viable function in an overload set) that identifies the |
| 4219 | // best function template (if it exists). |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4220 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
| 4221 | Matches.end()); |
| 4222 | return getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4223 | TPOC_Other, From->getLocStart(), |
| 4224 | PartialDiagnostic(0), |
| 4225 | PartialDiagnostic(diag::err_addr_ovl_ambiguous) |
| 4226 | << TemplateMatches[0]->getDeclName(), |
| 4227 | PartialDiagnostic(diag::err_ovl_template_candidate)); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4228 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4229 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4230 | // [...] any function template specializations in the set are |
| 4231 | // eliminated if the set also contains a non-template function, [...] |
| 4232 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4233 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4234 | if ((*M)->getPrimaryTemplate() == 0) |
| 4235 | RemainingMatches.push_back(*M); |
| 4236 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4238 | // selected function. |
| 4239 | if (RemainingMatches.size() == 1) |
| 4240 | return RemainingMatches.front(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4242 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4243 | // returns (even if we issue the diagnostics here). |
| 4244 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4245 | << RemainingMatches[0]->getDeclName(); |
| 4246 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 4247 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4248 | return 0; |
| 4249 | } |
| 4250 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4251 | /// \brief Add a single candidate to the overload set. |
| 4252 | static void AddOverloadedCallCandidate(Sema &S, |
| 4253 | AnyFunctionDecl Callee, |
| 4254 | bool &ArgumentDependentLookup, |
| 4255 | bool HasExplicitTemplateArgs, |
| 4256 | const TemplateArgument *ExplicitTemplateArgs, |
| 4257 | unsigned NumExplicitTemplateArgs, |
| 4258 | Expr **Args, unsigned NumArgs, |
| 4259 | OverloadCandidateSet &CandidateSet, |
| 4260 | bool PartialOverloading) { |
| 4261 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
| 4262 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
| 4263 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 4264 | PartialOverloading); |
| 4265 | |
| 4266 | if (Func->getDeclContext()->isRecord() || |
| 4267 | Func->getDeclContext()->isFunctionOrMethod()) |
| 4268 | ArgumentDependentLookup = false; |
| 4269 | return; |
| 4270 | } |
| 4271 | |
| 4272 | FunctionTemplateDecl *FuncTemplate = cast<FunctionTemplateDecl>(Callee); |
| 4273 | S.AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 4274 | ExplicitTemplateArgs, |
| 4275 | NumExplicitTemplateArgs, |
| 4276 | Args, NumArgs, CandidateSet); |
| 4277 | |
| 4278 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 4279 | ArgumentDependentLookup = false; |
| 4280 | } |
| 4281 | |
| 4282 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 4283 | /// dependent lookup to the given overload set. |
| 4284 | void Sema::AddOverloadedCallCandidates(NamedDecl *Callee, |
| 4285 | DeclarationName &UnqualifiedName, |
| 4286 | bool &ArgumentDependentLookup, |
| 4287 | bool HasExplicitTemplateArgs, |
| 4288 | const TemplateArgument *ExplicitTemplateArgs, |
| 4289 | unsigned NumExplicitTemplateArgs, |
| 4290 | Expr **Args, unsigned NumArgs, |
| 4291 | OverloadCandidateSet &CandidateSet, |
| 4292 | bool PartialOverloading) { |
| 4293 | // Add the functions denoted by Callee to the set of candidate |
| 4294 | // functions. While we're doing so, track whether argument-dependent |
| 4295 | // lookup still applies, per: |
| 4296 | // |
| 4297 | // C++0x [basic.lookup.argdep]p3: |
| 4298 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4299 | // and let Y be the lookup set produced by argument dependent |
| 4300 | // lookup (defined as follows). If X contains |
| 4301 | // |
| 4302 | // -- a declaration of a class member, or |
| 4303 | // |
| 4304 | // -- a block-scope function declaration that is not a |
| 4305 | // using-declaration (FIXME: check for using declaration), or |
| 4306 | // |
| 4307 | // -- a declaration that is neither a function or a function |
| 4308 | // template |
| 4309 | // |
| 4310 | // then Y is empty. |
| 4311 | if (!Callee) { |
| 4312 | // Nothing to do. |
| 4313 | } else if (OverloadedFunctionDecl *Ovl |
| 4314 | = dyn_cast<OverloadedFunctionDecl>(Callee)) { |
| 4315 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4316 | FuncEnd = Ovl->function_end(); |
| 4317 | Func != FuncEnd; ++Func) |
| 4318 | AddOverloadedCallCandidate(*this, *Func, ArgumentDependentLookup, |
| 4319 | HasExplicitTemplateArgs, |
| 4320 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4321 | Args, NumArgs, CandidateSet, |
| 4322 | PartialOverloading); |
| 4323 | } else if (isa<FunctionDecl>(Callee) || isa<FunctionTemplateDecl>(Callee)) |
| 4324 | AddOverloadedCallCandidate(*this, |
| 4325 | AnyFunctionDecl::getFromNamedDecl(Callee), |
| 4326 | ArgumentDependentLookup, |
| 4327 | HasExplicitTemplateArgs, |
| 4328 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4329 | Args, NumArgs, CandidateSet, |
| 4330 | PartialOverloading); |
| 4331 | // FIXME: assert isa<FunctionDecl> || isa<FunctionTemplateDecl> rather than |
| 4332 | // checking dynamically. |
| 4333 | |
| 4334 | if (Callee) |
| 4335 | UnqualifiedName = Callee->getDeclName(); |
| 4336 | |
| 4337 | if (ArgumentDependentLookup) |
| 4338 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
| 4339 | HasExplicitTemplateArgs, |
| 4340 | ExplicitTemplateArgs, |
| 4341 | NumExplicitTemplateArgs, |
| 4342 | CandidateSet, |
| 4343 | PartialOverloading); |
| 4344 | } |
| 4345 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4346 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4347 | /// (which eventually refers to the declaration Func) and the call |
| 4348 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4349 | /// to a specific function. If overload resolution succeeds, returns |
| 4350 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4351 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4352 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4353 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4354 | DeclarationName UnqualifiedName, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4355 | bool HasExplicitTemplateArgs, |
| 4356 | const TemplateArgument *ExplicitTemplateArgs, |
| 4357 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4358 | SourceLocation LParenLoc, |
| 4359 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4360 | SourceLocation *CommaLocs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4361 | SourceLocation RParenLoc, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4362 | bool &ArgumentDependentLookup) { |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4363 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4364 | |
| 4365 | // Add the functions denoted by Callee to the set of candidate |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4366 | // functions. |
| 4367 | AddOverloadedCallCandidates(Callee, UnqualifiedName, ArgumentDependentLookup, |
| 4368 | HasExplicitTemplateArgs, ExplicitTemplateArgs, |
| 4369 | NumExplicitTemplateArgs, Args, NumArgs, |
| 4370 | CandidateSet); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4371 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4372 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4373 | case OR_Success: |
| 4374 | return Best->Function; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4375 | |
| 4376 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4377 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4378 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4379 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4380 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4381 | break; |
| 4382 | |
| 4383 | case OR_Ambiguous: |
| 4384 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4385 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4386 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4387 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4388 | |
| 4389 | case OR_Deleted: |
| 4390 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4391 | << Best->Function->isDeleted() |
| 4392 | << UnqualifiedName |
| 4393 | << Fn->getSourceRange(); |
| 4394 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4395 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4399 | // return NULL. |
| 4400 | Fn->Destroy(Context); |
| 4401 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4402 | Args[Arg]->Destroy(Context); |
| 4403 | return 0; |
| 4404 | } |
| 4405 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4406 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4407 | /// operator. |
| 4408 | /// |
| 4409 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4410 | /// |
| 4411 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4412 | /// operator. |
| 4413 | /// |
| 4414 | /// \param Functions The set of non-member functions that will be |
| 4415 | /// considered by overload resolution. The caller needs to build this |
| 4416 | /// set based on the context using, e.g., |
| 4417 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4418 | /// set should not contain any member functions; those will be added |
| 4419 | /// by CreateOverloadedUnaryOp(). |
| 4420 | /// |
| 4421 | /// \param input The input argument. |
| 4422 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4423 | unsigned OpcIn, |
| 4424 | FunctionSet &Functions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4425 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4426 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4427 | Expr *Input = (Expr *)input.get(); |
| 4428 | |
| 4429 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 4430 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 4431 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4432 | |
| 4433 | Expr *Args[2] = { Input, 0 }; |
| 4434 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4436 | // For post-increment and post-decrement, add the implicit '0' as |
| 4437 | // the second argument, so that we know this is a post-increment or |
| 4438 | // post-decrement. |
| 4439 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 4440 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4441 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4442 | SourceLocation()); |
| 4443 | NumArgs = 2; |
| 4444 | } |
| 4445 | |
| 4446 | if (Input->isTypeDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4447 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4448 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4449 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4450 | FuncEnd = Functions.end(); |
| 4451 | Func != FuncEnd; ++Func) |
| 4452 | Overloads->addOverload(*Func); |
| 4453 | |
| 4454 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4455 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4457 | input.release(); |
| 4458 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4459 | &Args[0], NumArgs, |
| 4460 | Context.DependentTy, |
| 4461 | OpLoc)); |
| 4462 | } |
| 4463 | |
| 4464 | // Build an empty overload set. |
| 4465 | OverloadCandidateSet CandidateSet; |
| 4466 | |
| 4467 | // Add the candidates from the given function set. |
| 4468 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4469 | |
| 4470 | // Add operator candidates that are member functions. |
| 4471 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4472 | |
| 4473 | // Add builtin operator candidates. |
| 4474 | AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet); |
| 4475 | |
| 4476 | // Perform overload resolution. |
| 4477 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4478 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4479 | case OR_Success: { |
| 4480 | // We found a built-in operator or an overloaded operator. |
| 4481 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4482 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4483 | if (FnDecl) { |
| 4484 | // We matched an overloaded operator. Build a call to that |
| 4485 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4486 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4487 | // Convert the arguments. |
| 4488 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4489 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4490 | return ExprError(); |
| 4491 | } else { |
| 4492 | // Convert the arguments. |
| 4493 | if (PerformCopyInitialization(Input, |
| 4494 | FnDecl->getParamDecl(0)->getType(), |
| 4495 | "passing")) |
| 4496 | return ExprError(); |
| 4497 | } |
| 4498 | |
| 4499 | // Determine the result type |
| 4500 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4501 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4502 | ResultTy = ResultTy.getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4503 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4504 | // Build the actual expression node. |
| 4505 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4506 | SourceLocation()); |
| 4507 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4508 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4509 | input.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4510 | |
| 4511 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4512 | &Input, 1, ResultTy, OpLoc); |
| 4513 | return MaybeBindToTemporary(CE); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4514 | } else { |
| 4515 | // We matched a built-in operator. Convert the arguments, then |
| 4516 | // break out so that we will build the appropriate built-in |
| 4517 | // operator node. |
| 4518 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4519 | Best->Conversions[0], "passing")) |
| 4520 | return ExprError(); |
| 4521 | |
| 4522 | break; |
| 4523 | } |
| 4524 | } |
| 4525 | |
| 4526 | case OR_No_Viable_Function: |
| 4527 | // No viable function; fall through to handling this as a |
| 4528 | // built-in operator, which will produce an error message for us. |
| 4529 | break; |
| 4530 | |
| 4531 | case OR_Ambiguous: |
| 4532 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4533 | << UnaryOperator::getOpcodeStr(Opc) |
| 4534 | << Input->getSourceRange(); |
| 4535 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4536 | return ExprError(); |
| 4537 | |
| 4538 | case OR_Deleted: |
| 4539 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4540 | << Best->Function->isDeleted() |
| 4541 | << UnaryOperator::getOpcodeStr(Opc) |
| 4542 | << Input->getSourceRange(); |
| 4543 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4544 | return ExprError(); |
| 4545 | } |
| 4546 | |
| 4547 | // Either we found no viable overloaded operator or we matched a |
| 4548 | // built-in operator. In either case, fall through to trying to |
| 4549 | // build a built-in operation. |
| 4550 | input.release(); |
| 4551 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4552 | } |
| 4553 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4554 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4555 | /// operator. |
| 4556 | /// |
| 4557 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4558 | /// |
| 4559 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4560 | /// operator. |
| 4561 | /// |
| 4562 | /// \param Functions The set of non-member functions that will be |
| 4563 | /// considered by overload resolution. The caller needs to build this |
| 4564 | /// set based on the context using, e.g., |
| 4565 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4566 | /// set should not contain any member functions; those will be added |
| 4567 | /// by CreateOverloadedBinOp(). |
| 4568 | /// |
| 4569 | /// \param LHS Left-hand argument. |
| 4570 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4572 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4573 | unsigned OpcIn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4574 | FunctionSet &Functions, |
| 4575 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4576 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4577 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4578 | |
| 4579 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4580 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4581 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4582 | |
| 4583 | // If either side is type-dependent, create an appropriate dependent |
| 4584 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4585 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4586 | // .* cannot be overloaded. |
| 4587 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4588 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4589 | Context.DependentTy, OpLoc)); |
| 4590 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4592 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4594 | FuncEnd = Functions.end(); |
| 4595 | Func != FuncEnd; ++Func) |
| 4596 | Overloads->addOverload(*Func); |
| 4597 | |
| 4598 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4599 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4601 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4602 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4603 | Context.DependentTy, |
| 4604 | OpLoc)); |
| 4605 | } |
| 4606 | |
| 4607 | // If this is the .* operator, which is not overloadable, just |
| 4608 | // create a built-in binary operator. |
| 4609 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4610 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4611 | |
| 4612 | // If this is one of the assignment operators, we only perform |
| 4613 | // overload resolution if the left-hand side is a class or |
| 4614 | // enumeration type (C++ [expr.ass]p3). |
| 4615 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4616 | !Args[0]->getType()->isOverloadableType()) |
| 4617 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4619 | // Build an empty overload set. |
| 4620 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4621 | |
| 4622 | // Add the candidates from the given function set. |
| 4623 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4624 | |
| 4625 | // Add operator candidates that are member functions. |
| 4626 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4627 | |
| 4628 | // Add builtin operator candidates. |
| 4629 | AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet); |
| 4630 | |
| 4631 | // Perform overload resolution. |
| 4632 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4633 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4634 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4635 | // We found a built-in operator or an overloaded operator. |
| 4636 | FunctionDecl *FnDecl = Best->Function; |
| 4637 | |
| 4638 | if (FnDecl) { |
| 4639 | // We matched an overloaded operator. Build a call to that |
| 4640 | // operator. |
| 4641 | |
| 4642 | // Convert the arguments. |
| 4643 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4644 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 4645 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4646 | "passing")) |
| 4647 | return ExprError(); |
| 4648 | } else { |
| 4649 | // Convert the arguments. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4650 | if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4651 | "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4652 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4653 | "passing")) |
| 4654 | return ExprError(); |
| 4655 | } |
| 4656 | |
| 4657 | // Determine the result type |
| 4658 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4659 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4660 | ResultTy = ResultTy.getNonReferenceType(); |
| 4661 | |
| 4662 | // Build the actual expression node. |
| 4663 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4664 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4665 | UsualUnaryConversions(FnExpr); |
| 4666 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4667 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4668 | Args, 2, ResultTy, OpLoc); |
| 4669 | return MaybeBindToTemporary(CE); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4670 | } else { |
| 4671 | // We matched a built-in operator. Convert the arguments, then |
| 4672 | // break out so that we will build the appropriate built-in |
| 4673 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4674 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4675 | Best->Conversions[0], "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4676 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4677 | Best->Conversions[1], "passing")) |
| 4678 | return ExprError(); |
| 4679 | |
| 4680 | break; |
| 4681 | } |
| 4682 | } |
| 4683 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4684 | case OR_No_Viable_Function: { |
| 4685 | // C++ [over.match.oper]p9: |
| 4686 | // If the operator is the operator , [...] and there are no |
| 4687 | // viable functions, then the operator is assumed to be the |
| 4688 | // built-in operator and interpreted according to clause 5. |
| 4689 | if (Opc == BinaryOperator::Comma) |
| 4690 | break; |
| 4691 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4692 | // For class as left operand for assignment or compound assigment operator |
| 4693 | // do not fall through to handling in built-in, but report that no overloaded |
| 4694 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4695 | OwningExprResult Result = ExprError(); |
| 4696 | if (Args[0]->getType()->isRecordType() && |
| 4697 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4698 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4699 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4700 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4701 | } else { |
| 4702 | // No viable function; try to create a built-in operation, which will |
| 4703 | // produce an error. Then, show the non-viable candidates. |
| 4704 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4705 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4706 | assert(Result.isInvalid() && |
| 4707 | "C++ binary operator overloading is missing candidates!"); |
| 4708 | if (Result.isInvalid()) |
| 4709 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4710 | return move(Result); |
| 4711 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4712 | |
| 4713 | case OR_Ambiguous: |
| 4714 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4715 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4716 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4717 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4718 | return ExprError(); |
| 4719 | |
| 4720 | case OR_Deleted: |
| 4721 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4722 | << Best->Function->isDeleted() |
| 4723 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4724 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4725 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4726 | return ExprError(); |
| 4727 | } |
| 4728 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4729 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4730 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4733 | /// BuildCallToMemberFunction - Build a call to a member |
| 4734 | /// function. MemExpr is the expression that refers to the member |
| 4735 | /// function (and includes the object parameter), Args/NumArgs are the |
| 4736 | /// arguments to the function call (not including the object |
| 4737 | /// parameter). The caller needs to validate that the member |
| 4738 | /// expression refers to a member function or an overloaded member |
| 4739 | /// function. |
| 4740 | Sema::ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 4742 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4743 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 4744 | SourceLocation RParenLoc) { |
| 4745 | // Dig out the member expression. This holds both the object |
| 4746 | // argument and the member function we're referring to. |
| 4747 | MemberExpr *MemExpr = 0; |
| 4748 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 4749 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 4750 | else |
| 4751 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 4752 | assert(MemExpr && "Building member call without member expression"); |
| 4753 | |
| 4754 | // Extract the object argument. |
| 4755 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4757 | CXXMethodDecl *Method = 0; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4758 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 4759 | isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4760 | // Add overload candidates |
| 4761 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4762 | DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4764 | for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd; |
| 4765 | Func != FuncEnd; ++Func) { |
| 4766 | if ((Method = dyn_cast<CXXMethodDecl>(*Func))) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4767 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4768 | /*SuppressUserConversions=*/false); |
| 4769 | else |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 4770 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func), |
| 4771 | MemExpr->hasExplicitTemplateArgumentList(), |
| 4772 | MemExpr->getTemplateArgs(), |
| 4773 | MemExpr->getNumTemplateArgs(), |
| 4774 | ObjectArg, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4775 | CandidateSet, |
| 4776 | /*SuppressUsedConversions=*/false); |
| 4777 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4778 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4779 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4780 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4781 | case OR_Success: |
| 4782 | Method = cast<CXXMethodDecl>(Best->Function); |
| 4783 | break; |
| 4784 | |
| 4785 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4786 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4787 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4788 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4789 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4790 | // FIXME: Leaking incoming expressions! |
| 4791 | return true; |
| 4792 | |
| 4793 | case OR_Ambiguous: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4794 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4795 | diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4796 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4797 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4798 | // FIXME: Leaking incoming expressions! |
| 4799 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4800 | |
| 4801 | case OR_Deleted: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4802 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4803 | diag::err_ovl_deleted_member_call) |
| 4804 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4805 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4806 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4807 | // FIXME: Leaking incoming expressions! |
| 4808 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | FixOverloadedFunctionReference(MemExpr, Method); |
| 4812 | } else { |
| 4813 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 4814 | } |
| 4815 | |
| 4816 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4817 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4818 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4819 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4820 | Method->getResultType().getNonReferenceType(), |
| 4821 | RParenLoc)); |
| 4822 | |
| 4823 | // Convert the object argument (for a non-static member function call). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4824 | if (!Method->isStatic() && |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4825 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 4826 | return true; |
| 4827 | MemExpr->setBase(ObjectArg); |
| 4828 | |
| 4829 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4830 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4832 | RParenLoc)) |
| 4833 | return true; |
| 4834 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4835 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4836 | return true; |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 4837 | |
| 4838 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4839 | } |
| 4840 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4841 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 4842 | /// type (C++ [over.call.object]), which can end up invoking an |
| 4843 | /// overloaded function call operator (@c operator()) or performing a |
| 4844 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | Sema::ExprResult |
| 4846 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 4847 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4848 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4850 | SourceLocation RParenLoc) { |
| 4851 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4852 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4853 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4854 | // C++ [over.call.object]p1: |
| 4855 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4856 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4857 | // candidate functions includes at least the function call |
| 4858 | // operators of T. The function call operators of T are obtained by |
| 4859 | // ordinary lookup of the name operator() in the context of |
| 4860 | // (E).operator(). |
| 4861 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4862 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4863 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4864 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4865 | Oper != OperEnd; ++Oper) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4866 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4867 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4868 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4869 | // C++ [over.call.object]p2: |
| 4870 | // In addition, for each conversion function declared in T of the |
| 4871 | // form |
| 4872 | // |
| 4873 | // operator conversion-type-id () cv-qualifier; |
| 4874 | // |
| 4875 | // where cv-qualifier is the same cv-qualification as, or a |
| 4876 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 4877 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 4878 | // R", or the type "reference to pointer to function of |
| 4879 | // (P1,...,Pn) returning R", or the type "reference to function |
| 4880 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4881 | // is also considered as a candidate function. Similarly, |
| 4882 | // surrogate call functions are added to the set of candidate |
| 4883 | // functions for each conversion function declared in an |
| 4884 | // accessible base class provided the function is not hidden |
| 4885 | // within T by another intervening declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4886 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4887 | if (!RequireCompleteType(SourceLocation(), Object->getType(), 0)) { |
| 4888 | // FIXME: Look in base classes for more conversion operators! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4889 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4890 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4892 | Func = Conversions->function_begin(), |
| 4893 | FuncEnd = Conversions->function_end(); |
| 4894 | Func != FuncEnd; ++Func) { |
| 4895 | CXXConversionDecl *Conv; |
| 4896 | FunctionTemplateDecl *ConvTemplate; |
| 4897 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4898 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4899 | // Skip over templated conversion functions; they aren't |
| 4900 | // surrogates. |
| 4901 | if (ConvTemplate) |
| 4902 | continue; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4903 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4904 | // Strip the reference type (if any) and then the pointer type (if |
| 4905 | // any) to get down to what might be a function type. |
| 4906 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 4907 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4908 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4909 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4910 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4911 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 4912 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4913 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4915 | // Perform overload resolution. |
| 4916 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4917 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4918 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4919 | // Overload resolution succeeded; we'll build the appropriate call |
| 4920 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4921 | break; |
| 4922 | |
| 4923 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | Diag(Object->getSourceRange().getBegin(), |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4925 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4926 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4927 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4928 | break; |
| 4929 | |
| 4930 | case OR_Ambiguous: |
| 4931 | Diag(Object->getSourceRange().getBegin(), |
| 4932 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4933 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4934 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4935 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4936 | |
| 4937 | case OR_Deleted: |
| 4938 | Diag(Object->getSourceRange().getBegin(), |
| 4939 | diag::err_ovl_deleted_object_call) |
| 4940 | << Best->Function->isDeleted() |
| 4941 | << Object->getType() << Object->getSourceRange(); |
| 4942 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4943 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4946 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4947 | // We had an error; delete all of the subexpressions and return |
| 4948 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4949 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4950 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4951 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4952 | return true; |
| 4953 | } |
| 4954 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4955 | if (Best->Function == 0) { |
| 4956 | // Since there is no function declaration, this is one of the |
| 4957 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4958 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4959 | = cast<CXXConversionDecl>( |
| 4960 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 4961 | |
| 4962 | // We selected one of the surrogate functions that converts the |
| 4963 | // object parameter to a function pointer. Perform the conversion |
| 4964 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 4965 | |
| 4966 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4967 | // and then call it. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 4968 | CXXMemberCallExpr *CE = |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4969 | BuildCXXMemberCallExpr(Object, Conv); |
| 4970 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 4971 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4972 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 4973 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 4977 | // that calls this method, using Object for the implicit object |
| 4978 | // parameter and passing along the remaining arguments. |
| 4979 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4980 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4981 | |
| 4982 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4983 | unsigned NumArgsToCheck = NumArgs; |
| 4984 | |
| 4985 | // Build the full argument list for the method call (the |
| 4986 | // implicit object parameter is placed at the beginning of the |
| 4987 | // list). |
| 4988 | Expr **MethodArgs; |
| 4989 | if (NumArgs < NumArgsInProto) { |
| 4990 | NumArgsToCheck = NumArgsInProto; |
| 4991 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 4992 | } else { |
| 4993 | MethodArgs = new Expr*[NumArgs + 1]; |
| 4994 | } |
| 4995 | MethodArgs[0] = Object; |
| 4996 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4997 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4998 | |
| 4999 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5000 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5001 | UsualUnaryConversions(NewFn); |
| 5002 | |
| 5003 | // Once we've built TheCall, all of the expressions are properly |
| 5004 | // owned. |
| 5005 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5006 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5007 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5008 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5009 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5010 | delete [] MethodArgs; |
| 5011 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5012 | // We may have default arguments. If so, we need to allocate more |
| 5013 | // slots in the call for them. |
| 5014 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5015 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5016 | else if (NumArgs > NumArgsInProto) |
| 5017 | NumArgsToCheck = NumArgsInProto; |
| 5018 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5019 | bool IsError = false; |
| 5020 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5021 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5022 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5023 | TheCall->setArg(0, Object); |
| 5024 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5025 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5026 | // Check the argument types. |
| 5027 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5028 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5029 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5030 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5031 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5032 | // Pass the argument. |
| 5033 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5034 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5035 | } else { |
Anders Carlsson | f1480ee | 2009-08-14 18:30:22 +0000 | [diff] [blame] | 5036 | Arg = CXXDefaultArgExpr::Create(Context, Method->getParamDecl(i)); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5037 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5038 | |
| 5039 | TheCall->setArg(i + 1, Arg); |
| 5040 | } |
| 5041 | |
| 5042 | // If this is a variadic call, handle args passed through "...". |
| 5043 | if (Proto->isVariadic()) { |
| 5044 | // Promote the arguments (C99 6.5.2.2p7). |
| 5045 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 5046 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5047 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5048 | TheCall->setArg(i + 1, Arg); |
| 5049 | } |
| 5050 | } |
| 5051 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5052 | if (IsError) return true; |
| 5053 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5054 | if (CheckFunctionCall(Method, TheCall.get())) |
| 5055 | return true; |
| 5056 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 5057 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5060 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5061 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5062 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5063 | Sema::OwningExprResult |
| 5064 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 5065 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5066 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5067 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5068 | // C++ [over.ref]p1: |
| 5069 | // |
| 5070 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 5071 | // for a class object x of type T if T::operator->() exists and if |
| 5072 | // the operator is selected as the best match function by the |
| 5073 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5074 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 5075 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5076 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5077 | |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5078 | LookupResult R = LookupQualifiedName(BaseRecord->getDecl(), OpName, |
| 5079 | LookupOrdinaryName); |
| 5080 | |
| 5081 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 5082 | Oper != OperEnd; ++Oper) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5083 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5084 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5085 | |
| 5086 | // Perform overload resolution. |
| 5087 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5088 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5089 | case OR_Success: |
| 5090 | // Overload resolution succeeded; we'll build the call below. |
| 5091 | break; |
| 5092 | |
| 5093 | case OR_No_Viable_Function: |
| 5094 | if (CandidateSet.empty()) |
| 5095 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5096 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5097 | else |
| 5098 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5099 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5100 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5101 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5102 | |
| 5103 | case OR_Ambiguous: |
| 5104 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5105 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5106 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5107 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5108 | |
| 5109 | case OR_Deleted: |
| 5110 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5111 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5112 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5113 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5114 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | // Convert the object parameter. |
| 5118 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5119 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5120 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5121 | |
| 5122 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5123 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5124 | |
| 5125 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5126 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 5127 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5128 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5129 | Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5130 | Method->getResultType().getNonReferenceType(), |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 5131 | Method->getLocation()); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5132 | return Owned(Base); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5135 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 5136 | /// a C++ overloaded function (possibly with some parentheses and |
| 5137 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 5138 | /// to the function declaration Fn, so patch up the expression E to |
| 5139 | /// refer (possibly indirectly) to Fn. |
| 5140 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 5141 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 5142 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 5143 | E->setType(PE->getSubExpr()->getType()); |
| 5144 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5145 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5146 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5147 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5148 | if (Method->isStatic()) { |
| 5149 | // Do nothing: static member functions aren't any different |
| 5150 | // from non-member functions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5151 | } else if (QualifiedDeclRefExpr *DRE |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5152 | = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) { |
| 5153 | // We have taken the address of a pointer to member |
| 5154 | // function. Perform the computation here so that we get the |
| 5155 | // appropriate pointer to member type. |
| 5156 | DRE->setDecl(Fn); |
| 5157 | DRE->setType(Fn->getType()); |
| 5158 | QualType ClassType |
| 5159 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5160 | E->setType(Context.getMemberPointerType(Fn->getType(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5161 | ClassType.getTypePtr())); |
| 5162 | return; |
| 5163 | } |
| 5164 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5165 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5166 | E->setType(Context.getPointerType(UnOp->getSubExpr()->getType())); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5167 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5168 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5169 | isa<FunctionTemplateDecl>(DR->getDecl())) && |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5170 | "Expected overloaded function or function template"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5171 | DR->setDecl(Fn); |
| 5172 | E->setType(Fn->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5173 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 5174 | MemExpr->setMemberDecl(Fn); |
| 5175 | E->setType(Fn->getType()); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5176 | } else { |
| 5177 | assert(false && "Invalid reference to overloaded function"); |
| 5178 | } |
| 5179 | } |
| 5180 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5181 | } // end namespace clang |