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 | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 353 | false, TPL_TemplateMatch) || |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 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) |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 467 | if (Cand->Viable) |
| 468 | ICS.ConversionFunctionSet.push_back(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 471 | |
| 472 | return ICS; |
| 473 | } |
| 474 | |
| 475 | /// IsStandardConversion - Determines whether there is a standard |
| 476 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 477 | /// expression From to the type ToType. Standard conversion sequences |
| 478 | /// only consider non-class types; for conversions that involve class |
| 479 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 480 | /// contain the standard conversion sequence required to perform this |
| 481 | /// conversion and this routine will return true. Otherwise, this |
| 482 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | bool |
| 484 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 485 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 487 | QualType FromType = From->getType(); |
| 488 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 489 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 490 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 491 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 492 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 493 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 494 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 496 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 498 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 499 | if (getLangOptions().CPlusPlus) |
| 500 | return false; |
| 501 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 505 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 506 | // array-to-pointer conversion, or function-to-pointer conversion |
| 507 | // (C++ 4p1). |
| 508 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 510 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 511 | // converted to an rvalue. |
| 512 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 514 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 515 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 516 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 517 | |
| 518 | // If T is a non-class type, the type of the rvalue is the |
| 519 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 520 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 521 | // just strip the qualifiers because they don't matter. |
| 522 | |
| 523 | // FIXME: Doesn't see through to qualifiers behind a typedef! |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 524 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 525 | } else if (FromType->isArrayType()) { |
| 526 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 527 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 528 | |
| 529 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 530 | // bound of T" can be converted to an rvalue of type "pointer to |
| 531 | // T" (C++ 4.2p1). |
| 532 | FromType = Context.getArrayDecayedType(FromType); |
| 533 | |
| 534 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 535 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 536 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 537 | |
| 538 | // For the purpose of ranking in overload resolution |
| 539 | // (13.3.3.1.1), this conversion is considered an |
| 540 | // array-to-pointer conversion followed by a qualification |
| 541 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 542 | SCS.Second = ICK_Identity; |
| 543 | SCS.Third = ICK_Qualification; |
| 544 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 545 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 546 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 547 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 548 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 549 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 550 | |
| 551 | // An lvalue of function type T can be converted to an rvalue of |
| 552 | // type "pointer to T." The result is a pointer to the |
| 553 | // function. (C++ 4.3p1). |
| 554 | FromType = Context.getPointerType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | } else if (FunctionDecl *Fn |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 556 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 557 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 558 | SCS.First = ICK_Function_To_Pointer; |
| 559 | |
| 560 | // We were able to resolve the address of the overloaded function, |
| 561 | // so we can convert to the type of that function. |
| 562 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 563 | if (ToType->isLValueReferenceType()) |
| 564 | FromType = Context.getLValueReferenceType(FromType); |
| 565 | else if (ToType->isRValueReferenceType()) |
| 566 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 567 | else if (ToType->isMemberPointerType()) { |
| 568 | // Resolve address only succeeds if both sides are member pointers, |
| 569 | // but it doesn't have to be the same class. See DR 247. |
| 570 | // Note that this means that the type of &Derived::fn can be |
| 571 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 572 | // base class, even if it was brought into the derived class via a |
| 573 | // using declaration. The standard isn't clear on this issue at all. |
| 574 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 575 | FromType = Context.getMemberPointerType(FromType, |
| 576 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 577 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 578 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 579 | } else { |
| 580 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 581 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // The second conversion can be an integral promotion, floating |
| 585 | // point promotion, integral conversion, floating point conversion, |
| 586 | // floating-integral conversion, pointer conversion, |
| 587 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 588 | // For overloading in C, this can also be a "compatible-type" |
| 589 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 590 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 591 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 592 | // The unqualified versions of the types are the same: there's no |
| 593 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 594 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 595 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 597 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 599 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 600 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 601 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 602 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 603 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 604 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 605 | SCS.Second = ICK_Complex_Promotion; |
| 606 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 607 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 608 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 609 | // Integral conversions (C++ 4.7). |
| 610 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 611 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 612 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 613 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 614 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 615 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 616 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 617 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 618 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 619 | SCS.Second = ICK_Complex_Conversion; |
| 620 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 621 | } else if ((FromType->isFloatingType() && |
| 622 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 623 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 625 | ToType->isFloatingType())) { |
| 626 | // Floating-integral conversions (C++ 4.9). |
| 627 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 628 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 629 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 630 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 631 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 632 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 633 | SCS.Second = ICK_Complex_Real; |
| 634 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 635 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 636 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 637 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 638 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 639 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 640 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 641 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 642 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 643 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 644 | } else if (ToType->isBooleanType() && |
| 645 | (FromType->isArithmeticType() || |
| 646 | FromType->isEnumeralType() || |
| 647 | FromType->isPointerType() || |
| 648 | FromType->isBlockPointerType() || |
| 649 | FromType->isMemberPointerType() || |
| 650 | FromType->isNullPtrType())) { |
| 651 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 652 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 653 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 655 | Context.typesAreCompatible(ToType, FromType)) { |
| 656 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 657 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 658 | } else { |
| 659 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 660 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 663 | QualType CanonFrom; |
| 664 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 665 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 666 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 667 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 668 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 669 | CanonFrom = Context.getCanonicalType(FromType); |
| 670 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 671 | } else { |
| 672 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 673 | SCS.Third = ICK_Identity; |
| 674 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 676 | // [...] Any difference in top-level cv-qualification is |
| 677 | // subsumed by the initialization itself and does not constitute |
| 678 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 679 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 681 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 682 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 683 | FromType = ToType; |
| 684 | CanonFrom = CanonTo; |
| 685 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // If we have not converted the argument type to the parameter type, |
| 689 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 690 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 691 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 693 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 694 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 698 | /// expression From (whose potentially-adjusted type is FromType) to |
| 699 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 700 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 702 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 703 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 704 | if (!To) { |
| 705 | return false; |
| 706 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 707 | |
| 708 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 709 | // unsigned short int can be converted to an rvalue of type int if |
| 710 | // int can represent all the values of the source type; otherwise, |
| 711 | // the source rvalue can be converted to an rvalue of type unsigned |
| 712 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 713 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 714 | if (// We can promote any signed, promotable integer type to an int |
| 715 | (FromType->isSignedIntegerType() || |
| 716 | // We can promote any unsigned integer type whose size is |
| 717 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 719 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 720 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 723 | return To->getKind() == BuiltinType::UInt; |
| 724 | } |
| 725 | |
| 726 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 727 | // can be converted to an rvalue of the first of the following types |
| 728 | // that can represent all the values of its underlying type: int, |
| 729 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 730 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 731 | && ToType->isIntegerType()) { |
| 732 | // Determine whether the type we're converting from is signed or |
| 733 | // unsigned. |
| 734 | bool FromIsSigned; |
| 735 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 736 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 737 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 738 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 739 | } else { |
| 740 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 741 | FromIsSigned = true; |
| 742 | } |
| 743 | |
| 744 | // The types we'll try to promote to, in the appropriate |
| 745 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | QualType PromoteTypes[6] = { |
| 747 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 748 | Context.LongTy, Context.UnsignedLongTy , |
| 749 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 750 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 751 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 752 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 753 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 755 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 756 | // We found the type that we can promote to. If this is the |
| 757 | // type we wanted, we have a promotion. Otherwise, no |
| 758 | // promotion. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 759 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 760 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 766 | // rvalue of type int if int can represent all the values of the |
| 767 | // bit-field; otherwise, it can be converted to unsigned int if |
| 768 | // unsigned int can represent all the values of the bit-field. If |
| 769 | // the bit-field is larger yet, no integral promotion applies to |
| 770 | // it. If the bit-field has an enumerated type, it is treated as any |
| 771 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 772 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 773 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 774 | using llvm::APSInt; |
| 775 | if (From) |
| 776 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 777 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 778 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 779 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 780 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 781 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 783 | // Are we promoting to an int from a bitfield that fits in an int? |
| 784 | if (BitWidth < ToSize || |
| 785 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 786 | return To->getKind() == BuiltinType::Int; |
| 787 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 789 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 790 | // that fits into an unsigned int? |
| 791 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 792 | return To->getKind() == BuiltinType::UInt; |
| 793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 795 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 796 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 797 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 799 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 800 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 801 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 802 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 803 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 804 | |
| 805 | return false; |
| 806 | } |
| 807 | |
| 808 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 809 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 810 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 812 | /// An rvalue of type float can be converted to an rvalue of type |
| 813 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 814 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 815 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 816 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 817 | ToBuiltin->getKind() == BuiltinType::Double) |
| 818 | return true; |
| 819 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 820 | // C99 6.3.1.5p1: |
| 821 | // When a float is promoted to double or long double, or a |
| 822 | // double is promoted to long double [...]. |
| 823 | if (!getLangOptions().CPlusPlus && |
| 824 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 825 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 826 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 827 | return true; |
| 828 | } |
| 829 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 830 | return false; |
| 831 | } |
| 832 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 833 | /// \brief Determine if a conversion is a complex promotion. |
| 834 | /// |
| 835 | /// A complex promotion is defined as a complex -> complex conversion |
| 836 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 837 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 838 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 839 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 840 | if (!FromComplex) |
| 841 | return false; |
| 842 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 843 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 844 | if (!ToComplex) |
| 845 | return false; |
| 846 | |
| 847 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 848 | ToComplex->getElementType()) || |
| 849 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 850 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 853 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 854 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 855 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 856 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 857 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | static QualType |
| 859 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 860 | QualType ToPointee, QualType ToType, |
| 861 | ASTContext &Context) { |
| 862 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 863 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 864 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
| 866 | // Exact qualifier match -> return the pointer type we're converting to. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 867 | if (CanonToPointee.getQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 868 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 869 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 870 | return ToType; |
| 871 | |
| 872 | // Build a pointer to ToPointee. It has the right qualifiers |
| 873 | // already. |
| 874 | return Context.getPointerType(ToPointee); |
| 875 | } |
| 876 | |
| 877 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 878 | return Context.getPointerType( |
| 879 | Context.getQualifiedType(CanonToPointee.getUnqualifiedType(), Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 883 | bool InOverloadResolution, |
| 884 | ASTContext &Context) { |
| 885 | // Handle value-dependent integral null pointer constants correctly. |
| 886 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 887 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 888 | Expr->getType()->isIntegralType()) |
| 889 | return !InOverloadResolution; |
| 890 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 891 | return Expr->isNullPointerConstant(Context, |
| 892 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 893 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 894 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 896 | /// IsPointerConversion - Determines whether the conversion of the |
| 897 | /// expression From, which has the (possibly adjusted) type FromType, |
| 898 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 899 | /// 4.10). If so, returns true and places the converted type (that |
| 900 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 901 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 902 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 903 | /// This routine also supports conversions to and from block pointers |
| 904 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 905 | /// pointers to interfaces. FIXME: Once we've determined the |
| 906 | /// appropriate overloading rules for Objective-C, we may want to |
| 907 | /// split the Objective-C checks into a different routine; however, |
| 908 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 909 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 910 | /// set if the conversion is an allowed Objective-C conversion that |
| 911 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 912 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 913 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 914 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 916 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 917 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 918 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 919 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 921 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 922 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 923 | ConvertedType = ToType; |
| 924 | return true; |
| 925 | } |
| 926 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 927 | // Blocks: Block pointers can be converted to void*. |
| 928 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 929 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 930 | ConvertedType = ToType; |
| 931 | return true; |
| 932 | } |
| 933 | // Blocks: A null pointer constant can be converted to a block |
| 934 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 936 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 937 | ConvertedType = ToType; |
| 938 | return true; |
| 939 | } |
| 940 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 941 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 942 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 944 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 945 | ConvertedType = ToType; |
| 946 | return true; |
| 947 | } |
| 948 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 949 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 950 | if (!ToTypePtr) |
| 951 | return false; |
| 952 | |
| 953 | // 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] | 954 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 955 | ConvertedType = ToType; |
| 956 | return true; |
| 957 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 959 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 960 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 961 | if (!FromTypePtr) |
| 962 | return false; |
| 963 | |
| 964 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 965 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 966 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 967 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 968 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 969 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 970 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 972 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 973 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 974 | return true; |
| 975 | } |
| 976 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 977 | // When we're overloading in C, we allow a special kind of pointer |
| 978 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 980 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 982 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 984 | return true; |
| 985 | } |
| 986 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 987 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 989 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 990 | // can be converted to an rvalue of type "pointer to cv B," where |
| 991 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 992 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 993 | // necessitates this conversion is ill-formed. The result of the |
| 994 | // conversion is a pointer to the base class sub-object of the |
| 995 | // derived class object. The null pointer value is converted to |
| 996 | // the null pointer value of the destination type. |
| 997 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 998 | // Note that we do not check for ambiguity or inaccessibility |
| 999 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1000 | if (getLangOptions().CPlusPlus && |
| 1001 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1002 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1003 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1005 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1006 | ToType, Context); |
| 1007 | return true; |
| 1008 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1010 | return false; |
| 1011 | } |
| 1012 | |
| 1013 | /// isObjCPointerConversion - Determines whether this is an |
| 1014 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1015 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1017 | QualType& ConvertedType, |
| 1018 | bool &IncompatibleObjC) { |
| 1019 | if (!getLangOptions().ObjC1) |
| 1020 | return false; |
| 1021 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1022 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1023 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1025 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1026 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1027 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1028 | // 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] | 1029 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1030 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1031 | ConvertedType = ToType; |
| 1032 | return true; |
| 1033 | } |
| 1034 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1036 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1038 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1039 | ConvertedType = ToType; |
| 1040 | return true; |
| 1041 | } |
| 1042 | // Objective C++: We're able to convert from a pointer to an |
| 1043 | // interface to a pointer to a different interface. |
| 1044 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1045 | ConvertedType = ToType; |
| 1046 | return true; |
| 1047 | } |
| 1048 | |
| 1049 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1050 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1051 | // interfaces, which is permitted. However, we're going to |
| 1052 | // complain about it. |
| 1053 | IncompatibleObjC = true; |
| 1054 | ConvertedType = FromType; |
| 1055 | return true; |
| 1056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1058 | // 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] | 1059 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1060 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1061 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1062 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1063 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1064 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1065 | return false; |
| 1066 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1067 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1068 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1069 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1070 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1071 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1072 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1073 | return false; |
| 1074 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1075 | // If we have pointers to pointers, recursively check whether this |
| 1076 | // is an Objective-C conversion. |
| 1077 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1078 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1079 | IncompatibleObjC)) { |
| 1080 | // We always complain about this conversion. |
| 1081 | IncompatibleObjC = true; |
| 1082 | ConvertedType = ToType; |
| 1083 | return true; |
| 1084 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1085 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1086 | // differences in the argument and result types are in Objective-C |
| 1087 | // pointer conversions. If so, we permit the conversion (but |
| 1088 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1090 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1091 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1092 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1093 | if (FromFunctionType && ToFunctionType) { |
| 1094 | // If the function types are exactly the same, this isn't an |
| 1095 | // Objective-C pointer conversion. |
| 1096 | if (Context.getCanonicalType(FromPointeeType) |
| 1097 | == Context.getCanonicalType(ToPointeeType)) |
| 1098 | return false; |
| 1099 | |
| 1100 | // Perform the quick checks that will tell us whether these |
| 1101 | // function types are obviously different. |
| 1102 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1103 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1104 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1105 | return false; |
| 1106 | |
| 1107 | bool HasObjCConversion = false; |
| 1108 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1109 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1110 | // Okay, the types match exactly. Nothing to do. |
| 1111 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1112 | ToFunctionType->getResultType(), |
| 1113 | ConvertedType, IncompatibleObjC)) { |
| 1114 | // Okay, we have an Objective-C pointer conversion. |
| 1115 | HasObjCConversion = true; |
| 1116 | } else { |
| 1117 | // Function types are too different. Abort. |
| 1118 | return false; |
| 1119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1121 | // Check argument types. |
| 1122 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1123 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1124 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1125 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1126 | if (Context.getCanonicalType(FromArgType) |
| 1127 | == Context.getCanonicalType(ToArgType)) { |
| 1128 | // Okay, the types match exactly. Nothing to do. |
| 1129 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1130 | ConvertedType, IncompatibleObjC)) { |
| 1131 | // Okay, we have an Objective-C pointer conversion. |
| 1132 | HasObjCConversion = true; |
| 1133 | } else { |
| 1134 | // Argument types are too different. Abort. |
| 1135 | return false; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | if (HasObjCConversion) { |
| 1140 | // We had an Objective-C conversion. Allow this pointer |
| 1141 | // conversion, but complain about it. |
| 1142 | ConvertedType = ToType; |
| 1143 | IncompatibleObjC = true; |
| 1144 | return true; |
| 1145 | } |
| 1146 | } |
| 1147 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1148 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1151 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1152 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1153 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1154 | /// conversions for which IsPointerConversion has already returned |
| 1155 | /// true. It returns true and produces a diagnostic if there was an |
| 1156 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1157 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
| 1158 | CastExpr::CastKind &Kind) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1159 | QualType FromType = From->getType(); |
| 1160 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1161 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1162 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1163 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1164 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1166 | if (FromPointeeType->isRecordType() && |
| 1167 | ToPointeeType->isRecordType()) { |
| 1168 | // We must have a derived-to-base conversion. Check an |
| 1169 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1170 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1171 | From->getExprLoc(), |
| 1172 | From->getSourceRange())) |
| 1173 | return true; |
| 1174 | |
| 1175 | // The conversion was successful. |
| 1176 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1180 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1182 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1183 | // Objective-C++ conversions are always okay. |
| 1184 | // FIXME: We should have a different class of conversions for the |
| 1185 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1186 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1187 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1188 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1189 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1190 | return false; |
| 1191 | } |
| 1192 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1193 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1194 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1195 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1196 | /// If so, returns true and places the converted type (that might differ from |
| 1197 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1198 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1199 | QualType ToType, |
| 1200 | bool InOverloadResolution, |
| 1201 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1202 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1203 | if (!ToTypePtr) |
| 1204 | return false; |
| 1205 | |
| 1206 | // 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] | 1207 | if (From->isNullPointerConstant(Context, |
| 1208 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1209 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1210 | ConvertedType = ToType; |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1215 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1216 | if (!FromTypePtr) |
| 1217 | return false; |
| 1218 | |
| 1219 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1220 | // where D is derived from B (C++ 4.11p2). |
| 1221 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1222 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1223 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1224 | |
| 1225 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1226 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1227 | ToClass.getTypePtr()); |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | return false; |
| 1232 | } |
| 1233 | |
| 1234 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1235 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1236 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1237 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1238 | /// true and produces a diagnostic if there was an error, or returns false |
| 1239 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1241 | CastExpr::CastKind &Kind) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1242 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1243 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1244 | if (!FromPtrType) { |
| 1245 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1246 | assert(From->isNullPointerConstant(Context, |
| 1247 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1248 | "Expr must be null pointer constant!"); |
| 1249 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1250 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1251 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1252 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1253 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1254 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1255 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1256 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1257 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1258 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1259 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1260 | // FIXME: What about dependent types? |
| 1261 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1262 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1264 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1265 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1266 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1267 | assert(DerivationOkay && |
| 1268 | "Should not have been called if derivation isn't OK."); |
| 1269 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1270 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1271 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1272 | getUnqualifiedType())) { |
| 1273 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1274 | Paths.clear(); |
| 1275 | Paths.setRecordingPaths(true); |
| 1276 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1277 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1278 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1279 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1280 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1281 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1282 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1283 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1284 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1285 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1286 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1287 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1288 | << FromClass << ToClass << QualType(VBase, 0) |
| 1289 | << From->getSourceRange(); |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1293 | // Must be a base to derived member conversion. |
| 1294 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
| 1297 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1298 | /// IsQualificationConversion - Determines whether the conversion from |
| 1299 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1300 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | bool |
| 1302 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1303 | FromType = Context.getCanonicalType(FromType); |
| 1304 | ToType = Context.getCanonicalType(ToType); |
| 1305 | |
| 1306 | // If FromType and ToType are the same type, this is not a |
| 1307 | // qualification conversion. |
| 1308 | if (FromType == ToType) |
| 1309 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1311 | // (C++ 4.4p4): |
| 1312 | // A conversion can add cv-qualifiers at levels other than the first |
| 1313 | // in multi-level pointers, subject to the following rules: [...] |
| 1314 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1315 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1316 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1317 | // Within each iteration of the loop, we check the qualifiers to |
| 1318 | // determine if this still looks like a qualification |
| 1319 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1320 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1321 | // until there are no more pointers or pointers-to-members left to |
| 1322 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1323 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1324 | |
| 1325 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1326 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1327 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1328 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1330 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1331 | // every cv for 0 < k < j. |
| 1332 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1333 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1334 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1336 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1337 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1339 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1340 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1341 | |
| 1342 | // We are left with FromType and ToType being the pointee types |
| 1343 | // after unwrapping the original FromType and ToType the same number |
| 1344 | // of types. If we unwrapped any pointers, and if FromType and |
| 1345 | // ToType have the same unqualified type (since we checked |
| 1346 | // qualifiers above), then this is a qualification conversion. |
| 1347 | return UnwrappedAnyPointer && |
| 1348 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1349 | } |
| 1350 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1351 | /// \brief Given a function template or function, extract the function template |
| 1352 | /// declaration (if any) and the underlying function declaration. |
| 1353 | template<typename T> |
| 1354 | static void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function, |
| 1355 | FunctionTemplateDecl *&FunctionTemplate) { |
| 1356 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig); |
| 1357 | if (FunctionTemplate) |
| 1358 | Function = cast<T>(FunctionTemplate->getTemplatedDecl()); |
| 1359 | else |
| 1360 | Function = cast<T>(Orig); |
| 1361 | } |
| 1362 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1363 | /// Determines whether there is a user-defined conversion sequence |
| 1364 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1365 | /// ToType. If such a conversion exists, User will contain the |
| 1366 | /// user-defined conversion sequence that performs such a conversion |
| 1367 | /// and this routine will return true. Otherwise, this routine returns |
| 1368 | /// false and User is unspecified. |
| 1369 | /// |
| 1370 | /// \param AllowConversionFunctions true if the conversion should |
| 1371 | /// consider conversion functions at all. If false, only constructors |
| 1372 | /// will be considered. |
| 1373 | /// |
| 1374 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1375 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1376 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1377 | /// |
| 1378 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1379 | /// for overload resolution. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1380 | /// \param UserCast true if looking for user defined conversion for a static |
| 1381 | /// cast. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1382 | Sema::OverloadingResult Sema::IsUserDefinedConversion( |
| 1383 | Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1384 | UserDefinedConversionSequence& User, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 1385 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1386 | bool AllowConversionFunctions, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1387 | bool AllowExplicit, bool ForceRValue, |
| 1388 | bool UserCast) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1389 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1390 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1391 | // We're not going to find any constructors. |
| 1392 | } else if (CXXRecordDecl *ToRecordDecl |
| 1393 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1394 | // C++ [over.match.ctor]p1: |
| 1395 | // When objects of class type are direct-initialized (8.5), or |
| 1396 | // copy-initialized from an expression of the same or a |
| 1397 | // derived class type (8.5), overload resolution selects the |
| 1398 | // constructor. [...] For copy-initialization, the candidate |
| 1399 | // functions are all the converting constructors (12.3.1) of |
| 1400 | // that class. The argument list is the expression-list within |
| 1401 | // the parentheses of the initializer. |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1402 | bool SuppressUserConversions = !UserCast; |
| 1403 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1404 | IsDerivedFrom(From->getType(), ToType)) { |
| 1405 | SuppressUserConversions = false; |
| 1406 | AllowConversionFunctions = false; |
| 1407 | } |
| 1408 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1410 | = Context.DeclarationNames.getCXXConstructorName( |
| 1411 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1412 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1414 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1415 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1416 | // Find the constructor (which may be a template). |
| 1417 | CXXConstructorDecl *Constructor = 0; |
| 1418 | FunctionTemplateDecl *ConstructorTmpl |
| 1419 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1420 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1422 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1423 | else |
| 1424 | Constructor = cast<CXXConstructorDecl>(*Con); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1426 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1427 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1428 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1430 | 1, CandidateSet, |
| 1431 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1432 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1433 | // Allow one user-defined conversion when user specifies a |
| 1434 | // From->ToType conversion via an static cast (c-style, etc). |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1435 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1436 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1437 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1438 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1442 | if (!AllowConversionFunctions) { |
| 1443 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1445 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1446 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1447 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1449 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1451 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1452 | // Add all of the conversion functions as candidates. |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1453 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1454 | = FromRecordDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1455 | for (OverloadedFunctionDecl::function_iterator Func |
| 1456 | = Conversions->function_begin(); |
| 1457 | Func != Conversions->function_end(); ++Func) { |
| 1458 | CXXConversionDecl *Conv; |
| 1459 | FunctionTemplateDecl *ConvTemplate; |
| 1460 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 1461 | if (ConvTemplate) |
| 1462 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1463 | else |
| 1464 | Conv = dyn_cast<CXXConversionDecl>(*Func); |
| 1465 | |
| 1466 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1467 | if (ConvTemplate) |
| 1468 | AddTemplateConversionCandidate(ConvTemplate, From, ToType, |
| 1469 | CandidateSet); |
| 1470 | else |
| 1471 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1472 | } |
| 1473 | } |
| 1474 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1475 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1476 | |
| 1477 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1478 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1479 | case OR_Success: |
| 1480 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1482 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1483 | // C++ [over.ics.user]p1: |
| 1484 | // If the user-defined conversion is specified by a |
| 1485 | // constructor (12.3.1), the initial standard conversion |
| 1486 | // sequence converts the source type to the type required by |
| 1487 | // the argument of the constructor. |
| 1488 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1489 | QualType ThisType = Constructor->getThisType(Context); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1490 | if (Best->Conversions[0].ConversionKind == |
| 1491 | ImplicitConversionSequence::EllipsisConversion) |
| 1492 | User.EllipsisConversion = true; |
| 1493 | else { |
| 1494 | User.Before = Best->Conversions[0].Standard; |
| 1495 | User.EllipsisConversion = false; |
| 1496 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1497 | User.ConversionFunction = Constructor; |
| 1498 | User.After.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | User.After.FromTypePtr |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1500 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1501 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1502 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1503 | } else if (CXXConversionDecl *Conversion |
| 1504 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1505 | // C++ [over.ics.user]p1: |
| 1506 | // |
| 1507 | // [...] If the user-defined conversion is specified by a |
| 1508 | // conversion function (12.3.2), the initial standard |
| 1509 | // conversion sequence converts the source type to the |
| 1510 | // implicit object parameter of the conversion function. |
| 1511 | User.Before = Best->Conversions[0].Standard; |
| 1512 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1513 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
| 1515 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1516 | // The second standard conversion sequence converts the |
| 1517 | // result of the user-defined conversion to the target type |
| 1518 | // for the sequence. Since an implicit conversion sequence |
| 1519 | // is an initialization, the special rules for |
| 1520 | // initialization by user-defined conversion apply when |
| 1521 | // selecting the best user-defined conversion for a |
| 1522 | // user-defined conversion sequence (see 13.3.3 and |
| 1523 | // 13.3.3.1). |
| 1524 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1525 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1526 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1527 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1528 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1531 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1532 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1533 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1534 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1535 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1536 | |
| 1537 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1538 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1541 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1542 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1543 | |
| 1544 | bool |
| 1545 | Sema::DiagnoseAmbiguousUserDefinedConversion(Expr *From, QualType ToType) { |
| 1546 | ImplicitConversionSequence ICS; |
| 1547 | OverloadCandidateSet CandidateSet; |
| 1548 | OverloadingResult OvResult = |
| 1549 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1550 | CandidateSet, true, false, false); |
| 1551 | if (OvResult != OR_Ambiguous) |
| 1552 | return false; |
| 1553 | Diag(From->getSourceRange().getBegin(), |
| 1554 | diag::err_typecheck_ambiguous_condition) |
| 1555 | << From->getType() << ToType << From->getSourceRange(); |
| 1556 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 1557 | return true; |
| 1558 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1559 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1560 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1561 | /// conversion sequences to determine whether one is better than the |
| 1562 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1564 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1565 | const ImplicitConversionSequence& ICS2) |
| 1566 | { |
| 1567 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1568 | // conversion sequences (as defined in 13.3.3.1) |
| 1569 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1570 | // conversion sequence than a user-defined conversion sequence or |
| 1571 | // an ellipsis conversion sequence, and |
| 1572 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1573 | // conversion sequence than an ellipsis conversion sequence |
| 1574 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | // |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1576 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1577 | return ImplicitConversionSequence::Better; |
| 1578 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1579 | return ImplicitConversionSequence::Worse; |
| 1580 | |
| 1581 | // Two implicit conversion sequences of the same form are |
| 1582 | // indistinguishable conversion sequences unless one of the |
| 1583 | // following rules apply: (C++ 13.3.3.2p3): |
| 1584 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1585 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | else if (ICS1.ConversionKind == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1587 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1588 | // User-defined conversion sequence U1 is a better conversion |
| 1589 | // sequence than another user-defined conversion sequence U2 if |
| 1590 | // they contain the same user-defined conversion function or |
| 1591 | // constructor and if the second standard conversion sequence of |
| 1592 | // U1 is better than the second standard conversion sequence of |
| 1593 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1595 | ICS2.UserDefined.ConversionFunction) |
| 1596 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1597 | ICS2.UserDefined.After); |
| 1598 | } |
| 1599 | |
| 1600 | return ImplicitConversionSequence::Indistinguishable; |
| 1601 | } |
| 1602 | |
| 1603 | /// CompareStandardConversionSequences - Compare two standard |
| 1604 | /// conversion sequences to determine whether one is better than the |
| 1605 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1607 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1608 | const StandardConversionSequence& SCS2) |
| 1609 | { |
| 1610 | // Standard conversion sequence S1 is a better conversion sequence |
| 1611 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1612 | |
| 1613 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1614 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1615 | // excluding any Lvalue Transformation; the identity conversion |
| 1616 | // sequence is considered to be a subsequence of any |
| 1617 | // non-identity conversion sequence) or, if not that, |
| 1618 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1619 | // Neither is a proper subsequence of the other. Do nothing. |
| 1620 | ; |
| 1621 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1622 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1624 | SCS1.Third == ICK_Identity)) |
| 1625 | // SCS1 is a proper subsequence of SCS2. |
| 1626 | return ImplicitConversionSequence::Better; |
| 1627 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1628 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1630 | SCS2.Third == ICK_Identity)) |
| 1631 | // SCS2 is a proper subsequence of SCS1. |
| 1632 | return ImplicitConversionSequence::Worse; |
| 1633 | |
| 1634 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1635 | // defined below), or, if not that, |
| 1636 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1637 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1638 | if (Rank1 < Rank2) |
| 1639 | return ImplicitConversionSequence::Better; |
| 1640 | else if (Rank2 < Rank1) |
| 1641 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1642 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1643 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1644 | // are indistinguishable unless one of the following rules |
| 1645 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1647 | // A conversion that is not a conversion of a pointer, or |
| 1648 | // pointer to member, to bool is better than another conversion |
| 1649 | // that is such a conversion. |
| 1650 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1651 | return SCS2.isPointerConversionToBool() |
| 1652 | ? ImplicitConversionSequence::Better |
| 1653 | : ImplicitConversionSequence::Worse; |
| 1654 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1655 | // C++ [over.ics.rank]p4b2: |
| 1656 | // |
| 1657 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1658 | // conversion of B* to A* is better than conversion of B* to |
| 1659 | // void*, and conversion of A* to void* is better than conversion |
| 1660 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1662 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1664 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1665 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1666 | // Exactly one of the conversion sequences is a conversion to |
| 1667 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1668 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1669 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1670 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1671 | // Neither conversion sequence converts to a void pointer; compare |
| 1672 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1673 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1674 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1675 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1676 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1677 | // Both conversion sequences are conversions to void |
| 1678 | // pointers. Compare the source types to determine if there's an |
| 1679 | // inheritance relationship in their sources. |
| 1680 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1681 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1682 | |
| 1683 | // Adjust the types we're converting from via the array-to-pointer |
| 1684 | // conversion, if we need to. |
| 1685 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1686 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1687 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1688 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1689 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1691 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1692 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1693 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1694 | |
| 1695 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1696 | return ImplicitConversionSequence::Better; |
| 1697 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1698 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Objective-C++: If one interface is more specific than the |
| 1701 | // other, it is the better one. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1702 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1703 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1704 | if (FromIface1 && FromIface1) { |
| 1705 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1706 | return ImplicitConversionSequence::Better; |
| 1707 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1708 | return ImplicitConversionSequence::Worse; |
| 1709 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1710 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1711 | |
| 1712 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1713 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1715 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1716 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1718 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1719 | // C++0x [over.ics.rank]p3b4: |
| 1720 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1721 | // implicit object parameter of a non-static member function declared |
| 1722 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1723 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1724 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1725 | // or if the member function in this case has a ref qualifier. |
| 1726 | // (Of course, we don't have ref qualifiers yet.) |
| 1727 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1728 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1729 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1730 | |
| 1731 | // C++ [over.ics.rank]p3b4: |
| 1732 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1733 | // which the references refer are the same type except for |
| 1734 | // top-level cv-qualifiers, and the type to which the reference |
| 1735 | // initialized by S2 refers is more cv-qualified than the type |
| 1736 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1737 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1738 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1739 | T1 = Context.getCanonicalType(T1); |
| 1740 | T2 = Context.getCanonicalType(T2); |
| 1741 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1742 | if (T2.isMoreQualifiedThan(T1)) |
| 1743 | return ImplicitConversionSequence::Better; |
| 1744 | else if (T1.isMoreQualifiedThan(T2)) |
| 1745 | return ImplicitConversionSequence::Worse; |
| 1746 | } |
| 1747 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1748 | |
| 1749 | return ImplicitConversionSequence::Indistinguishable; |
| 1750 | } |
| 1751 | |
| 1752 | /// CompareQualificationConversions - Compares two standard conversion |
| 1753 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1755 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1756 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1758 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1759 | // -- S1 and S2 differ only in their qualification conversion and |
| 1760 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1761 | // cv-qualification signature of type T1 is a proper subset of |
| 1762 | // the cv-qualification signature of type T2, and S1 is not the |
| 1763 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1764 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1765 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1766 | return ImplicitConversionSequence::Indistinguishable; |
| 1767 | |
| 1768 | // FIXME: the example in the standard doesn't use a qualification |
| 1769 | // conversion (!) |
| 1770 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1771 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1772 | T1 = Context.getCanonicalType(T1); |
| 1773 | T2 = Context.getCanonicalType(T2); |
| 1774 | |
| 1775 | // If the types are the same, we won't learn anything by unwrapped |
| 1776 | // them. |
| 1777 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1778 | return ImplicitConversionSequence::Indistinguishable; |
| 1779 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1781 | = ImplicitConversionSequence::Indistinguishable; |
| 1782 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1783 | // Within each iteration of the loop, we check the qualifiers to |
| 1784 | // determine if this still looks like a qualification |
| 1785 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1786 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1787 | // until there are no more pointers or pointers-to-members left |
| 1788 | // to unwrap. This essentially mimics what |
| 1789 | // IsQualificationConversion does, but here we're checking for a |
| 1790 | // strict subset of qualifiers. |
| 1791 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1792 | // The qualifiers are the same, so this doesn't tell us anything |
| 1793 | // about how the sequences rank. |
| 1794 | ; |
| 1795 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1796 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1797 | if (Result == ImplicitConversionSequence::Worse) |
| 1798 | // Neither has qualifiers that are a subset of the other's |
| 1799 | // qualifiers. |
| 1800 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1802 | Result = ImplicitConversionSequence::Better; |
| 1803 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1804 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1805 | if (Result == ImplicitConversionSequence::Better) |
| 1806 | // Neither has qualifiers that are a subset of the other's |
| 1807 | // qualifiers. |
| 1808 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1810 | Result = ImplicitConversionSequence::Worse; |
| 1811 | } else { |
| 1812 | // Qualifiers are disjoint. |
| 1813 | return ImplicitConversionSequence::Indistinguishable; |
| 1814 | } |
| 1815 | |
| 1816 | // If the types after this point are equivalent, we're done. |
| 1817 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1818 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1821 | // Check that the winning standard conversion sequence isn't using |
| 1822 | // the deprecated string literal array to pointer conversion. |
| 1823 | switch (Result) { |
| 1824 | case ImplicitConversionSequence::Better: |
| 1825 | if (SCS1.Deprecated) |
| 1826 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1827 | break; |
| 1828 | |
| 1829 | case ImplicitConversionSequence::Indistinguishable: |
| 1830 | break; |
| 1831 | |
| 1832 | case ImplicitConversionSequence::Worse: |
| 1833 | if (SCS2.Deprecated) |
| 1834 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1835 | break; |
| 1836 | } |
| 1837 | |
| 1838 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1841 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1842 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1843 | /// various kinds of derived-to-base conversions (C++ |
| 1844 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1845 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1846 | ImplicitConversionSequence::CompareKind |
| 1847 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1848 | const StandardConversionSequence& SCS2) { |
| 1849 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1850 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1851 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1852 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1853 | |
| 1854 | // Adjust the types we're converting from via the array-to-pointer |
| 1855 | // conversion, if we need to. |
| 1856 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1857 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1858 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1859 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1860 | |
| 1861 | // Canonicalize all of the types. |
| 1862 | FromType1 = Context.getCanonicalType(FromType1); |
| 1863 | ToType1 = Context.getCanonicalType(ToType1); |
| 1864 | FromType2 = Context.getCanonicalType(FromType2); |
| 1865 | ToType2 = Context.getCanonicalType(ToType2); |
| 1866 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1867 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1868 | // |
| 1869 | // If class B is derived directly or indirectly from class A and |
| 1870 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1871 | // |
| 1872 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1873 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1877 | SCS2.Second == ICK_Pointer_Conversion && |
| 1878 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1879 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1880 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1882 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1884 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1885 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1886 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1887 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1888 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1889 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1890 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1891 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1892 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 1893 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1894 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1895 | // -- 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] | 1896 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1897 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1898 | return ImplicitConversionSequence::Better; |
| 1899 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1900 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1901 | |
| 1902 | if (ToIface1 && ToIface2) { |
| 1903 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1904 | return ImplicitConversionSequence::Better; |
| 1905 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1906 | return ImplicitConversionSequence::Worse; |
| 1907 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1908 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1909 | |
| 1910 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1911 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1912 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1913 | return ImplicitConversionSequence::Better; |
| 1914 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1915 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1917 | if (FromIface1 && FromIface2) { |
| 1918 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1919 | return ImplicitConversionSequence::Better; |
| 1920 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1921 | return ImplicitConversionSequence::Worse; |
| 1922 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1923 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1926 | // Compare based on reference bindings. |
| 1927 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1928 | SCS1.Second == ICK_Derived_To_Base) { |
| 1929 | // -- binding of an expression of type C to a reference of type |
| 1930 | // B& is better than binding an expression of type C to a |
| 1931 | // reference of type A&, |
| 1932 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1933 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1934 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1935 | return ImplicitConversionSequence::Better; |
| 1936 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1937 | return ImplicitConversionSequence::Worse; |
| 1938 | } |
| 1939 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1940 | // -- binding of an expression of type B to a reference of type |
| 1941 | // A& is better than binding an expression of type C to a |
| 1942 | // reference of type A&, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1943 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1944 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1945 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1946 | return ImplicitConversionSequence::Better; |
| 1947 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1948 | return ImplicitConversionSequence::Worse; |
| 1949 | } |
| 1950 | } |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 1951 | |
| 1952 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 1953 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 1954 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 1955 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 1956 | const MemberPointerType * FromMemPointer1 = |
| 1957 | FromType1->getAs<MemberPointerType>(); |
| 1958 | const MemberPointerType * ToMemPointer1 = |
| 1959 | ToType1->getAs<MemberPointerType>(); |
| 1960 | const MemberPointerType * FromMemPointer2 = |
| 1961 | FromType2->getAs<MemberPointerType>(); |
| 1962 | const MemberPointerType * ToMemPointer2 = |
| 1963 | ToType2->getAs<MemberPointerType>(); |
| 1964 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 1965 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 1966 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 1967 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 1968 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 1969 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 1970 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 1971 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 1972 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 1973 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1974 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1975 | return ImplicitConversionSequence::Worse; |
| 1976 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1977 | return ImplicitConversionSequence::Better; |
| 1978 | } |
| 1979 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 1980 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 1981 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1982 | return ImplicitConversionSequence::Better; |
| 1983 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1984 | return ImplicitConversionSequence::Worse; |
| 1985 | } |
| 1986 | } |
| 1987 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1988 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1989 | SCS1.Second == ICK_Derived_To_Base) { |
| 1990 | // -- conversion of C to B is better than conversion of C to A, |
| 1991 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1992 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1993 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1994 | return ImplicitConversionSequence::Better; |
| 1995 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1996 | return ImplicitConversionSequence::Worse; |
| 1997 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1999 | // -- conversion of B to A is better than conversion of C to A. |
| 2000 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 2001 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 2002 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2003 | return ImplicitConversionSequence::Better; |
| 2004 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2005 | return ImplicitConversionSequence::Worse; |
| 2006 | } |
| 2007 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2009 | return ImplicitConversionSequence::Indistinguishable; |
| 2010 | } |
| 2011 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2012 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2013 | /// ToType from the expression From. Return the implicit conversion |
| 2014 | /// sequence required to pass this argument, which may be a bad |
| 2015 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2016 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2017 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2018 | /// 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] | 2019 | ImplicitConversionSequence |
| 2020 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2021 | bool SuppressUserConversions, bool ForceRValue, |
| 2022 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2023 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2024 | ImplicitConversionSequence ICS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2026 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2027 | SuppressUserConversions, |
| 2028 | /*AllowExplicit=*/false, |
| 2029 | ForceRValue, |
| 2030 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2031 | return ICS; |
| 2032 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2034 | SuppressUserConversions, |
| 2035 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2036 | ForceRValue, |
| 2037 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2041 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2042 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2043 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2044 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2045 | /// differently in C++0x for this case. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2047 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2048 | if (!getLangOptions().CPlusPlus) { |
| 2049 | // In C, argument passing is the same as performing an assignment. |
| 2050 | QualType FromType = From->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2052 | AssignConvertType ConvTy = |
| 2053 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2054 | if (ConvTy != Compatible && |
| 2055 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2056 | ConvTy = Compatible; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2058 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 2059 | FromType, From, Flavor); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2060 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2061 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2062 | if (ToType->isReferenceType()) |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2063 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2064 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2065 | /*SuppressUserConversions=*/false, |
| 2066 | /*AllowExplicit=*/false, |
| 2067 | /*ForceRValue=*/false); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2068 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2069 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 2070 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2071 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2072 | if (!DiagnoseAmbiguousUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2073 | return Diag(From->getSourceRange().getBegin(), |
| 2074 | diag::err_typecheck_convert_incompatible) |
| 2075 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2076 | return true; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2079 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2080 | /// parameter of the given member function (@c Method) from the |
| 2081 | /// expression @p From. |
| 2082 | ImplicitConversionSequence |
| 2083 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 2084 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2085 | QualType ImplicitParamType |
| 2086 | = Context.getCVRQualifiedType(ClassType, Method->getTypeQualifiers()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2087 | |
| 2088 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2089 | // to exit early. |
| 2090 | ImplicitConversionSequence ICS; |
| 2091 | ICS.Standard.setAsIdentityConversion(); |
| 2092 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2093 | |
| 2094 | // We need to have an object of class type. |
| 2095 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2096 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2097 | FromType = PT->getPointeeType(); |
| 2098 | |
| 2099 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2100 | |
| 2101 | // The implicit object parmeter is has the type "reference to cv X", |
| 2102 | // where X is the class of which the function is a member |
| 2103 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2104 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2106 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2107 | // reference binding here, that allows class rvalues to bind to |
| 2108 | // non-constant references. |
| 2109 | |
| 2110 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2111 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2112 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | b1c2ea5 | 2009-11-05 00:07:36 +0000 | [diff] [blame] | 2113 | if (ImplicitParamType.getCVRQualifiers() != FromTypeCanon.getCVRQualifiers() && |
| 2114 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2115 | return ICS; |
| 2116 | |
| 2117 | // Check that we have either the same type or a derived type. It |
| 2118 | // affects the conversion rank. |
| 2119 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 2120 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 2121 | ICS.Standard.Second = ICK_Identity; |
| 2122 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2123 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2124 | else |
| 2125 | return ICS; |
| 2126 | |
| 2127 | // Success. Mark this as a reference binding. |
| 2128 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2129 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2130 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2131 | ICS.Standard.ReferenceBinding = true; |
| 2132 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2133 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2134 | return ICS; |
| 2135 | } |
| 2136 | |
| 2137 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2138 | /// the implicit object parameter for the given Method with the given |
| 2139 | /// expression. |
| 2140 | bool |
| 2141 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2142 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2144 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2146 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2147 | FromRecordType = PT->getPointeeType(); |
| 2148 | DestType = Method->getThisType(Context); |
| 2149 | } else { |
| 2150 | FromRecordType = From->getType(); |
| 2151 | DestType = ImplicitParamRecordType; |
| 2152 | } |
| 2153 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | ImplicitConversionSequence ICS |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2155 | = TryObjectArgumentInitialization(From, Method); |
| 2156 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2157 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2158 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2159 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2161 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2162 | CheckDerivedToBaseConversion(FromRecordType, |
| 2163 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2164 | From->getSourceRange().getBegin(), |
| 2165 | From->getSourceRange())) |
| 2166 | return true; |
| 2167 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 116b7d9 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2169 | /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2170 | return false; |
| 2171 | } |
| 2172 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2173 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2174 | /// expression From to bool (C++0x [conv]p3). |
| 2175 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2177 | // FIXME: Are these flags correct? |
| 2178 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2180 | /*ForceRValue=*/false, |
| 2181 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2185 | /// of the expression From to bool (C++0x [conv]p3). |
| 2186 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2187 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2188 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2189 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2190 | |
| 2191 | if (!DiagnoseAmbiguousUserDefinedConversion(From, Context.BoolTy)) |
| 2192 | return Diag(From->getSourceRange().getBegin(), |
| 2193 | diag::err_typecheck_bool_condition) |
| 2194 | << From->getType() << From->getSourceRange(); |
| 2195 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2198 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2199 | /// candidate functions, using the given function call arguments. If |
| 2200 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2201 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2202 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2203 | /// hacky way to implement the overloading rules for elidable copy |
| 2204 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2205 | /// |
| 2206 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2207 | /// based on an incomplete set of function arguments. This feature is used by |
| 2208 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | void |
| 2210 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2211 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2212 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2213 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2214 | bool ForceRValue, |
| 2215 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2217 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2218 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2220 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2222 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2224 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2225 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2226 | // If we get here, it's because we're calling a member function |
| 2227 | // that is named without a member access expression (e.g., |
| 2228 | // "this->f") that was either written explicitly or created |
| 2229 | // implicitly. This can happen with a qualified call to a member |
| 2230 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2231 | // object argument (C++ [over.call.func]p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2233 | SuppressUserConversions, ForceRValue); |
| 2234 | return; |
| 2235 | } |
| 2236 | // We treat a constructor like a non-member function, since its object |
| 2237 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2240 | if (!CandidateSet.isNewCandidate(Function)) |
| 2241 | return; |
| 2242 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2243 | // Add this candidate |
| 2244 | CandidateSet.push_back(OverloadCandidate()); |
| 2245 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2246 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2247 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2248 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2249 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2250 | |
| 2251 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2252 | |
| 2253 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2254 | // parameters is viable only if it has an ellipsis in its parameter |
| 2255 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2256 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2257 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2258 | Candidate.Viable = false; |
| 2259 | return; |
| 2260 | } |
| 2261 | |
| 2262 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2263 | // is viable only if the (m+1)st parameter has a default argument |
| 2264 | // (8.3.6). For the purposes of overload resolution, the |
| 2265 | // parameter list is truncated on the right, so that there are |
| 2266 | // exactly m parameters. |
| 2267 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2268 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2269 | // Not enough arguments. |
| 2270 | Candidate.Viable = false; |
| 2271 | return; |
| 2272 | } |
| 2273 | |
| 2274 | // Determine the implicit conversion sequences for each of the |
| 2275 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2276 | Candidate.Conversions.resize(NumArgs); |
| 2277 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2278 | if (ArgIdx < NumArgsInProto) { |
| 2279 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2280 | // exist for each argument an implicit conversion sequence |
| 2281 | // (13.3.3.1) that converts that argument to the corresponding |
| 2282 | // parameter of F. |
| 2283 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | Candidate.Conversions[ArgIdx] |
| 2285 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2286 | SuppressUserConversions, ForceRValue, |
| 2287 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2289 | == ImplicitConversionSequence::BadConversion) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2290 | // 13.3.3.1-p10 If several different sequences of conversions exist that |
| 2291 | // each convert the argument to the parameter type, the implicit conversion |
| 2292 | // sequence associated with the parameter is defined to be the unique conversion |
| 2293 | // sequence designated the ambiguous conversion sequence. For the purpose of |
| 2294 | // ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous |
| 2295 | // conversion sequence is treated as a user-defined sequence that is |
| 2296 | // indistinguishable from any other user-defined conversion sequence |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2297 | if (!Candidate.Conversions[ArgIdx].ConversionFunctionSet.empty()) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2298 | Candidate.Conversions[ArgIdx].ConversionKind = |
| 2299 | ImplicitConversionSequence::UserDefinedConversion; |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2300 | // Set the conversion function to one of them. As due to ambiguity, |
| 2301 | // they carry the same weight and is needed for overload resolution |
| 2302 | // later. |
| 2303 | Candidate.Conversions[ArgIdx].UserDefined.ConversionFunction = |
| 2304 | Candidate.Conversions[ArgIdx].ConversionFunctionSet[0]; |
| 2305 | } |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2306 | else { |
| 2307 | Candidate.Viable = false; |
| 2308 | break; |
| 2309 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2310 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2311 | } else { |
| 2312 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2313 | // argument for which there is no corresponding parameter is |
| 2314 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2316 | = ImplicitConversionSequence::EllipsisConversion; |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2321 | /// \brief Add all of the function declarations in the given function set to |
| 2322 | /// the overload canddiate set. |
| 2323 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2324 | Expr **Args, unsigned NumArgs, |
| 2325 | OverloadCandidateSet& CandidateSet, |
| 2326 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2328 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2329 | F != FEnd; ++F) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2330 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2331 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2332 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
| 2333 | Args[0], Args + 1, NumArgs - 1, |
| 2334 | CandidateSet, SuppressUserConversions); |
| 2335 | else |
| 2336 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2337 | SuppressUserConversions); |
| 2338 | } else { |
| 2339 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2340 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2341 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2342 | AddMethodTemplateCandidate(FunTmpl, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2343 | /*FIXME: explicit args */false, 0, 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2344 | Args[0], Args + 1, NumArgs - 1, |
| 2345 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2346 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2347 | else |
| 2348 | AddTemplateOverloadCandidate(FunTmpl, |
| 2349 | /*FIXME: explicit args */false, 0, 0, |
| 2350 | Args, NumArgs, CandidateSet, |
| 2351 | SuppressUserConversions); |
| 2352 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2353 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2356 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2357 | /// of candidate functions, using the given function call arguments |
| 2358 | /// and the object argument (@c Object). For example, in a call |
| 2359 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2360 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2361 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2362 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2363 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2364 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | void |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2366 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2367 | Expr **Args, unsigned NumArgs, |
| 2368 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | bool SuppressUserConversions, bool ForceRValue) { |
| 2370 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2371 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2372 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2373 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2374 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2375 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2376 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2378 | if (!CandidateSet.isNewCandidate(Method)) |
| 2379 | return; |
| 2380 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2381 | // Add this candidate |
| 2382 | CandidateSet.push_back(OverloadCandidate()); |
| 2383 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2384 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2385 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2386 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2387 | |
| 2388 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2389 | |
| 2390 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2391 | // parameters is viable only if it has an ellipsis in its parameter |
| 2392 | // list (8.3.5). |
| 2393 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2394 | Candidate.Viable = false; |
| 2395 | return; |
| 2396 | } |
| 2397 | |
| 2398 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2399 | // is viable only if the (m+1)st parameter has a default argument |
| 2400 | // (8.3.6). For the purposes of overload resolution, the |
| 2401 | // parameter list is truncated on the right, so that there are |
| 2402 | // exactly m parameters. |
| 2403 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2404 | if (NumArgs < MinRequiredArgs) { |
| 2405 | // Not enough arguments. |
| 2406 | Candidate.Viable = false; |
| 2407 | return; |
| 2408 | } |
| 2409 | |
| 2410 | Candidate.Viable = true; |
| 2411 | Candidate.Conversions.resize(NumArgs + 1); |
| 2412 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2413 | if (Method->isStatic() || !Object) |
| 2414 | // The implicit object argument is ignored. |
| 2415 | Candidate.IgnoreObjectArgument = true; |
| 2416 | else { |
| 2417 | // Determine the implicit conversion sequence for the object |
| 2418 | // parameter. |
| 2419 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2421 | == ImplicitConversionSequence::BadConversion) { |
| 2422 | Candidate.Viable = false; |
| 2423 | return; |
| 2424 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | // Determine the implicit conversion sequences for each of the |
| 2428 | // arguments. |
| 2429 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2430 | if (ArgIdx < NumArgsInProto) { |
| 2431 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2432 | // exist for each argument an implicit conversion sequence |
| 2433 | // (13.3.3.1) that converts that argument to the corresponding |
| 2434 | // parameter of F. |
| 2435 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | Candidate.Conversions[ArgIdx + 1] |
| 2437 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2438 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2439 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2441 | == ImplicitConversionSequence::BadConversion) { |
| 2442 | Candidate.Viable = false; |
| 2443 | break; |
| 2444 | } |
| 2445 | } else { |
| 2446 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2447 | // argument for which there is no corresponding parameter is |
| 2448 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2450 | = ImplicitConversionSequence::EllipsisConversion; |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2455 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2456 | /// set, using template argument deduction to produce an appropriate member |
| 2457 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2459 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2460 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2461 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2462 | unsigned NumExplicitTemplateArgs, |
| 2463 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2464 | OverloadCandidateSet& CandidateSet, |
| 2465 | bool SuppressUserConversions, |
| 2466 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2467 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2468 | return; |
| 2469 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2470 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2471 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2472 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | // 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] | 2474 | // candidate functions in the usual way.113) A given name can refer to one |
| 2475 | // or more function templates and also to a set of overloaded non-template |
| 2476 | // functions. In such a case, the candidate functions generated from each |
| 2477 | // function template are combined with the set of non-template candidate |
| 2478 | // functions. |
| 2479 | TemplateDeductionInfo Info(Context); |
| 2480 | FunctionDecl *Specialization = 0; |
| 2481 | if (TemplateDeductionResult Result |
| 2482 | = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs, |
| 2483 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2484 | Args, NumArgs, Specialization, Info)) { |
| 2485 | // FIXME: Record what happened with template argument deduction, so |
| 2486 | // that we can give the user a beautiful diagnostic. |
| 2487 | (void)Result; |
| 2488 | return; |
| 2489 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2491 | // Add the function template specialization produced by template argument |
| 2492 | // deduction as a candidate. |
| 2493 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2494 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2495 | "Specialization is not a member function?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2496 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2497 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2498 | } |
| 2499 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2500 | /// \brief Add a C++ function template specialization as a candidate |
| 2501 | /// in the candidate set, using template argument deduction to produce |
| 2502 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2504 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2505 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2506 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2507 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2508 | Expr **Args, unsigned NumArgs, |
| 2509 | OverloadCandidateSet& CandidateSet, |
| 2510 | bool SuppressUserConversions, |
| 2511 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2512 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2513 | return; |
| 2514 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2515 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2516 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2517 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | // 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] | 2519 | // candidate functions in the usual way.113) A given name can refer to one |
| 2520 | // or more function templates and also to a set of overloaded non-template |
| 2521 | // functions. In such a case, the candidate functions generated from each |
| 2522 | // function template are combined with the set of non-template candidate |
| 2523 | // functions. |
| 2524 | TemplateDeductionInfo Info(Context); |
| 2525 | FunctionDecl *Specialization = 0; |
| 2526 | if (TemplateDeductionResult Result |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2527 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2528 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2529 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2530 | // FIXME: Record what happened with template argument deduction, so |
| 2531 | // that we can give the user a beautiful diagnostic. |
| 2532 | (void)Result; |
| 2533 | return; |
| 2534 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2536 | // Add the function template specialization produced by template argument |
| 2537 | // deduction as a candidate. |
| 2538 | assert(Specialization && "Missing function template specialization?"); |
| 2539 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2540 | SuppressUserConversions, ForceRValue); |
| 2541 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2543 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2545 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | /// 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] | 2547 | /// (which may or may not be the same type as the type that the |
| 2548 | /// conversion function produces). |
| 2549 | void |
| 2550 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2551 | Expr *From, QualType ToType, |
| 2552 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2553 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2554 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2555 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2556 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2557 | return; |
| 2558 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2559 | // Add this candidate |
| 2560 | CandidateSet.push_back(OverloadCandidate()); |
| 2561 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2562 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2563 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2564 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2565 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2567 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2568 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2569 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2570 | // Determine the implicit conversion sequence for the implicit |
| 2571 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2572 | Candidate.Viable = true; |
| 2573 | Candidate.Conversions.resize(1); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2574 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2575 | // Conversion functions to a different type in the base class is visible in |
| 2576 | // the derived class. So, a derived to base conversion should not participate |
| 2577 | // in overload resolution. |
| 2578 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2579 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2581 | == ImplicitConversionSequence::BadConversion) { |
| 2582 | Candidate.Viable = false; |
| 2583 | return; |
| 2584 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2585 | |
| 2586 | // We won't go through a user-define type conversion function to convert a |
| 2587 | // derived to base as such conversions are given Conversion Rank. They only |
| 2588 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2589 | QualType FromCanon |
| 2590 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2591 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2592 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2593 | Candidate.Viable = false; |
| 2594 | return; |
| 2595 | } |
| 2596 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2597 | |
| 2598 | // To determine what the conversion from the result of calling the |
| 2599 | // conversion function to the type we're eventually trying to |
| 2600 | // convert to (ToType), we need to synthesize a call to the |
| 2601 | // conversion function and attempt copy initialization from it. This |
| 2602 | // makes sure that we get the right semantics with respect to |
| 2603 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2604 | // call on the stack and we don't need its arguments to be |
| 2605 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2607 | SourceLocation()); |
| 2608 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2609 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2610 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | |
| 2612 | // 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] | 2613 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2614 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2616 | Conversion->getConversionType().getNonReferenceType(), |
| 2617 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | ImplicitConversionSequence ICS = |
| 2619 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2620 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2621 | /*ForceRValue=*/false, |
| 2622 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2623 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2624 | switch (ICS.ConversionKind) { |
| 2625 | case ImplicitConversionSequence::StandardConversion: |
| 2626 | Candidate.FinalConversion = ICS.Standard; |
| 2627 | break; |
| 2628 | |
| 2629 | case ImplicitConversionSequence::BadConversion: |
| 2630 | Candidate.Viable = false; |
| 2631 | break; |
| 2632 | |
| 2633 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2635 | "Can only end up with a standard conversion sequence or failure"); |
| 2636 | } |
| 2637 | } |
| 2638 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2639 | /// \brief Adds a conversion function template specialization |
| 2640 | /// candidate to the overload set, using template argument deduction |
| 2641 | /// to deduce the template arguments of the conversion function |
| 2642 | /// template from the type that we are converting to (C++ |
| 2643 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2645 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2646 | Expr *From, QualType ToType, |
| 2647 | OverloadCandidateSet &CandidateSet) { |
| 2648 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2649 | "Only conversion function templates permitted here"); |
| 2650 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2651 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2652 | return; |
| 2653 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2654 | TemplateDeductionInfo Info(Context); |
| 2655 | CXXConversionDecl *Specialization = 0; |
| 2656 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2658 | Specialization, Info)) { |
| 2659 | // FIXME: Record what happened with template argument deduction, so |
| 2660 | // that we can give the user a beautiful diagnostic. |
| 2661 | (void)Result; |
| 2662 | return; |
| 2663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2665 | // Add the conversion function template specialization produced by |
| 2666 | // template argument deduction as a candidate. |
| 2667 | assert(Specialization && "Missing function template specialization?"); |
| 2668 | AddConversionCandidate(Specialization, From, ToType, CandidateSet); |
| 2669 | } |
| 2670 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2671 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2672 | /// converts the given @c Object to a function pointer via the |
| 2673 | /// conversion function @c Conversion, and then attempts to call it |
| 2674 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2675 | /// the type of function that we'll eventually be calling. |
| 2676 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2677 | const FunctionProtoType *Proto, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2678 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2679 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2680 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2681 | return; |
| 2682 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2683 | CandidateSet.push_back(OverloadCandidate()); |
| 2684 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2685 | Candidate.Function = 0; |
| 2686 | Candidate.Surrogate = Conversion; |
| 2687 | Candidate.Viable = true; |
| 2688 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2689 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2690 | Candidate.Conversions.resize(NumArgs + 1); |
| 2691 | |
| 2692 | // Determine the implicit conversion sequence for the implicit |
| 2693 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | ImplicitConversionSequence ObjectInit |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2695 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2696 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2697 | Candidate.Viable = false; |
| 2698 | return; |
| 2699 | } |
| 2700 | |
| 2701 | // The first conversion is actually a user-defined conversion whose |
| 2702 | // first conversion is ObjectInit's standard conversion (which is |
| 2703 | // effectively a reference binding). Record it as such. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2705 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2706 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2707 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2708 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2710 | = Candidate.Conversions[0].UserDefined.Before; |
| 2711 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2712 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2713 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2714 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2715 | |
| 2716 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2717 | // parameters is viable only if it has an ellipsis in its parameter |
| 2718 | // list (8.3.5). |
| 2719 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2720 | Candidate.Viable = false; |
| 2721 | return; |
| 2722 | } |
| 2723 | |
| 2724 | // Function types don't have any default arguments, so just check if |
| 2725 | // we have enough arguments. |
| 2726 | if (NumArgs < NumArgsInProto) { |
| 2727 | // Not enough arguments. |
| 2728 | Candidate.Viable = false; |
| 2729 | return; |
| 2730 | } |
| 2731 | |
| 2732 | // Determine the implicit conversion sequences for each of the |
| 2733 | // arguments. |
| 2734 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2735 | if (ArgIdx < NumArgsInProto) { |
| 2736 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2737 | // exist for each argument an implicit conversion sequence |
| 2738 | // (13.3.3.1) that converts that argument to the corresponding |
| 2739 | // parameter of F. |
| 2740 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | Candidate.Conversions[ArgIdx + 1] |
| 2742 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2743 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2744 | /*ForceRValue=*/false, |
| 2745 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2746 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2747 | == ImplicitConversionSequence::BadConversion) { |
| 2748 | Candidate.Viable = false; |
| 2749 | break; |
| 2750 | } |
| 2751 | } else { |
| 2752 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2753 | // argument for which there is no corresponding parameter is |
| 2754 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2756 | = ImplicitConversionSequence::EllipsisConversion; |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2761 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2762 | // operator overloading logic over to the scheme used by binary operators, which |
| 2763 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2764 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2765 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2766 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2767 | OverloadCandidateSet& CandidateSet, |
| 2768 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2769 | FunctionSet Functions; |
| 2770 | |
| 2771 | QualType T1 = Args[0]->getType(); |
| 2772 | QualType T2; |
| 2773 | if (NumArgs > 1) |
| 2774 | T2 = Args[1]->getType(); |
| 2775 | |
| 2776 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2777 | if (S) |
| 2778 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2779 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, NumArgs, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2780 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2781 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2782 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | /// \brief Add overload candidates for overloaded operators that are |
| 2786 | /// member functions. |
| 2787 | /// |
| 2788 | /// Add the overloaded operator candidates that are member functions |
| 2789 | /// for the operator Op that was used in an operator expression such |
| 2790 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2791 | /// CandidateSet will store the added overload candidates. (C++ |
| 2792 | /// [over.match.oper]). |
| 2793 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2794 | SourceLocation OpLoc, |
| 2795 | Expr **Args, unsigned NumArgs, |
| 2796 | OverloadCandidateSet& CandidateSet, |
| 2797 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2798 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2799 | |
| 2800 | // C++ [over.match.oper]p3: |
| 2801 | // For a unary operator @ with an operand of a type whose |
| 2802 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2803 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2804 | // a right operand of a type whose cv-unqualified version is T2, |
| 2805 | // three sets of candidate functions, designated member |
| 2806 | // candidates, non-member candidates and built-in candidates, are |
| 2807 | // constructed as follows: |
| 2808 | QualType T1 = Args[0]->getType(); |
| 2809 | QualType T2; |
| 2810 | if (NumArgs > 1) |
| 2811 | T2 = Args[1]->getType(); |
| 2812 | |
| 2813 | // -- If T1 is a class type, the set of member candidates is the |
| 2814 | // result of the qualified lookup of T1::operator@ |
| 2815 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2816 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2817 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2818 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2819 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2820 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2822 | LookupResult Operators; |
| 2823 | LookupQualifiedName(Operators, T1Rec->getDecl(), OpName, |
| 2824 | LookupOrdinaryName, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2826 | OperEnd = Operators.end(); |
| 2827 | Oper != OperEnd; |
Douglas Gregor | d9842d0 | 2009-10-14 16:50:13 +0000 | [diff] [blame] | 2828 | ++Oper) { |
| 2829 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Oper)) { |
| 2830 | AddMethodCandidate(Method, Args[0], Args+1, NumArgs - 1, CandidateSet, |
| 2831 | /*SuppressUserConversions=*/false); |
| 2832 | continue; |
| 2833 | } |
| 2834 | |
| 2835 | assert(isa<FunctionTemplateDecl>(*Oper) && |
| 2836 | isa<CXXMethodDecl>(cast<FunctionTemplateDecl>(*Oper) |
| 2837 | ->getTemplatedDecl()) && |
| 2838 | "Expected a member function template"); |
| 2839 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Oper), false, 0, 0, |
| 2840 | Args[0], Args+1, NumArgs - 1, CandidateSet, |
| 2841 | /*SuppressUserConversions=*/false); |
| 2842 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2843 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2846 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2847 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2848 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2849 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2850 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2851 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2852 | /// (at the beginning of the argument list) that will be contextually |
| 2853 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2855 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2856 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2857 | bool IsAssignmentOperator, |
| 2858 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2859 | // Add this candidate |
| 2860 | CandidateSet.push_back(OverloadCandidate()); |
| 2861 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2862 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2863 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2864 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2865 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2866 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2867 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2868 | |
| 2869 | // Determine the implicit conversion sequences for each of the |
| 2870 | // arguments. |
| 2871 | Candidate.Viable = true; |
| 2872 | Candidate.Conversions.resize(NumArgs); |
| 2873 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2874 | // C++ [over.match.oper]p4: |
| 2875 | // For the built-in assignment operators, conversions of the |
| 2876 | // left operand are restricted as follows: |
| 2877 | // -- no temporaries are introduced to hold the left operand, and |
| 2878 | // -- no user-defined conversions are applied to the left |
| 2879 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2881 | // |
| 2882 | // We block these conversions by turning off user-defined |
| 2883 | // conversions, since that is the only way that initialization of |
| 2884 | // a reference to a non-class type can occur from something that |
| 2885 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2886 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2888 | "Contextual conversion to bool requires bool type"); |
| 2889 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2890 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | Candidate.Conversions[ArgIdx] |
| 2892 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2893 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2894 | /*ForceRValue=*/false, |
| 2895 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2898 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2899 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2900 | break; |
| 2901 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2906 | /// candidate operator functions for built-in operators (C++ |
| 2907 | /// [over.built]). The types are separated into pointer types and |
| 2908 | /// enumeration types. |
| 2909 | class BuiltinCandidateTypeSet { |
| 2910 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2911 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2912 | |
| 2913 | /// PointerTypes - The set of pointer types that will be used in the |
| 2914 | /// built-in candidates. |
| 2915 | TypeSet PointerTypes; |
| 2916 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2917 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2918 | /// used in the built-in candidates. |
| 2919 | TypeSet MemberPointerTypes; |
| 2920 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2921 | /// EnumerationTypes - The set of enumeration types that will be |
| 2922 | /// used in the built-in candidates. |
| 2923 | TypeSet EnumerationTypes; |
| 2924 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2925 | /// Sema - The semantic analysis instance where we are building the |
| 2926 | /// candidate type set. |
| 2927 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2928 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2929 | /// Context - The AST context in which we will build the type sets. |
| 2930 | ASTContext &Context; |
| 2931 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 2932 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 2933 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2934 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2935 | |
| 2936 | public: |
| 2937 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2938 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2939 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2941 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2942 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2943 | void AddTypesConvertedFrom(QualType Ty, |
| 2944 | SourceLocation Loc, |
| 2945 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 2946 | bool AllowExplicitConversions, |
| 2947 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2948 | |
| 2949 | /// pointer_begin - First pointer type found; |
| 2950 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2951 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2952 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2953 | iterator pointer_end() { return PointerTypes.end(); } |
| 2954 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2955 | /// member_pointer_begin - First member pointer type found; |
| 2956 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2957 | |
| 2958 | /// member_pointer_end - Past the last member pointer type found; |
| 2959 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2960 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2961 | /// enumeration_begin - First enumeration type found; |
| 2962 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2963 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2964 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2965 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2966 | }; |
| 2967 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2968 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2969 | /// the set of pointer types along with any more-qualified variants of |
| 2970 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2971 | /// will add "int const *", "int const volatile *", "int const |
| 2972 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2973 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2974 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2975 | /// |
| 2976 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2977 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2978 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 2979 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2981 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2982 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2983 | return false; |
| 2984 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2985 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 2986 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2987 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2988 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2989 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 2990 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 2991 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 2992 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 2993 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 2994 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2995 | // Iterate through all strict supersets of BaseCVR. |
| 2996 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 2997 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 2998 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 2999 | // in the types. |
| 3000 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3001 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3002 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3003 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | return true; |
| 3007 | } |
| 3008 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3009 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3010 | /// to the set of pointer types along with any more-qualified variants of |
| 3011 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3012 | /// will add "int const *", "int const volatile *", "int const |
| 3013 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3014 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3015 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3016 | /// |
| 3017 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3018 | bool |
| 3019 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3020 | QualType Ty) { |
| 3021 | // Insert this type. |
| 3022 | if (!MemberPointerTypes.insert(Ty)) |
| 3023 | return false; |
| 3024 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3025 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3026 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3027 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3028 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3029 | const Type *ClassTy = PointerTy->getClass(); |
| 3030 | |
| 3031 | // Iterate through all strict supersets of the pointee type's CVR |
| 3032 | // qualifiers. |
| 3033 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3034 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3035 | if ((CVR | BaseCVR) != CVR) continue; |
| 3036 | |
| 3037 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3038 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | return true; |
| 3042 | } |
| 3043 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3044 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3045 | /// 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] | 3046 | /// primarily interested in pointer types and enumeration types. We also |
| 3047 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3048 | /// AllowUserConversions is true if we should look at the conversion |
| 3049 | /// functions of a class type, and AllowExplicitConversions if we |
| 3050 | /// should also include the explicit conversion functions of a class |
| 3051 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3053 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3054 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3055 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3056 | bool AllowExplicitConversions, |
| 3057 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3058 | // Only deal with canonical types. |
| 3059 | Ty = Context.getCanonicalType(Ty); |
| 3060 | |
| 3061 | // Look through reference types; they aren't part of the type of an |
| 3062 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3063 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3064 | Ty = RefTy->getPointeeType(); |
| 3065 | |
| 3066 | // We don't care about qualifiers on the type. |
| 3067 | Ty = Ty.getUnqualifiedType(); |
| 3068 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3069 | // If we're dealing with an array type, decay to the pointer. |
| 3070 | if (Ty->isArrayType()) |
| 3071 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3072 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3073 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3074 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3075 | |
| 3076 | // Insert our type, and its more-qualified variants, into the set |
| 3077 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3078 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3079 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3080 | } else if (Ty->isMemberPointerType()) { |
| 3081 | // Member pointers are far easier, since the pointee can't be converted. |
| 3082 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3083 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3084 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3085 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3086 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3087 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3088 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3089 | // No conversion functions in incomplete types. |
| 3090 | return; |
| 3091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3093 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3095 | = ClassDecl->getVisibleConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3097 | = Conversions->function_begin(); |
| 3098 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3099 | CXXConversionDecl *Conv; |
| 3100 | FunctionTemplateDecl *ConvTemplate; |
| 3101 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 3102 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3104 | // about which builtin types we can convert to. |
| 3105 | if (ConvTemplate) |
| 3106 | continue; |
| 3107 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3108 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3109 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3110 | VisibleQuals); |
| 3111 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | } |
| 3115 | } |
| 3116 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3117 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3118 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3119 | /// given type to the candidate set. |
| 3120 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3121 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3123 | unsigned NumArgs, |
| 3124 | OverloadCandidateSet &CandidateSet) { |
| 3125 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3127 | // T& operator=(T&, T) |
| 3128 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3129 | ParamTypes[1] = T; |
| 3130 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3131 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3132 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3133 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3134 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3135 | ParamTypes[0] |
| 3136 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3137 | ParamTypes[1] = T; |
| 3138 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3140 | } |
| 3141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3143 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3144 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3145 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3146 | Qualifiers VRQuals; |
| 3147 | const RecordType *TyRec; |
| 3148 | if (const MemberPointerType *RHSMPType = |
| 3149 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3150 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3151 | else |
| 3152 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3153 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3154 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3155 | VRQuals.addVolatile(); |
| 3156 | VRQuals.addRestrict(); |
| 3157 | return VRQuals; |
| 3158 | } |
| 3159 | |
| 3160 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 3161 | OverloadedFunctionDecl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3162 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3163 | |
| 3164 | for (OverloadedFunctionDecl::function_iterator Func |
| 3165 | = Conversions->function_begin(); |
| 3166 | Func != Conversions->function_end(); ++Func) { |
| 3167 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*Func)) { |
| 3168 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3169 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3170 | CanTy = ResTypeRef->getPointeeType(); |
| 3171 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3172 | // as see them. |
| 3173 | bool done = false; |
| 3174 | while (!done) { |
| 3175 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3176 | CanTy = ResTypePtr->getPointeeType(); |
| 3177 | else if (const MemberPointerType *ResTypeMPtr = |
| 3178 | CanTy->getAs<MemberPointerType>()) |
| 3179 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3180 | else |
| 3181 | done = true; |
| 3182 | if (CanTy.isVolatileQualified()) |
| 3183 | VRQuals.addVolatile(); |
| 3184 | if (CanTy.isRestrictQualified()) |
| 3185 | VRQuals.addRestrict(); |
| 3186 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3187 | return VRQuals; |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | return VRQuals; |
| 3192 | } |
| 3193 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3194 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3195 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3196 | /// on the operator @p Op and the arguments given. For example, if the |
| 3197 | /// operator is a binary '+', this routine might add "int |
| 3198 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3199 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3201 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3202 | Expr **Args, unsigned NumArgs, |
| 3203 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3204 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3205 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3206 | // that the first few of these types are the promoted integral |
| 3207 | // types; these types need to be first. |
| 3208 | // FIXME: What about complex? |
| 3209 | const unsigned FirstIntegralType = 0; |
| 3210 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3211 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3212 | LastPromotedIntegralType = 13; |
| 3213 | const unsigned FirstPromotedArithmeticType = 7, |
| 3214 | LastPromotedArithmeticType = 16; |
| 3215 | const unsigned NumArithmeticTypes = 16; |
| 3216 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3218 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3219 | Context.SignedCharTy, Context.ShortTy, |
| 3220 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3221 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3222 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3223 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3224 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3225 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3226 | "Invalid first promoted integral type"); |
| 3227 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3228 | == Context.UnsignedLongLongTy && |
| 3229 | "Invalid last promoted integral type"); |
| 3230 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3231 | "Invalid first promoted arithmetic type"); |
| 3232 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3233 | == Context.LongDoubleTy && |
| 3234 | "Invalid last promoted arithmetic type"); |
| 3235 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3236 | // Find all of the types that the arguments can convert to, but only |
| 3237 | // if the operator we're looking at has built-in operator candidates |
| 3238 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3239 | Qualifiers VisibleTypeConversionsQuals; |
| 3240 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3241 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3242 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3243 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3244 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3245 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3246 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3247 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3248 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3249 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3250 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3251 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3252 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3253 | OpLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3254 | true, |
| 3255 | (Op == OO_Exclaim || |
| 3256 | Op == OO_AmpAmp || |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3257 | Op == OO_PipePipe), |
| 3258 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | bool isComparison = false; |
| 3262 | switch (Op) { |
| 3263 | case OO_None: |
| 3264 | case NUM_OVERLOADED_OPERATORS: |
| 3265 | assert(false && "Expected an overloaded operator"); |
| 3266 | break; |
| 3267 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3268 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3270 | goto UnaryStar; |
| 3271 | else |
| 3272 | goto BinaryStar; |
| 3273 | break; |
| 3274 | |
| 3275 | case OO_Plus: // '+' is either unary or binary |
| 3276 | if (NumArgs == 1) |
| 3277 | goto UnaryPlus; |
| 3278 | else |
| 3279 | goto BinaryPlus; |
| 3280 | break; |
| 3281 | |
| 3282 | case OO_Minus: // '-' is either unary or binary |
| 3283 | if (NumArgs == 1) |
| 3284 | goto UnaryMinus; |
| 3285 | else |
| 3286 | goto BinaryMinus; |
| 3287 | break; |
| 3288 | |
| 3289 | case OO_Amp: // '&' is either unary or binary |
| 3290 | if (NumArgs == 1) |
| 3291 | goto UnaryAmp; |
| 3292 | else |
| 3293 | goto BinaryAmp; |
| 3294 | |
| 3295 | case OO_PlusPlus: |
| 3296 | case OO_MinusMinus: |
| 3297 | // C++ [over.built]p3: |
| 3298 | // |
| 3299 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3300 | // is either volatile or empty, there exist candidate operator |
| 3301 | // functions of the form |
| 3302 | // |
| 3303 | // VQ T& operator++(VQ T&); |
| 3304 | // T operator++(VQ T&, int); |
| 3305 | // |
| 3306 | // C++ [over.built]p4: |
| 3307 | // |
| 3308 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3309 | // than bool, and VQ is either volatile or empty, there exist |
| 3310 | // candidate operator functions of the form |
| 3311 | // |
| 3312 | // VQ T& operator--(VQ T&); |
| 3313 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3315 | Arith < NumArithmeticTypes; ++Arith) { |
| 3316 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3318 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3319 | |
| 3320 | // Non-volatile version. |
| 3321 | if (NumArgs == 1) |
| 3322 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3323 | else |
| 3324 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3325 | // heuristic to reduce number of builtin candidates in the set. |
| 3326 | // Add volatile version only if there are conversions to a volatile type. |
| 3327 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3328 | // Volatile version |
| 3329 | ParamTypes[0] |
| 3330 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3331 | if (NumArgs == 1) |
| 3332 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3333 | else |
| 3334 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3335 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | // C++ [over.built]p5: |
| 3339 | // |
| 3340 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3341 | // cv-unqualified object type, and VQ is either volatile or |
| 3342 | // empty, there exist candidate operator functions of the form |
| 3343 | // |
| 3344 | // T*VQ& operator++(T*VQ&); |
| 3345 | // T*VQ& operator--(T*VQ&); |
| 3346 | // T* operator++(T*VQ&, int); |
| 3347 | // T* operator--(T*VQ&, int); |
| 3348 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3349 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3350 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3351 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3352 | continue; |
| 3353 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3354 | QualType ParamTypes[2] = { |
| 3355 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3356 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3358 | // Without volatile |
| 3359 | if (NumArgs == 1) |
| 3360 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3361 | else |
| 3362 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3363 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3364 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3365 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3366 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3367 | ParamTypes[0] |
| 3368 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3369 | if (NumArgs == 1) |
| 3370 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3371 | else |
| 3372 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3373 | } |
| 3374 | } |
| 3375 | break; |
| 3376 | |
| 3377 | UnaryStar: |
| 3378 | // C++ [over.built]p6: |
| 3379 | // For every cv-qualified or cv-unqualified object type T, there |
| 3380 | // exist candidate operator functions of the form |
| 3381 | // |
| 3382 | // T& operator*(T*); |
| 3383 | // |
| 3384 | // C++ [over.built]p7: |
| 3385 | // For every function type T, there exist candidate operator |
| 3386 | // functions of the form |
| 3387 | // T& operator*(T*); |
| 3388 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3389 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3390 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3391 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3393 | &ParamTy, Args, 1, CandidateSet); |
| 3394 | } |
| 3395 | break; |
| 3396 | |
| 3397 | UnaryPlus: |
| 3398 | // C++ [over.built]p8: |
| 3399 | // For every type T, there exist candidate operator functions of |
| 3400 | // the form |
| 3401 | // |
| 3402 | // T* operator+(T*); |
| 3403 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3404 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3405 | QualType ParamTy = *Ptr; |
| 3406 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3409 | // Fall through |
| 3410 | |
| 3411 | UnaryMinus: |
| 3412 | // C++ [over.built]p9: |
| 3413 | // For every promoted arithmetic type T, there exist candidate |
| 3414 | // operator functions of the form |
| 3415 | // |
| 3416 | // T operator+(T); |
| 3417 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3419 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3420 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3421 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3422 | } |
| 3423 | break; |
| 3424 | |
| 3425 | case OO_Tilde: |
| 3426 | // C++ [over.built]p10: |
| 3427 | // For every promoted integral type T, there exist candidate |
| 3428 | // operator functions of the form |
| 3429 | // |
| 3430 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3432 | Int < LastPromotedIntegralType; ++Int) { |
| 3433 | QualType IntTy = ArithmeticTypes[Int]; |
| 3434 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3435 | } |
| 3436 | break; |
| 3437 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3438 | case OO_New: |
| 3439 | case OO_Delete: |
| 3440 | case OO_Array_New: |
| 3441 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3442 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3443 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3444 | break; |
| 3445 | |
| 3446 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3447 | UnaryAmp: |
| 3448 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3449 | // C++ [over.match.oper]p3: |
| 3450 | // -- For the operator ',', the unary operator '&', or the |
| 3451 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3452 | break; |
| 3453 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3454 | case OO_EqualEqual: |
| 3455 | case OO_ExclaimEqual: |
| 3456 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | // For every pointer to member type T, there exist candidate operator |
| 3458 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3459 | // |
| 3460 | // bool operator==(T,T); |
| 3461 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3463 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3464 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3465 | MemPtr != MemPtrEnd; |
| 3466 | ++MemPtr) { |
| 3467 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3468 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3470 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3471 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3473 | case OO_Less: |
| 3474 | case OO_Greater: |
| 3475 | case OO_LessEqual: |
| 3476 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3477 | // C++ [over.built]p15: |
| 3478 | // |
| 3479 | // For every pointer or enumeration type T, there exist |
| 3480 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3481 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3482 | // bool operator<(T, T); |
| 3483 | // bool operator>(T, T); |
| 3484 | // bool operator<=(T, T); |
| 3485 | // bool operator>=(T, T); |
| 3486 | // bool operator==(T, T); |
| 3487 | // bool operator!=(T, T); |
| 3488 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3489 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3490 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3491 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3494 | = CandidateTypes.enumeration_begin(); |
| 3495 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3496 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3497 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3498 | } |
| 3499 | |
| 3500 | // Fall through. |
| 3501 | isComparison = true; |
| 3502 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3503 | BinaryPlus: |
| 3504 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3505 | if (!isComparison) { |
| 3506 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3507 | |
| 3508 | // C++ [over.built]p13: |
| 3509 | // |
| 3510 | // For every cv-qualified or cv-unqualified object type T |
| 3511 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3513 | // T* operator+(T*, ptrdiff_t); |
| 3514 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3515 | // T* operator-(T*, ptrdiff_t); |
| 3516 | // T* operator+(ptrdiff_t, T*); |
| 3517 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3518 | // |
| 3519 | // C++ [over.built]p14: |
| 3520 | // |
| 3521 | // For every T, where T is a pointer to object type, there |
| 3522 | // exist candidate operator functions of the form |
| 3523 | // |
| 3524 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3525 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3526 | = CandidateTypes.pointer_begin(); |
| 3527 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3528 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3529 | |
| 3530 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3531 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3532 | |
| 3533 | if (Op == OO_Plus) { |
| 3534 | // T* operator+(ptrdiff_t, T*); |
| 3535 | ParamTypes[0] = ParamTypes[1]; |
| 3536 | ParamTypes[1] = *Ptr; |
| 3537 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3538 | } else { |
| 3539 | // ptrdiff_t operator-(T, T); |
| 3540 | ParamTypes[1] = *Ptr; |
| 3541 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3542 | Args, 2, CandidateSet); |
| 3543 | } |
| 3544 | } |
| 3545 | } |
| 3546 | // Fall through |
| 3547 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3548 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3549 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3550 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3551 | // C++ [over.built]p12: |
| 3552 | // |
| 3553 | // For every pair of promoted arithmetic types L and R, there |
| 3554 | // exist candidate operator functions of the form |
| 3555 | // |
| 3556 | // LR operator*(L, R); |
| 3557 | // LR operator/(L, R); |
| 3558 | // LR operator+(L, R); |
| 3559 | // LR operator-(L, R); |
| 3560 | // bool operator<(L, R); |
| 3561 | // bool operator>(L, R); |
| 3562 | // bool operator<=(L, R); |
| 3563 | // bool operator>=(L, R); |
| 3564 | // bool operator==(L, R); |
| 3565 | // bool operator!=(L, R); |
| 3566 | // |
| 3567 | // where LR is the result of the usual arithmetic conversions |
| 3568 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3569 | // |
| 3570 | // C++ [over.built]p24: |
| 3571 | // |
| 3572 | // For every pair of promoted arithmetic types L and R, there exist |
| 3573 | // candidate operator functions of the form |
| 3574 | // |
| 3575 | // LR operator?(bool, L, R); |
| 3576 | // |
| 3577 | // where LR is the result of the usual arithmetic conversions |
| 3578 | // between types L and R. |
| 3579 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3580 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3581 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3583 | Right < LastPromotedArithmeticType; ++Right) { |
| 3584 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3585 | QualType Result |
| 3586 | = isComparison |
| 3587 | ? Context.BoolTy |
| 3588 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3589 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3590 | } |
| 3591 | } |
| 3592 | break; |
| 3593 | |
| 3594 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3595 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3596 | case OO_Caret: |
| 3597 | case OO_Pipe: |
| 3598 | case OO_LessLess: |
| 3599 | case OO_GreaterGreater: |
| 3600 | // C++ [over.built]p17: |
| 3601 | // |
| 3602 | // For every pair of promoted integral types L and R, there |
| 3603 | // exist candidate operator functions of the form |
| 3604 | // |
| 3605 | // LR operator%(L, R); |
| 3606 | // LR operator&(L, R); |
| 3607 | // LR operator^(L, R); |
| 3608 | // LR operator|(L, R); |
| 3609 | // L operator<<(L, R); |
| 3610 | // L operator>>(L, R); |
| 3611 | // |
| 3612 | // where LR is the result of the usual arithmetic conversions |
| 3613 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3615 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3616 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3617 | Right < LastPromotedIntegralType; ++Right) { |
| 3618 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3619 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3620 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3621 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3622 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3623 | } |
| 3624 | } |
| 3625 | break; |
| 3626 | |
| 3627 | case OO_Equal: |
| 3628 | // C++ [over.built]p20: |
| 3629 | // |
| 3630 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3631 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3632 | // empty, there exist candidate operator functions of the form |
| 3633 | // |
| 3634 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3635 | for (BuiltinCandidateTypeSet::iterator |
| 3636 | Enum = CandidateTypes.enumeration_begin(), |
| 3637 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3638 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3640 | CandidateSet); |
| 3641 | for (BuiltinCandidateTypeSet::iterator |
| 3642 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3643 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3644 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3646 | CandidateSet); |
| 3647 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3648 | |
| 3649 | case OO_PlusEqual: |
| 3650 | case OO_MinusEqual: |
| 3651 | // C++ [over.built]p19: |
| 3652 | // |
| 3653 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3654 | // volatile or empty, there exist candidate operator functions |
| 3655 | // of the form |
| 3656 | // |
| 3657 | // T*VQ& operator=(T*VQ&, T*); |
| 3658 | // |
| 3659 | // C++ [over.built]p21: |
| 3660 | // |
| 3661 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3662 | // cv-unqualified object type and VQ is either volatile or |
| 3663 | // empty, there exist candidate operator functions of the form |
| 3664 | // |
| 3665 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3666 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3667 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3668 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3669 | QualType ParamTypes[2]; |
| 3670 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3671 | |
| 3672 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3673 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3674 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3675 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3676 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3677 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3678 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3679 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3680 | ParamTypes[0] |
| 3681 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3682 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3683 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3684 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3685 | } |
| 3686 | // Fall through. |
| 3687 | |
| 3688 | case OO_StarEqual: |
| 3689 | case OO_SlashEqual: |
| 3690 | // C++ [over.built]p18: |
| 3691 | // |
| 3692 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3693 | // VQ is either volatile or empty, and R is a promoted |
| 3694 | // arithmetic type, there exist candidate operator functions of |
| 3695 | // the form |
| 3696 | // |
| 3697 | // VQ L& operator=(VQ L&, R); |
| 3698 | // VQ L& operator*=(VQ L&, R); |
| 3699 | // VQ L& operator/=(VQ L&, R); |
| 3700 | // VQ L& operator+=(VQ L&, R); |
| 3701 | // VQ L& operator-=(VQ L&, R); |
| 3702 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3704 | Right < LastPromotedArithmeticType; ++Right) { |
| 3705 | QualType ParamTypes[2]; |
| 3706 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3707 | |
| 3708 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3709 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3710 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3711 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3712 | |
| 3713 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3714 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3715 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3716 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3717 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3718 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3719 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3720 | } |
| 3721 | } |
| 3722 | break; |
| 3723 | |
| 3724 | case OO_PercentEqual: |
| 3725 | case OO_LessLessEqual: |
| 3726 | case OO_GreaterGreaterEqual: |
| 3727 | case OO_AmpEqual: |
| 3728 | case OO_CaretEqual: |
| 3729 | case OO_PipeEqual: |
| 3730 | // C++ [over.built]p22: |
| 3731 | // |
| 3732 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3733 | // is either volatile or empty, and R is a promoted integral |
| 3734 | // type, there exist candidate operator functions of the form |
| 3735 | // |
| 3736 | // VQ L& operator%=(VQ L&, R); |
| 3737 | // VQ L& operator<<=(VQ L&, R); |
| 3738 | // VQ L& operator>>=(VQ L&, R); |
| 3739 | // VQ L& operator&=(VQ L&, R); |
| 3740 | // VQ L& operator^=(VQ L&, R); |
| 3741 | // VQ L& operator|=(VQ L&, R); |
| 3742 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3744 | Right < LastPromotedIntegralType; ++Right) { |
| 3745 | QualType ParamTypes[2]; |
| 3746 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3747 | |
| 3748 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3749 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3750 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 3751 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3752 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3753 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3754 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 3755 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3756 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3757 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3758 | } |
| 3759 | } |
| 3760 | break; |
| 3761 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3762 | case OO_Exclaim: { |
| 3763 | // C++ [over.operator]p23: |
| 3764 | // |
| 3765 | // There also exist candidate operator functions of the form |
| 3766 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3767 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3768 | // bool operator&&(bool, bool); [BELOW] |
| 3769 | // bool operator||(bool, bool); [BELOW] |
| 3770 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3771 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3772 | /*IsAssignmentOperator=*/false, |
| 3773 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3774 | break; |
| 3775 | } |
| 3776 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3777 | case OO_AmpAmp: |
| 3778 | case OO_PipePipe: { |
| 3779 | // C++ [over.operator]p23: |
| 3780 | // |
| 3781 | // There also exist candidate operator functions of the form |
| 3782 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3783 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3784 | // bool operator&&(bool, bool); |
| 3785 | // bool operator||(bool, bool); |
| 3786 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3787 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3788 | /*IsAssignmentOperator=*/false, |
| 3789 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3790 | break; |
| 3791 | } |
| 3792 | |
| 3793 | case OO_Subscript: |
| 3794 | // C++ [over.built]p13: |
| 3795 | // |
| 3796 | // For every cv-qualified or cv-unqualified object type T there |
| 3797 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3799 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3800 | // T& operator[](T*, ptrdiff_t); |
| 3801 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3802 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3803 | // T& operator[](ptrdiff_t, T*); |
| 3804 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3805 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3806 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3807 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3808 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3809 | |
| 3810 | // T& operator[](T*, ptrdiff_t) |
| 3811 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3812 | |
| 3813 | // T& operator[](ptrdiff_t, T*); |
| 3814 | ParamTypes[0] = ParamTypes[1]; |
| 3815 | ParamTypes[1] = *Ptr; |
| 3816 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3817 | } |
| 3818 | break; |
| 3819 | |
| 3820 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3821 | // C++ [over.built]p11: |
| 3822 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 3823 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 3824 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 3825 | // there exist candidate operator functions of the form |
| 3826 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 3827 | // where CV12 is the union of CV1 and CV2. |
| 3828 | { |
| 3829 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3830 | CandidateTypes.pointer_begin(); |
| 3831 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3832 | QualType C1Ty = (*Ptr); |
| 3833 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3834 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3835 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3836 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3837 | if (!isa<RecordType>(C1)) |
| 3838 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3839 | // heuristic to reduce number of builtin candidates in the set. |
| 3840 | // Add volatile/restrict version only if there are conversions to a |
| 3841 | // volatile/restrict type. |
| 3842 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 3843 | continue; |
| 3844 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 3845 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3846 | } |
| 3847 | for (BuiltinCandidateTypeSet::iterator |
| 3848 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3849 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3850 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 3851 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 3852 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 3853 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3854 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 3855 | break; |
| 3856 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 3857 | // build CV12 T& |
| 3858 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3859 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 3860 | T.isVolatileQualified()) |
| 3861 | continue; |
| 3862 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 3863 | T.isRestrictQualified()) |
| 3864 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3865 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3866 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 3867 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3868 | } |
| 3869 | } |
| 3870 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3871 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3872 | |
| 3873 | case OO_Conditional: |
| 3874 | // Note that we don't consider the first argument, since it has been |
| 3875 | // contextually converted to bool long ago. The candidates below are |
| 3876 | // therefore added as binary. |
| 3877 | // |
| 3878 | // C++ [over.built]p24: |
| 3879 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3880 | // there exist candidate operator functions of the form |
| 3881 | // |
| 3882 | // T operator?(bool, T, T); |
| 3883 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3884 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3885 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3886 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3887 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3888 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3889 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3890 | CandidateTypes.member_pointer_begin(), |
| 3891 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3892 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3893 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3894 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3895 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3899 | /// \brief Add function candidates found via argument-dependent lookup |
| 3900 | /// to the set of overloading candidates. |
| 3901 | /// |
| 3902 | /// This routine performs argument-dependent name lookup based on the |
| 3903 | /// given function name (which may also be an operator name) and adds |
| 3904 | /// all of the overload candidates found by ADL to the overload |
| 3905 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3907 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3908 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3909 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3910 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3911 | unsigned NumExplicitTemplateArgs, |
| 3912 | OverloadCandidateSet& CandidateSet, |
| 3913 | bool PartialOverloading) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3914 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3915 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3916 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 3917 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3918 | // Record all of the function candidates that we've already |
| 3919 | // added to the overload set, so that we don't add those same |
| 3920 | // candidates a second time. |
| 3921 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3922 | CandEnd = CandidateSet.end(); |
| 3923 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3924 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3925 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3926 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3927 | Functions.insert(FunTmpl); |
| 3928 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3929 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3930 | // FIXME: Pass in the explicit template arguments? |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 3931 | ArgumentDependentLookup(Name, /*Operator*/false, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3933 | // Erase all of the candidates we already knew about. |
| 3934 | // FIXME: This is suboptimal. Is there a better way? |
| 3935 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3936 | CandEnd = CandidateSet.end(); |
| 3937 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3938 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3939 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3940 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3941 | Functions.erase(FunTmpl); |
| 3942 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3943 | |
| 3944 | // For each of the ADL candidates we found, add it to the overload |
| 3945 | // set. |
| 3946 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3947 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3948 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3949 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
| 3950 | if (HasExplicitTemplateArgs) |
| 3951 | continue; |
| 3952 | |
| 3953 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 3954 | false, false, PartialOverloading); |
| 3955 | } else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3957 | HasExplicitTemplateArgs, |
| 3958 | ExplicitTemplateArgs, |
| 3959 | NumExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3960 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3961 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3964 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3965 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3966 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3967 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3969 | // Define viable functions to be better candidates than non-viable |
| 3970 | // functions. |
| 3971 | if (!Cand2.Viable) |
| 3972 | return Cand1.Viable; |
| 3973 | else if (!Cand1.Viable) |
| 3974 | return false; |
| 3975 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3976 | // C++ [over.match.best]p1: |
| 3977 | // |
| 3978 | // -- if F is a static member function, ICS1(F) is defined such |
| 3979 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3980 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3981 | // better nor worse than ICS1(F). |
| 3982 | unsigned StartArg = 0; |
| 3983 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3984 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3985 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3986 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | // A viable function F1 is defined to be a better function than another |
| 3988 | // 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] | 3989 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3990 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3991 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3992 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3993 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3994 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3995 | Cand2.Conversions[ArgIdx])) { |
| 3996 | case ImplicitConversionSequence::Better: |
| 3997 | // Cand1 has a better conversion sequence. |
| 3998 | HasBetterConversion = true; |
| 3999 | break; |
| 4000 | |
| 4001 | case ImplicitConversionSequence::Worse: |
| 4002 | // Cand1 can't be better than Cand2. |
| 4003 | return false; |
| 4004 | |
| 4005 | case ImplicitConversionSequence::Indistinguishable: |
| 4006 | // Do nothing. |
| 4007 | break; |
| 4008 | } |
| 4009 | } |
| 4010 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4012 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4013 | if (HasBetterConversion) |
| 4014 | return true; |
| 4015 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4017 | // specialization, or, if not that, |
| 4018 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4019 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4020 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4021 | |
| 4022 | // -- F1 and F2 are function template specializations, and the function |
| 4023 | // template for F1 is more specialized than the template for F2 |
| 4024 | // 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] | 4025 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4026 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4027 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4028 | if (FunctionTemplateDecl *BetterTemplate |
| 4029 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4030 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4031 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4032 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4033 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4034 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4035 | // -- the context is an initialization by user-defined conversion |
| 4036 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4037 | // from the return type of F1 to the destination type (i.e., |
| 4038 | // the type of the entity being initialized) is a better |
| 4039 | // conversion sequence than the standard conversion sequence |
| 4040 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4041 | if (Cand1.Function && Cand2.Function && |
| 4042 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4043 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4044 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4045 | Cand2.FinalConversion)) { |
| 4046 | case ImplicitConversionSequence::Better: |
| 4047 | // Cand1 has a better conversion sequence. |
| 4048 | return true; |
| 4049 | |
| 4050 | case ImplicitConversionSequence::Worse: |
| 4051 | // Cand1 can't be better than Cand2. |
| 4052 | return false; |
| 4053 | |
| 4054 | case ImplicitConversionSequence::Indistinguishable: |
| 4055 | // Do nothing |
| 4056 | break; |
| 4057 | } |
| 4058 | } |
| 4059 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4060 | return false; |
| 4061 | } |
| 4062 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4064 | /// within an overload candidate set. |
| 4065 | /// |
| 4066 | /// \param CandidateSet the set of candidate functions. |
| 4067 | /// |
| 4068 | /// \param Loc the location of the function name (or operator symbol) for |
| 4069 | /// which overload resolution occurs. |
| 4070 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4072 | /// function, Best points to the candidate function found. |
| 4073 | /// |
| 4074 | /// \returns The result of overload resolution. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | Sema::OverloadingResult |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4076 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4077 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4079 | // Find the best viable function. |
| 4080 | Best = CandidateSet.end(); |
| 4081 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4082 | Cand != CandidateSet.end(); ++Cand) { |
| 4083 | if (Cand->Viable) { |
| 4084 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 4085 | Best = Cand; |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | // If we didn't find any viable functions, abort. |
| 4090 | if (Best == CandidateSet.end()) |
| 4091 | return OR_No_Viable_Function; |
| 4092 | |
| 4093 | // Make sure that this function is better than every other viable |
| 4094 | // function. If not, we have an ambiguity. |
| 4095 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4096 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4098 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4099 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 4100 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4101 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4102 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4105 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4106 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4108 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4109 | return OR_Deleted; |
| 4110 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4111 | // C++ [basic.def.odr]p2: |
| 4112 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4114 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4115 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4116 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4117 | if (Best->Function) |
| 4118 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4119 | return OR_Success; |
| 4120 | } |
| 4121 | |
| 4122 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4123 | /// diagnostic messages containing the candidates in the candidate |
| 4124 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4125 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4126 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4127 | bool OnlyViable, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4128 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4129 | SourceLocation OpLoc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4130 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4131 | LastCand = CandidateSet.end(); |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4132 | bool Reported = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4133 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4134 | if (Cand->Viable || !OnlyViable) { |
| 4135 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4136 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4137 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4138 | // Deleted or "unavailable" function. |
| 4139 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 4140 | << Cand->Function->isDeleted(); |
Douglas Gregor | 1fdd89b | 2009-09-15 20:11:42 +0000 | [diff] [blame] | 4141 | } else if (FunctionTemplateDecl *FunTmpl |
| 4142 | = Cand->Function->getPrimaryTemplate()) { |
| 4143 | // Function template specialization |
| 4144 | // FIXME: Give a better reason! |
| 4145 | Diag(Cand->Function->getLocation(), diag::err_ovl_template_candidate) |
| 4146 | << getTemplateArgumentBindingsText(FunTmpl->getTemplateParameters(), |
| 4147 | *Cand->Function->getTemplateSpecializationArgs()); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4148 | } else { |
| 4149 | // Normal function |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 4150 | bool errReported = false; |
| 4151 | if (!Cand->Viable && Cand->Conversions.size() > 0) { |
| 4152 | for (int i = Cand->Conversions.size()-1; i >= 0; i--) { |
| 4153 | const ImplicitConversionSequence &Conversion = |
| 4154 | Cand->Conversions[i]; |
| 4155 | if ((Conversion.ConversionKind != |
| 4156 | ImplicitConversionSequence::BadConversion) || |
| 4157 | Conversion.ConversionFunctionSet.size() == 0) |
| 4158 | continue; |
| 4159 | Diag(Cand->Function->getLocation(), |
| 4160 | diag::err_ovl_candidate_not_viable) << (i+1); |
| 4161 | errReported = true; |
| 4162 | for (int j = Conversion.ConversionFunctionSet.size()-1; |
| 4163 | j >= 0; j--) { |
| 4164 | FunctionDecl *Func = Conversion.ConversionFunctionSet[j]; |
| 4165 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 4166 | } |
| 4167 | } |
| 4168 | } |
| 4169 | if (!errReported) |
| 4170 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4171 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4172 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4173 | // Desugar the type of the surrogate down to a function type, |
| 4174 | // retaining as many typedefs as possible while still showing |
| 4175 | // the function type (and, therefore, its parameter types). |
| 4176 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4177 | bool isLValueReference = false; |
| 4178 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4179 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4180 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4181 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4182 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4183 | isLValueReference = true; |
| 4184 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4185 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4186 | FnType = FnTypeRef->getPointeeType(); |
| 4187 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4188 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4189 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4190 | FnType = FnTypePtr->getPointeeType(); |
| 4191 | isPointer = true; |
| 4192 | } |
| 4193 | // Desugar down to a function type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4194 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4195 | // Reconstruct the pointer/reference as appropriate. |
| 4196 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4197 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 4198 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4199 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4200 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4201 | << FnType; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4202 | } else if (OnlyViable) { |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4203 | assert(Cand->Conversions.size() <= 2 && |
Fariborz Jahanian | ad3607d | 2009-10-09 17:09:58 +0000 | [diff] [blame] | 4204 | "builtin-binary-operator-not-binary"); |
Fariborz Jahanian | 866b274 | 2009-10-16 23:25:02 +0000 | [diff] [blame] | 4205 | std::string TypeStr("operator"); |
| 4206 | TypeStr += Opc; |
| 4207 | TypeStr += "("; |
| 4208 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4209 | if (Cand->Conversions.size() == 1) { |
| 4210 | TypeStr += ")"; |
| 4211 | Diag(OpLoc, diag::err_ovl_builtin_unary_candidate) << TypeStr; |
| 4212 | } |
| 4213 | else { |
| 4214 | TypeStr += ", "; |
| 4215 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4216 | TypeStr += ")"; |
| 4217 | Diag(OpLoc, diag::err_ovl_builtin_binary_candidate) << TypeStr; |
| 4218 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4219 | } |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4220 | else if (!Cand->Viable && !Reported) { |
| 4221 | // Non-viability might be due to ambiguous user-defined conversions, |
| 4222 | // needed for built-in operators. Report them as well, but only once |
| 4223 | // as we have typically many built-in candidates. |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4224 | unsigned NoOperands = Cand->Conversions.size(); |
| 4225 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4226 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
| 4227 | if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion || |
| 4228 | ICS.ConversionFunctionSet.empty()) |
| 4229 | continue; |
| 4230 | if (CXXConversionDecl *Func = dyn_cast<CXXConversionDecl>( |
| 4231 | Cand->Conversions[ArgIdx].ConversionFunctionSet[0])) { |
| 4232 | QualType FromTy = |
| 4233 | QualType( |
| 4234 | static_cast<Type*>(ICS.UserDefined.Before.FromTypePtr),0); |
| 4235 | Diag(OpLoc,diag::note_ambiguous_type_conversion) |
| 4236 | << FromTy << Func->getConversionType(); |
| 4237 | } |
| 4238 | for (unsigned j = 0; j < ICS.ConversionFunctionSet.size(); j++) { |
| 4239 | FunctionDecl *Func = |
| 4240 | Cand->Conversions[ArgIdx].ConversionFunctionSet[j]; |
| 4241 | Diag(Func->getLocation(),diag::err_ovl_candidate); |
| 4242 | } |
| 4243 | } |
| 4244 | Reported = true; |
| 4245 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4246 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4250 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4251 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4252 | /// expression with overloaded function type and @p ToType is the type |
| 4253 | /// we're trying to resolve to. For example: |
| 4254 | /// |
| 4255 | /// @code |
| 4256 | /// int f(double); |
| 4257 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4258 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4259 | /// int (*pfd)(double) = f; // selects f(double) |
| 4260 | /// @endcode |
| 4261 | /// |
| 4262 | /// This routine returns the resulting FunctionDecl if it could be |
| 4263 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4264 | /// routine will emit diagnostics if there is an error. |
| 4265 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4266 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4267 | bool Complain) { |
| 4268 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4269 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4270 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4271 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4272 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4273 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4274 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4275 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4276 | FunctionType = MemTypePtr->getPointeeType(); |
| 4277 | IsMember = true; |
| 4278 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4279 | |
| 4280 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4281 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4282 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4283 | return 0; |
| 4284 | |
| 4285 | // Find the actual overloaded function declaration. |
| 4286 | OverloadedFunctionDecl *Ovl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4287 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4288 | // C++ [over.over]p1: |
| 4289 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4290 | // overloaded function name is ignored (5.1). ] |
| 4291 | Expr *OvlExpr = From->IgnoreParens(); |
| 4292 | |
| 4293 | // C++ [over.over]p1: |
| 4294 | // [...] The overloaded function name can be preceded by the & |
| 4295 | // operator. |
| 4296 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4297 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4298 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4299 | } |
| 4300 | |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4301 | bool HasExplicitTemplateArgs = false; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4302 | const TemplateArgumentLoc *ExplicitTemplateArgs = 0; |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4303 | unsigned NumExplicitTemplateArgs = 0; |
| 4304 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4305 | // Try to dig out the overloaded function. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4306 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 4307 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4308 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4309 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4310 | HasExplicitTemplateArgs = DR->hasExplicitTemplateArgumentList(); |
| 4311 | ExplicitTemplateArgs = DR->getTemplateArgs(); |
| 4312 | NumExplicitTemplateArgs = DR->getNumTemplateArgs(); |
Anders Carlsson | 6e8f550 | 2009-10-07 22:26:29 +0000 | [diff] [blame] | 4313 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(OvlExpr)) { |
| 4314 | Ovl = dyn_cast<OverloadedFunctionDecl>(ME->getMemberDecl()); |
| 4315 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(ME->getMemberDecl()); |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4316 | HasExplicitTemplateArgs = ME->hasExplicitTemplateArgumentList(); |
| 4317 | ExplicitTemplateArgs = ME->getTemplateArgs(); |
| 4318 | NumExplicitTemplateArgs = ME->getNumTemplateArgs(); |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4319 | } else if (TemplateIdRefExpr *TIRE = dyn_cast<TemplateIdRefExpr>(OvlExpr)) { |
| 4320 | TemplateName Name = TIRE->getTemplateName(); |
| 4321 | Ovl = Name.getAsOverloadedFunctionDecl(); |
| 4322 | FunctionTemplate = |
| 4323 | dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 4324 | |
| 4325 | HasExplicitTemplateArgs = true; |
| 4326 | ExplicitTemplateArgs = TIRE->getTemplateArgs(); |
| 4327 | NumExplicitTemplateArgs = TIRE->getNumTemplateArgs(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4328 | } |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4329 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4330 | // If there's no overloaded function declaration or function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4331 | // we're done. |
| 4332 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4333 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4334 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4335 | OverloadIterator Fun; |
| 4336 | if (Ovl) |
| 4337 | Fun = Ovl; |
| 4338 | else |
| 4339 | Fun = FunctionTemplate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4341 | // Look through all of the overloaded functions, searching for one |
| 4342 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4343 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4344 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4345 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4346 | // C++ [over.over]p3: |
| 4347 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4348 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4349 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4350 | // type "pointer-to-member-function." |
| 4351 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4352 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4353 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4354 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4355 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4356 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4357 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4358 | // static when converting to member pointer. |
| 4359 | if (Method->isStatic() == IsMember) |
| 4360 | continue; |
| 4361 | } else if (IsMember) |
| 4362 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4363 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4364 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | // If the name is a function template, template argument deduction is |
| 4366 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4367 | // resulting template argument list is used to generate a single |
| 4368 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4369 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4370 | // 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] | 4371 | FunctionDecl *Specialization = 0; |
| 4372 | TemplateDeductionInfo Info(Context); |
| 4373 | if (TemplateDeductionResult Result |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4374 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 4375 | ExplicitTemplateArgs, |
| 4376 | NumExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4377 | FunctionType, Specialization, Info)) { |
| 4378 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4379 | (void)Result; |
| 4380 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4381 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4382 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4384 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4385 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4386 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4387 | } |
| 4388 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4389 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4390 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 4391 | // Skip non-static functions when converting to pointer, and static |
| 4392 | // when converting to member pointer. |
| 4393 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4394 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4395 | |
| 4396 | // If we have explicit template arguments, skip non-templates. |
| 4397 | if (HasExplicitTemplateArgs) |
| 4398 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4399 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4400 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4402 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4403 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4404 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4405 | FoundNonTemplateFunction = true; |
| 4406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4410 | // If there were 0 or 1 matches, we're done. |
| 4411 | if (Matches.empty()) |
| 4412 | return 0; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4413 | else if (Matches.size() == 1) { |
| 4414 | FunctionDecl *Result = *Matches.begin(); |
| 4415 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4416 | return Result; |
| 4417 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4418 | |
| 4419 | // C++ [over.over]p4: |
| 4420 | // If more than one function is selected, [...] |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4421 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4422 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4423 | // [...] and any given function template specialization F1 is |
| 4424 | // eliminated if the set contains a second function template |
| 4425 | // specialization whose function template is more specialized |
| 4426 | // than the function template of F1 according to the partial |
| 4427 | // ordering rules of 14.5.5.2. |
| 4428 | |
| 4429 | // The algorithm specified above is quadratic. We instead use a |
| 4430 | // two-pass algorithm (similar to the one used to identify the |
| 4431 | // best viable function in an overload set) that identifies the |
| 4432 | // best function template (if it exists). |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4433 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4434 | Matches.end()); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4435 | FunctionDecl *Result = |
| 4436 | getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4437 | TPOC_Other, From->getLocStart(), |
| 4438 | PDiag(), |
| 4439 | PDiag(diag::err_addr_ovl_ambiguous) |
| 4440 | << TemplateMatches[0]->getDeclName(), |
| 4441 | PDiag(diag::err_ovl_template_candidate)); |
| 4442 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4443 | return Result; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4444 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4446 | // [...] any function template specializations in the set are |
| 4447 | // eliminated if the set also contains a non-template function, [...] |
| 4448 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4449 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4450 | if ((*M)->getPrimaryTemplate() == 0) |
| 4451 | RemainingMatches.push_back(*M); |
| 4452 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4453 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4454 | // selected function. |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4455 | if (RemainingMatches.size() == 1) { |
| 4456 | FunctionDecl *Result = RemainingMatches.front(); |
| 4457 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4458 | return Result; |
| 4459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4460 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4461 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4462 | // returns (even if we issue the diagnostics here). |
| 4463 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4464 | << RemainingMatches[0]->getDeclName(); |
| 4465 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 4466 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4467 | return 0; |
| 4468 | } |
| 4469 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4470 | /// \brief Add a single candidate to the overload set. |
| 4471 | static void AddOverloadedCallCandidate(Sema &S, |
| 4472 | AnyFunctionDecl Callee, |
| 4473 | bool &ArgumentDependentLookup, |
| 4474 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4475 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4476 | unsigned NumExplicitTemplateArgs, |
| 4477 | Expr **Args, unsigned NumArgs, |
| 4478 | OverloadCandidateSet &CandidateSet, |
| 4479 | bool PartialOverloading) { |
| 4480 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
| 4481 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
| 4482 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 4483 | PartialOverloading); |
| 4484 | |
| 4485 | if (Func->getDeclContext()->isRecord() || |
| 4486 | Func->getDeclContext()->isFunctionOrMethod()) |
| 4487 | ArgumentDependentLookup = false; |
| 4488 | return; |
| 4489 | } |
| 4490 | |
| 4491 | FunctionTemplateDecl *FuncTemplate = cast<FunctionTemplateDecl>(Callee); |
| 4492 | S.AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 4493 | ExplicitTemplateArgs, |
| 4494 | NumExplicitTemplateArgs, |
| 4495 | Args, NumArgs, CandidateSet); |
| 4496 | |
| 4497 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 4498 | ArgumentDependentLookup = false; |
| 4499 | } |
| 4500 | |
| 4501 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 4502 | /// dependent lookup to the given overload set. |
| 4503 | void Sema::AddOverloadedCallCandidates(NamedDecl *Callee, |
| 4504 | DeclarationName &UnqualifiedName, |
| 4505 | bool &ArgumentDependentLookup, |
| 4506 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4507 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4508 | unsigned NumExplicitTemplateArgs, |
| 4509 | Expr **Args, unsigned NumArgs, |
| 4510 | OverloadCandidateSet &CandidateSet, |
| 4511 | bool PartialOverloading) { |
| 4512 | // Add the functions denoted by Callee to the set of candidate |
| 4513 | // functions. While we're doing so, track whether argument-dependent |
| 4514 | // lookup still applies, per: |
| 4515 | // |
| 4516 | // C++0x [basic.lookup.argdep]p3: |
| 4517 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4518 | // and let Y be the lookup set produced by argument dependent |
| 4519 | // lookup (defined as follows). If X contains |
| 4520 | // |
| 4521 | // -- a declaration of a class member, or |
| 4522 | // |
| 4523 | // -- a block-scope function declaration that is not a |
| 4524 | // using-declaration (FIXME: check for using declaration), or |
| 4525 | // |
| 4526 | // -- a declaration that is neither a function or a function |
| 4527 | // template |
| 4528 | // |
| 4529 | // then Y is empty. |
| 4530 | if (!Callee) { |
| 4531 | // Nothing to do. |
| 4532 | } else if (OverloadedFunctionDecl *Ovl |
| 4533 | = dyn_cast<OverloadedFunctionDecl>(Callee)) { |
| 4534 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4535 | FuncEnd = Ovl->function_end(); |
| 4536 | Func != FuncEnd; ++Func) |
| 4537 | AddOverloadedCallCandidate(*this, *Func, ArgumentDependentLookup, |
| 4538 | HasExplicitTemplateArgs, |
| 4539 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4540 | Args, NumArgs, CandidateSet, |
| 4541 | PartialOverloading); |
| 4542 | } else if (isa<FunctionDecl>(Callee) || isa<FunctionTemplateDecl>(Callee)) |
| 4543 | AddOverloadedCallCandidate(*this, |
| 4544 | AnyFunctionDecl::getFromNamedDecl(Callee), |
| 4545 | ArgumentDependentLookup, |
| 4546 | HasExplicitTemplateArgs, |
| 4547 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4548 | Args, NumArgs, CandidateSet, |
| 4549 | PartialOverloading); |
| 4550 | // FIXME: assert isa<FunctionDecl> || isa<FunctionTemplateDecl> rather than |
| 4551 | // checking dynamically. |
| 4552 | |
| 4553 | if (Callee) |
| 4554 | UnqualifiedName = Callee->getDeclName(); |
| 4555 | |
| 4556 | if (ArgumentDependentLookup) |
| 4557 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
| 4558 | HasExplicitTemplateArgs, |
| 4559 | ExplicitTemplateArgs, |
| 4560 | NumExplicitTemplateArgs, |
| 4561 | CandidateSet, |
| 4562 | PartialOverloading); |
| 4563 | } |
| 4564 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4565 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4566 | /// (which eventually refers to the declaration Func) and the call |
| 4567 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4568 | /// to a specific function. If overload resolution succeeds, returns |
| 4569 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4570 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4571 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4572 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4573 | DeclarationName UnqualifiedName, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4574 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4575 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4576 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4577 | SourceLocation LParenLoc, |
| 4578 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4579 | SourceLocation *CommaLocs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4580 | SourceLocation RParenLoc, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4581 | bool &ArgumentDependentLookup) { |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4582 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4583 | |
| 4584 | // Add the functions denoted by Callee to the set of candidate |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4585 | // functions. |
| 4586 | AddOverloadedCallCandidates(Callee, UnqualifiedName, ArgumentDependentLookup, |
| 4587 | HasExplicitTemplateArgs, ExplicitTemplateArgs, |
| 4588 | NumExplicitTemplateArgs, Args, NumArgs, |
| 4589 | CandidateSet); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4590 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4591 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4592 | case OR_Success: |
| 4593 | return Best->Function; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4594 | |
| 4595 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4596 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4597 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4598 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4599 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4600 | break; |
| 4601 | |
| 4602 | case OR_Ambiguous: |
| 4603 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4604 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4605 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4606 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4607 | |
| 4608 | case OR_Deleted: |
| 4609 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4610 | << Best->Function->isDeleted() |
| 4611 | << UnqualifiedName |
| 4612 | << Fn->getSourceRange(); |
| 4613 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4614 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4618 | // return NULL. |
| 4619 | Fn->Destroy(Context); |
| 4620 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4621 | Args[Arg]->Destroy(Context); |
| 4622 | return 0; |
| 4623 | } |
| 4624 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4625 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4626 | /// operator. |
| 4627 | /// |
| 4628 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4629 | /// |
| 4630 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4631 | /// operator. |
| 4632 | /// |
| 4633 | /// \param Functions The set of non-member functions that will be |
| 4634 | /// considered by overload resolution. The caller needs to build this |
| 4635 | /// set based on the context using, e.g., |
| 4636 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4637 | /// set should not contain any member functions; those will be added |
| 4638 | /// by CreateOverloadedUnaryOp(). |
| 4639 | /// |
| 4640 | /// \param input The input argument. |
| 4641 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4642 | unsigned OpcIn, |
| 4643 | FunctionSet &Functions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4644 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4645 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4646 | Expr *Input = (Expr *)input.get(); |
| 4647 | |
| 4648 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 4649 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 4650 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4651 | |
| 4652 | Expr *Args[2] = { Input, 0 }; |
| 4653 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4655 | // For post-increment and post-decrement, add the implicit '0' as |
| 4656 | // the second argument, so that we know this is a post-increment or |
| 4657 | // post-decrement. |
| 4658 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 4659 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4661 | SourceLocation()); |
| 4662 | NumArgs = 2; |
| 4663 | } |
| 4664 | |
| 4665 | if (Input->isTypeDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4667 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4669 | FuncEnd = Functions.end(); |
| 4670 | Func != FuncEnd; ++Func) |
| 4671 | Overloads->addOverload(*Func); |
| 4672 | |
| 4673 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4674 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4676 | input.release(); |
| 4677 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4678 | &Args[0], NumArgs, |
| 4679 | Context.DependentTy, |
| 4680 | OpLoc)); |
| 4681 | } |
| 4682 | |
| 4683 | // Build an empty overload set. |
| 4684 | OverloadCandidateSet CandidateSet; |
| 4685 | |
| 4686 | // Add the candidates from the given function set. |
| 4687 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4688 | |
| 4689 | // Add operator candidates that are member functions. |
| 4690 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4691 | |
| 4692 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4693 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4694 | |
| 4695 | // Perform overload resolution. |
| 4696 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4697 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4698 | case OR_Success: { |
| 4699 | // We found a built-in operator or an overloaded operator. |
| 4700 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4701 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4702 | if (FnDecl) { |
| 4703 | // We matched an overloaded operator. Build a call to that |
| 4704 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4706 | // Convert the arguments. |
| 4707 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4708 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4709 | return ExprError(); |
| 4710 | } else { |
| 4711 | // Convert the arguments. |
| 4712 | if (PerformCopyInitialization(Input, |
| 4713 | FnDecl->getParamDecl(0)->getType(), |
| 4714 | "passing")) |
| 4715 | return ExprError(); |
| 4716 | } |
| 4717 | |
| 4718 | // Determine the result type |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 4719 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4721 | // Build the actual expression node. |
| 4722 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4723 | SourceLocation()); |
| 4724 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4725 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4726 | input.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 4728 | ExprOwningPtr<CallExpr> TheCall(this, |
| 4729 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 4730 | &Input, 1, ResultTy, OpLoc)); |
| 4731 | |
| 4732 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 4733 | FnDecl)) |
| 4734 | return ExprError(); |
| 4735 | |
| 4736 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4737 | } else { |
| 4738 | // We matched a built-in operator. Convert the arguments, then |
| 4739 | // break out so that we will build the appropriate built-in |
| 4740 | // operator node. |
| 4741 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4742 | Best->Conversions[0], "passing")) |
| 4743 | return ExprError(); |
| 4744 | |
| 4745 | break; |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | case OR_No_Viable_Function: |
| 4750 | // No viable function; fall through to handling this as a |
| 4751 | // built-in operator, which will produce an error message for us. |
| 4752 | break; |
| 4753 | |
| 4754 | case OR_Ambiguous: |
| 4755 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4756 | << UnaryOperator::getOpcodeStr(Opc) |
| 4757 | << Input->getSourceRange(); |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4758 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 4759 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4760 | return ExprError(); |
| 4761 | |
| 4762 | case OR_Deleted: |
| 4763 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4764 | << Best->Function->isDeleted() |
| 4765 | << UnaryOperator::getOpcodeStr(Opc) |
| 4766 | << Input->getSourceRange(); |
| 4767 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4768 | return ExprError(); |
| 4769 | } |
| 4770 | |
| 4771 | // Either we found no viable overloaded operator or we matched a |
| 4772 | // built-in operator. In either case, fall through to trying to |
| 4773 | // build a built-in operation. |
| 4774 | input.release(); |
| 4775 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4776 | } |
| 4777 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4778 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4779 | /// operator. |
| 4780 | /// |
| 4781 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4782 | /// |
| 4783 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4784 | /// operator. |
| 4785 | /// |
| 4786 | /// \param Functions The set of non-member functions that will be |
| 4787 | /// considered by overload resolution. The caller needs to build this |
| 4788 | /// set based on the context using, e.g., |
| 4789 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4790 | /// set should not contain any member functions; those will be added |
| 4791 | /// by CreateOverloadedBinOp(). |
| 4792 | /// |
| 4793 | /// \param LHS Left-hand argument. |
| 4794 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4796 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | unsigned OpcIn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4798 | FunctionSet &Functions, |
| 4799 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4800 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4801 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4802 | |
| 4803 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4804 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4805 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4806 | |
| 4807 | // If either side is type-dependent, create an appropriate dependent |
| 4808 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4809 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 4810 | if (Functions.empty()) { |
| 4811 | // If there are no functions to store, just build a dependent |
| 4812 | // BinaryOperator or CompoundAssignment. |
| 4813 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 4814 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 4815 | Context.DependentTy, OpLoc)); |
| 4816 | |
| 4817 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 4818 | Context.DependentTy, |
| 4819 | Context.DependentTy, |
| 4820 | Context.DependentTy, |
| 4821 | OpLoc)); |
| 4822 | } |
| 4823 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4824 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4825 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4827 | FuncEnd = Functions.end(); |
| 4828 | Func != FuncEnd; ++Func) |
| 4829 | Overloads->addOverload(*Func); |
| 4830 | |
| 4831 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4832 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4833 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4834 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4835 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4836 | Context.DependentTy, |
| 4837 | OpLoc)); |
| 4838 | } |
| 4839 | |
| 4840 | // If this is the .* operator, which is not overloadable, just |
| 4841 | // create a built-in binary operator. |
| 4842 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4843 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4844 | |
| 4845 | // If this is one of the assignment operators, we only perform |
| 4846 | // overload resolution if the left-hand side is a class or |
| 4847 | // enumeration type (C++ [expr.ass]p3). |
| 4848 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4849 | !Args[0]->getType()->isOverloadableType()) |
| 4850 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4852 | // Build an empty overload set. |
| 4853 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4854 | |
| 4855 | // Add the candidates from the given function set. |
| 4856 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4857 | |
| 4858 | // Add operator candidates that are member functions. |
| 4859 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4860 | |
| 4861 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4862 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4863 | |
| 4864 | // Perform overload resolution. |
| 4865 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4866 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4867 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4868 | // We found a built-in operator or an overloaded operator. |
| 4869 | FunctionDecl *FnDecl = Best->Function; |
| 4870 | |
| 4871 | if (FnDecl) { |
| 4872 | // We matched an overloaded operator. Build a call to that |
| 4873 | // operator. |
| 4874 | |
| 4875 | // Convert the arguments. |
| 4876 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4877 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 4878 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4879 | "passing")) |
| 4880 | return ExprError(); |
| 4881 | } else { |
| 4882 | // Convert the arguments. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4883 | if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4884 | "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4885 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4886 | "passing")) |
| 4887 | return ExprError(); |
| 4888 | } |
| 4889 | |
| 4890 | // Determine the result type |
| 4891 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4892 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4893 | ResultTy = ResultTy.getNonReferenceType(); |
| 4894 | |
| 4895 | // Build the actual expression node. |
| 4896 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4897 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4898 | UsualUnaryConversions(FnExpr); |
| 4899 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 4900 | ExprOwningPtr<CXXOperatorCallExpr> |
| 4901 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 4902 | Args, 2, ResultTy, |
| 4903 | OpLoc)); |
| 4904 | |
| 4905 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 4906 | FnDecl)) |
| 4907 | return ExprError(); |
| 4908 | |
| 4909 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4910 | } else { |
| 4911 | // We matched a built-in operator. Convert the arguments, then |
| 4912 | // break out so that we will build the appropriate built-in |
| 4913 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4914 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4915 | Best->Conversions[0], "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4916 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4917 | Best->Conversions[1], "passing")) |
| 4918 | return ExprError(); |
| 4919 | |
| 4920 | break; |
| 4921 | } |
| 4922 | } |
| 4923 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4924 | case OR_No_Viable_Function: { |
| 4925 | // C++ [over.match.oper]p9: |
| 4926 | // If the operator is the operator , [...] and there are no |
| 4927 | // viable functions, then the operator is assumed to be the |
| 4928 | // built-in operator and interpreted according to clause 5. |
| 4929 | if (Opc == BinaryOperator::Comma) |
| 4930 | break; |
| 4931 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4932 | // For class as left operand for assignment or compound assigment operator |
| 4933 | // do not fall through to handling in built-in, but report that no overloaded |
| 4934 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4935 | OwningExprResult Result = ExprError(); |
| 4936 | if (Args[0]->getType()->isRecordType() && |
| 4937 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4938 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4939 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4940 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4941 | } else { |
| 4942 | // No viable function; try to create a built-in operation, which will |
| 4943 | // produce an error. Then, show the non-viable candidates. |
| 4944 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4945 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4946 | assert(Result.isInvalid() && |
| 4947 | "C++ binary operator overloading is missing candidates!"); |
| 4948 | if (Result.isInvalid()) |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4949 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false, |
| 4950 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4951 | return move(Result); |
| 4952 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4953 | |
| 4954 | case OR_Ambiguous: |
| 4955 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4956 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4957 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4958 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 4959 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4960 | return ExprError(); |
| 4961 | |
| 4962 | case OR_Deleted: |
| 4963 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4964 | << Best->Function->isDeleted() |
| 4965 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4966 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4967 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4968 | return ExprError(); |
| 4969 | } |
| 4970 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4971 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4972 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 4975 | Action::OwningExprResult |
| 4976 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 4977 | SourceLocation RLoc, |
| 4978 | ExprArg Base, ExprArg Idx) { |
| 4979 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 4980 | static_cast<Expr*>(Idx.get()) }; |
| 4981 | DeclarationName OpName = |
| 4982 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 4983 | |
| 4984 | // If either side is type-dependent, create an appropriate dependent |
| 4985 | // expression. |
| 4986 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 4987 | |
| 4988 | OverloadedFunctionDecl *Overloads |
| 4989 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
| 4990 | |
| 4991 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4992 | LLoc, false, false); |
| 4993 | |
| 4994 | Base.release(); |
| 4995 | Idx.release(); |
| 4996 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 4997 | Args, 2, |
| 4998 | Context.DependentTy, |
| 4999 | RLoc)); |
| 5000 | } |
| 5001 | |
| 5002 | // Build an empty overload set. |
| 5003 | OverloadCandidateSet CandidateSet; |
| 5004 | |
| 5005 | // Subscript can only be overloaded as a member function. |
| 5006 | |
| 5007 | // Add operator candidates that are member functions. |
| 5008 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5009 | |
| 5010 | // Add builtin operator candidates. |
| 5011 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5012 | |
| 5013 | // Perform overload resolution. |
| 5014 | OverloadCandidateSet::iterator Best; |
| 5015 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5016 | case OR_Success: { |
| 5017 | // We found a built-in operator or an overloaded operator. |
| 5018 | FunctionDecl *FnDecl = Best->Function; |
| 5019 | |
| 5020 | if (FnDecl) { |
| 5021 | // We matched an overloaded operator. Build a call to that |
| 5022 | // operator. |
| 5023 | |
| 5024 | // Convert the arguments. |
| 5025 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5026 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 5027 | PerformCopyInitialization(Args[1], |
| 5028 | FnDecl->getParamDecl(0)->getType(), |
| 5029 | "passing")) |
| 5030 | return ExprError(); |
| 5031 | |
| 5032 | // Determine the result type |
| 5033 | QualType ResultTy |
| 5034 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5035 | ResultTy = ResultTy.getNonReferenceType(); |
| 5036 | |
| 5037 | // Build the actual expression node. |
| 5038 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5039 | LLoc); |
| 5040 | UsualUnaryConversions(FnExpr); |
| 5041 | |
| 5042 | Base.release(); |
| 5043 | Idx.release(); |
| 5044 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5045 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5046 | FnExpr, Args, 2, |
| 5047 | ResultTy, RLoc)); |
| 5048 | |
| 5049 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5050 | FnDecl)) |
| 5051 | return ExprError(); |
| 5052 | |
| 5053 | return MaybeBindToTemporary(TheCall.release()); |
| 5054 | } else { |
| 5055 | // We matched a built-in operator. Convert the arguments, then |
| 5056 | // break out so that we will build the appropriate built-in |
| 5057 | // operator node. |
| 5058 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 5059 | Best->Conversions[0], "passing") || |
| 5060 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 5061 | Best->Conversions[1], "passing")) |
| 5062 | return ExprError(); |
| 5063 | |
| 5064 | break; |
| 5065 | } |
| 5066 | } |
| 5067 | |
| 5068 | case OR_No_Viable_Function: { |
| 5069 | // No viable function; try to create a built-in operation, which will |
| 5070 | // produce an error. Then, show the non-viable candidates. |
| 5071 | OwningExprResult Result = |
| 5072 | CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 5073 | assert(Result.isInvalid() && |
| 5074 | "C++ subscript operator overloading is missing candidates!"); |
| 5075 | if (Result.isInvalid()) |
| 5076 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false, |
| 5077 | "[]", LLoc); |
| 5078 | return move(Result); |
| 5079 | } |
| 5080 | |
| 5081 | case OR_Ambiguous: |
| 5082 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5083 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5084 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 5085 | "[]", LLoc); |
| 5086 | return ExprError(); |
| 5087 | |
| 5088 | case OR_Deleted: |
| 5089 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5090 | << Best->Function->isDeleted() << "[]" |
| 5091 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5092 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5093 | return ExprError(); |
| 5094 | } |
| 5095 | |
| 5096 | // We matched a built-in operator; build it. |
| 5097 | Base.release(); |
| 5098 | Idx.release(); |
| 5099 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 5100 | Owned(Args[1]), RLoc); |
| 5101 | } |
| 5102 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5103 | /// BuildCallToMemberFunction - Build a call to a member |
| 5104 | /// function. MemExpr is the expression that refers to the member |
| 5105 | /// function (and includes the object parameter), Args/NumArgs are the |
| 5106 | /// arguments to the function call (not including the object |
| 5107 | /// parameter). The caller needs to validate that the member |
| 5108 | /// expression refers to a member function or an overloaded member |
| 5109 | /// function. |
| 5110 | Sema::ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5111 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 5112 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5113 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 5114 | SourceLocation RParenLoc) { |
| 5115 | // Dig out the member expression. This holds both the object |
| 5116 | // argument and the member function we're referring to. |
| 5117 | MemberExpr *MemExpr = 0; |
| 5118 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 5119 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 5120 | else |
| 5121 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 5122 | assert(MemExpr && "Building member call without member expression"); |
| 5123 | |
| 5124 | // Extract the object argument. |
| 5125 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5127 | CXXMethodDecl *Method = 0; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5128 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 5129 | isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5130 | // Add overload candidates |
| 5131 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5132 | DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5134 | for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd; |
| 5135 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5136 | if ((Method = dyn_cast<CXXMethodDecl>(*Func))) { |
| 5137 | // If explicit template arguments were provided, we can't call a |
| 5138 | // non-template member function. |
| 5139 | if (MemExpr->hasExplicitTemplateArgumentList()) |
| 5140 | continue; |
| 5141 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5142 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5143 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5144 | } else |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 5145 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func), |
| 5146 | MemExpr->hasExplicitTemplateArgumentList(), |
| 5147 | MemExpr->getTemplateArgs(), |
| 5148 | MemExpr->getNumTemplateArgs(), |
| 5149 | ObjectArg, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5150 | CandidateSet, |
| 5151 | /*SuppressUsedConversions=*/false); |
| 5152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5154 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5155 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5156 | case OR_Success: |
| 5157 | Method = cast<CXXMethodDecl>(Best->Function); |
| 5158 | break; |
| 5159 | |
| 5160 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5161 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5162 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5163 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5164 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5165 | // FIXME: Leaking incoming expressions! |
| 5166 | return true; |
| 5167 | |
| 5168 | case OR_Ambiguous: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5169 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5170 | diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5171 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5172 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5173 | // FIXME: Leaking incoming expressions! |
| 5174 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5175 | |
| 5176 | case OR_Deleted: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5177 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5178 | diag::err_ovl_deleted_member_call) |
| 5179 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5180 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5181 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5182 | // FIXME: Leaking incoming expressions! |
| 5183 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | FixOverloadedFunctionReference(MemExpr, Method); |
| 5187 | } else { |
| 5188 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 5189 | } |
| 5190 | |
| 5191 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5192 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5193 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5194 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5195 | Method->getResultType().getNonReferenceType(), |
| 5196 | RParenLoc)); |
| 5197 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5198 | // Check for a valid return type. |
| 5199 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 5200 | TheCall.get(), Method)) |
| 5201 | return true; |
| 5202 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5203 | // Convert the object argument (for a non-static member function call). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5204 | if (!Method->isStatic() && |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5205 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 5206 | return true; |
| 5207 | MemExpr->setBase(ObjectArg); |
| 5208 | |
| 5209 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5210 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5211 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5212 | RParenLoc)) |
| 5213 | return true; |
| 5214 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5215 | if (CheckFunctionCall(Method, TheCall.get())) |
| 5216 | return true; |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 5217 | |
| 5218 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5221 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 5222 | /// type (C++ [over.call.object]), which can end up invoking an |
| 5223 | /// overloaded function call operator (@c operator()) or performing a |
| 5224 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5225 | Sema::ExprResult |
| 5226 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 5227 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5228 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5229 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5230 | SourceLocation RParenLoc) { |
| 5231 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5232 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5233 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5234 | // C++ [over.call.object]p1: |
| 5235 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5236 | // 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] | 5237 | // candidate functions includes at least the function call |
| 5238 | // operators of T. The function call operators of T are obtained by |
| 5239 | // ordinary lookup of the name operator() in the context of |
| 5240 | // (E).operator(). |
| 5241 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5242 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5243 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5244 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5245 | Oper != OperEnd; ++Oper) { |
| 5246 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(*Oper)) { |
| 5247 | AddMethodTemplateCandidate(FunTmpl, false, 0, 0, Object, Args, NumArgs, |
| 5248 | CandidateSet, |
| 5249 | /*SuppressUserConversions=*/false); |
| 5250 | continue; |
| 5251 | } |
| 5252 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5254 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5255 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5256 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5257 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 5258 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 5259 | << Object->getSourceRange())) |
| 5260 | return true; |
| 5261 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5262 | // C++ [over.call.object]p2: |
| 5263 | // In addition, for each conversion function declared in T of the |
| 5264 | // form |
| 5265 | // |
| 5266 | // operator conversion-type-id () cv-qualifier; |
| 5267 | // |
| 5268 | // where cv-qualifier is the same cv-qualification as, or a |
| 5269 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 5270 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 5271 | // R", or the type "reference to pointer to function of |
| 5272 | // (P1,...,Pn) returning R", or the type "reference to function |
| 5273 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5274 | // is also considered as a candidate function. Similarly, |
| 5275 | // surrogate call functions are added to the set of candidate |
| 5276 | // functions for each conversion function declared in an |
| 5277 | // accessible base class provided the function is not hidden |
| 5278 | // within T by another intervening declaration. |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5279 | // FIXME: Look in base classes for more conversion operators! |
| 5280 | OverloadedFunctionDecl *Conversions |
| 5281 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
| 5282 | for (OverloadedFunctionDecl::function_iterator |
| 5283 | Func = Conversions->function_begin(), |
| 5284 | FuncEnd = Conversions->function_end(); |
| 5285 | Func != FuncEnd; ++Func) { |
| 5286 | CXXConversionDecl *Conv; |
| 5287 | FunctionTemplateDecl *ConvTemplate; |
| 5288 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5290 | // Skip over templated conversion functions; they aren't |
| 5291 | // surrogates. |
| 5292 | if (ConvTemplate) |
| 5293 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5294 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5295 | // Strip the reference type (if any) and then the pointer type (if |
| 5296 | // any) to get down to what might be a function type. |
| 5297 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 5298 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5299 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5300 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5301 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 5302 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5303 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5304 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5305 | // Perform overload resolution. |
| 5306 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5307 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5308 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5309 | // Overload resolution succeeded; we'll build the appropriate call |
| 5310 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5311 | break; |
| 5312 | |
| 5313 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5314 | Diag(Object->getSourceRange().getBegin(), |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 5315 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5316 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 5317 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5318 | break; |
| 5319 | |
| 5320 | case OR_Ambiguous: |
| 5321 | Diag(Object->getSourceRange().getBegin(), |
| 5322 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5323 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5324 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5325 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5326 | |
| 5327 | case OR_Deleted: |
| 5328 | Diag(Object->getSourceRange().getBegin(), |
| 5329 | diag::err_ovl_deleted_object_call) |
| 5330 | << Best->Function->isDeleted() |
| 5331 | << Object->getType() << Object->getSourceRange(); |
| 5332 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5333 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5335 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5336 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5337 | // We had an error; delete all of the subexpressions and return |
| 5338 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5339 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5340 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5341 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5342 | return true; |
| 5343 | } |
| 5344 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5345 | if (Best->Function == 0) { |
| 5346 | // Since there is no function declaration, this is one of the |
| 5347 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5349 | = cast<CXXConversionDecl>( |
| 5350 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 5351 | |
| 5352 | // We selected one of the surrogate functions that converts the |
| 5353 | // object parameter to a function pointer. Perform the conversion |
| 5354 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5355 | |
| 5356 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5357 | // and then call it. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5358 | CXXMemberCallExpr *CE = |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5359 | BuildCXXMemberCallExpr(Object, Conv); |
| 5360 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5361 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5362 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 5363 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 5367 | // that calls this method, using Object for the implicit object |
| 5368 | // parameter and passing along the remaining arguments. |
| 5369 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5370 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5371 | |
| 5372 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5373 | unsigned NumArgsToCheck = NumArgs; |
| 5374 | |
| 5375 | // Build the full argument list for the method call (the |
| 5376 | // implicit object parameter is placed at the beginning of the |
| 5377 | // list). |
| 5378 | Expr **MethodArgs; |
| 5379 | if (NumArgs < NumArgsInProto) { |
| 5380 | NumArgsToCheck = NumArgsInProto; |
| 5381 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 5382 | } else { |
| 5383 | MethodArgs = new Expr*[NumArgs + 1]; |
| 5384 | } |
| 5385 | MethodArgs[0] = Object; |
| 5386 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5387 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5388 | |
| 5389 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5390 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5391 | UsualUnaryConversions(NewFn); |
| 5392 | |
| 5393 | // Once we've built TheCall, all of the expressions are properly |
| 5394 | // owned. |
| 5395 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5396 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5397 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5398 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5399 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5400 | delete [] MethodArgs; |
| 5401 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 5402 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 5403 | Method)) |
| 5404 | return true; |
| 5405 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5406 | // We may have default arguments. If so, we need to allocate more |
| 5407 | // slots in the call for them. |
| 5408 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5409 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5410 | else if (NumArgs > NumArgsInProto) |
| 5411 | NumArgsToCheck = NumArgsInProto; |
| 5412 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5413 | bool IsError = false; |
| 5414 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5415 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5416 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5417 | TheCall->setArg(0, Object); |
| 5418 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5419 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5420 | // Check the argument types. |
| 5421 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5422 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5423 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5424 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5426 | // Pass the argument. |
| 5427 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5428 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5429 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 5430 | OwningExprResult DefArg |
| 5431 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 5432 | if (DefArg.isInvalid()) { |
| 5433 | IsError = true; |
| 5434 | break; |
| 5435 | } |
| 5436 | |
| 5437 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5438 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5439 | |
| 5440 | TheCall->setArg(i + 1, Arg); |
| 5441 | } |
| 5442 | |
| 5443 | // If this is a variadic call, handle args passed through "...". |
| 5444 | if (Proto->isVariadic()) { |
| 5445 | // Promote the arguments (C99 6.5.2.2p7). |
| 5446 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 5447 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5448 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5449 | TheCall->setArg(i + 1, Arg); |
| 5450 | } |
| 5451 | } |
| 5452 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5453 | if (IsError) return true; |
| 5454 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5455 | if (CheckFunctionCall(Method, TheCall.get())) |
| 5456 | return true; |
| 5457 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 5458 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5461 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5462 | /// (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] | 5463 | /// @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] | 5464 | Sema::OwningExprResult |
| 5465 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 5466 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5467 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5468 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5469 | // C++ [over.ref]p1: |
| 5470 | // |
| 5471 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 5472 | // for a class object x of type T if T::operator->() exists and if |
| 5473 | // the operator is selected as the best match function by the |
| 5474 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5475 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 5476 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5477 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5478 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5479 | LookupResult R; |
| 5480 | LookupQualifiedName(R, BaseRecord->getDecl(), OpName, LookupOrdinaryName); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5481 | |
| 5482 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 5483 | Oper != OperEnd; ++Oper) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5484 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5485 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5486 | |
| 5487 | // Perform overload resolution. |
| 5488 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5489 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5490 | case OR_Success: |
| 5491 | // Overload resolution succeeded; we'll build the call below. |
| 5492 | break; |
| 5493 | |
| 5494 | case OR_No_Viable_Function: |
| 5495 | if (CandidateSet.empty()) |
| 5496 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5497 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5498 | else |
| 5499 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5500 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5501 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5502 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5503 | |
| 5504 | case OR_Ambiguous: |
| 5505 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5506 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5507 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5508 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5509 | |
| 5510 | case OR_Deleted: |
| 5511 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5512 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5513 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5514 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5515 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5516 | } |
| 5517 | |
| 5518 | // Convert the object parameter. |
| 5519 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5520 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5521 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5522 | |
| 5523 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5524 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5525 | |
| 5526 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5527 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 5528 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5529 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5530 | |
| 5531 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 5532 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5533 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 5534 | &Base, 1, ResultTy, OpLoc)); |
| 5535 | |
| 5536 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 5537 | Method)) |
| 5538 | return ExprError(); |
| 5539 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5542 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 5543 | /// a C++ overloaded function (possibly with some parentheses and |
| 5544 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 5545 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5546 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 5547 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5548 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5549 | Expr *NewExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5550 | PE->setSubExpr(NewExpr); |
| 5551 | PE->setType(NewExpr->getType()); |
| 5552 | } else if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 5553 | Expr *NewExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
| 5554 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
| 5555 | NewExpr->getType()) && |
| 5556 | "Implicit cast type cannot be determined from overload"); |
| 5557 | ICE->setSubExpr(NewExpr); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5558 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5559 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5560 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5561 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5562 | if (Method->isStatic()) { |
| 5563 | // Do nothing: static member functions aren't any different |
| 5564 | // from non-member functions. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5565 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr())) { |
| 5566 | if (DRE->getQualifier()) { |
| 5567 | // We have taken the address of a pointer to member |
| 5568 | // function. Perform the computation here so that we get the |
| 5569 | // appropriate pointer to member type. |
| 5570 | DRE->setDecl(Fn); |
| 5571 | DRE->setType(Fn->getType()); |
| 5572 | QualType ClassType |
| 5573 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 5574 | E->setType(Context.getMemberPointerType(Fn->getType(), |
| 5575 | ClassType.getTypePtr())); |
| 5576 | return E; |
| 5577 | } |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5578 | } |
Douglas Gregor | 423a4e0 | 2009-10-22 18:02:20 +0000 | [diff] [blame] | 5579 | // FIXME: TemplateIdRefExpr referring to a member function template |
| 5580 | // specialization! |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5581 | } |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5582 | Expr *NewExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 5583 | UnOp->setSubExpr(NewExpr); |
| 5584 | UnOp->setType(Context.getPointerType(NewExpr->getType())); |
| 5585 | |
| 5586 | return UnOp; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5587 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5588 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5589 | isa<FunctionTemplateDecl>(DR->getDecl()) || |
| 5590 | isa<FunctionDecl>(DR->getDecl())) && |
| 5591 | "Expected function or function template"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5592 | DR->setDecl(Fn); |
| 5593 | E->setType(Fn->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5594 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 5595 | MemExpr->setMemberDecl(Fn); |
| 5596 | E->setType(Fn->getType()); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5597 | } else if (TemplateIdRefExpr *TID = dyn_cast<TemplateIdRefExpr>(E)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5598 | E = DeclRefExpr::Create(Context, |
| 5599 | TID->getQualifier(), TID->getQualifierRange(), |
| 5600 | Fn, TID->getTemplateNameLoc(), |
| 5601 | true, |
| 5602 | TID->getLAngleLoc(), |
| 5603 | TID->getTemplateArgs(), |
| 5604 | TID->getNumTemplateArgs(), |
| 5605 | TID->getRAngleLoc(), |
| 5606 | Fn->getType(), |
| 5607 | /*FIXME?*/false, /*FIXME?*/false); |
Douglas Gregor | 423a4e0 | 2009-10-22 18:02:20 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5609 | // FIXME: Don't destroy TID here, since we need its template arguments |
| 5610 | // to survive. |
| 5611 | // TID->Destroy(Context); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5612 | } else if (isa<UnresolvedFunctionNameExpr>(E)) { |
| 5613 | return DeclRefExpr::Create(Context, |
| 5614 | /*Qualifier=*/0, |
| 5615 | /*QualifierRange=*/SourceRange(), |
| 5616 | Fn, E->getLocStart(), |
| 5617 | Fn->getType(), false, false); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5618 | } else { |
| 5619 | assert(false && "Invalid reference to overloaded function"); |
| 5620 | } |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5621 | |
| 5622 | return E; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5625 | } // end namespace clang |