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. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 522 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 523 | } else if (FromType->isArrayType()) { |
| 524 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 525 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 526 | |
| 527 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 528 | // bound of T" can be converted to an rvalue of type "pointer to |
| 529 | // T" (C++ 4.2p1). |
| 530 | FromType = Context.getArrayDecayedType(FromType); |
| 531 | |
| 532 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 533 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 534 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 535 | |
| 536 | // For the purpose of ranking in overload resolution |
| 537 | // (13.3.3.1.1), this conversion is considered an |
| 538 | // array-to-pointer conversion followed by a qualification |
| 539 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 540 | SCS.Second = ICK_Identity; |
| 541 | SCS.Third = ICK_Qualification; |
| 542 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 543 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 544 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 545 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 546 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 547 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 548 | |
| 549 | // An lvalue of function type T can be converted to an rvalue of |
| 550 | // type "pointer to T." The result is a pointer to the |
| 551 | // function. (C++ 4.3p1). |
| 552 | FromType = Context.getPointerType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | } else if (FunctionDecl *Fn |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 554 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 555 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 556 | SCS.First = ICK_Function_To_Pointer; |
| 557 | |
| 558 | // We were able to resolve the address of the overloaded function, |
| 559 | // so we can convert to the type of that function. |
| 560 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 561 | if (ToType->isLValueReferenceType()) |
| 562 | FromType = Context.getLValueReferenceType(FromType); |
| 563 | else if (ToType->isRValueReferenceType()) |
| 564 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 565 | else if (ToType->isMemberPointerType()) { |
| 566 | // Resolve address only succeeds if both sides are member pointers, |
| 567 | // but it doesn't have to be the same class. See DR 247. |
| 568 | // Note that this means that the type of &Derived::fn can be |
| 569 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 570 | // base class, even if it was brought into the derived class via a |
| 571 | // using declaration. The standard isn't clear on this issue at all. |
| 572 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 573 | FromType = Context.getMemberPointerType(FromType, |
| 574 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 575 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 576 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 577 | } else { |
| 578 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 579 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // The second conversion can be an integral promotion, floating |
| 583 | // point promotion, integral conversion, floating point conversion, |
| 584 | // floating-integral conversion, pointer conversion, |
| 585 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 586 | // For overloading in C, this can also be a "compatible-type" |
| 587 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 588 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 589 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 590 | // The unqualified versions of the types are the same: there's no |
| 591 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 592 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 593 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 595 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 596 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 597 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 598 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 599 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 600 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 601 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 602 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 603 | SCS.Second = ICK_Complex_Promotion; |
| 604 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 605 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 606 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 607 | // Integral conversions (C++ 4.7). |
| 608 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 609 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 610 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 611 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 612 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 613 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 614 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 615 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 616 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 617 | SCS.Second = ICK_Complex_Conversion; |
| 618 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 619 | } else if ((FromType->isFloatingType() && |
| 620 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 621 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 623 | ToType->isFloatingType())) { |
| 624 | // Floating-integral conversions (C++ 4.9). |
| 625 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 626 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 627 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 628 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 629 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 630 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 631 | SCS.Second = ICK_Complex_Real; |
| 632 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 633 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 634 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 635 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 636 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 637 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 638 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 639 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 640 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 641 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 642 | } else if (ToType->isBooleanType() && |
| 643 | (FromType->isArithmeticType() || |
| 644 | FromType->isEnumeralType() || |
| 645 | FromType->isPointerType() || |
| 646 | FromType->isBlockPointerType() || |
| 647 | FromType->isMemberPointerType() || |
| 648 | FromType->isNullPtrType())) { |
| 649 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 650 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 651 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 653 | Context.typesAreCompatible(ToType, FromType)) { |
| 654 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 655 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 656 | } else { |
| 657 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 658 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 661 | QualType CanonFrom; |
| 662 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 663 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 664 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 665 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 666 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 667 | CanonFrom = Context.getCanonicalType(FromType); |
| 668 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 669 | } else { |
| 670 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 671 | SCS.Third = ICK_Identity; |
| 672 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 674 | // [...] Any difference in top-level cv-qualification is |
| 675 | // subsumed by the initialization itself and does not constitute |
| 676 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 677 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 679 | if (CanonFrom.getLocalUnqualifiedType() |
| 680 | == CanonTo.getLocalUnqualifiedType() && |
| 681 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 682 | FromType = ToType; |
| 683 | CanonFrom = CanonTo; |
| 684 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | // If we have not converted the argument type to the parameter type, |
| 688 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 689 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 690 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 691 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 692 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 693 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 697 | /// expression From (whose potentially-adjusted type is FromType) to |
| 698 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 699 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 701 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 702 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 703 | if (!To) { |
| 704 | return false; |
| 705 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 706 | |
| 707 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 708 | // unsigned short int can be converted to an rvalue of type int if |
| 709 | // int can represent all the values of the source type; otherwise, |
| 710 | // the source rvalue can be converted to an rvalue of type unsigned |
| 711 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 712 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 713 | if (// We can promote any signed, promotable integer type to an int |
| 714 | (FromType->isSignedIntegerType() || |
| 715 | // We can promote any unsigned integer type whose size is |
| 716 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 718 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 719 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 722 | return To->getKind() == BuiltinType::UInt; |
| 723 | } |
| 724 | |
| 725 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 726 | // can be converted to an rvalue of the first of the following types |
| 727 | // that can represent all the values of its underlying type: int, |
| 728 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 729 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 730 | && ToType->isIntegerType()) { |
| 731 | // Determine whether the type we're converting from is signed or |
| 732 | // unsigned. |
| 733 | bool FromIsSigned; |
| 734 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 735 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 736 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 737 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 738 | } else { |
| 739 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 740 | FromIsSigned = true; |
| 741 | } |
| 742 | |
| 743 | // The types we'll try to promote to, in the appropriate |
| 744 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | QualType PromoteTypes[6] = { |
| 746 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 747 | Context.LongTy, Context.UnsignedLongTy , |
| 748 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 749 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 750 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 751 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 752 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 754 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 755 | // We found the type that we can promote to. If this is the |
| 756 | // type we wanted, we have a promotion. Otherwise, no |
| 757 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 758 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 764 | // rvalue of type int if int can represent all the values of the |
| 765 | // bit-field; otherwise, it can be converted to unsigned int if |
| 766 | // unsigned int can represent all the values of the bit-field. If |
| 767 | // the bit-field is larger yet, no integral promotion applies to |
| 768 | // it. If the bit-field has an enumerated type, it is treated as any |
| 769 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 770 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 771 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 772 | using llvm::APSInt; |
| 773 | if (From) |
| 774 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 775 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 776 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 777 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 778 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 779 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 781 | // Are we promoting to an int from a bitfield that fits in an int? |
| 782 | if (BitWidth < ToSize || |
| 783 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 784 | return To->getKind() == BuiltinType::Int; |
| 785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 787 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 788 | // that fits into an unsigned int? |
| 789 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 790 | return To->getKind() == BuiltinType::UInt; |
| 791 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 793 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 794 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 795 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 797 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 798 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 799 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 800 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 801 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 802 | |
| 803 | return false; |
| 804 | } |
| 805 | |
| 806 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 807 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 808 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 810 | /// An rvalue of type float can be converted to an rvalue of type |
| 811 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 812 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 813 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 814 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 815 | ToBuiltin->getKind() == BuiltinType::Double) |
| 816 | return true; |
| 817 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 818 | // C99 6.3.1.5p1: |
| 819 | // When a float is promoted to double or long double, or a |
| 820 | // double is promoted to long double [...]. |
| 821 | if (!getLangOptions().CPlusPlus && |
| 822 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 823 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 824 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 825 | return true; |
| 826 | } |
| 827 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 831 | /// \brief Determine if a conversion is a complex promotion. |
| 832 | /// |
| 833 | /// A complex promotion is defined as a complex -> complex conversion |
| 834 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 835 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 836 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 837 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 838 | if (!FromComplex) |
| 839 | return false; |
| 840 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 841 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 842 | if (!ToComplex) |
| 843 | return false; |
| 844 | |
| 845 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 846 | ToComplex->getElementType()) || |
| 847 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 848 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 851 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 852 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 853 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 854 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 855 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | static QualType |
| 857 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 858 | QualType ToPointee, QualType ToType, |
| 859 | ASTContext &Context) { |
| 860 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 861 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 862 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
| 864 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 865 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 866 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 867 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 868 | return ToType; |
| 869 | |
| 870 | // Build a pointer to ToPointee. It has the right qualifiers |
| 871 | // already. |
| 872 | return Context.getPointerType(ToPointee); |
| 873 | } |
| 874 | |
| 875 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 876 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 877 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 878 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 882 | bool InOverloadResolution, |
| 883 | ASTContext &Context) { |
| 884 | // Handle value-dependent integral null pointer constants correctly. |
| 885 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 886 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 887 | Expr->getType()->isIntegralType()) |
| 888 | return !InOverloadResolution; |
| 889 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 890 | return Expr->isNullPointerConstant(Context, |
| 891 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 892 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 895 | /// IsPointerConversion - Determines whether the conversion of the |
| 896 | /// expression From, which has the (possibly adjusted) type FromType, |
| 897 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 898 | /// 4.10). If so, returns true and places the converted type (that |
| 899 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 900 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 901 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 902 | /// This routine also supports conversions to and from block pointers |
| 903 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 904 | /// pointers to interfaces. FIXME: Once we've determined the |
| 905 | /// appropriate overloading rules for Objective-C, we may want to |
| 906 | /// split the Objective-C checks into a different routine; however, |
| 907 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 908 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 909 | /// set if the conversion is an allowed Objective-C conversion that |
| 910 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 911 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 912 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 913 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 915 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 916 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 917 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 918 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 920 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 921 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 922 | ConvertedType = ToType; |
| 923 | return true; |
| 924 | } |
| 925 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 926 | // Blocks: Block pointers can be converted to void*. |
| 927 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 928 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 929 | ConvertedType = ToType; |
| 930 | return true; |
| 931 | } |
| 932 | // Blocks: A null pointer constant can be converted to a block |
| 933 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 935 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 936 | ConvertedType = ToType; |
| 937 | return true; |
| 938 | } |
| 939 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 940 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 941 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 943 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 944 | ConvertedType = ToType; |
| 945 | return true; |
| 946 | } |
| 947 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 948 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 949 | if (!ToTypePtr) |
| 950 | return false; |
| 951 | |
| 952 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 953 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 954 | ConvertedType = ToType; |
| 955 | return true; |
| 956 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 958 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 959 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 960 | if (!FromTypePtr) |
| 961 | return false; |
| 962 | |
| 963 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 964 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 965 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 966 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 967 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 968 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 969 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 971 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 972 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 976 | // When we're overloading in C, we allow a special kind of pointer |
| 977 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 979 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 981 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 983 | return true; |
| 984 | } |
| 985 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 986 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 988 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 989 | // can be converted to an rvalue of type "pointer to cv B," where |
| 990 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 991 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 992 | // necessitates this conversion is ill-formed. The result of the |
| 993 | // conversion is a pointer to the base class sub-object of the |
| 994 | // derived class object. The null pointer value is converted to |
| 995 | // the null pointer value of the destination type. |
| 996 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 997 | // Note that we do not check for ambiguity or inaccessibility |
| 998 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 999 | if (getLangOptions().CPlusPlus && |
| 1000 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1001 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1002 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1004 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1005 | ToType, Context); |
| 1006 | return true; |
| 1007 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1008 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | /// isObjCPointerConversion - Determines whether this is an |
| 1013 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1014 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1016 | QualType& ConvertedType, |
| 1017 | bool &IncompatibleObjC) { |
| 1018 | if (!getLangOptions().ObjC1) |
| 1019 | return false; |
| 1020 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1021 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1022 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1024 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1025 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1026 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1027 | // 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] | 1028 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1029 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1030 | ConvertedType = ToType; |
| 1031 | return true; |
| 1032 | } |
| 1033 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1035 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1037 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1038 | ConvertedType = ToType; |
| 1039 | return true; |
| 1040 | } |
| 1041 | // Objective C++: We're able to convert from a pointer to an |
| 1042 | // interface to a pointer to a different interface. |
| 1043 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1044 | ConvertedType = ToType; |
| 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1049 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1050 | // interfaces, which is permitted. However, we're going to |
| 1051 | // complain about it. |
| 1052 | IncompatibleObjC = true; |
| 1053 | ConvertedType = FromType; |
| 1054 | return true; |
| 1055 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1057 | // 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] | 1058 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1059 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1060 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1061 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1062 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1063 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1064 | return false; |
| 1065 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1066 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1067 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1068 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1069 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1070 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1071 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1072 | return false; |
| 1073 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1074 | // If we have pointers to pointers, recursively check whether this |
| 1075 | // is an Objective-C conversion. |
| 1076 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1077 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1078 | IncompatibleObjC)) { |
| 1079 | // We always complain about this conversion. |
| 1080 | IncompatibleObjC = true; |
| 1081 | ConvertedType = ToType; |
| 1082 | return true; |
| 1083 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1084 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1085 | // differences in the argument and result types are in Objective-C |
| 1086 | // pointer conversions. If so, we permit the conversion (but |
| 1087 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1089 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1090 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1091 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1092 | if (FromFunctionType && ToFunctionType) { |
| 1093 | // If the function types are exactly the same, this isn't an |
| 1094 | // Objective-C pointer conversion. |
| 1095 | if (Context.getCanonicalType(FromPointeeType) |
| 1096 | == Context.getCanonicalType(ToPointeeType)) |
| 1097 | return false; |
| 1098 | |
| 1099 | // Perform the quick checks that will tell us whether these |
| 1100 | // function types are obviously different. |
| 1101 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1102 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1103 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1104 | return false; |
| 1105 | |
| 1106 | bool HasObjCConversion = false; |
| 1107 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1108 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1109 | // Okay, the types match exactly. Nothing to do. |
| 1110 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1111 | ToFunctionType->getResultType(), |
| 1112 | ConvertedType, IncompatibleObjC)) { |
| 1113 | // Okay, we have an Objective-C pointer conversion. |
| 1114 | HasObjCConversion = true; |
| 1115 | } else { |
| 1116 | // Function types are too different. Abort. |
| 1117 | return false; |
| 1118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1120 | // Check argument types. |
| 1121 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1122 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1123 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1124 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1125 | if (Context.getCanonicalType(FromArgType) |
| 1126 | == Context.getCanonicalType(ToArgType)) { |
| 1127 | // Okay, the types match exactly. Nothing to do. |
| 1128 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1129 | ConvertedType, IncompatibleObjC)) { |
| 1130 | // Okay, we have an Objective-C pointer conversion. |
| 1131 | HasObjCConversion = true; |
| 1132 | } else { |
| 1133 | // Argument types are too different. Abort. |
| 1134 | return false; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | if (HasObjCConversion) { |
| 1139 | // We had an Objective-C conversion. Allow this pointer |
| 1140 | // conversion, but complain about it. |
| 1141 | ConvertedType = ToType; |
| 1142 | IncompatibleObjC = true; |
| 1143 | return true; |
| 1144 | } |
| 1145 | } |
| 1146 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1147 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1150 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1151 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1152 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1153 | /// conversions for which IsPointerConversion has already returned |
| 1154 | /// true. It returns true and produces a diagnostic if there was an |
| 1155 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1156 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1157 | CastExpr::CastKind &Kind, |
| 1158 | bool IgnoreBaseAccess) { |
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(), |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1172 | From->getSourceRange(), |
| 1173 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1174 | return true; |
| 1175 | |
| 1176 | // The conversion was successful. |
| 1177 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1181 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1183 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1184 | // Objective-C++ conversions are always okay. |
| 1185 | // FIXME: We should have a different class of conversions for the |
| 1186 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1187 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1188 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1189 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1190 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1191 | return false; |
| 1192 | } |
| 1193 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1194 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1195 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1196 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1197 | /// If so, returns true and places the converted type (that might differ from |
| 1198 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1199 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1200 | QualType ToType, |
| 1201 | bool InOverloadResolution, |
| 1202 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1203 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1204 | if (!ToTypePtr) |
| 1205 | return false; |
| 1206 | |
| 1207 | // 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] | 1208 | if (From->isNullPointerConstant(Context, |
| 1209 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1210 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1211 | ConvertedType = ToType; |
| 1212 | return true; |
| 1213 | } |
| 1214 | |
| 1215 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1216 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1217 | if (!FromTypePtr) |
| 1218 | return false; |
| 1219 | |
| 1220 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1221 | // where D is derived from B (C++ 4.11p2). |
| 1222 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1223 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1224 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1225 | |
| 1226 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1227 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1228 | ToClass.getTypePtr()); |
| 1229 | return true; |
| 1230 | } |
| 1231 | |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
| 1235 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1236 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1237 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1238 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1239 | /// true and produces a diagnostic if there was an error, or returns false |
| 1240 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1242 | CastExpr::CastKind &Kind, |
| 1243 | bool IgnoreBaseAccess) { |
| 1244 | (void)IgnoreBaseAccess; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1245 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1246 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1247 | if (!FromPtrType) { |
| 1248 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1249 | assert(From->isNullPointerConstant(Context, |
| 1250 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1251 | "Expr must be null pointer constant!"); |
| 1252 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1253 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1254 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1255 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1256 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1257 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1258 | "that is not a member pointer."); |
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 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1261 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1262 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1263 | // FIXME: What about dependent types? |
| 1264 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1265 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1266 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1267 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1268 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1269 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1270 | assert(DerivationOkay && |
| 1271 | "Should not have been called if derivation isn't OK."); |
| 1272 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1273 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1274 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1275 | getUnqualifiedType())) { |
| 1276 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1277 | Paths.clear(); |
| 1278 | Paths.setRecordingPaths(true); |
| 1279 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1280 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1281 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1282 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1283 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1284 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1285 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1286 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1287 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1288 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1289 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1290 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1291 | << FromClass << ToClass << QualType(VBase, 0) |
| 1292 | << From->getSourceRange(); |
| 1293 | return true; |
| 1294 | } |
| 1295 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1296 | // Must be a base to derived member conversion. |
| 1297 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1298 | return false; |
| 1299 | } |
| 1300 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1301 | /// IsQualificationConversion - Determines whether the conversion from |
| 1302 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1303 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | bool |
| 1305 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1306 | FromType = Context.getCanonicalType(FromType); |
| 1307 | ToType = Context.getCanonicalType(ToType); |
| 1308 | |
| 1309 | // If FromType and ToType are the same type, this is not a |
| 1310 | // qualification conversion. |
| 1311 | if (FromType == ToType) |
| 1312 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1314 | // (C++ 4.4p4): |
| 1315 | // A conversion can add cv-qualifiers at levels other than the first |
| 1316 | // in multi-level pointers, subject to the following rules: [...] |
| 1317 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1318 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1319 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1320 | // Within each iteration of the loop, we check the qualifiers to |
| 1321 | // determine if this still looks like a qualification |
| 1322 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1323 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1324 | // until there are no more pointers or pointers-to-members left to |
| 1325 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1326 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1327 | |
| 1328 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1329 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1330 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1331 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1333 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1334 | // every cv for 0 < k < j. |
| 1335 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1336 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1337 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1339 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1340 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1342 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1343 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1344 | |
| 1345 | // We are left with FromType and ToType being the pointee types |
| 1346 | // after unwrapping the original FromType and ToType the same number |
| 1347 | // of types. If we unwrapped any pointers, and if FromType and |
| 1348 | // ToType have the same unqualified type (since we checked |
| 1349 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1350 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1353 | /// \brief Given a function template or function, extract the function template |
| 1354 | /// declaration (if any) and the underlying function declaration. |
| 1355 | template<typename T> |
| 1356 | static void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function, |
| 1357 | FunctionTemplateDecl *&FunctionTemplate) { |
| 1358 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig); |
| 1359 | if (FunctionTemplate) |
| 1360 | Function = cast<T>(FunctionTemplate->getTemplatedDecl()); |
| 1361 | else |
| 1362 | Function = cast<T>(Orig); |
| 1363 | } |
| 1364 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1365 | /// Determines whether there is a user-defined conversion sequence |
| 1366 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1367 | /// ToType. If such a conversion exists, User will contain the |
| 1368 | /// user-defined conversion sequence that performs such a conversion |
| 1369 | /// and this routine will return true. Otherwise, this routine returns |
| 1370 | /// false and User is unspecified. |
| 1371 | /// |
| 1372 | /// \param AllowConversionFunctions true if the conversion should |
| 1373 | /// consider conversion functions at all. If false, only constructors |
| 1374 | /// will be considered. |
| 1375 | /// |
| 1376 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1377 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1378 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1379 | /// |
| 1380 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1381 | /// for overload resolution. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1382 | /// \param UserCast true if looking for user defined conversion for a static |
| 1383 | /// cast. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1384 | Sema::OverloadingResult Sema::IsUserDefinedConversion( |
| 1385 | Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1386 | UserDefinedConversionSequence& User, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 1387 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1388 | bool AllowConversionFunctions, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1389 | bool AllowExplicit, bool ForceRValue, |
| 1390 | bool UserCast) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1391 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1392 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1393 | // We're not going to find any constructors. |
| 1394 | } else if (CXXRecordDecl *ToRecordDecl |
| 1395 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1396 | // C++ [over.match.ctor]p1: |
| 1397 | // When objects of class type are direct-initialized (8.5), or |
| 1398 | // copy-initialized from an expression of the same or a |
| 1399 | // derived class type (8.5), overload resolution selects the |
| 1400 | // constructor. [...] For copy-initialization, the candidate |
| 1401 | // functions are all the converting constructors (12.3.1) of |
| 1402 | // that class. The argument list is the expression-list within |
| 1403 | // the parentheses of the initializer. |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1404 | bool SuppressUserConversions = !UserCast; |
| 1405 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1406 | IsDerivedFrom(From->getType(), ToType)) { |
| 1407 | SuppressUserConversions = false; |
| 1408 | AllowConversionFunctions = false; |
| 1409 | } |
| 1410 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1412 | = Context.DeclarationNames.getCXXConstructorName( |
| 1413 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1414 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1416 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1417 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1418 | // Find the constructor (which may be a template). |
| 1419 | CXXConstructorDecl *Constructor = 0; |
| 1420 | FunctionTemplateDecl *ConstructorTmpl |
| 1421 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1422 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1424 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1425 | else |
| 1426 | Constructor = cast<CXXConstructorDecl>(*Con); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1427 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1428 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1429 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1430 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1432 | 1, CandidateSet, |
| 1433 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1434 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1435 | // Allow one user-defined conversion when user specifies a |
| 1436 | // From->ToType conversion via an static cast (c-style, etc). |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1437 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1438 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1439 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1440 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1444 | if (!AllowConversionFunctions) { |
| 1445 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1447 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1448 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1449 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1451 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1453 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1454 | // Add all of the conversion functions as candidates. |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1455 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1456 | = FromRecordDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1457 | for (OverloadedFunctionDecl::function_iterator Func |
| 1458 | = Conversions->function_begin(); |
| 1459 | Func != Conversions->function_end(); ++Func) { |
| 1460 | CXXConversionDecl *Conv; |
| 1461 | FunctionTemplateDecl *ConvTemplate; |
| 1462 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 1463 | if (ConvTemplate) |
| 1464 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1465 | else |
| 1466 | Conv = dyn_cast<CXXConversionDecl>(*Func); |
| 1467 | |
| 1468 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1469 | if (ConvTemplate) |
| 1470 | AddTemplateConversionCandidate(ConvTemplate, From, ToType, |
| 1471 | CandidateSet); |
| 1472 | else |
| 1473 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1474 | } |
| 1475 | } |
| 1476 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1477 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1478 | |
| 1479 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1480 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1481 | case OR_Success: |
| 1482 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1484 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1485 | // C++ [over.ics.user]p1: |
| 1486 | // If the user-defined conversion is specified by a |
| 1487 | // constructor (12.3.1), the initial standard conversion |
| 1488 | // sequence converts the source type to the type required by |
| 1489 | // the argument of the constructor. |
| 1490 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1491 | QualType ThisType = Constructor->getThisType(Context); |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1492 | if (Best->Conversions[0].ConversionKind == |
| 1493 | ImplicitConversionSequence::EllipsisConversion) |
| 1494 | User.EllipsisConversion = true; |
| 1495 | else { |
| 1496 | User.Before = Best->Conversions[0].Standard; |
| 1497 | User.EllipsisConversion = false; |
| 1498 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1499 | User.ConversionFunction = Constructor; |
| 1500 | User.After.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | User.After.FromTypePtr |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1502 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1503 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1504 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1505 | } else if (CXXConversionDecl *Conversion |
| 1506 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1507 | // C++ [over.ics.user]p1: |
| 1508 | // |
| 1509 | // [...] If the user-defined conversion is specified by a |
| 1510 | // conversion function (12.3.2), the initial standard |
| 1511 | // conversion sequence converts the source type to the |
| 1512 | // implicit object parameter of the conversion function. |
| 1513 | User.Before = Best->Conversions[0].Standard; |
| 1514 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1515 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
| 1517 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1518 | // The second standard conversion sequence converts the |
| 1519 | // result of the user-defined conversion to the target type |
| 1520 | // for the sequence. Since an implicit conversion sequence |
| 1521 | // is an initialization, the special rules for |
| 1522 | // initialization by user-defined conversion apply when |
| 1523 | // selecting the best user-defined conversion for a |
| 1524 | // user-defined conversion sequence (see 13.3.3 and |
| 1525 | // 13.3.3.1). |
| 1526 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1527 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1528 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1529 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1530 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1533 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1534 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1535 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1536 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1537 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1538 | |
| 1539 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1540 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1543 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1544 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1545 | |
| 1546 | bool |
| 1547 | Sema::DiagnoseAmbiguousUserDefinedConversion(Expr *From, QualType ToType) { |
| 1548 | ImplicitConversionSequence ICS; |
| 1549 | OverloadCandidateSet CandidateSet; |
| 1550 | OverloadingResult OvResult = |
| 1551 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1552 | CandidateSet, true, false, false); |
| 1553 | if (OvResult != OR_Ambiguous) |
| 1554 | return false; |
| 1555 | Diag(From->getSourceRange().getBegin(), |
| 1556 | diag::err_typecheck_ambiguous_condition) |
| 1557 | << From->getType() << ToType << From->getSourceRange(); |
| 1558 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 1559 | return true; |
| 1560 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1561 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1562 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1563 | /// conversion sequences to determine whether one is better than the |
| 1564 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1566 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1567 | const ImplicitConversionSequence& ICS2) |
| 1568 | { |
| 1569 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1570 | // conversion sequences (as defined in 13.3.3.1) |
| 1571 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1572 | // conversion sequence than a user-defined conversion sequence or |
| 1573 | // an ellipsis conversion sequence, and |
| 1574 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1575 | // conversion sequence than an ellipsis conversion sequence |
| 1576 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | // |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1578 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1579 | return ImplicitConversionSequence::Better; |
| 1580 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1581 | return ImplicitConversionSequence::Worse; |
| 1582 | |
| 1583 | // Two implicit conversion sequences of the same form are |
| 1584 | // indistinguishable conversion sequences unless one of the |
| 1585 | // following rules apply: (C++ 13.3.3.2p3): |
| 1586 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1587 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | else if (ICS1.ConversionKind == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1589 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1590 | // User-defined conversion sequence U1 is a better conversion |
| 1591 | // sequence than another user-defined conversion sequence U2 if |
| 1592 | // they contain the same user-defined conversion function or |
| 1593 | // constructor and if the second standard conversion sequence of |
| 1594 | // U1 is better than the second standard conversion sequence of |
| 1595 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1597 | ICS2.UserDefined.ConversionFunction) |
| 1598 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1599 | ICS2.UserDefined.After); |
| 1600 | } |
| 1601 | |
| 1602 | return ImplicitConversionSequence::Indistinguishable; |
| 1603 | } |
| 1604 | |
| 1605 | /// CompareStandardConversionSequences - Compare two standard |
| 1606 | /// conversion sequences to determine whether one is better than the |
| 1607 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1609 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1610 | const StandardConversionSequence& SCS2) |
| 1611 | { |
| 1612 | // Standard conversion sequence S1 is a better conversion sequence |
| 1613 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1614 | |
| 1615 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1616 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1617 | // excluding any Lvalue Transformation; the identity conversion |
| 1618 | // sequence is considered to be a subsequence of any |
| 1619 | // non-identity conversion sequence) or, if not that, |
| 1620 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1621 | // Neither is a proper subsequence of the other. Do nothing. |
| 1622 | ; |
| 1623 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1624 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1626 | SCS1.Third == ICK_Identity)) |
| 1627 | // SCS1 is a proper subsequence of SCS2. |
| 1628 | return ImplicitConversionSequence::Better; |
| 1629 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1630 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1632 | SCS2.Third == ICK_Identity)) |
| 1633 | // SCS2 is a proper subsequence of SCS1. |
| 1634 | return ImplicitConversionSequence::Worse; |
| 1635 | |
| 1636 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1637 | // defined below), or, if not that, |
| 1638 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1639 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1640 | if (Rank1 < Rank2) |
| 1641 | return ImplicitConversionSequence::Better; |
| 1642 | else if (Rank2 < Rank1) |
| 1643 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1644 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1645 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1646 | // are indistinguishable unless one of the following rules |
| 1647 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1649 | // A conversion that is not a conversion of a pointer, or |
| 1650 | // pointer to member, to bool is better than another conversion |
| 1651 | // that is such a conversion. |
| 1652 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1653 | return SCS2.isPointerConversionToBool() |
| 1654 | ? ImplicitConversionSequence::Better |
| 1655 | : ImplicitConversionSequence::Worse; |
| 1656 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1657 | // C++ [over.ics.rank]p4b2: |
| 1658 | // |
| 1659 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1660 | // conversion of B* to A* is better than conversion of B* to |
| 1661 | // void*, and conversion of A* to void* is better than conversion |
| 1662 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1664 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1666 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1667 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1668 | // Exactly one of the conversion sequences is a conversion to |
| 1669 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1670 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1671 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1672 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1673 | // Neither conversion sequence converts to a void pointer; compare |
| 1674 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1675 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1676 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1677 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1678 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1679 | // Both conversion sequences are conversions to void |
| 1680 | // pointers. Compare the source types to determine if there's an |
| 1681 | // inheritance relationship in their sources. |
| 1682 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1683 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1684 | |
| 1685 | // Adjust the types we're converting from via the array-to-pointer |
| 1686 | // conversion, if we need to. |
| 1687 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1688 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1689 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1690 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1691 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1693 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1694 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1695 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1696 | |
| 1697 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1698 | return ImplicitConversionSequence::Better; |
| 1699 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1700 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1701 | |
| 1702 | // Objective-C++: If one interface is more specific than the |
| 1703 | // other, it is the better one. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1704 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1705 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1706 | if (FromIface1 && FromIface1) { |
| 1707 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1708 | return ImplicitConversionSequence::Better; |
| 1709 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1710 | return ImplicitConversionSequence::Worse; |
| 1711 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1712 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1715 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1717 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1718 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1719 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1720 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1721 | // C++0x [over.ics.rank]p3b4: |
| 1722 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1723 | // implicit object parameter of a non-static member function declared |
| 1724 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1725 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1726 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1727 | // or if the member function in this case has a ref qualifier. |
| 1728 | // (Of course, we don't have ref qualifiers yet.) |
| 1729 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1730 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1731 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1732 | |
| 1733 | // C++ [over.ics.rank]p3b4: |
| 1734 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1735 | // which the references refer are the same type except for |
| 1736 | // top-level cv-qualifiers, and the type to which the reference |
| 1737 | // initialized by S2 refers is more cv-qualified than the type |
| 1738 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1739 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1740 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1741 | T1 = Context.getCanonicalType(T1); |
| 1742 | T2 = Context.getCanonicalType(T2); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1743 | if (Context.hasSameUnqualifiedType(T1, T2)) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1744 | if (T2.isMoreQualifiedThan(T1)) |
| 1745 | return ImplicitConversionSequence::Better; |
| 1746 | else if (T1.isMoreQualifiedThan(T2)) |
| 1747 | return ImplicitConversionSequence::Worse; |
| 1748 | } |
| 1749 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1750 | |
| 1751 | return ImplicitConversionSequence::Indistinguishable; |
| 1752 | } |
| 1753 | |
| 1754 | /// CompareQualificationConversions - Compares two standard conversion |
| 1755 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1757 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1758 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1760 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1761 | // -- S1 and S2 differ only in their qualification conversion and |
| 1762 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1763 | // cv-qualification signature of type T1 is a proper subset of |
| 1764 | // the cv-qualification signature of type T2, and S1 is not the |
| 1765 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1766 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1767 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1768 | return ImplicitConversionSequence::Indistinguishable; |
| 1769 | |
| 1770 | // FIXME: the example in the standard doesn't use a qualification |
| 1771 | // conversion (!) |
| 1772 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1773 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1774 | T1 = Context.getCanonicalType(T1); |
| 1775 | T2 = Context.getCanonicalType(T2); |
| 1776 | |
| 1777 | // If the types are the same, we won't learn anything by unwrapped |
| 1778 | // them. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1779 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1780 | return ImplicitConversionSequence::Indistinguishable; |
| 1781 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1783 | = ImplicitConversionSequence::Indistinguishable; |
| 1784 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1785 | // Within each iteration of the loop, we check the qualifiers to |
| 1786 | // determine if this still looks like a qualification |
| 1787 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1788 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1789 | // until there are no more pointers or pointers-to-members left |
| 1790 | // to unwrap. This essentially mimics what |
| 1791 | // IsQualificationConversion does, but here we're checking for a |
| 1792 | // strict subset of qualifiers. |
| 1793 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1794 | // The qualifiers are the same, so this doesn't tell us anything |
| 1795 | // about how the sequences rank. |
| 1796 | ; |
| 1797 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1798 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1799 | if (Result == ImplicitConversionSequence::Worse) |
| 1800 | // Neither has qualifiers that are a subset of the other's |
| 1801 | // qualifiers. |
| 1802 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1804 | Result = ImplicitConversionSequence::Better; |
| 1805 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1806 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1807 | if (Result == ImplicitConversionSequence::Better) |
| 1808 | // Neither has qualifiers that are a subset of the other's |
| 1809 | // qualifiers. |
| 1810 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1812 | Result = ImplicitConversionSequence::Worse; |
| 1813 | } else { |
| 1814 | // Qualifiers are disjoint. |
| 1815 | return ImplicitConversionSequence::Indistinguishable; |
| 1816 | } |
| 1817 | |
| 1818 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1819 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1820 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1823 | // Check that the winning standard conversion sequence isn't using |
| 1824 | // the deprecated string literal array to pointer conversion. |
| 1825 | switch (Result) { |
| 1826 | case ImplicitConversionSequence::Better: |
| 1827 | if (SCS1.Deprecated) |
| 1828 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1829 | break; |
| 1830 | |
| 1831 | case ImplicitConversionSequence::Indistinguishable: |
| 1832 | break; |
| 1833 | |
| 1834 | case ImplicitConversionSequence::Worse: |
| 1835 | if (SCS2.Deprecated) |
| 1836 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1837 | break; |
| 1838 | } |
| 1839 | |
| 1840 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1843 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1844 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1845 | /// various kinds of derived-to-base conversions (C++ |
| 1846 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1847 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1848 | ImplicitConversionSequence::CompareKind |
| 1849 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1850 | const StandardConversionSequence& SCS2) { |
| 1851 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1852 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1853 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1854 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1855 | |
| 1856 | // Adjust the types we're converting from via the array-to-pointer |
| 1857 | // conversion, if we need to. |
| 1858 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1859 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1860 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1861 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1862 | |
| 1863 | // Canonicalize all of the types. |
| 1864 | FromType1 = Context.getCanonicalType(FromType1); |
| 1865 | ToType1 = Context.getCanonicalType(ToType1); |
| 1866 | FromType2 = Context.getCanonicalType(FromType2); |
| 1867 | ToType2 = Context.getCanonicalType(ToType2); |
| 1868 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1869 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1870 | // |
| 1871 | // If class B is derived directly or indirectly from class A and |
| 1872 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1873 | // |
| 1874 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1875 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1879 | SCS2.Second == ICK_Pointer_Conversion && |
| 1880 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1881 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1882 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1884 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1886 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1887 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1888 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1889 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1890 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1891 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1892 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1893 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1894 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 1895 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1897 | // -- 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] | 1898 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1899 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1900 | return ImplicitConversionSequence::Better; |
| 1901 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1902 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1903 | |
| 1904 | if (ToIface1 && ToIface2) { |
| 1905 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1906 | return ImplicitConversionSequence::Better; |
| 1907 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1908 | return ImplicitConversionSequence::Worse; |
| 1909 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1910 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1911 | |
| 1912 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1913 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1914 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1915 | return ImplicitConversionSequence::Better; |
| 1916 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1917 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1919 | if (FromIface1 && FromIface2) { |
| 1920 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1921 | return ImplicitConversionSequence::Better; |
| 1922 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1923 | return ImplicitConversionSequence::Worse; |
| 1924 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1925 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1928 | // Compare based on reference bindings. |
| 1929 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1930 | SCS1.Second == ICK_Derived_To_Base) { |
| 1931 | // -- binding of an expression of type C to a reference of type |
| 1932 | // B& is better than binding an expression of type C to a |
| 1933 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1934 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 1935 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1936 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1937 | return ImplicitConversionSequence::Better; |
| 1938 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1939 | return ImplicitConversionSequence::Worse; |
| 1940 | } |
| 1941 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1942 | // -- binding of an expression of type B to a reference of type |
| 1943 | // A& is better than binding an expression of type C to a |
| 1944 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1945 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 1946 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1947 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1948 | return ImplicitConversionSequence::Better; |
| 1949 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1950 | return ImplicitConversionSequence::Worse; |
| 1951 | } |
| 1952 | } |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 1955 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 1956 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 1957 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 1958 | const MemberPointerType * FromMemPointer1 = |
| 1959 | FromType1->getAs<MemberPointerType>(); |
| 1960 | const MemberPointerType * ToMemPointer1 = |
| 1961 | ToType1->getAs<MemberPointerType>(); |
| 1962 | const MemberPointerType * FromMemPointer2 = |
| 1963 | FromType2->getAs<MemberPointerType>(); |
| 1964 | const MemberPointerType * ToMemPointer2 = |
| 1965 | ToType2->getAs<MemberPointerType>(); |
| 1966 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 1967 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 1968 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 1969 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 1970 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 1971 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 1972 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 1973 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 1974 | // 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] | 1975 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1976 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1977 | return ImplicitConversionSequence::Worse; |
| 1978 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1979 | return ImplicitConversionSequence::Better; |
| 1980 | } |
| 1981 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 1982 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 1983 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1984 | return ImplicitConversionSequence::Better; |
| 1985 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1986 | return ImplicitConversionSequence::Worse; |
| 1987 | } |
| 1988 | } |
| 1989 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1990 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1991 | SCS1.Second == ICK_Derived_To_Base) { |
| 1992 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1993 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 1994 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1995 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1996 | return ImplicitConversionSequence::Better; |
| 1997 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1998 | return ImplicitConversionSequence::Worse; |
| 1999 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2001 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2002 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2003 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2004 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2005 | return ImplicitConversionSequence::Better; |
| 2006 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2007 | return ImplicitConversionSequence::Worse; |
| 2008 | } |
| 2009 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2010 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2011 | return ImplicitConversionSequence::Indistinguishable; |
| 2012 | } |
| 2013 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2014 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2015 | /// ToType from the expression From. Return the implicit conversion |
| 2016 | /// sequence required to pass this argument, which may be a bad |
| 2017 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2018 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2019 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2020 | /// 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] | 2021 | ImplicitConversionSequence |
| 2022 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2023 | bool SuppressUserConversions, bool ForceRValue, |
| 2024 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2025 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2026 | ImplicitConversionSequence ICS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2028 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2029 | SuppressUserConversions, |
| 2030 | /*AllowExplicit=*/false, |
| 2031 | ForceRValue, |
| 2032 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2033 | return ICS; |
| 2034 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2036 | SuppressUserConversions, |
| 2037 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2038 | ForceRValue, |
| 2039 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2043 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2044 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2045 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2046 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2047 | /// differently in C++0x for this case. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2049 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2050 | if (!getLangOptions().CPlusPlus) { |
| 2051 | // In C, argument passing is the same as performing an assignment. |
| 2052 | QualType FromType = From->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2054 | AssignConvertType ConvTy = |
| 2055 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2056 | if (ConvTy != Compatible && |
| 2057 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2058 | ConvTy = Compatible; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2060 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 2061 | FromType, From, Flavor); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2062 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2064 | if (ToType->isReferenceType()) |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2065 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2066 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2067 | /*SuppressUserConversions=*/false, |
| 2068 | /*AllowExplicit=*/false, |
| 2069 | /*ForceRValue=*/false); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2070 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2071 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 2072 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2073 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2074 | if (!DiagnoseAmbiguousUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2075 | return Diag(From->getSourceRange().getBegin(), |
| 2076 | diag::err_typecheck_convert_incompatible) |
| 2077 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2078 | return true; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2081 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2082 | /// parameter of the given member function (@c Method) from the |
| 2083 | /// expression @p From. |
| 2084 | ImplicitConversionSequence |
| 2085 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 2086 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2087 | QualType ImplicitParamType |
| 2088 | = Context.getCVRQualifiedType(ClassType, Method->getTypeQualifiers()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2089 | |
| 2090 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2091 | // to exit early. |
| 2092 | ImplicitConversionSequence ICS; |
| 2093 | ICS.Standard.setAsIdentityConversion(); |
| 2094 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2095 | |
| 2096 | // We need to have an object of class type. |
| 2097 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2098 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2099 | FromType = PT->getPointeeType(); |
| 2100 | |
| 2101 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2102 | |
| 2103 | // The implicit object parmeter is has the type "reference to cv X", |
| 2104 | // where X is the class of which the function is a member |
| 2105 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2106 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2108 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2109 | // reference binding here, that allows class rvalues to bind to |
| 2110 | // non-constant references. |
| 2111 | |
| 2112 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2113 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2114 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2115 | if (ImplicitParamType.getCVRQualifiers() |
| 2116 | != FromTypeCanon.getLocalCVRQualifiers() && |
Douglas Gregor | b1c2ea5 | 2009-11-05 00:07:36 +0000 | [diff] [blame] | 2117 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2118 | return ICS; |
| 2119 | |
| 2120 | // Check that we have either the same type or a derived type. It |
| 2121 | // affects the conversion rank. |
| 2122 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2123 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2124 | ICS.Standard.Second = ICK_Identity; |
| 2125 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2126 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2127 | else |
| 2128 | return ICS; |
| 2129 | |
| 2130 | // Success. Mark this as a reference binding. |
| 2131 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2132 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2133 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2134 | ICS.Standard.ReferenceBinding = true; |
| 2135 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2136 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2137 | return ICS; |
| 2138 | } |
| 2139 | |
| 2140 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2141 | /// the implicit object parameter for the given Method with the given |
| 2142 | /// expression. |
| 2143 | bool |
| 2144 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2145 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2147 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2149 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2150 | FromRecordType = PT->getPointeeType(); |
| 2151 | DestType = Method->getThisType(Context); |
| 2152 | } else { |
| 2153 | FromRecordType = From->getType(); |
| 2154 | DestType = ImplicitParamRecordType; |
| 2155 | } |
| 2156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | ImplicitConversionSequence ICS |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2158 | = TryObjectArgumentInitialization(From, Method); |
| 2159 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2160 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2161 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2162 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2164 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2165 | CheckDerivedToBaseConversion(FromRecordType, |
| 2166 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2167 | From->getSourceRange().getBegin(), |
| 2168 | From->getSourceRange())) |
| 2169 | return true; |
| 2170 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 116b7d9 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2172 | /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2173 | return false; |
| 2174 | } |
| 2175 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2176 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2177 | /// expression From to bool (C++0x [conv]p3). |
| 2178 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2180 | // FIXME: Are these flags correct? |
| 2181 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2183 | /*ForceRValue=*/false, |
| 2184 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2188 | /// of the expression From to bool (C++0x [conv]p3). |
| 2189 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2190 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2191 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2192 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2193 | |
| 2194 | if (!DiagnoseAmbiguousUserDefinedConversion(From, Context.BoolTy)) |
| 2195 | return Diag(From->getSourceRange().getBegin(), |
| 2196 | diag::err_typecheck_bool_condition) |
| 2197 | << From->getType() << From->getSourceRange(); |
| 2198 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2201 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2202 | /// candidate functions, using the given function call arguments. If |
| 2203 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2204 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2205 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2206 | /// hacky way to implement the overloading rules for elidable copy |
| 2207 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2208 | /// |
| 2209 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2210 | /// based on an incomplete set of function arguments. This feature is used by |
| 2211 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | void |
| 2213 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2214 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2215 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2216 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2217 | bool ForceRValue, |
| 2218 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2220 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2221 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2223 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2225 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2227 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2228 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2229 | // If we get here, it's because we're calling a member function |
| 2230 | // that is named without a member access expression (e.g., |
| 2231 | // "this->f") that was either written explicitly or created |
| 2232 | // implicitly. This can happen with a qualified call to a member |
| 2233 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2234 | // object argument (C++ [over.call.func]p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2236 | SuppressUserConversions, ForceRValue); |
| 2237 | return; |
| 2238 | } |
| 2239 | // We treat a constructor like a non-member function, since its object |
| 2240 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2243 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2244 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2245 | |
| 2246 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2247 | // C++ [class.copy]p3: |
| 2248 | // A member function template is never instantiated to perform the copy |
| 2249 | // of a class object to an object of its class type. |
| 2250 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2251 | if (NumArgs == 1 && |
| 2252 | Constructor->isCopyConstructorLikeSpecialization() && |
| 2253 | Context.hasSameUnqualifiedType(ClassType, Args[0]->getType())) |
| 2254 | return; |
| 2255 | } |
| 2256 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2257 | // Add this candidate |
| 2258 | CandidateSet.push_back(OverloadCandidate()); |
| 2259 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2260 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2261 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2262 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2263 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2264 | |
| 2265 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2266 | |
| 2267 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2268 | // parameters is viable only if it has an ellipsis in its parameter |
| 2269 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2270 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2271 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2272 | Candidate.Viable = false; |
| 2273 | return; |
| 2274 | } |
| 2275 | |
| 2276 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2277 | // is viable only if the (m+1)st parameter has a default argument |
| 2278 | // (8.3.6). For the purposes of overload resolution, the |
| 2279 | // parameter list is truncated on the right, so that there are |
| 2280 | // exactly m parameters. |
| 2281 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2282 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2283 | // Not enough arguments. |
| 2284 | Candidate.Viable = false; |
| 2285 | return; |
| 2286 | } |
| 2287 | |
| 2288 | // Determine the implicit conversion sequences for each of the |
| 2289 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2290 | Candidate.Conversions.resize(NumArgs); |
| 2291 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2292 | if (ArgIdx < NumArgsInProto) { |
| 2293 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2294 | // exist for each argument an implicit conversion sequence |
| 2295 | // (13.3.3.1) that converts that argument to the corresponding |
| 2296 | // parameter of F. |
| 2297 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | Candidate.Conversions[ArgIdx] |
| 2299 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2300 | SuppressUserConversions, ForceRValue, |
| 2301 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2303 | == ImplicitConversionSequence::BadConversion) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2304 | // 13.3.3.1-p10 If several different sequences of conversions exist that |
| 2305 | // each convert the argument to the parameter type, the implicit conversion |
| 2306 | // sequence associated with the parameter is defined to be the unique conversion |
| 2307 | // sequence designated the ambiguous conversion sequence. For the purpose of |
| 2308 | // ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous |
| 2309 | // conversion sequence is treated as a user-defined sequence that is |
| 2310 | // indistinguishable from any other user-defined conversion sequence |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2311 | if (!Candidate.Conversions[ArgIdx].ConversionFunctionSet.empty()) { |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2312 | Candidate.Conversions[ArgIdx].ConversionKind = |
| 2313 | ImplicitConversionSequence::UserDefinedConversion; |
Fariborz Jahanian | 4a6a2b8 | 2009-09-29 17:31:54 +0000 | [diff] [blame] | 2314 | // Set the conversion function to one of them. As due to ambiguity, |
| 2315 | // they carry the same weight and is needed for overload resolution |
| 2316 | // later. |
| 2317 | Candidate.Conversions[ArgIdx].UserDefined.ConversionFunction = |
| 2318 | Candidate.Conversions[ArgIdx].ConversionFunctionSet[0]; |
| 2319 | } |
Fariborz Jahanian | 99d6c44 | 2009-09-28 19:06:58 +0000 | [diff] [blame] | 2320 | else { |
| 2321 | Candidate.Viable = false; |
| 2322 | break; |
| 2323 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2324 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2325 | } else { |
| 2326 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2327 | // argument for which there is no corresponding parameter is |
| 2328 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2330 | = ImplicitConversionSequence::EllipsisConversion; |
| 2331 | } |
| 2332 | } |
| 2333 | } |
| 2334 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2335 | /// \brief Add all of the function declarations in the given function set to |
| 2336 | /// the overload canddiate set. |
| 2337 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2338 | Expr **Args, unsigned NumArgs, |
| 2339 | OverloadCandidateSet& CandidateSet, |
| 2340 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2342 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2343 | F != FEnd; ++F) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2344 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2345 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2346 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
| 2347 | Args[0], Args + 1, NumArgs - 1, |
| 2348 | CandidateSet, SuppressUserConversions); |
| 2349 | else |
| 2350 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2351 | SuppressUserConversions); |
| 2352 | } else { |
| 2353 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2354 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2355 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2356 | AddMethodTemplateCandidate(FunTmpl, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2357 | /*FIXME: explicit args */false, 0, 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2358 | Args[0], Args + 1, NumArgs - 1, |
| 2359 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2360 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2361 | else |
| 2362 | AddTemplateOverloadCandidate(FunTmpl, |
| 2363 | /*FIXME: explicit args */false, 0, 0, |
| 2364 | Args, NumArgs, CandidateSet, |
| 2365 | SuppressUserConversions); |
| 2366 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2367 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2370 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2371 | /// of candidate functions, using the given function call arguments |
| 2372 | /// and the object argument (@c Object). For example, in a call |
| 2373 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2374 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2375 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2376 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2377 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2378 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | void |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2380 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2381 | Expr **Args, unsigned NumArgs, |
| 2382 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | bool SuppressUserConversions, bool ForceRValue) { |
| 2384 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2385 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2386 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2387 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2388 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2389 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2390 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2391 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2392 | if (!CandidateSet.isNewCandidate(Method)) |
| 2393 | return; |
| 2394 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2395 | // Add this candidate |
| 2396 | CandidateSet.push_back(OverloadCandidate()); |
| 2397 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2398 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2399 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2400 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2401 | |
| 2402 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2403 | |
| 2404 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2405 | // parameters is viable only if it has an ellipsis in its parameter |
| 2406 | // list (8.3.5). |
| 2407 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2408 | Candidate.Viable = false; |
| 2409 | return; |
| 2410 | } |
| 2411 | |
| 2412 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2413 | // is viable only if the (m+1)st parameter has a default argument |
| 2414 | // (8.3.6). For the purposes of overload resolution, the |
| 2415 | // parameter list is truncated on the right, so that there are |
| 2416 | // exactly m parameters. |
| 2417 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2418 | if (NumArgs < MinRequiredArgs) { |
| 2419 | // Not enough arguments. |
| 2420 | Candidate.Viable = false; |
| 2421 | return; |
| 2422 | } |
| 2423 | |
| 2424 | Candidate.Viable = true; |
| 2425 | Candidate.Conversions.resize(NumArgs + 1); |
| 2426 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2427 | if (Method->isStatic() || !Object) |
| 2428 | // The implicit object argument is ignored. |
| 2429 | Candidate.IgnoreObjectArgument = true; |
| 2430 | else { |
| 2431 | // Determine the implicit conversion sequence for the object |
| 2432 | // parameter. |
| 2433 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2435 | == ImplicitConversionSequence::BadConversion) { |
| 2436 | Candidate.Viable = false; |
| 2437 | return; |
| 2438 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | // Determine the implicit conversion sequences for each of the |
| 2442 | // arguments. |
| 2443 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2444 | if (ArgIdx < NumArgsInProto) { |
| 2445 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2446 | // exist for each argument an implicit conversion sequence |
| 2447 | // (13.3.3.1) that converts that argument to the corresponding |
| 2448 | // parameter of F. |
| 2449 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | Candidate.Conversions[ArgIdx + 1] |
| 2451 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2452 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2453 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2455 | == ImplicitConversionSequence::BadConversion) { |
| 2456 | Candidate.Viable = false; |
| 2457 | break; |
| 2458 | } |
| 2459 | } else { |
| 2460 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2461 | // argument for which there is no corresponding parameter is |
| 2462 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2464 | = ImplicitConversionSequence::EllipsisConversion; |
| 2465 | } |
| 2466 | } |
| 2467 | } |
| 2468 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2469 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2470 | /// set, using template argument deduction to produce an appropriate member |
| 2471 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2473 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2474 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2475 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2476 | unsigned NumExplicitTemplateArgs, |
| 2477 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2478 | OverloadCandidateSet& CandidateSet, |
| 2479 | bool SuppressUserConversions, |
| 2480 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2481 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2482 | return; |
| 2483 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2484 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2485 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2486 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2487 | // 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] | 2488 | // candidate functions in the usual way.113) A given name can refer to one |
| 2489 | // or more function templates and also to a set of overloaded non-template |
| 2490 | // functions. In such a case, the candidate functions generated from each |
| 2491 | // function template are combined with the set of non-template candidate |
| 2492 | // functions. |
| 2493 | TemplateDeductionInfo Info(Context); |
| 2494 | FunctionDecl *Specialization = 0; |
| 2495 | if (TemplateDeductionResult Result |
| 2496 | = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs, |
| 2497 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2498 | Args, NumArgs, Specialization, Info)) { |
| 2499 | // FIXME: Record what happened with template argument deduction, so |
| 2500 | // that we can give the user a beautiful diagnostic. |
| 2501 | (void)Result; |
| 2502 | return; |
| 2503 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2505 | // Add the function template specialization produced by template argument |
| 2506 | // deduction as a candidate. |
| 2507 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2508 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2509 | "Specialization is not a member function?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2511 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2512 | } |
| 2513 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2514 | /// \brief Add a C++ function template specialization as a candidate |
| 2515 | /// in the candidate set, using template argument deduction to produce |
| 2516 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2518 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2519 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2520 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2521 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2522 | Expr **Args, unsigned NumArgs, |
| 2523 | OverloadCandidateSet& CandidateSet, |
| 2524 | bool SuppressUserConversions, |
| 2525 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2526 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2527 | return; |
| 2528 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2529 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2531 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2532 | // 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] | 2533 | // candidate functions in the usual way.113) A given name can refer to one |
| 2534 | // or more function templates and also to a set of overloaded non-template |
| 2535 | // functions. In such a case, the candidate functions generated from each |
| 2536 | // function template are combined with the set of non-template candidate |
| 2537 | // functions. |
| 2538 | TemplateDeductionInfo Info(Context); |
| 2539 | FunctionDecl *Specialization = 0; |
| 2540 | if (TemplateDeductionResult Result |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2541 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2542 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2543 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2544 | // FIXME: Record what happened with template argument deduction, so |
| 2545 | // that we can give the user a beautiful diagnostic. |
| 2546 | (void)Result; |
| 2547 | return; |
| 2548 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2550 | // Add the function template specialization produced by template argument |
| 2551 | // deduction as a candidate. |
| 2552 | assert(Specialization && "Missing function template specialization?"); |
| 2553 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2554 | SuppressUserConversions, ForceRValue); |
| 2555 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2557 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2559 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | /// 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] | 2561 | /// (which may or may not be the same type as the type that the |
| 2562 | /// conversion function produces). |
| 2563 | void |
| 2564 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2565 | Expr *From, QualType ToType, |
| 2566 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2567 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2568 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2569 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2570 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2571 | return; |
| 2572 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2573 | // Add this candidate |
| 2574 | CandidateSet.push_back(OverloadCandidate()); |
| 2575 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2576 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2577 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2578 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2579 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2581 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2582 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2583 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2584 | // Determine the implicit conversion sequence for the implicit |
| 2585 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2586 | Candidate.Viable = true; |
| 2587 | Candidate.Conversions.resize(1); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2588 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2589 | // Conversion functions to a different type in the base class is visible in |
| 2590 | // the derived class. So, a derived to base conversion should not participate |
| 2591 | // in overload resolution. |
| 2592 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2593 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2595 | == ImplicitConversionSequence::BadConversion) { |
| 2596 | Candidate.Viable = false; |
| 2597 | return; |
| 2598 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2599 | |
| 2600 | // We won't go through a user-define type conversion function to convert a |
| 2601 | // derived to base as such conversions are given Conversion Rank. They only |
| 2602 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2603 | QualType FromCanon |
| 2604 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2605 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2606 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2607 | Candidate.Viable = false; |
| 2608 | return; |
| 2609 | } |
| 2610 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2611 | |
| 2612 | // To determine what the conversion from the result of calling the |
| 2613 | // conversion function to the type we're eventually trying to |
| 2614 | // convert to (ToType), we need to synthesize a call to the |
| 2615 | // conversion function and attempt copy initialization from it. This |
| 2616 | // makes sure that we get the right semantics with respect to |
| 2617 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2618 | // call on the stack and we don't need its arguments to be |
| 2619 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2620 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2621 | SourceLocation()); |
| 2622 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2623 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2624 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2625 | |
| 2626 | // 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] | 2627 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2628 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2630 | Conversion->getConversionType().getNonReferenceType(), |
| 2631 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | ImplicitConversionSequence ICS = |
| 2633 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2634 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2635 | /*ForceRValue=*/false, |
| 2636 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2638 | switch (ICS.ConversionKind) { |
| 2639 | case ImplicitConversionSequence::StandardConversion: |
| 2640 | Candidate.FinalConversion = ICS.Standard; |
| 2641 | break; |
| 2642 | |
| 2643 | case ImplicitConversionSequence::BadConversion: |
| 2644 | Candidate.Viable = false; |
| 2645 | break; |
| 2646 | |
| 2647 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2649 | "Can only end up with a standard conversion sequence or failure"); |
| 2650 | } |
| 2651 | } |
| 2652 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2653 | /// \brief Adds a conversion function template specialization |
| 2654 | /// candidate to the overload set, using template argument deduction |
| 2655 | /// to deduce the template arguments of the conversion function |
| 2656 | /// template from the type that we are converting to (C++ |
| 2657 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2659 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2660 | Expr *From, QualType ToType, |
| 2661 | OverloadCandidateSet &CandidateSet) { |
| 2662 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2663 | "Only conversion function templates permitted here"); |
| 2664 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2665 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2666 | return; |
| 2667 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2668 | TemplateDeductionInfo Info(Context); |
| 2669 | CXXConversionDecl *Specialization = 0; |
| 2670 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2672 | Specialization, Info)) { |
| 2673 | // FIXME: Record what happened with template argument deduction, so |
| 2674 | // that we can give the user a beautiful diagnostic. |
| 2675 | (void)Result; |
| 2676 | return; |
| 2677 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2679 | // Add the conversion function template specialization produced by |
| 2680 | // template argument deduction as a candidate. |
| 2681 | assert(Specialization && "Missing function template specialization?"); |
| 2682 | AddConversionCandidate(Specialization, From, ToType, CandidateSet); |
| 2683 | } |
| 2684 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2685 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2686 | /// converts the given @c Object to a function pointer via the |
| 2687 | /// conversion function @c Conversion, and then attempts to call it |
| 2688 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2689 | /// the type of function that we'll eventually be calling. |
| 2690 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2691 | const FunctionProtoType *Proto, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2692 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2693 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2694 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2695 | return; |
| 2696 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2697 | CandidateSet.push_back(OverloadCandidate()); |
| 2698 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2699 | Candidate.Function = 0; |
| 2700 | Candidate.Surrogate = Conversion; |
| 2701 | Candidate.Viable = true; |
| 2702 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2703 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2704 | Candidate.Conversions.resize(NumArgs + 1); |
| 2705 | |
| 2706 | // Determine the implicit conversion sequence for the implicit |
| 2707 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | ImplicitConversionSequence ObjectInit |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2709 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2710 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2711 | Candidate.Viable = false; |
| 2712 | return; |
| 2713 | } |
| 2714 | |
| 2715 | // The first conversion is actually a user-defined conversion whose |
| 2716 | // first conversion is ObjectInit's standard conversion (which is |
| 2717 | // effectively a reference binding). Record it as such. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2719 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2720 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2721 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2722 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2724 | = Candidate.Conversions[0].UserDefined.Before; |
| 2725 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2726 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2728 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2729 | |
| 2730 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2731 | // parameters is viable only if it has an ellipsis in its parameter |
| 2732 | // list (8.3.5). |
| 2733 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2734 | Candidate.Viable = false; |
| 2735 | return; |
| 2736 | } |
| 2737 | |
| 2738 | // Function types don't have any default arguments, so just check if |
| 2739 | // we have enough arguments. |
| 2740 | if (NumArgs < NumArgsInProto) { |
| 2741 | // Not enough arguments. |
| 2742 | Candidate.Viable = false; |
| 2743 | return; |
| 2744 | } |
| 2745 | |
| 2746 | // Determine the implicit conversion sequences for each of the |
| 2747 | // arguments. |
| 2748 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2749 | if (ArgIdx < NumArgsInProto) { |
| 2750 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2751 | // exist for each argument an implicit conversion sequence |
| 2752 | // (13.3.3.1) that converts that argument to the corresponding |
| 2753 | // parameter of F. |
| 2754 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | Candidate.Conversions[ArgIdx + 1] |
| 2756 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2757 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2758 | /*ForceRValue=*/false, |
| 2759 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2761 | == ImplicitConversionSequence::BadConversion) { |
| 2762 | Candidate.Viable = false; |
| 2763 | break; |
| 2764 | } |
| 2765 | } else { |
| 2766 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2767 | // argument for which there is no corresponding parameter is |
| 2768 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2770 | = ImplicitConversionSequence::EllipsisConversion; |
| 2771 | } |
| 2772 | } |
| 2773 | } |
| 2774 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2775 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2776 | // operator overloading logic over to the scheme used by binary operators, which |
| 2777 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2778 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2779 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2780 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2781 | OverloadCandidateSet& CandidateSet, |
| 2782 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2783 | FunctionSet Functions; |
| 2784 | |
| 2785 | QualType T1 = Args[0]->getType(); |
| 2786 | QualType T2; |
| 2787 | if (NumArgs > 1) |
| 2788 | T2 = Args[1]->getType(); |
| 2789 | |
| 2790 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2791 | if (S) |
| 2792 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2793 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, NumArgs, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2794 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2795 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2796 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | /// \brief Add overload candidates for overloaded operators that are |
| 2800 | /// member functions. |
| 2801 | /// |
| 2802 | /// Add the overloaded operator candidates that are member functions |
| 2803 | /// for the operator Op that was used in an operator expression such |
| 2804 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2805 | /// CandidateSet will store the added overload candidates. (C++ |
| 2806 | /// [over.match.oper]). |
| 2807 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2808 | SourceLocation OpLoc, |
| 2809 | Expr **Args, unsigned NumArgs, |
| 2810 | OverloadCandidateSet& CandidateSet, |
| 2811 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2812 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2813 | |
| 2814 | // C++ [over.match.oper]p3: |
| 2815 | // For a unary operator @ with an operand of a type whose |
| 2816 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2817 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2818 | // a right operand of a type whose cv-unqualified version is T2, |
| 2819 | // three sets of candidate functions, designated member |
| 2820 | // candidates, non-member candidates and built-in candidates, are |
| 2821 | // constructed as follows: |
| 2822 | QualType T1 = Args[0]->getType(); |
| 2823 | QualType T2; |
| 2824 | if (NumArgs > 1) |
| 2825 | T2 = Args[1]->getType(); |
| 2826 | |
| 2827 | // -- If T1 is a class type, the set of member candidates is the |
| 2828 | // result of the qualified lookup of T1::operator@ |
| 2829 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2830 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2831 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2832 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2833 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2834 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2835 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 2836 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 2837 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 2838 | Operators.suppressDiagnostics(); |
| 2839 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2840 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2841 | OperEnd = Operators.end(); |
| 2842 | Oper != OperEnd; |
Douglas Gregor | d9842d0 | 2009-10-14 16:50:13 +0000 | [diff] [blame] | 2843 | ++Oper) { |
| 2844 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Oper)) { |
| 2845 | AddMethodCandidate(Method, Args[0], Args+1, NumArgs - 1, CandidateSet, |
| 2846 | /*SuppressUserConversions=*/false); |
| 2847 | continue; |
| 2848 | } |
| 2849 | |
| 2850 | assert(isa<FunctionTemplateDecl>(*Oper) && |
| 2851 | isa<CXXMethodDecl>(cast<FunctionTemplateDecl>(*Oper) |
| 2852 | ->getTemplatedDecl()) && |
| 2853 | "Expected a member function template"); |
| 2854 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Oper), false, 0, 0, |
| 2855 | Args[0], Args+1, NumArgs - 1, CandidateSet, |
| 2856 | /*SuppressUserConversions=*/false); |
| 2857 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2858 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2861 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2862 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2863 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2864 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2865 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2866 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2867 | /// (at the beginning of the argument list) that will be contextually |
| 2868 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2869 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2870 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2871 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2872 | bool IsAssignmentOperator, |
| 2873 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2874 | // Add this candidate |
| 2875 | CandidateSet.push_back(OverloadCandidate()); |
| 2876 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2877 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2878 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2879 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2880 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2881 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2882 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2883 | |
| 2884 | // Determine the implicit conversion sequences for each of the |
| 2885 | // arguments. |
| 2886 | Candidate.Viable = true; |
| 2887 | Candidate.Conversions.resize(NumArgs); |
| 2888 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2889 | // C++ [over.match.oper]p4: |
| 2890 | // For the built-in assignment operators, conversions of the |
| 2891 | // left operand are restricted as follows: |
| 2892 | // -- no temporaries are introduced to hold the left operand, and |
| 2893 | // -- no user-defined conversions are applied to the left |
| 2894 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2896 | // |
| 2897 | // We block these conversions by turning off user-defined |
| 2898 | // conversions, since that is the only way that initialization of |
| 2899 | // a reference to a non-class type can occur from something that |
| 2900 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2901 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2902 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2903 | "Contextual conversion to bool requires bool type"); |
| 2904 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2905 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | Candidate.Conversions[ArgIdx] |
| 2907 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2908 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2909 | /*ForceRValue=*/false, |
| 2910 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2911 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2913 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2914 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2915 | break; |
| 2916 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2921 | /// candidate operator functions for built-in operators (C++ |
| 2922 | /// [over.built]). The types are separated into pointer types and |
| 2923 | /// enumeration types. |
| 2924 | class BuiltinCandidateTypeSet { |
| 2925 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2926 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2927 | |
| 2928 | /// PointerTypes - The set of pointer types that will be used in the |
| 2929 | /// built-in candidates. |
| 2930 | TypeSet PointerTypes; |
| 2931 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2932 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2933 | /// used in the built-in candidates. |
| 2934 | TypeSet MemberPointerTypes; |
| 2935 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2936 | /// EnumerationTypes - The set of enumeration types that will be |
| 2937 | /// used in the built-in candidates. |
| 2938 | TypeSet EnumerationTypes; |
| 2939 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2940 | /// Sema - The semantic analysis instance where we are building the |
| 2941 | /// candidate type set. |
| 2942 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2944 | /// Context - The AST context in which we will build the type sets. |
| 2945 | ASTContext &Context; |
| 2946 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 2947 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 2948 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2949 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2950 | |
| 2951 | public: |
| 2952 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2953 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2954 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2956 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2958 | void AddTypesConvertedFrom(QualType Ty, |
| 2959 | SourceLocation Loc, |
| 2960 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 2961 | bool AllowExplicitConversions, |
| 2962 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2963 | |
| 2964 | /// pointer_begin - First pointer type found; |
| 2965 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2966 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2967 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2968 | iterator pointer_end() { return PointerTypes.end(); } |
| 2969 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2970 | /// member_pointer_begin - First member pointer type found; |
| 2971 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2972 | |
| 2973 | /// member_pointer_end - Past the last member pointer type found; |
| 2974 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2975 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2976 | /// enumeration_begin - First enumeration type found; |
| 2977 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2978 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2979 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2980 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2981 | }; |
| 2982 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2983 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2984 | /// the set of pointer types along with any more-qualified variants of |
| 2985 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2986 | /// will add "int const *", "int const volatile *", "int const |
| 2987 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2988 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2989 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2990 | /// |
| 2991 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2992 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2993 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 2994 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2996 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2997 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2998 | return false; |
| 2999 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3000 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3001 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3002 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3003 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3004 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3005 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3006 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3007 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3008 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3009 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3010 | // Iterate through all strict supersets of BaseCVR. |
| 3011 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3012 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3013 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3014 | // in the types. |
| 3015 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3016 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3017 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3018 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | return true; |
| 3022 | } |
| 3023 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3024 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3025 | /// to the set of pointer types along with any more-qualified variants of |
| 3026 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3027 | /// will add "int const *", "int const volatile *", "int const |
| 3028 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3029 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3030 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3031 | /// |
| 3032 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3033 | bool |
| 3034 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3035 | QualType Ty) { |
| 3036 | // Insert this type. |
| 3037 | if (!MemberPointerTypes.insert(Ty)) |
| 3038 | return false; |
| 3039 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3040 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3041 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3042 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3043 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3044 | const Type *ClassTy = PointerTy->getClass(); |
| 3045 | |
| 3046 | // Iterate through all strict supersets of the pointee type's CVR |
| 3047 | // qualifiers. |
| 3048 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3049 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3050 | if ((CVR | BaseCVR) != CVR) continue; |
| 3051 | |
| 3052 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3053 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | return true; |
| 3057 | } |
| 3058 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3059 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3060 | /// 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] | 3061 | /// primarily interested in pointer types and enumeration types. We also |
| 3062 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3063 | /// AllowUserConversions is true if we should look at the conversion |
| 3064 | /// functions of a class type, and AllowExplicitConversions if we |
| 3065 | /// should also include the explicit conversion functions of a class |
| 3066 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3068 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3069 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3070 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3071 | bool AllowExplicitConversions, |
| 3072 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3073 | // Only deal with canonical types. |
| 3074 | Ty = Context.getCanonicalType(Ty); |
| 3075 | |
| 3076 | // Look through reference types; they aren't part of the type of an |
| 3077 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3078 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3079 | Ty = RefTy->getPointeeType(); |
| 3080 | |
| 3081 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3082 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3083 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3084 | // If we're dealing with an array type, decay to the pointer. |
| 3085 | if (Ty->isArrayType()) |
| 3086 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3087 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3088 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3089 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3090 | |
| 3091 | // Insert our type, and its more-qualified variants, into the set |
| 3092 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3093 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3094 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3095 | } else if (Ty->isMemberPointerType()) { |
| 3096 | // Member pointers are far easier, since the pointee can't be converted. |
| 3097 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3098 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3099 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3100 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3101 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3102 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3103 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3104 | // No conversion functions in incomplete types. |
| 3105 | return; |
| 3106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3108 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3109 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3110 | = ClassDecl->getVisibleConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3112 | = Conversions->function_begin(); |
| 3113 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3114 | CXXConversionDecl *Conv; |
| 3115 | FunctionTemplateDecl *ConvTemplate; |
| 3116 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 3117 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3119 | // about which builtin types we can convert to. |
| 3120 | if (ConvTemplate) |
| 3121 | continue; |
| 3122 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3123 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3124 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3125 | VisibleQuals); |
| 3126 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | } |
| 3130 | } |
| 3131 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3132 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3133 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3134 | /// given type to the candidate set. |
| 3135 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3136 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3138 | unsigned NumArgs, |
| 3139 | OverloadCandidateSet &CandidateSet) { |
| 3140 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3141 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3142 | // T& operator=(T&, T) |
| 3143 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3144 | ParamTypes[1] = T; |
| 3145 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3146 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3148 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3149 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3150 | ParamTypes[0] |
| 3151 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3152 | ParamTypes[1] = T; |
| 3153 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3155 | } |
| 3156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3158 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3159 | /// 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] | 3160 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3161 | Qualifiers VRQuals; |
| 3162 | const RecordType *TyRec; |
| 3163 | if (const MemberPointerType *RHSMPType = |
| 3164 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3165 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3166 | else |
| 3167 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3168 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3169 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3170 | VRQuals.addVolatile(); |
| 3171 | VRQuals.addRestrict(); |
| 3172 | return VRQuals; |
| 3173 | } |
| 3174 | |
| 3175 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 3176 | OverloadedFunctionDecl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3177 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3178 | |
| 3179 | for (OverloadedFunctionDecl::function_iterator Func |
| 3180 | = Conversions->function_begin(); |
| 3181 | Func != Conversions->function_end(); ++Func) { |
| 3182 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*Func)) { |
| 3183 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3184 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3185 | CanTy = ResTypeRef->getPointeeType(); |
| 3186 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3187 | // as see them. |
| 3188 | bool done = false; |
| 3189 | while (!done) { |
| 3190 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3191 | CanTy = ResTypePtr->getPointeeType(); |
| 3192 | else if (const MemberPointerType *ResTypeMPtr = |
| 3193 | CanTy->getAs<MemberPointerType>()) |
| 3194 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3195 | else |
| 3196 | done = true; |
| 3197 | if (CanTy.isVolatileQualified()) |
| 3198 | VRQuals.addVolatile(); |
| 3199 | if (CanTy.isRestrictQualified()) |
| 3200 | VRQuals.addRestrict(); |
| 3201 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3202 | return VRQuals; |
| 3203 | } |
| 3204 | } |
| 3205 | } |
| 3206 | return VRQuals; |
| 3207 | } |
| 3208 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3209 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3210 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3211 | /// on the operator @p Op and the arguments given. For example, if the |
| 3212 | /// operator is a binary '+', this routine might add "int |
| 3213 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3214 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3215 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3216 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3217 | Expr **Args, unsigned NumArgs, |
| 3218 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3219 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3220 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3221 | // that the first few of these types are the promoted integral |
| 3222 | // types; these types need to be first. |
| 3223 | // FIXME: What about complex? |
| 3224 | const unsigned FirstIntegralType = 0; |
| 3225 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3227 | LastPromotedIntegralType = 13; |
| 3228 | const unsigned FirstPromotedArithmeticType = 7, |
| 3229 | LastPromotedArithmeticType = 16; |
| 3230 | const unsigned NumArithmeticTypes = 16; |
| 3231 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3233 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3234 | Context.SignedCharTy, Context.ShortTy, |
| 3235 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3236 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3237 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3238 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3239 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3240 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3241 | "Invalid first promoted integral type"); |
| 3242 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3243 | == Context.UnsignedLongLongTy && |
| 3244 | "Invalid last promoted integral type"); |
| 3245 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3246 | "Invalid first promoted arithmetic type"); |
| 3247 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3248 | == Context.LongDoubleTy && |
| 3249 | "Invalid last promoted arithmetic type"); |
| 3250 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3251 | // Find all of the types that the arguments can convert to, but only |
| 3252 | // if the operator we're looking at has built-in operator candidates |
| 3253 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3254 | Qualifiers VisibleTypeConversionsQuals; |
| 3255 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3256 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3257 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3258 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3259 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3260 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3261 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3262 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3263 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3264 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3265 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3266 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3267 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3268 | OpLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3269 | true, |
| 3270 | (Op == OO_Exclaim || |
| 3271 | Op == OO_AmpAmp || |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3272 | Op == OO_PipePipe), |
| 3273 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | bool isComparison = false; |
| 3277 | switch (Op) { |
| 3278 | case OO_None: |
| 3279 | case NUM_OVERLOADED_OPERATORS: |
| 3280 | assert(false && "Expected an overloaded operator"); |
| 3281 | break; |
| 3282 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3283 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3285 | goto UnaryStar; |
| 3286 | else |
| 3287 | goto BinaryStar; |
| 3288 | break; |
| 3289 | |
| 3290 | case OO_Plus: // '+' is either unary or binary |
| 3291 | if (NumArgs == 1) |
| 3292 | goto UnaryPlus; |
| 3293 | else |
| 3294 | goto BinaryPlus; |
| 3295 | break; |
| 3296 | |
| 3297 | case OO_Minus: // '-' is either unary or binary |
| 3298 | if (NumArgs == 1) |
| 3299 | goto UnaryMinus; |
| 3300 | else |
| 3301 | goto BinaryMinus; |
| 3302 | break; |
| 3303 | |
| 3304 | case OO_Amp: // '&' is either unary or binary |
| 3305 | if (NumArgs == 1) |
| 3306 | goto UnaryAmp; |
| 3307 | else |
| 3308 | goto BinaryAmp; |
| 3309 | |
| 3310 | case OO_PlusPlus: |
| 3311 | case OO_MinusMinus: |
| 3312 | // C++ [over.built]p3: |
| 3313 | // |
| 3314 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3315 | // is either volatile or empty, there exist candidate operator |
| 3316 | // functions of the form |
| 3317 | // |
| 3318 | // VQ T& operator++(VQ T&); |
| 3319 | // T operator++(VQ T&, int); |
| 3320 | // |
| 3321 | // C++ [over.built]p4: |
| 3322 | // |
| 3323 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3324 | // than bool, and VQ is either volatile or empty, there exist |
| 3325 | // candidate operator functions of the form |
| 3326 | // |
| 3327 | // VQ T& operator--(VQ T&); |
| 3328 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3330 | Arith < NumArithmeticTypes; ++Arith) { |
| 3331 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3333 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3334 | |
| 3335 | // Non-volatile version. |
| 3336 | if (NumArgs == 1) |
| 3337 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3338 | else |
| 3339 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3340 | // heuristic to reduce number of builtin candidates in the set. |
| 3341 | // Add volatile version only if there are conversions to a volatile type. |
| 3342 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3343 | // Volatile version |
| 3344 | ParamTypes[0] |
| 3345 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3346 | if (NumArgs == 1) |
| 3347 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3348 | else |
| 3349 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3350 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | // C++ [over.built]p5: |
| 3354 | // |
| 3355 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3356 | // cv-unqualified object type, and VQ is either volatile or |
| 3357 | // empty, there exist candidate operator functions of the form |
| 3358 | // |
| 3359 | // T*VQ& operator++(T*VQ&); |
| 3360 | // T*VQ& operator--(T*VQ&); |
| 3361 | // T* operator++(T*VQ&, int); |
| 3362 | // T* operator--(T*VQ&, int); |
| 3363 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3364 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3365 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3366 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3367 | continue; |
| 3368 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | QualType ParamTypes[2] = { |
| 3370 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3371 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3373 | // Without volatile |
| 3374 | if (NumArgs == 1) |
| 3375 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3376 | else |
| 3377 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3378 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3379 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3380 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3381 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3382 | ParamTypes[0] |
| 3383 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3384 | if (NumArgs == 1) |
| 3385 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3386 | else |
| 3387 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3388 | } |
| 3389 | } |
| 3390 | break; |
| 3391 | |
| 3392 | UnaryStar: |
| 3393 | // C++ [over.built]p6: |
| 3394 | // For every cv-qualified or cv-unqualified object type T, there |
| 3395 | // exist candidate operator functions of the form |
| 3396 | // |
| 3397 | // T& operator*(T*); |
| 3398 | // |
| 3399 | // C++ [over.built]p7: |
| 3400 | // For every function type T, there exist candidate operator |
| 3401 | // functions of the form |
| 3402 | // T& operator*(T*); |
| 3403 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3404 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3405 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3406 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3407 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3408 | &ParamTy, Args, 1, CandidateSet); |
| 3409 | } |
| 3410 | break; |
| 3411 | |
| 3412 | UnaryPlus: |
| 3413 | // C++ [over.built]p8: |
| 3414 | // For every type T, there exist candidate operator functions of |
| 3415 | // the form |
| 3416 | // |
| 3417 | // T* operator+(T*); |
| 3418 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3419 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3420 | QualType ParamTy = *Ptr; |
| 3421 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3424 | // Fall through |
| 3425 | |
| 3426 | UnaryMinus: |
| 3427 | // C++ [over.built]p9: |
| 3428 | // For every promoted arithmetic type T, there exist candidate |
| 3429 | // operator functions of the form |
| 3430 | // |
| 3431 | // T operator+(T); |
| 3432 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3434 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3435 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3436 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3437 | } |
| 3438 | break; |
| 3439 | |
| 3440 | case OO_Tilde: |
| 3441 | // C++ [over.built]p10: |
| 3442 | // For every promoted integral type T, there exist candidate |
| 3443 | // operator functions of the form |
| 3444 | // |
| 3445 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3447 | Int < LastPromotedIntegralType; ++Int) { |
| 3448 | QualType IntTy = ArithmeticTypes[Int]; |
| 3449 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3450 | } |
| 3451 | break; |
| 3452 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3453 | case OO_New: |
| 3454 | case OO_Delete: |
| 3455 | case OO_Array_New: |
| 3456 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3457 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3458 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3459 | break; |
| 3460 | |
| 3461 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3462 | UnaryAmp: |
| 3463 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3464 | // C++ [over.match.oper]p3: |
| 3465 | // -- For the operator ',', the unary operator '&', or the |
| 3466 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3467 | break; |
| 3468 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3469 | case OO_EqualEqual: |
| 3470 | case OO_ExclaimEqual: |
| 3471 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | // For every pointer to member type T, there exist candidate operator |
| 3473 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3474 | // |
| 3475 | // bool operator==(T,T); |
| 3476 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3478 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3479 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3480 | MemPtr != MemPtrEnd; |
| 3481 | ++MemPtr) { |
| 3482 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3483 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3486 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3488 | case OO_Less: |
| 3489 | case OO_Greater: |
| 3490 | case OO_LessEqual: |
| 3491 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3492 | // C++ [over.built]p15: |
| 3493 | // |
| 3494 | // For every pointer or enumeration type T, there exist |
| 3495 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3497 | // bool operator<(T, T); |
| 3498 | // bool operator>(T, T); |
| 3499 | // bool operator<=(T, T); |
| 3500 | // bool operator>=(T, T); |
| 3501 | // bool operator==(T, T); |
| 3502 | // bool operator!=(T, T); |
| 3503 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3504 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3505 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3506 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3507 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3509 | = CandidateTypes.enumeration_begin(); |
| 3510 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3511 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3512 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3513 | } |
| 3514 | |
| 3515 | // Fall through. |
| 3516 | isComparison = true; |
| 3517 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3518 | BinaryPlus: |
| 3519 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3520 | if (!isComparison) { |
| 3521 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3522 | |
| 3523 | // C++ [over.built]p13: |
| 3524 | // |
| 3525 | // For every cv-qualified or cv-unqualified object type T |
| 3526 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3528 | // T* operator+(T*, ptrdiff_t); |
| 3529 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3530 | // T* operator-(T*, ptrdiff_t); |
| 3531 | // T* operator+(ptrdiff_t, T*); |
| 3532 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3533 | // |
| 3534 | // C++ [over.built]p14: |
| 3535 | // |
| 3536 | // For every T, where T is a pointer to object type, there |
| 3537 | // exist candidate operator functions of the form |
| 3538 | // |
| 3539 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3541 | = CandidateTypes.pointer_begin(); |
| 3542 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3543 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3544 | |
| 3545 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3546 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3547 | |
| 3548 | if (Op == OO_Plus) { |
| 3549 | // T* operator+(ptrdiff_t, T*); |
| 3550 | ParamTypes[0] = ParamTypes[1]; |
| 3551 | ParamTypes[1] = *Ptr; |
| 3552 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3553 | } else { |
| 3554 | // ptrdiff_t operator-(T, T); |
| 3555 | ParamTypes[1] = *Ptr; |
| 3556 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3557 | Args, 2, CandidateSet); |
| 3558 | } |
| 3559 | } |
| 3560 | } |
| 3561 | // Fall through |
| 3562 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3563 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3564 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3565 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3566 | // C++ [over.built]p12: |
| 3567 | // |
| 3568 | // For every pair of promoted arithmetic types L and R, there |
| 3569 | // exist candidate operator functions of the form |
| 3570 | // |
| 3571 | // LR operator*(L, R); |
| 3572 | // LR operator/(L, R); |
| 3573 | // LR operator+(L, R); |
| 3574 | // LR operator-(L, R); |
| 3575 | // bool operator<(L, R); |
| 3576 | // bool operator>(L, R); |
| 3577 | // bool operator<=(L, R); |
| 3578 | // bool operator>=(L, R); |
| 3579 | // bool operator==(L, R); |
| 3580 | // bool operator!=(L, R); |
| 3581 | // |
| 3582 | // where LR is the result of the usual arithmetic conversions |
| 3583 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3584 | // |
| 3585 | // C++ [over.built]p24: |
| 3586 | // |
| 3587 | // For every pair of promoted arithmetic types L and R, there exist |
| 3588 | // candidate operator functions of the form |
| 3589 | // |
| 3590 | // LR operator?(bool, L, R); |
| 3591 | // |
| 3592 | // where LR is the result of the usual arithmetic conversions |
| 3593 | // between types L and R. |
| 3594 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3596 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3597 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3598 | Right < LastPromotedArithmeticType; ++Right) { |
| 3599 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3600 | QualType Result |
| 3601 | = isComparison |
| 3602 | ? Context.BoolTy |
| 3603 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3604 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3605 | } |
| 3606 | } |
| 3607 | break; |
| 3608 | |
| 3609 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3610 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3611 | case OO_Caret: |
| 3612 | case OO_Pipe: |
| 3613 | case OO_LessLess: |
| 3614 | case OO_GreaterGreater: |
| 3615 | // C++ [over.built]p17: |
| 3616 | // |
| 3617 | // For every pair of promoted integral types L and R, there |
| 3618 | // exist candidate operator functions of the form |
| 3619 | // |
| 3620 | // LR operator%(L, R); |
| 3621 | // LR operator&(L, R); |
| 3622 | // LR operator^(L, R); |
| 3623 | // LR operator|(L, R); |
| 3624 | // L operator<<(L, R); |
| 3625 | // L operator>>(L, R); |
| 3626 | // |
| 3627 | // where LR is the result of the usual arithmetic conversions |
| 3628 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3629 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3630 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3631 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3632 | Right < LastPromotedIntegralType; ++Right) { |
| 3633 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3634 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3635 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3636 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3637 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3638 | } |
| 3639 | } |
| 3640 | break; |
| 3641 | |
| 3642 | case OO_Equal: |
| 3643 | // C++ [over.built]p20: |
| 3644 | // |
| 3645 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3646 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3647 | // empty, there exist candidate operator functions of the form |
| 3648 | // |
| 3649 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3650 | for (BuiltinCandidateTypeSet::iterator |
| 3651 | Enum = CandidateTypes.enumeration_begin(), |
| 3652 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3653 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3655 | CandidateSet); |
| 3656 | for (BuiltinCandidateTypeSet::iterator |
| 3657 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3658 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3659 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3661 | CandidateSet); |
| 3662 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3663 | |
| 3664 | case OO_PlusEqual: |
| 3665 | case OO_MinusEqual: |
| 3666 | // C++ [over.built]p19: |
| 3667 | // |
| 3668 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3669 | // volatile or empty, there exist candidate operator functions |
| 3670 | // of the form |
| 3671 | // |
| 3672 | // T*VQ& operator=(T*VQ&, T*); |
| 3673 | // |
| 3674 | // C++ [over.built]p21: |
| 3675 | // |
| 3676 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3677 | // cv-unqualified object type and VQ is either volatile or |
| 3678 | // empty, there exist candidate operator functions of the form |
| 3679 | // |
| 3680 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3681 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3682 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3683 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3684 | QualType ParamTypes[2]; |
| 3685 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3686 | |
| 3687 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3688 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3689 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3690 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3691 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3692 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3693 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3694 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3695 | ParamTypes[0] |
| 3696 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3697 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3698 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3699 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3700 | } |
| 3701 | // Fall through. |
| 3702 | |
| 3703 | case OO_StarEqual: |
| 3704 | case OO_SlashEqual: |
| 3705 | // C++ [over.built]p18: |
| 3706 | // |
| 3707 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3708 | // VQ is either volatile or empty, and R is a promoted |
| 3709 | // arithmetic type, there exist candidate operator functions of |
| 3710 | // the form |
| 3711 | // |
| 3712 | // VQ L& operator=(VQ L&, R); |
| 3713 | // VQ L& operator*=(VQ L&, R); |
| 3714 | // VQ L& operator/=(VQ L&, R); |
| 3715 | // VQ L& operator+=(VQ L&, R); |
| 3716 | // VQ L& operator-=(VQ L&, R); |
| 3717 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3718 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3719 | Right < LastPromotedArithmeticType; ++Right) { |
| 3720 | QualType ParamTypes[2]; |
| 3721 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3722 | |
| 3723 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3724 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3725 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3726 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3727 | |
| 3728 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3729 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3730 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3731 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3732 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3733 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3734 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3735 | } |
| 3736 | } |
| 3737 | break; |
| 3738 | |
| 3739 | case OO_PercentEqual: |
| 3740 | case OO_LessLessEqual: |
| 3741 | case OO_GreaterGreaterEqual: |
| 3742 | case OO_AmpEqual: |
| 3743 | case OO_CaretEqual: |
| 3744 | case OO_PipeEqual: |
| 3745 | // C++ [over.built]p22: |
| 3746 | // |
| 3747 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3748 | // is either volatile or empty, and R is a promoted integral |
| 3749 | // type, there exist candidate operator functions of the form |
| 3750 | // |
| 3751 | // VQ L& operator%=(VQ L&, R); |
| 3752 | // VQ L& operator<<=(VQ L&, R); |
| 3753 | // VQ L& operator>>=(VQ L&, R); |
| 3754 | // VQ L& operator&=(VQ L&, R); |
| 3755 | // VQ L& operator^=(VQ L&, R); |
| 3756 | // VQ L& operator|=(VQ L&, R); |
| 3757 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3759 | Right < LastPromotedIntegralType; ++Right) { |
| 3760 | QualType ParamTypes[2]; |
| 3761 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3762 | |
| 3763 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3764 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3765 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 3766 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3767 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3768 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3769 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 3770 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3771 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3772 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3773 | } |
| 3774 | } |
| 3775 | break; |
| 3776 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3777 | case OO_Exclaim: { |
| 3778 | // C++ [over.operator]p23: |
| 3779 | // |
| 3780 | // There also exist candidate operator functions of the form |
| 3781 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3783 | // bool operator&&(bool, bool); [BELOW] |
| 3784 | // bool operator||(bool, bool); [BELOW] |
| 3785 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3786 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3787 | /*IsAssignmentOperator=*/false, |
| 3788 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3789 | break; |
| 3790 | } |
| 3791 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3792 | case OO_AmpAmp: |
| 3793 | case OO_PipePipe: { |
| 3794 | // C++ [over.operator]p23: |
| 3795 | // |
| 3796 | // There also exist candidate operator functions of the form |
| 3797 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3798 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3799 | // bool operator&&(bool, bool); |
| 3800 | // bool operator||(bool, bool); |
| 3801 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3802 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3803 | /*IsAssignmentOperator=*/false, |
| 3804 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3805 | break; |
| 3806 | } |
| 3807 | |
| 3808 | case OO_Subscript: |
| 3809 | // C++ [over.built]p13: |
| 3810 | // |
| 3811 | // For every cv-qualified or cv-unqualified object type T there |
| 3812 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3814 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3815 | // T& operator[](T*, ptrdiff_t); |
| 3816 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3817 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3818 | // T& operator[](ptrdiff_t, T*); |
| 3819 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3820 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3821 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3822 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3823 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3824 | |
| 3825 | // T& operator[](T*, ptrdiff_t) |
| 3826 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3827 | |
| 3828 | // T& operator[](ptrdiff_t, T*); |
| 3829 | ParamTypes[0] = ParamTypes[1]; |
| 3830 | ParamTypes[1] = *Ptr; |
| 3831 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3832 | } |
| 3833 | break; |
| 3834 | |
| 3835 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3836 | // C++ [over.built]p11: |
| 3837 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 3838 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 3839 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 3840 | // there exist candidate operator functions of the form |
| 3841 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 3842 | // where CV12 is the union of CV1 and CV2. |
| 3843 | { |
| 3844 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3845 | CandidateTypes.pointer_begin(); |
| 3846 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3847 | QualType C1Ty = (*Ptr); |
| 3848 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3849 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3850 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3851 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3852 | if (!isa<RecordType>(C1)) |
| 3853 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3854 | // heuristic to reduce number of builtin candidates in the set. |
| 3855 | // Add volatile/restrict version only if there are conversions to a |
| 3856 | // volatile/restrict type. |
| 3857 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 3858 | continue; |
| 3859 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 3860 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3861 | } |
| 3862 | for (BuiltinCandidateTypeSet::iterator |
| 3863 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3864 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3865 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 3866 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 3867 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 3868 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3869 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 3870 | break; |
| 3871 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 3872 | // build CV12 T& |
| 3873 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3874 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 3875 | T.isVolatileQualified()) |
| 3876 | continue; |
| 3877 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 3878 | T.isRestrictQualified()) |
| 3879 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3880 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3881 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 3882 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3883 | } |
| 3884 | } |
| 3885 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3886 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3887 | |
| 3888 | case OO_Conditional: |
| 3889 | // Note that we don't consider the first argument, since it has been |
| 3890 | // contextually converted to bool long ago. The candidates below are |
| 3891 | // therefore added as binary. |
| 3892 | // |
| 3893 | // C++ [over.built]p24: |
| 3894 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3895 | // there exist candidate operator functions of the form |
| 3896 | // |
| 3897 | // T operator?(bool, T, T); |
| 3898 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3899 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3900 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3901 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3902 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3903 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3904 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3905 | CandidateTypes.member_pointer_begin(), |
| 3906 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3907 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3908 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3909 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3910 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3911 | } |
| 3912 | } |
| 3913 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3914 | /// \brief Add function candidates found via argument-dependent lookup |
| 3915 | /// to the set of overloading candidates. |
| 3916 | /// |
| 3917 | /// This routine performs argument-dependent name lookup based on the |
| 3918 | /// given function name (which may also be an operator name) and adds |
| 3919 | /// all of the overload candidates found by ADL to the overload |
| 3920 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3921 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3922 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3923 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3924 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3925 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3926 | unsigned NumExplicitTemplateArgs, |
| 3927 | OverloadCandidateSet& CandidateSet, |
| 3928 | bool PartialOverloading) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3929 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3930 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3931 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 3932 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3933 | // Record all of the function candidates that we've already |
| 3934 | // added to the overload set, so that we don't add those same |
| 3935 | // candidates a second time. |
| 3936 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3937 | CandEnd = CandidateSet.end(); |
| 3938 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3939 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3940 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3941 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3942 | Functions.insert(FunTmpl); |
| 3943 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3945 | // FIXME: Pass in the explicit template arguments? |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 3946 | ArgumentDependentLookup(Name, /*Operator*/false, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3947 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3948 | // Erase all of the candidates we already knew about. |
| 3949 | // FIXME: This is suboptimal. Is there a better way? |
| 3950 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3951 | CandEnd = CandidateSet.end(); |
| 3952 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3953 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3954 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3955 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3956 | Functions.erase(FunTmpl); |
| 3957 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3958 | |
| 3959 | // For each of the ADL candidates we found, add it to the overload |
| 3960 | // set. |
| 3961 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3962 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3963 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3964 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
| 3965 | if (HasExplicitTemplateArgs) |
| 3966 | continue; |
| 3967 | |
| 3968 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 3969 | false, false, PartialOverloading); |
| 3970 | } else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3971 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3972 | HasExplicitTemplateArgs, |
| 3973 | ExplicitTemplateArgs, |
| 3974 | NumExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3975 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3976 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3979 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3980 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3982 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3984 | // Define viable functions to be better candidates than non-viable |
| 3985 | // functions. |
| 3986 | if (!Cand2.Viable) |
| 3987 | return Cand1.Viable; |
| 3988 | else if (!Cand1.Viable) |
| 3989 | return false; |
| 3990 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3991 | // C++ [over.match.best]p1: |
| 3992 | // |
| 3993 | // -- if F is a static member function, ICS1(F) is defined such |
| 3994 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3995 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3996 | // better nor worse than ICS1(F). |
| 3997 | unsigned StartArg = 0; |
| 3998 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3999 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4000 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4001 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4002 | // A viable function F1 is defined to be a better function than another |
| 4003 | // 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] | 4004 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4005 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4006 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4007 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4008 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4009 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4010 | Cand2.Conversions[ArgIdx])) { |
| 4011 | case ImplicitConversionSequence::Better: |
| 4012 | // Cand1 has a better conversion sequence. |
| 4013 | HasBetterConversion = true; |
| 4014 | break; |
| 4015 | |
| 4016 | case ImplicitConversionSequence::Worse: |
| 4017 | // Cand1 can't be better than Cand2. |
| 4018 | return false; |
| 4019 | |
| 4020 | case ImplicitConversionSequence::Indistinguishable: |
| 4021 | // Do nothing. |
| 4022 | break; |
| 4023 | } |
| 4024 | } |
| 4025 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4026 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4027 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4028 | if (HasBetterConversion) |
| 4029 | return true; |
| 4030 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4032 | // specialization, or, if not that, |
| 4033 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4034 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4035 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4036 | |
| 4037 | // -- F1 and F2 are function template specializations, and the function |
| 4038 | // template for F1 is more specialized than the template for F2 |
| 4039 | // 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] | 4040 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4041 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4042 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4043 | if (FunctionTemplateDecl *BetterTemplate |
| 4044 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4045 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4046 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4047 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4048 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4049 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4050 | // -- the context is an initialization by user-defined conversion |
| 4051 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4052 | // from the return type of F1 to the destination type (i.e., |
| 4053 | // the type of the entity being initialized) is a better |
| 4054 | // conversion sequence than the standard conversion sequence |
| 4055 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | if (Cand1.Function && Cand2.Function && |
| 4057 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4058 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4059 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4060 | Cand2.FinalConversion)) { |
| 4061 | case ImplicitConversionSequence::Better: |
| 4062 | // Cand1 has a better conversion sequence. |
| 4063 | return true; |
| 4064 | |
| 4065 | case ImplicitConversionSequence::Worse: |
| 4066 | // Cand1 can't be better than Cand2. |
| 4067 | return false; |
| 4068 | |
| 4069 | case ImplicitConversionSequence::Indistinguishable: |
| 4070 | // Do nothing |
| 4071 | break; |
| 4072 | } |
| 4073 | } |
| 4074 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4075 | return false; |
| 4076 | } |
| 4077 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4079 | /// within an overload candidate set. |
| 4080 | /// |
| 4081 | /// \param CandidateSet the set of candidate functions. |
| 4082 | /// |
| 4083 | /// \param Loc the location of the function name (or operator symbol) for |
| 4084 | /// which overload resolution occurs. |
| 4085 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4086 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4087 | /// function, Best points to the candidate function found. |
| 4088 | /// |
| 4089 | /// \returns The result of overload resolution. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | Sema::OverloadingResult |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4091 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4092 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4094 | // Find the best viable function. |
| 4095 | Best = CandidateSet.end(); |
| 4096 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4097 | Cand != CandidateSet.end(); ++Cand) { |
| 4098 | if (Cand->Viable) { |
| 4099 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 4100 | Best = Cand; |
| 4101 | } |
| 4102 | } |
| 4103 | |
| 4104 | // If we didn't find any viable functions, abort. |
| 4105 | if (Best == CandidateSet.end()) |
| 4106 | return OR_No_Viable_Function; |
| 4107 | |
| 4108 | // Make sure that this function is better than every other viable |
| 4109 | // function. If not, we have an ambiguity. |
| 4110 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4111 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4112 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4113 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4114 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 4115 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4116 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4117 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4120 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4121 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4123 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4124 | return OR_Deleted; |
| 4125 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4126 | // C++ [basic.def.odr]p2: |
| 4127 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4129 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4130 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4131 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4132 | if (Best->Function) |
| 4133 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4134 | return OR_Success; |
| 4135 | } |
| 4136 | |
| 4137 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4138 | /// diagnostic messages containing the candidates in the candidate |
| 4139 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4141 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4142 | bool OnlyViable, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4143 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4144 | SourceLocation OpLoc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4145 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4146 | LastCand = CandidateSet.end(); |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4147 | bool Reported = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4148 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4149 | if (Cand->Viable || !OnlyViable) { |
| 4150 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4151 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4152 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4153 | // Deleted or "unavailable" function. |
| 4154 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 4155 | << Cand->Function->isDeleted(); |
Douglas Gregor | 1fdd89b | 2009-09-15 20:11:42 +0000 | [diff] [blame] | 4156 | } else if (FunctionTemplateDecl *FunTmpl |
| 4157 | = Cand->Function->getPrimaryTemplate()) { |
| 4158 | // Function template specialization |
| 4159 | // FIXME: Give a better reason! |
| 4160 | Diag(Cand->Function->getLocation(), diag::err_ovl_template_candidate) |
| 4161 | << getTemplateArgumentBindingsText(FunTmpl->getTemplateParameters(), |
| 4162 | *Cand->Function->getTemplateSpecializationArgs()); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4163 | } else { |
| 4164 | // Normal function |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 4165 | bool errReported = false; |
| 4166 | if (!Cand->Viable && Cand->Conversions.size() > 0) { |
| 4167 | for (int i = Cand->Conversions.size()-1; i >= 0; i--) { |
| 4168 | const ImplicitConversionSequence &Conversion = |
| 4169 | Cand->Conversions[i]; |
| 4170 | if ((Conversion.ConversionKind != |
| 4171 | ImplicitConversionSequence::BadConversion) || |
| 4172 | Conversion.ConversionFunctionSet.size() == 0) |
| 4173 | continue; |
| 4174 | Diag(Cand->Function->getLocation(), |
| 4175 | diag::err_ovl_candidate_not_viable) << (i+1); |
| 4176 | errReported = true; |
| 4177 | for (int j = Conversion.ConversionFunctionSet.size()-1; |
| 4178 | j >= 0; j--) { |
| 4179 | FunctionDecl *Func = Conversion.ConversionFunctionSet[j]; |
| 4180 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 4181 | } |
| 4182 | } |
| 4183 | } |
| 4184 | if (!errReported) |
| 4185 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4186 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4187 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4188 | // Desugar the type of the surrogate down to a function type, |
| 4189 | // retaining as many typedefs as possible while still showing |
| 4190 | // the function type (and, therefore, its parameter types). |
| 4191 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4192 | bool isLValueReference = false; |
| 4193 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4194 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4195 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4196 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4197 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4198 | isLValueReference = true; |
| 4199 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4200 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4201 | FnType = FnTypeRef->getPointeeType(); |
| 4202 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4203 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4204 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4205 | FnType = FnTypePtr->getPointeeType(); |
| 4206 | isPointer = true; |
| 4207 | } |
| 4208 | // Desugar down to a function type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4209 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4210 | // Reconstruct the pointer/reference as appropriate. |
| 4211 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4212 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 4213 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4214 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4215 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4216 | << FnType; |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4217 | } else if (OnlyViable) { |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4218 | assert(Cand->Conversions.size() <= 2 && |
Fariborz Jahanian | ad3607d | 2009-10-09 17:09:58 +0000 | [diff] [blame] | 4219 | "builtin-binary-operator-not-binary"); |
Fariborz Jahanian | 866b274 | 2009-10-16 23:25:02 +0000 | [diff] [blame] | 4220 | std::string TypeStr("operator"); |
| 4221 | TypeStr += Opc; |
| 4222 | TypeStr += "("; |
| 4223 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4224 | if (Cand->Conversions.size() == 1) { |
| 4225 | TypeStr += ")"; |
| 4226 | Diag(OpLoc, diag::err_ovl_builtin_unary_candidate) << TypeStr; |
| 4227 | } |
| 4228 | else { |
| 4229 | TypeStr += ", "; |
| 4230 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4231 | TypeStr += ")"; |
| 4232 | Diag(OpLoc, diag::err_ovl_builtin_binary_candidate) << TypeStr; |
| 4233 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4234 | } |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4235 | else if (!Cand->Viable && !Reported) { |
| 4236 | // Non-viability might be due to ambiguous user-defined conversions, |
| 4237 | // needed for built-in operators. Report them as well, but only once |
| 4238 | // as we have typically many built-in candidates. |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4239 | unsigned NoOperands = Cand->Conversions.size(); |
| 4240 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
Fariborz Jahanian | 27687cf | 2009-10-12 17:51:19 +0000 | [diff] [blame] | 4241 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
| 4242 | if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion || |
| 4243 | ICS.ConversionFunctionSet.empty()) |
| 4244 | continue; |
| 4245 | if (CXXConversionDecl *Func = dyn_cast<CXXConversionDecl>( |
| 4246 | Cand->Conversions[ArgIdx].ConversionFunctionSet[0])) { |
| 4247 | QualType FromTy = |
| 4248 | QualType( |
| 4249 | static_cast<Type*>(ICS.UserDefined.Before.FromTypePtr),0); |
| 4250 | Diag(OpLoc,diag::note_ambiguous_type_conversion) |
| 4251 | << FromTy << Func->getConversionType(); |
| 4252 | } |
| 4253 | for (unsigned j = 0; j < ICS.ConversionFunctionSet.size(); j++) { |
| 4254 | FunctionDecl *Func = |
| 4255 | Cand->Conversions[ArgIdx].ConversionFunctionSet[j]; |
| 4256 | Diag(Func->getLocation(),diag::err_ovl_candidate); |
| 4257 | } |
| 4258 | } |
| 4259 | Reported = true; |
| 4260 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4261 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4262 | } |
| 4263 | } |
| 4264 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4265 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4266 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4267 | /// expression with overloaded function type and @p ToType is the type |
| 4268 | /// we're trying to resolve to. For example: |
| 4269 | /// |
| 4270 | /// @code |
| 4271 | /// int f(double); |
| 4272 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4274 | /// int (*pfd)(double) = f; // selects f(double) |
| 4275 | /// @endcode |
| 4276 | /// |
| 4277 | /// This routine returns the resulting FunctionDecl if it could be |
| 4278 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4279 | /// routine will emit diagnostics if there is an error. |
| 4280 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4281 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4282 | bool Complain) { |
| 4283 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4284 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4285 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4286 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4287 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4288 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4289 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4290 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4291 | FunctionType = MemTypePtr->getPointeeType(); |
| 4292 | IsMember = true; |
| 4293 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4294 | |
| 4295 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4296 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4297 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4298 | return 0; |
| 4299 | |
| 4300 | // Find the actual overloaded function declaration. |
| 4301 | OverloadedFunctionDecl *Ovl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4303 | // C++ [over.over]p1: |
| 4304 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4305 | // overloaded function name is ignored (5.1). ] |
| 4306 | Expr *OvlExpr = From->IgnoreParens(); |
| 4307 | |
| 4308 | // C++ [over.over]p1: |
| 4309 | // [...] The overloaded function name can be preceded by the & |
| 4310 | // operator. |
| 4311 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4312 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4313 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4314 | } |
| 4315 | |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4316 | bool HasExplicitTemplateArgs = false; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4317 | const TemplateArgumentLoc *ExplicitTemplateArgs = 0; |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4318 | unsigned NumExplicitTemplateArgs = 0; |
| 4319 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4320 | // Try to dig out the overloaded function. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4321 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 4322 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4323 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4324 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4325 | HasExplicitTemplateArgs = DR->hasExplicitTemplateArgumentList(); |
| 4326 | ExplicitTemplateArgs = DR->getTemplateArgs(); |
| 4327 | NumExplicitTemplateArgs = DR->getNumTemplateArgs(); |
Anders Carlsson | 6e8f550 | 2009-10-07 22:26:29 +0000 | [diff] [blame] | 4328 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(OvlExpr)) { |
| 4329 | Ovl = dyn_cast<OverloadedFunctionDecl>(ME->getMemberDecl()); |
| 4330 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(ME->getMemberDecl()); |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4331 | HasExplicitTemplateArgs = ME->hasExplicitTemplateArgumentList(); |
| 4332 | ExplicitTemplateArgs = ME->getTemplateArgs(); |
| 4333 | NumExplicitTemplateArgs = ME->getNumTemplateArgs(); |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4334 | } else if (TemplateIdRefExpr *TIRE = dyn_cast<TemplateIdRefExpr>(OvlExpr)) { |
| 4335 | TemplateName Name = TIRE->getTemplateName(); |
| 4336 | Ovl = Name.getAsOverloadedFunctionDecl(); |
| 4337 | FunctionTemplate = |
| 4338 | dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 4339 | |
| 4340 | HasExplicitTemplateArgs = true; |
| 4341 | ExplicitTemplateArgs = TIRE->getTemplateArgs(); |
| 4342 | NumExplicitTemplateArgs = TIRE->getNumTemplateArgs(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4343 | } |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4344 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4345 | // If there's no overloaded function declaration or function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4346 | // we're done. |
| 4347 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4348 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4350 | OverloadIterator Fun; |
| 4351 | if (Ovl) |
| 4352 | Fun = Ovl; |
| 4353 | else |
| 4354 | Fun = FunctionTemplate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4356 | // Look through all of the overloaded functions, searching for one |
| 4357 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4358 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4359 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4360 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4361 | // C++ [over.over]p3: |
| 4362 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4363 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4364 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4365 | // type "pointer-to-member-function." |
| 4366 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4367 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4368 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4369 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4370 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4371 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4373 | // static when converting to member pointer. |
| 4374 | if (Method->isStatic() == IsMember) |
| 4375 | continue; |
| 4376 | } else if (IsMember) |
| 4377 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4378 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4379 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4380 | // If the name is a function template, template argument deduction is |
| 4381 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4382 | // resulting template argument list is used to generate a single |
| 4383 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4384 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4385 | // 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] | 4386 | FunctionDecl *Specialization = 0; |
| 4387 | TemplateDeductionInfo Info(Context); |
| 4388 | if (TemplateDeductionResult Result |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4389 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 4390 | ExplicitTemplateArgs, |
| 4391 | NumExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4392 | FunctionType, Specialization, Info)) { |
| 4393 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4394 | (void)Result; |
| 4395 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4396 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4397 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4398 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4399 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4400 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4401 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4402 | } |
| 4403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4404 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4405 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 4406 | // Skip non-static functions when converting to pointer, and static |
| 4407 | // when converting to member pointer. |
| 4408 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4409 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4410 | |
| 4411 | // If we have explicit template arguments, skip non-templates. |
| 4412 | if (HasExplicitTemplateArgs) |
| 4413 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4414 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4415 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4416 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4417 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4418 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4419 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4420 | FoundNonTemplateFunction = true; |
| 4421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4425 | // If there were 0 or 1 matches, we're done. |
| 4426 | if (Matches.empty()) |
| 4427 | return 0; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4428 | else if (Matches.size() == 1) { |
| 4429 | FunctionDecl *Result = *Matches.begin(); |
| 4430 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4431 | return Result; |
| 4432 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4433 | |
| 4434 | // C++ [over.over]p4: |
| 4435 | // If more than one function is selected, [...] |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4436 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4437 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4438 | // [...] and any given function template specialization F1 is |
| 4439 | // eliminated if the set contains a second function template |
| 4440 | // specialization whose function template is more specialized |
| 4441 | // than the function template of F1 according to the partial |
| 4442 | // ordering rules of 14.5.5.2. |
| 4443 | |
| 4444 | // The algorithm specified above is quadratic. We instead use a |
| 4445 | // two-pass algorithm (similar to the one used to identify the |
| 4446 | // best viable function in an overload set) that identifies the |
| 4447 | // best function template (if it exists). |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4448 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4449 | Matches.end()); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4450 | FunctionDecl *Result = |
| 4451 | getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4452 | TPOC_Other, From->getLocStart(), |
| 4453 | PDiag(), |
| 4454 | PDiag(diag::err_addr_ovl_ambiguous) |
| 4455 | << TemplateMatches[0]->getDeclName(), |
| 4456 | PDiag(diag::err_ovl_template_candidate)); |
| 4457 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4458 | return Result; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4460 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4461 | // [...] any function template specializations in the set are |
| 4462 | // eliminated if the set also contains a non-template function, [...] |
| 4463 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4464 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4465 | if ((*M)->getPrimaryTemplate() == 0) |
| 4466 | RemainingMatches.push_back(*M); |
| 4467 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4469 | // selected function. |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4470 | if (RemainingMatches.size() == 1) { |
| 4471 | FunctionDecl *Result = RemainingMatches.front(); |
| 4472 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4473 | return Result; |
| 4474 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4476 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4477 | // returns (even if we issue the diagnostics here). |
| 4478 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4479 | << RemainingMatches[0]->getDeclName(); |
| 4480 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 4481 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4482 | return 0; |
| 4483 | } |
| 4484 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4485 | /// \brief Add a single candidate to the overload set. |
| 4486 | static void AddOverloadedCallCandidate(Sema &S, |
| 4487 | AnyFunctionDecl Callee, |
| 4488 | bool &ArgumentDependentLookup, |
| 4489 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4490 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4491 | unsigned NumExplicitTemplateArgs, |
| 4492 | Expr **Args, unsigned NumArgs, |
| 4493 | OverloadCandidateSet &CandidateSet, |
| 4494 | bool PartialOverloading) { |
| 4495 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
| 4496 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
| 4497 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 4498 | PartialOverloading); |
| 4499 | |
| 4500 | if (Func->getDeclContext()->isRecord() || |
| 4501 | Func->getDeclContext()->isFunctionOrMethod()) |
| 4502 | ArgumentDependentLookup = false; |
| 4503 | return; |
| 4504 | } |
| 4505 | |
| 4506 | FunctionTemplateDecl *FuncTemplate = cast<FunctionTemplateDecl>(Callee); |
| 4507 | S.AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 4508 | ExplicitTemplateArgs, |
| 4509 | NumExplicitTemplateArgs, |
| 4510 | Args, NumArgs, CandidateSet); |
| 4511 | |
| 4512 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 4513 | ArgumentDependentLookup = false; |
| 4514 | } |
| 4515 | |
| 4516 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 4517 | /// dependent lookup to the given overload set. |
| 4518 | void Sema::AddOverloadedCallCandidates(NamedDecl *Callee, |
| 4519 | DeclarationName &UnqualifiedName, |
| 4520 | bool &ArgumentDependentLookup, |
| 4521 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4522 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4523 | unsigned NumExplicitTemplateArgs, |
| 4524 | Expr **Args, unsigned NumArgs, |
| 4525 | OverloadCandidateSet &CandidateSet, |
| 4526 | bool PartialOverloading) { |
| 4527 | // Add the functions denoted by Callee to the set of candidate |
| 4528 | // functions. While we're doing so, track whether argument-dependent |
| 4529 | // lookup still applies, per: |
| 4530 | // |
| 4531 | // C++0x [basic.lookup.argdep]p3: |
| 4532 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4533 | // and let Y be the lookup set produced by argument dependent |
| 4534 | // lookup (defined as follows). If X contains |
| 4535 | // |
| 4536 | // -- a declaration of a class member, or |
| 4537 | // |
| 4538 | // -- a block-scope function declaration that is not a |
| 4539 | // using-declaration (FIXME: check for using declaration), or |
| 4540 | // |
| 4541 | // -- a declaration that is neither a function or a function |
| 4542 | // template |
| 4543 | // |
| 4544 | // then Y is empty. |
| 4545 | if (!Callee) { |
| 4546 | // Nothing to do. |
| 4547 | } else if (OverloadedFunctionDecl *Ovl |
| 4548 | = dyn_cast<OverloadedFunctionDecl>(Callee)) { |
| 4549 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4550 | FuncEnd = Ovl->function_end(); |
| 4551 | Func != FuncEnd; ++Func) |
| 4552 | AddOverloadedCallCandidate(*this, *Func, ArgumentDependentLookup, |
| 4553 | HasExplicitTemplateArgs, |
| 4554 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4555 | Args, NumArgs, CandidateSet, |
| 4556 | PartialOverloading); |
| 4557 | } else if (isa<FunctionDecl>(Callee) || isa<FunctionTemplateDecl>(Callee)) |
| 4558 | AddOverloadedCallCandidate(*this, |
| 4559 | AnyFunctionDecl::getFromNamedDecl(Callee), |
| 4560 | ArgumentDependentLookup, |
| 4561 | HasExplicitTemplateArgs, |
| 4562 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4563 | Args, NumArgs, CandidateSet, |
| 4564 | PartialOverloading); |
| 4565 | // FIXME: assert isa<FunctionDecl> || isa<FunctionTemplateDecl> rather than |
| 4566 | // checking dynamically. |
| 4567 | |
| 4568 | if (Callee) |
| 4569 | UnqualifiedName = Callee->getDeclName(); |
| 4570 | |
| 4571 | if (ArgumentDependentLookup) |
| 4572 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
| 4573 | HasExplicitTemplateArgs, |
| 4574 | ExplicitTemplateArgs, |
| 4575 | NumExplicitTemplateArgs, |
| 4576 | CandidateSet, |
| 4577 | PartialOverloading); |
| 4578 | } |
| 4579 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4580 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4581 | /// (which eventually refers to the declaration Func) and the call |
| 4582 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4583 | /// to a specific function. If overload resolution succeeds, returns |
| 4584 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4585 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4586 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4587 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4588 | DeclarationName UnqualifiedName, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4589 | bool HasExplicitTemplateArgs, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4590 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4591 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4592 | SourceLocation LParenLoc, |
| 4593 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4594 | SourceLocation *CommaLocs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4595 | SourceLocation RParenLoc, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4596 | bool &ArgumentDependentLookup) { |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4597 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4598 | |
| 4599 | // Add the functions denoted by Callee to the set of candidate |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4600 | // functions. |
| 4601 | AddOverloadedCallCandidates(Callee, UnqualifiedName, ArgumentDependentLookup, |
| 4602 | HasExplicitTemplateArgs, ExplicitTemplateArgs, |
| 4603 | NumExplicitTemplateArgs, Args, NumArgs, |
| 4604 | CandidateSet); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4605 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4606 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4607 | case OR_Success: |
| 4608 | return Best->Function; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4609 | |
| 4610 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4611 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4612 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4613 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4614 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4615 | break; |
| 4616 | |
| 4617 | case OR_Ambiguous: |
| 4618 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4619 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4620 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4621 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4622 | |
| 4623 | case OR_Deleted: |
| 4624 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4625 | << Best->Function->isDeleted() |
| 4626 | << UnqualifiedName |
| 4627 | << Fn->getSourceRange(); |
| 4628 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4629 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4633 | // return NULL. |
| 4634 | Fn->Destroy(Context); |
| 4635 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4636 | Args[Arg]->Destroy(Context); |
| 4637 | return 0; |
| 4638 | } |
| 4639 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4640 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4641 | /// operator. |
| 4642 | /// |
| 4643 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4644 | /// |
| 4645 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4646 | /// operator. |
| 4647 | /// |
| 4648 | /// \param Functions The set of non-member functions that will be |
| 4649 | /// considered by overload resolution. The caller needs to build this |
| 4650 | /// set based on the context using, e.g., |
| 4651 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4652 | /// set should not contain any member functions; those will be added |
| 4653 | /// by CreateOverloadedUnaryOp(). |
| 4654 | /// |
| 4655 | /// \param input The input argument. |
| 4656 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4657 | unsigned OpcIn, |
| 4658 | FunctionSet &Functions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4660 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4661 | Expr *Input = (Expr *)input.get(); |
| 4662 | |
| 4663 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 4664 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 4665 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4666 | |
| 4667 | Expr *Args[2] = { Input, 0 }; |
| 4668 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4670 | // For post-increment and post-decrement, add the implicit '0' as |
| 4671 | // the second argument, so that we know this is a post-increment or |
| 4672 | // post-decrement. |
| 4673 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 4674 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4676 | SourceLocation()); |
| 4677 | NumArgs = 2; |
| 4678 | } |
| 4679 | |
| 4680 | if (Input->isTypeDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4682 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4684 | FuncEnd = Functions.end(); |
| 4685 | Func != FuncEnd; ++Func) |
| 4686 | Overloads->addOverload(*Func); |
| 4687 | |
| 4688 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4689 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4691 | input.release(); |
| 4692 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4693 | &Args[0], NumArgs, |
| 4694 | Context.DependentTy, |
| 4695 | OpLoc)); |
| 4696 | } |
| 4697 | |
| 4698 | // Build an empty overload set. |
| 4699 | OverloadCandidateSet CandidateSet; |
| 4700 | |
| 4701 | // Add the candidates from the given function set. |
| 4702 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4703 | |
| 4704 | // Add operator candidates that are member functions. |
| 4705 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4706 | |
| 4707 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4708 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4709 | |
| 4710 | // Perform overload resolution. |
| 4711 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4712 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4713 | case OR_Success: { |
| 4714 | // We found a built-in operator or an overloaded operator. |
| 4715 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4717 | if (FnDecl) { |
| 4718 | // We matched an overloaded operator. Build a call to that |
| 4719 | // operator. |
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 | // Convert the arguments. |
| 4722 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4723 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4724 | return ExprError(); |
| 4725 | } else { |
| 4726 | // Convert the arguments. |
| 4727 | if (PerformCopyInitialization(Input, |
| 4728 | FnDecl->getParamDecl(0)->getType(), |
| 4729 | "passing")) |
| 4730 | return ExprError(); |
| 4731 | } |
| 4732 | |
| 4733 | // Determine the result type |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 4734 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4736 | // Build the actual expression node. |
| 4737 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4738 | SourceLocation()); |
| 4739 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4741 | input.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4742 | |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 4743 | ExprOwningPtr<CallExpr> TheCall(this, |
| 4744 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 4745 | &Input, 1, ResultTy, OpLoc)); |
| 4746 | |
| 4747 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 4748 | FnDecl)) |
| 4749 | return ExprError(); |
| 4750 | |
| 4751 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4752 | } else { |
| 4753 | // We matched a built-in operator. Convert the arguments, then |
| 4754 | // break out so that we will build the appropriate built-in |
| 4755 | // operator node. |
| 4756 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4757 | Best->Conversions[0], "passing")) |
| 4758 | return ExprError(); |
| 4759 | |
| 4760 | break; |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | case OR_No_Viable_Function: |
| 4765 | // No viable function; fall through to handling this as a |
| 4766 | // built-in operator, which will produce an error message for us. |
| 4767 | break; |
| 4768 | |
| 4769 | case OR_Ambiguous: |
| 4770 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4771 | << UnaryOperator::getOpcodeStr(Opc) |
| 4772 | << Input->getSourceRange(); |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4773 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 4774 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4775 | return ExprError(); |
| 4776 | |
| 4777 | case OR_Deleted: |
| 4778 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4779 | << Best->Function->isDeleted() |
| 4780 | << UnaryOperator::getOpcodeStr(Opc) |
| 4781 | << Input->getSourceRange(); |
| 4782 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4783 | return ExprError(); |
| 4784 | } |
| 4785 | |
| 4786 | // Either we found no viable overloaded operator or we matched a |
| 4787 | // built-in operator. In either case, fall through to trying to |
| 4788 | // build a built-in operation. |
| 4789 | input.release(); |
| 4790 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4791 | } |
| 4792 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4793 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4794 | /// operator. |
| 4795 | /// |
| 4796 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4797 | /// |
| 4798 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4799 | /// operator. |
| 4800 | /// |
| 4801 | /// \param Functions The set of non-member functions that will be |
| 4802 | /// considered by overload resolution. The caller needs to build this |
| 4803 | /// set based on the context using, e.g., |
| 4804 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4805 | /// set should not contain any member functions; those will be added |
| 4806 | /// by CreateOverloadedBinOp(). |
| 4807 | /// |
| 4808 | /// \param LHS Left-hand argument. |
| 4809 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4810 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4811 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | unsigned OpcIn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4813 | FunctionSet &Functions, |
| 4814 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4815 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4816 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4817 | |
| 4818 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4819 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4820 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4821 | |
| 4822 | // If either side is type-dependent, create an appropriate dependent |
| 4823 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4824 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 4825 | if (Functions.empty()) { |
| 4826 | // If there are no functions to store, just build a dependent |
| 4827 | // BinaryOperator or CompoundAssignment. |
| 4828 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 4829 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 4830 | Context.DependentTy, OpLoc)); |
| 4831 | |
| 4832 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 4833 | Context.DependentTy, |
| 4834 | Context.DependentTy, |
| 4835 | Context.DependentTy, |
| 4836 | OpLoc)); |
| 4837 | } |
| 4838 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4839 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4840 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4841 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4842 | FuncEnd = Functions.end(); |
| 4843 | Func != FuncEnd; ++Func) |
| 4844 | Overloads->addOverload(*Func); |
| 4845 | |
| 4846 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4847 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4849 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4850 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4851 | Context.DependentTy, |
| 4852 | OpLoc)); |
| 4853 | } |
| 4854 | |
| 4855 | // If this is the .* operator, which is not overloadable, just |
| 4856 | // create a built-in binary operator. |
| 4857 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4858 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4859 | |
| 4860 | // If this is one of the assignment operators, we only perform |
| 4861 | // overload resolution if the left-hand side is a class or |
| 4862 | // enumeration type (C++ [expr.ass]p3). |
| 4863 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4864 | !Args[0]->getType()->isOverloadableType()) |
| 4865 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4867 | // Build an empty overload set. |
| 4868 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4869 | |
| 4870 | // Add the candidates from the given function set. |
| 4871 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4872 | |
| 4873 | // Add operator candidates that are member functions. |
| 4874 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4875 | |
| 4876 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4877 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4878 | |
| 4879 | // Perform overload resolution. |
| 4880 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4881 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4882 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4883 | // We found a built-in operator or an overloaded operator. |
| 4884 | FunctionDecl *FnDecl = Best->Function; |
| 4885 | |
| 4886 | if (FnDecl) { |
| 4887 | // We matched an overloaded operator. Build a call to that |
| 4888 | // operator. |
| 4889 | |
| 4890 | // Convert the arguments. |
| 4891 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4892 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 4893 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4894 | "passing")) |
| 4895 | return ExprError(); |
| 4896 | } else { |
| 4897 | // Convert the arguments. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4898 | if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4899 | "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4900 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4901 | "passing")) |
| 4902 | return ExprError(); |
| 4903 | } |
| 4904 | |
| 4905 | // Determine the result type |
| 4906 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4907 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4908 | ResultTy = ResultTy.getNonReferenceType(); |
| 4909 | |
| 4910 | // Build the actual expression node. |
| 4911 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4912 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4913 | UsualUnaryConversions(FnExpr); |
| 4914 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 4915 | ExprOwningPtr<CXXOperatorCallExpr> |
| 4916 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 4917 | Args, 2, ResultTy, |
| 4918 | OpLoc)); |
| 4919 | |
| 4920 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 4921 | FnDecl)) |
| 4922 | return ExprError(); |
| 4923 | |
| 4924 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4925 | } else { |
| 4926 | // We matched a built-in operator. Convert the arguments, then |
| 4927 | // break out so that we will build the appropriate built-in |
| 4928 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4929 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4930 | Best->Conversions[0], "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4931 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4932 | Best->Conversions[1], "passing")) |
| 4933 | return ExprError(); |
| 4934 | |
| 4935 | break; |
| 4936 | } |
| 4937 | } |
| 4938 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4939 | case OR_No_Viable_Function: { |
| 4940 | // C++ [over.match.oper]p9: |
| 4941 | // If the operator is the operator , [...] and there are no |
| 4942 | // viable functions, then the operator is assumed to be the |
| 4943 | // built-in operator and interpreted according to clause 5. |
| 4944 | if (Opc == BinaryOperator::Comma) |
| 4945 | break; |
| 4946 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4947 | // For class as left operand for assignment or compound assigment operator |
| 4948 | // do not fall through to handling in built-in, but report that no overloaded |
| 4949 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4950 | OwningExprResult Result = ExprError(); |
| 4951 | if (Args[0]->getType()->isRecordType() && |
| 4952 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4953 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4954 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4955 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4956 | } else { |
| 4957 | // No viable function; try to create a built-in operation, which will |
| 4958 | // produce an error. Then, show the non-viable candidates. |
| 4959 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4960 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4961 | assert(Result.isInvalid() && |
| 4962 | "C++ binary operator overloading is missing candidates!"); |
| 4963 | if (Result.isInvalid()) |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4964 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false, |
| 4965 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4966 | return move(Result); |
| 4967 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4968 | |
| 4969 | case OR_Ambiguous: |
| 4970 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4971 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4972 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4973 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 4974 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4975 | return ExprError(); |
| 4976 | |
| 4977 | case OR_Deleted: |
| 4978 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4979 | << Best->Function->isDeleted() |
| 4980 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4981 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4982 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4983 | return ExprError(); |
| 4984 | } |
| 4985 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 4986 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4987 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 4990 | Action::OwningExprResult |
| 4991 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 4992 | SourceLocation RLoc, |
| 4993 | ExprArg Base, ExprArg Idx) { |
| 4994 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 4995 | static_cast<Expr*>(Idx.get()) }; |
| 4996 | DeclarationName OpName = |
| 4997 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 4998 | |
| 4999 | // If either side is type-dependent, create an appropriate dependent |
| 5000 | // expression. |
| 5001 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5002 | |
| 5003 | OverloadedFunctionDecl *Overloads |
| 5004 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
| 5005 | |
| 5006 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 5007 | LLoc, false, false); |
| 5008 | |
| 5009 | Base.release(); |
| 5010 | Idx.release(); |
| 5011 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5012 | Args, 2, |
| 5013 | Context.DependentTy, |
| 5014 | RLoc)); |
| 5015 | } |
| 5016 | |
| 5017 | // Build an empty overload set. |
| 5018 | OverloadCandidateSet CandidateSet; |
| 5019 | |
| 5020 | // Subscript can only be overloaded as a member function. |
| 5021 | |
| 5022 | // Add operator candidates that are member functions. |
| 5023 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5024 | |
| 5025 | // Add builtin operator candidates. |
| 5026 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5027 | |
| 5028 | // Perform overload resolution. |
| 5029 | OverloadCandidateSet::iterator Best; |
| 5030 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5031 | case OR_Success: { |
| 5032 | // We found a built-in operator or an overloaded operator. |
| 5033 | FunctionDecl *FnDecl = Best->Function; |
| 5034 | |
| 5035 | if (FnDecl) { |
| 5036 | // We matched an overloaded operator. Build a call to that |
| 5037 | // operator. |
| 5038 | |
| 5039 | // Convert the arguments. |
| 5040 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5041 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 5042 | PerformCopyInitialization(Args[1], |
| 5043 | FnDecl->getParamDecl(0)->getType(), |
| 5044 | "passing")) |
| 5045 | return ExprError(); |
| 5046 | |
| 5047 | // Determine the result type |
| 5048 | QualType ResultTy |
| 5049 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5050 | ResultTy = ResultTy.getNonReferenceType(); |
| 5051 | |
| 5052 | // Build the actual expression node. |
| 5053 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5054 | LLoc); |
| 5055 | UsualUnaryConversions(FnExpr); |
| 5056 | |
| 5057 | Base.release(); |
| 5058 | Idx.release(); |
| 5059 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5060 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5061 | FnExpr, Args, 2, |
| 5062 | ResultTy, RLoc)); |
| 5063 | |
| 5064 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5065 | FnDecl)) |
| 5066 | return ExprError(); |
| 5067 | |
| 5068 | return MaybeBindToTemporary(TheCall.release()); |
| 5069 | } else { |
| 5070 | // We matched a built-in operator. Convert the arguments, then |
| 5071 | // break out so that we will build the appropriate built-in |
| 5072 | // operator node. |
| 5073 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
| 5074 | Best->Conversions[0], "passing") || |
| 5075 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
| 5076 | Best->Conversions[1], "passing")) |
| 5077 | return ExprError(); |
| 5078 | |
| 5079 | break; |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | case OR_No_Viable_Function: { |
| 5084 | // No viable function; try to create a built-in operation, which will |
| 5085 | // produce an error. Then, show the non-viable candidates. |
| 5086 | OwningExprResult Result = |
| 5087 | CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 5088 | assert(Result.isInvalid() && |
| 5089 | "C++ subscript operator overloading is missing candidates!"); |
| 5090 | if (Result.isInvalid()) |
| 5091 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false, |
| 5092 | "[]", LLoc); |
| 5093 | return move(Result); |
| 5094 | } |
| 5095 | |
| 5096 | case OR_Ambiguous: |
| 5097 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5098 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5099 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true, |
| 5100 | "[]", LLoc); |
| 5101 | return ExprError(); |
| 5102 | |
| 5103 | case OR_Deleted: |
| 5104 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5105 | << Best->Function->isDeleted() << "[]" |
| 5106 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5107 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5108 | return ExprError(); |
| 5109 | } |
| 5110 | |
| 5111 | // We matched a built-in operator; build it. |
| 5112 | Base.release(); |
| 5113 | Idx.release(); |
| 5114 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 5115 | Owned(Args[1]), RLoc); |
| 5116 | } |
| 5117 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5118 | /// BuildCallToMemberFunction - Build a call to a member |
| 5119 | /// function. MemExpr is the expression that refers to the member |
| 5120 | /// function (and includes the object parameter), Args/NumArgs are the |
| 5121 | /// arguments to the function call (not including the object |
| 5122 | /// parameter). The caller needs to validate that the member |
| 5123 | /// expression refers to a member function or an overloaded member |
| 5124 | /// function. |
| 5125 | Sema::ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5126 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 5127 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5128 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 5129 | SourceLocation RParenLoc) { |
| 5130 | // Dig out the member expression. This holds both the object |
| 5131 | // argument and the member function we're referring to. |
| 5132 | MemberExpr *MemExpr = 0; |
| 5133 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 5134 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 5135 | else |
| 5136 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 5137 | assert(MemExpr && "Building member call without member expression"); |
| 5138 | |
| 5139 | // Extract the object argument. |
| 5140 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 5141 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5142 | CXXMethodDecl *Method = 0; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5143 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 5144 | isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5145 | // Add overload candidates |
| 5146 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5147 | DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5148 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5149 | for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd; |
| 5150 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5151 | if ((Method = dyn_cast<CXXMethodDecl>(*Func))) { |
| 5152 | // If explicit template arguments were provided, we can't call a |
| 5153 | // non-template member function. |
| 5154 | if (MemExpr->hasExplicitTemplateArgumentList()) |
| 5155 | continue; |
| 5156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5157 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5158 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5159 | } else |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 5160 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func), |
| 5161 | MemExpr->hasExplicitTemplateArgumentList(), |
| 5162 | MemExpr->getTemplateArgs(), |
| 5163 | MemExpr->getNumTemplateArgs(), |
| 5164 | ObjectArg, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5165 | CandidateSet, |
| 5166 | /*SuppressUsedConversions=*/false); |
| 5167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5168 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5169 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5170 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5171 | case OR_Success: |
| 5172 | Method = cast<CXXMethodDecl>(Best->Function); |
| 5173 | break; |
| 5174 | |
| 5175 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5176 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5177 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5178 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5179 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5180 | // FIXME: Leaking incoming expressions! |
| 5181 | return true; |
| 5182 | |
| 5183 | case OR_Ambiguous: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5184 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5185 | diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5186 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5187 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5188 | // FIXME: Leaking incoming expressions! |
| 5189 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5190 | |
| 5191 | case OR_Deleted: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5192 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5193 | diag::err_ovl_deleted_member_call) |
| 5194 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5195 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5196 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 5197 | // FIXME: Leaking incoming expressions! |
| 5198 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | FixOverloadedFunctionReference(MemExpr, Method); |
| 5202 | } else { |
| 5203 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 5204 | } |
| 5205 | |
| 5206 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5207 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 5208 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5209 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5210 | Method->getResultType().getNonReferenceType(), |
| 5211 | RParenLoc)); |
| 5212 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5213 | // Check for a valid return type. |
| 5214 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 5215 | TheCall.get(), Method)) |
| 5216 | return true; |
| 5217 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5218 | // Convert the object argument (for a non-static member function call). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5219 | if (!Method->isStatic() && |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5220 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 5221 | return true; |
| 5222 | MemExpr->setBase(ObjectArg); |
| 5223 | |
| 5224 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5225 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5226 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5227 | RParenLoc)) |
| 5228 | return true; |
| 5229 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5230 | if (CheckFunctionCall(Method, TheCall.get())) |
| 5231 | return true; |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 5232 | |
| 5233 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5236 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 5237 | /// type (C++ [over.call.object]), which can end up invoking an |
| 5238 | /// overloaded function call operator (@c operator()) or performing a |
| 5239 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5240 | Sema::ExprResult |
| 5241 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 5242 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5243 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5244 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5245 | SourceLocation RParenLoc) { |
| 5246 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5247 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5248 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5249 | // C++ [over.call.object]p1: |
| 5250 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5251 | // 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] | 5252 | // candidate functions includes at least the function call |
| 5253 | // operators of T. The function call operators of T are obtained by |
| 5254 | // ordinary lookup of the name operator() in the context of |
| 5255 | // (E).operator(). |
| 5256 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5257 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5258 | |
| 5259 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 5260 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 5261 | << Object->getSourceRange())) |
| 5262 | return true; |
| 5263 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 5264 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 5265 | LookupQualifiedName(R, Record->getDecl()); |
| 5266 | R.suppressDiagnostics(); |
| 5267 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5268 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5269 | Oper != OperEnd; ++Oper) { |
| 5270 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(*Oper)) { |
| 5271 | AddMethodTemplateCandidate(FunTmpl, false, 0, 0, Object, Args, NumArgs, |
| 5272 | CandidateSet, |
| 5273 | /*SuppressUserConversions=*/false); |
| 5274 | continue; |
| 5275 | } |
| 5276 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5278 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5279 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5280 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5281 | // C++ [over.call.object]p2: |
| 5282 | // In addition, for each conversion function declared in T of the |
| 5283 | // form |
| 5284 | // |
| 5285 | // operator conversion-type-id () cv-qualifier; |
| 5286 | // |
| 5287 | // where cv-qualifier is the same cv-qualification as, or a |
| 5288 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 5289 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 5290 | // R", or the type "reference to pointer to function of |
| 5291 | // (P1,...,Pn) returning R", or the type "reference to function |
| 5292 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5293 | // is also considered as a candidate function. Similarly, |
| 5294 | // surrogate call functions are added to the set of candidate |
| 5295 | // functions for each conversion function declared in an |
| 5296 | // accessible base class provided the function is not hidden |
| 5297 | // within T by another intervening declaration. |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5298 | // FIXME: Look in base classes for more conversion operators! |
| 5299 | OverloadedFunctionDecl *Conversions |
| 5300 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
| 5301 | for (OverloadedFunctionDecl::function_iterator |
| 5302 | Func = Conversions->function_begin(), |
| 5303 | FuncEnd = Conversions->function_end(); |
| 5304 | Func != FuncEnd; ++Func) { |
| 5305 | CXXConversionDecl *Conv; |
| 5306 | FunctionTemplateDecl *ConvTemplate; |
| 5307 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5308 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5309 | // Skip over templated conversion functions; they aren't |
| 5310 | // surrogates. |
| 5311 | if (ConvTemplate) |
| 5312 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5313 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5314 | // Strip the reference type (if any) and then the pointer type (if |
| 5315 | // any) to get down to what might be a function type. |
| 5316 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 5317 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5318 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5319 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5320 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
| 5321 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5324 | // Perform overload resolution. |
| 5325 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5326 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5327 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5328 | // Overload resolution succeeded; we'll build the appropriate call |
| 5329 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5330 | break; |
| 5331 | |
| 5332 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5333 | Diag(Object->getSourceRange().getBegin(), |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 5334 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5335 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 5336 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5337 | break; |
| 5338 | |
| 5339 | case OR_Ambiguous: |
| 5340 | Diag(Object->getSourceRange().getBegin(), |
| 5341 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5342 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5343 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5344 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5345 | |
| 5346 | case OR_Deleted: |
| 5347 | Diag(Object->getSourceRange().getBegin(), |
| 5348 | diag::err_ovl_deleted_object_call) |
| 5349 | << Best->Function->isDeleted() |
| 5350 | << Object->getType() << Object->getSourceRange(); |
| 5351 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 5352 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5353 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5354 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5355 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5356 | // We had an error; delete all of the subexpressions and return |
| 5357 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5358 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5359 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5360 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5361 | return true; |
| 5362 | } |
| 5363 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5364 | if (Best->Function == 0) { |
| 5365 | // Since there is no function declaration, this is one of the |
| 5366 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5367 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5368 | = cast<CXXConversionDecl>( |
| 5369 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 5370 | |
| 5371 | // We selected one of the surrogate functions that converts the |
| 5372 | // object parameter to a function pointer. Perform the conversion |
| 5373 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5374 | |
| 5375 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5376 | // and then call it. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5377 | CXXMemberCallExpr *CE = |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5378 | BuildCXXMemberCallExpr(Object, Conv); |
| 5379 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5380 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5381 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 5382 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 5386 | // that calls this method, using Object for the implicit object |
| 5387 | // parameter and passing along the remaining arguments. |
| 5388 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5389 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5390 | |
| 5391 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5392 | unsigned NumArgsToCheck = NumArgs; |
| 5393 | |
| 5394 | // Build the full argument list for the method call (the |
| 5395 | // implicit object parameter is placed at the beginning of the |
| 5396 | // list). |
| 5397 | Expr **MethodArgs; |
| 5398 | if (NumArgs < NumArgsInProto) { |
| 5399 | NumArgsToCheck = NumArgsInProto; |
| 5400 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 5401 | } else { |
| 5402 | MethodArgs = new Expr*[NumArgs + 1]; |
| 5403 | } |
| 5404 | MethodArgs[0] = Object; |
| 5405 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5406 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5407 | |
| 5408 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5409 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5410 | UsualUnaryConversions(NewFn); |
| 5411 | |
| 5412 | // Once we've built TheCall, all of the expressions are properly |
| 5413 | // owned. |
| 5414 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5415 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5416 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5417 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5418 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5419 | delete [] MethodArgs; |
| 5420 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 5421 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 5422 | Method)) |
| 5423 | return true; |
| 5424 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5425 | // We may have default arguments. If so, we need to allocate more |
| 5426 | // slots in the call for them. |
| 5427 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5428 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5429 | else if (NumArgs > NumArgsInProto) |
| 5430 | NumArgsToCheck = NumArgsInProto; |
| 5431 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5432 | bool IsError = false; |
| 5433 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5434 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5435 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5436 | TheCall->setArg(0, Object); |
| 5437 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5438 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5439 | // Check the argument types. |
| 5440 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5441 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5442 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5443 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5444 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5445 | // Pass the argument. |
| 5446 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5447 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5448 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 5449 | OwningExprResult DefArg |
| 5450 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 5451 | if (DefArg.isInvalid()) { |
| 5452 | IsError = true; |
| 5453 | break; |
| 5454 | } |
| 5455 | |
| 5456 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5457 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5458 | |
| 5459 | TheCall->setArg(i + 1, Arg); |
| 5460 | } |
| 5461 | |
| 5462 | // If this is a variadic call, handle args passed through "...". |
| 5463 | if (Proto->isVariadic()) { |
| 5464 | // Promote the arguments (C99 6.5.2.2p7). |
| 5465 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 5466 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5467 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5468 | TheCall->setArg(i + 1, Arg); |
| 5469 | } |
| 5470 | } |
| 5471 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5472 | if (IsError) return true; |
| 5473 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5474 | if (CheckFunctionCall(Method, TheCall.get())) |
| 5475 | return true; |
| 5476 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 5477 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5480 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5481 | /// (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] | 5482 | /// @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] | 5483 | Sema::OwningExprResult |
| 5484 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 5485 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5486 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5488 | // C++ [over.ref]p1: |
| 5489 | // |
| 5490 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 5491 | // for a class object x of type T if T::operator->() exists and if |
| 5492 | // the operator is selected as the best match function by the |
| 5493 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5494 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 5495 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5496 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5497 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 5498 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 5499 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 5500 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5501 | |
| 5502 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 5503 | Oper != OperEnd; ++Oper) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5504 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5505 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5506 | |
| 5507 | // Perform overload resolution. |
| 5508 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5509 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5510 | case OR_Success: |
| 5511 | // Overload resolution succeeded; we'll build the call below. |
| 5512 | break; |
| 5513 | |
| 5514 | case OR_No_Viable_Function: |
| 5515 | if (CandidateSet.empty()) |
| 5516 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5517 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5518 | else |
| 5519 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5520 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5521 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5522 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5523 | |
| 5524 | case OR_Ambiguous: |
| 5525 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5526 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5527 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5528 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5529 | |
| 5530 | case OR_Deleted: |
| 5531 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5532 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5533 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5534 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5535 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | // Convert the object parameter. |
| 5539 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5540 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5541 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5542 | |
| 5543 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5544 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5545 | |
| 5546 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5547 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 5548 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5549 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5550 | |
| 5551 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 5552 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5553 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 5554 | &Base, 1, ResultTy, OpLoc)); |
| 5555 | |
| 5556 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 5557 | Method)) |
| 5558 | return ExprError(); |
| 5559 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5562 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 5563 | /// a C++ overloaded function (possibly with some parentheses and |
| 5564 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 5565 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5566 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 5567 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5568 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5569 | Expr *NewExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5570 | PE->setSubExpr(NewExpr); |
| 5571 | PE->setType(NewExpr->getType()); |
| 5572 | } else if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 5573 | Expr *NewExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
| 5574 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
| 5575 | NewExpr->getType()) && |
| 5576 | "Implicit cast type cannot be determined from overload"); |
| 5577 | ICE->setSubExpr(NewExpr); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5578 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5579 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5580 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5581 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5582 | if (Method->isStatic()) { |
| 5583 | // Do nothing: static member functions aren't any different |
| 5584 | // from non-member functions. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5585 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr())) { |
| 5586 | if (DRE->getQualifier()) { |
| 5587 | // We have taken the address of a pointer to member |
| 5588 | // function. Perform the computation here so that we get the |
| 5589 | // appropriate pointer to member type. |
| 5590 | DRE->setDecl(Fn); |
| 5591 | DRE->setType(Fn->getType()); |
| 5592 | QualType ClassType |
| 5593 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 5594 | E->setType(Context.getMemberPointerType(Fn->getType(), |
| 5595 | ClassType.getTypePtr())); |
| 5596 | return E; |
| 5597 | } |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5598 | } |
Douglas Gregor | 423a4e0 | 2009-10-22 18:02:20 +0000 | [diff] [blame] | 5599 | // FIXME: TemplateIdRefExpr referring to a member function template |
| 5600 | // specialization! |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5601 | } |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5602 | Expr *NewExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 5603 | UnOp->setSubExpr(NewExpr); |
| 5604 | UnOp->setType(Context.getPointerType(NewExpr->getType())); |
| 5605 | |
| 5606 | return UnOp; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5607 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5608 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5609 | isa<FunctionTemplateDecl>(DR->getDecl()) || |
| 5610 | isa<FunctionDecl>(DR->getDecl())) && |
| 5611 | "Expected function or function template"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5612 | DR->setDecl(Fn); |
| 5613 | E->setType(Fn->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5614 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 5615 | MemExpr->setMemberDecl(Fn); |
| 5616 | E->setType(Fn->getType()); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5617 | } else if (TemplateIdRefExpr *TID = dyn_cast<TemplateIdRefExpr>(E)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5618 | E = DeclRefExpr::Create(Context, |
| 5619 | TID->getQualifier(), TID->getQualifierRange(), |
| 5620 | Fn, TID->getTemplateNameLoc(), |
| 5621 | true, |
| 5622 | TID->getLAngleLoc(), |
| 5623 | TID->getTemplateArgs(), |
| 5624 | TID->getNumTemplateArgs(), |
| 5625 | TID->getRAngleLoc(), |
| 5626 | Fn->getType(), |
| 5627 | /*FIXME?*/false, /*FIXME?*/false); |
Douglas Gregor | 423a4e0 | 2009-10-22 18:02:20 +0000 | [diff] [blame] | 5628 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5629 | // FIXME: Don't destroy TID here, since we need its template arguments |
| 5630 | // to survive. |
| 5631 | // TID->Destroy(Context); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 5632 | } else if (isa<UnresolvedFunctionNameExpr>(E)) { |
| 5633 | return DeclRefExpr::Create(Context, |
| 5634 | /*Qualifier=*/0, |
| 5635 | /*QualifierRange=*/SourceRange(), |
| 5636 | Fn, E->getLocStart(), |
| 5637 | Fn->getType(), false, false); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5638 | } else { |
| 5639 | assert(false && "Invalid reference to overloaded function"); |
| 5640 | } |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 5641 | |
| 5642 | return E; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5645 | } // end namespace clang |