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" |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include <algorithm> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | /// GetConversionCategory - Retrieve the implicit conversion |
| 32 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 35 | static const ImplicitConversionCategory |
| 36 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 37 | ICC_Identity, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Lvalue_Transformation, |
| 40 | ICC_Lvalue_Transformation, |
| 41 | ICC_Qualification_Adjustment, |
| 42 | ICC_Promotion, |
| 43 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 44 | ICC_Promotion, |
| 45 | ICC_Conversion, |
| 46 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
| 50 | ICC_Conversion, |
| 51 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 52 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 54 | ICC_Conversion |
| 55 | }; |
| 56 | return Category[(int)Kind]; |
| 57 | } |
| 58 | |
| 59 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 60 | /// corresponding to the given implicit conversion kind. |
| 61 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 62 | static const ImplicitConversionRank |
| 63 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 64 | ICR_Exact_Match, |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Promotion, |
| 70 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Conversion, |
| 73 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 79 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 80 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 81 | ICR_Conversion |
| 82 | }; |
| 83 | return Rank[(int)Kind]; |
| 84 | } |
| 85 | |
| 86 | /// GetImplicitConversionName - Return the name of this kind of |
| 87 | /// implicit conversion. |
| 88 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 89 | static const char* Name[(int)ICK_Num_Conversion_Kinds] = { |
| 90 | "No conversion", |
| 91 | "Lvalue-to-rvalue", |
| 92 | "Array-to-pointer", |
| 93 | "Function-to-pointer", |
| 94 | "Qualification", |
| 95 | "Integral promotion", |
| 96 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 97 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | "Integral conversion", |
| 99 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Floating-integral conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 102 | "Complex-real conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 103 | "Pointer conversion", |
| 104 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 106 | "Compatible-types conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 107 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | }; |
| 109 | return Name[Kind]; |
| 110 | } |
| 111 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 112 | /// StandardConversionSequence - Set the standard conversion |
| 113 | /// sequence to the identity conversion. |
| 114 | void StandardConversionSequence::setAsIdentityConversion() { |
| 115 | First = ICK_Identity; |
| 116 | Second = ICK_Identity; |
| 117 | Third = ICK_Identity; |
| 118 | Deprecated = false; |
| 119 | ReferenceBinding = false; |
| 120 | DirectBinding = false; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 121 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 122 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 125 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 126 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 127 | /// implicit conversions. |
| 128 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 129 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 130 | if (GetConversionRank(First) > Rank) |
| 131 | Rank = GetConversionRank(First); |
| 132 | if (GetConversionRank(Second) > Rank) |
| 133 | Rank = GetConversionRank(Second); |
| 134 | if (GetConversionRank(Third) > Rank) |
| 135 | Rank = GetConversionRank(Third); |
| 136 | return Rank; |
| 137 | } |
| 138 | |
| 139 | /// isPointerConversionToBool - Determines whether this conversion is |
| 140 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 144 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 145 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 146 | |
| 147 | // Note that FromType has not necessarily been transformed by the |
| 148 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 149 | // check for their presence as well as checking whether FromType is |
| 150 | // a pointer. |
| 151 | if (ToType->isBooleanType() && |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 152 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 159 | /// isPointerConversionToVoidPointer - Determines whether this |
| 160 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 161 | /// used as part of the ranking of standard conversion sequences (C++ |
| 162 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 166 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 167 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
| 175 | if (Second == ICK_Pointer_Conversion) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
| 185 | bool PrintedSomething = false; |
| 186 | if (First != ICK_Identity) { |
| 187 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 188 | PrintedSomething = true; |
| 189 | } |
| 190 | |
| 191 | if (Second != ICK_Identity) { |
| 192 | if (PrintedSomething) { |
| 193 | fprintf(stderr, " -> "); |
| 194 | } |
| 195 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 196 | |
| 197 | if (CopyConstructor) { |
| 198 | fprintf(stderr, " (by copy constructor)"); |
| 199 | } else if (DirectBinding) { |
| 200 | fprintf(stderr, " (direct reference binding)"); |
| 201 | } else if (ReferenceBinding) { |
| 202 | fprintf(stderr, " (reference binding)"); |
| 203 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | PrintedSomething = true; |
| 205 | } |
| 206 | |
| 207 | if (Third != ICK_Identity) { |
| 208 | if (PrintedSomething) { |
| 209 | fprintf(stderr, " -> "); |
| 210 | } |
| 211 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 212 | PrintedSomething = true; |
| 213 | } |
| 214 | |
| 215 | if (!PrintedSomething) { |
| 216 | fprintf(stderr, "No conversions required"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 221 | /// error. Useful for debugging overloading issues. |
| 222 | void UserDefinedConversionSequence::DebugPrint() const { |
| 223 | if (Before.First || Before.Second || Before.Third) { |
| 224 | Before.DebugPrint(); |
| 225 | fprintf(stderr, " -> "); |
| 226 | } |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 227 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | if (After.First || After.Second || After.Third) { |
| 229 | fprintf(stderr, " -> "); |
| 230 | After.DebugPrint(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 235 | /// error. Useful for debugging overloading issues. |
| 236 | void ImplicitConversionSequence::DebugPrint() const { |
| 237 | switch (ConversionKind) { |
| 238 | case StandardConversion: |
| 239 | fprintf(stderr, "Standard conversion: "); |
| 240 | Standard.DebugPrint(); |
| 241 | break; |
| 242 | case UserDefinedConversion: |
| 243 | fprintf(stderr, "User-defined conversion: "); |
| 244 | UserDefined.DebugPrint(); |
| 245 | break; |
| 246 | case EllipsisConversion: |
| 247 | fprintf(stderr, "Ellipsis conversion"); |
| 248 | break; |
| 249 | case BadConversion: |
| 250 | fprintf(stderr, "Bad conversion"); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | fprintf(stderr, "\n"); |
| 255 | } |
| 256 | |
| 257 | // IsOverload - Determine whether the given New declaration is an |
| 258 | // overload of the Old declaration. This routine returns false if New |
| 259 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 260 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 261 | // function (or overload set). When it does return false and Old is an |
| 262 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | // FunctionDecl that New cannot be overloaded with. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 264 | // |
| 265 | // Example: Given the following input: |
| 266 | // |
| 267 | // void f(int, float); // #1 |
| 268 | // void f(int, int); // #2 |
| 269 | // int f(int, int); // #3 |
| 270 | // |
| 271 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 273 | // |
| 274 | // When we process #2, Old is a FunctionDecl for #1. By comparing the |
| 275 | // parameter types, we see that #1 and #2 are overloaded (since they |
| 276 | // have different signatures), so this routine returns false; |
| 277 | // MatchedDecl is unchanged. |
| 278 | // |
| 279 | // When we process #3, Old is an OverloadedFunctionDecl containing #1 |
| 280 | // and #2. We compare the signatures of #3 to #1 (they're overloaded, |
| 281 | // so we do nothing) and then #3 to #2. Since the signatures of #3 and |
| 282 | // #2 are identical (return types of functions are not part of the |
| 283 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 284 | // point to the FunctionDecl for #2. |
| 285 | bool |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 287 | OverloadedFunctionDecl::function_iterator& MatchedDecl) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 288 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 289 | // Is this new function an overload of every function in the |
| 290 | // overload set? |
| 291 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 292 | FuncEnd = Ovl->function_end(); |
| 293 | for (; Func != FuncEnd; ++Func) { |
| 294 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 295 | MatchedDecl = Func; |
| 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // This function overloads every function in the overload set. |
| 301 | return true; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 302 | } else if (FunctionTemplateDecl *Old = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 303 | return IsOverload(New, Old->getTemplatedDecl(), MatchedDecl); |
| 304 | else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 305 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 307 | |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 308 | // C++ [temp.fct]p2: |
| 309 | // A function template can be overloaded with other function templates |
| 310 | // and with normal (non-template) functions. |
| 311 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 312 | return true; |
| 313 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 314 | // Is the function New an overload of the function Old? |
| 315 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 316 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 317 | |
| 318 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 319 | // determine whether they are overloads. If we find any mismatch |
| 320 | // in the signature, they are overloads. |
| 321 | |
| 322 | // If either of these functions is a K&R-style function (no |
| 323 | // prototype), then we consider them to have matching signatures. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 324 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 325 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 326 | return false; |
| 327 | |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 328 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 329 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 330 | |
| 331 | // The signature of a function includes the types of its |
| 332 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 333 | // of the ellipsis; see C++ DR 357). |
| 334 | if (OldQType != NewQType && |
| 335 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 336 | OldType->isVariadic() != NewType->isVariadic() || |
| 337 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 338 | NewType->arg_type_begin()))) |
| 339 | return true; |
| 340 | |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 341 | // C++ [temp.over.link]p4: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | // The signature of a function template consists of its function |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 343 | // signature, its return type and its template parameter list. The names |
| 344 | // of the template parameters are significant only for establishing the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | // relationship between the template parameters and the rest of the |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 346 | // signature. |
| 347 | // |
| 348 | // We check the return type and template parameter lists for function |
| 349 | // templates first; the remaining checks follow. |
| 350 | if (NewTemplate && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 352 | OldTemplate->getTemplateParameters(), |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 353 | false, false, SourceLocation()) || |
| 354 | OldType->getResultType() != NewType->getResultType())) |
| 355 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 357 | // If the function is a class member, its signature includes the |
| 358 | // cv-qualifiers (if any) on the function itself. |
| 359 | // |
| 360 | // As part of this, also check whether one of the member functions |
| 361 | // is static, in which case they are not overloads (C++ |
| 362 | // 13.1p2). While not part of the definition of the signature, |
| 363 | // this check is important to determine whether these functions |
| 364 | // can be overloaded. |
| 365 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 366 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | if (OldMethod && NewMethod && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 368 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 369 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 370 | return true; |
| 371 | |
| 372 | // The signatures match; this is not an overload. |
| 373 | return false; |
| 374 | } else { |
| 375 | // (C++ 13p1): |
| 376 | // Only function declarations can be overloaded; object and type |
| 377 | // declarations cannot be overloaded. |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 382 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 383 | /// from the given expression (Expr) to the given type (ToType). This |
| 384 | /// function returns an implicit conversion sequence that can be used |
| 385 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 386 | /// |
| 387 | /// void f(float f); |
| 388 | /// void g(int i) { f(i); } |
| 389 | /// |
| 390 | /// this routine would produce an implicit conversion sequence to |
| 391 | /// describe the initialization of f from i, which will be a standard |
| 392 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 393 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 394 | // |
| 395 | /// Note that this routine only determines how the conversion can be |
| 396 | /// performed; it does not actually perform the conversion. As such, |
| 397 | /// it will not produce any diagnostics if no conversion is available, |
| 398 | /// but will instead return an implicit conversion sequence of kind |
| 399 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 400 | /// |
| 401 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 402 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 403 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 404 | /// permitted. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 405 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 406 | /// no matter its actual lvalueness. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 407 | ImplicitConversionSequence |
Anders Carlsson | 2974b5c | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 408 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 409 | bool SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 410 | bool AllowExplicit, bool ForceRValue, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | bool InOverloadResolution) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | ImplicitConversionSequence ICS; |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 413 | OverloadCandidateSet Conversions; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 414 | OverloadingResult UserDefResult = OR_Success; |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 415 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 416 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 417 | else if (getLangOptions().CPlusPlus && |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 418 | (UserDefResult = IsUserDefinedConversion(From, ToType, |
| 419 | ICS.UserDefined, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 420 | Conversions, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 421 | !SuppressUserConversions, AllowExplicit, |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 422 | ForceRValue)) == OR_Success) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 423 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 424 | // C++ [over.ics.user]p4: |
| 425 | // A conversion of an expression of class type to the same class |
| 426 | // type is given Exact Match rank, and a conversion of an |
| 427 | // expression of class type to a base class of that type is |
| 428 | // given Conversion rank, in spite of the fact that a copy |
| 429 | // constructor (i.e., a user-defined conversion function) is |
| 430 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 432 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 434 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 435 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 436 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 437 | // Turn this into a "standard" conversion sequence, so that it |
| 438 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 439 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 440 | ICS.Standard.setAsIdentityConversion(); |
| 441 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 442 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 443 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 444 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 445 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 446 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 447 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 448 | |
| 449 | // C++ [over.best.ics]p4: |
| 450 | // However, when considering the argument of a user-defined |
| 451 | // conversion function that is a candidate by 13.3.1.3 when |
| 452 | // invoked for the copying of the temporary in the second step |
| 453 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 454 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 455 | // ellipsis conversion sequences are allowed. |
| 456 | if (SuppressUserConversions && |
| 457 | ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) |
| 458 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 459 | } else { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 460 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 461 | if (UserDefResult == OR_Ambiguous) { |
| 462 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 463 | Cand != Conversions.end(); ++Cand) |
| 464 | ICS.ConversionFunctionSet.push_back(Cand->Function); |
| 465 | } |
| 466 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 467 | |
| 468 | return ICS; |
| 469 | } |
| 470 | |
| 471 | /// IsStandardConversion - Determines whether there is a standard |
| 472 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 473 | /// expression From to the type ToType. Standard conversion sequences |
| 474 | /// only consider non-class types; for conversions that involve class |
| 475 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 476 | /// contain the standard conversion sequence required to perform this |
| 477 | /// conversion and this routine will return true. Otherwise, this |
| 478 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | bool |
| 480 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 481 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | QualType FromType = From->getType(); |
| 484 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 485 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 486 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 487 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 488 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 489 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 490 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 492 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 494 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 495 | if (getLangOptions().CPlusPlus) |
| 496 | return false; |
| 497 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 501 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 502 | // array-to-pointer conversion, or function-to-pointer conversion |
| 503 | // (C++ 4p1). |
| 504 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 506 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 507 | // converted to an rvalue. |
| 508 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 510 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 511 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 512 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 513 | |
| 514 | // If T is a non-class type, the type of the rvalue is the |
| 515 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 516 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 517 | // just strip the qualifiers because they don't matter. |
| 518 | |
| 519 | // FIXME: Doesn't see through to qualifiers behind a typedef! |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 520 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 521 | } else if (FromType->isArrayType()) { |
| 522 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 523 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 524 | |
| 525 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 526 | // bound of T" can be converted to an rvalue of type "pointer to |
| 527 | // T" (C++ 4.2p1). |
| 528 | FromType = Context.getArrayDecayedType(FromType); |
| 529 | |
| 530 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 531 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 532 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 533 | |
| 534 | // For the purpose of ranking in overload resolution |
| 535 | // (13.3.3.1.1), this conversion is considered an |
| 536 | // array-to-pointer conversion followed by a qualification |
| 537 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 538 | SCS.Second = ICK_Identity; |
| 539 | SCS.Third = ICK_Qualification; |
| 540 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 541 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 542 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 543 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 544 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 545 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 546 | |
| 547 | // An lvalue of function type T can be converted to an rvalue of |
| 548 | // type "pointer to T." The result is a pointer to the |
| 549 | // function. (C++ 4.3p1). |
| 550 | FromType = Context.getPointerType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | } else if (FunctionDecl *Fn |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 552 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 553 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 554 | SCS.First = ICK_Function_To_Pointer; |
| 555 | |
| 556 | // We were able to resolve the address of the overloaded function, |
| 557 | // so we can convert to the type of that function. |
| 558 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 559 | if (ToType->isLValueReferenceType()) |
| 560 | FromType = Context.getLValueReferenceType(FromType); |
| 561 | else if (ToType->isRValueReferenceType()) |
| 562 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 563 | else if (ToType->isMemberPointerType()) { |
| 564 | // Resolve address only succeeds if both sides are member pointers, |
| 565 | // but it doesn't have to be the same class. See DR 247. |
| 566 | // Note that this means that the type of &Derived::fn can be |
| 567 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 568 | // base class, even if it was brought into the derived class via a |
| 569 | // using declaration. The standard isn't clear on this issue at all. |
| 570 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 571 | FromType = Context.getMemberPointerType(FromType, |
| 572 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 573 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 574 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 575 | } else { |
| 576 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 577 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // The second conversion can be an integral promotion, floating |
| 581 | // point promotion, integral conversion, floating point conversion, |
| 582 | // floating-integral conversion, pointer conversion, |
| 583 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 584 | // For overloading in C, this can also be a "compatible-type" |
| 585 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 586 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 587 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 588 | // The unqualified versions of the types are the same: there's no |
| 589 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 590 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 591 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 593 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 594 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 595 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 596 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 597 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 599 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 600 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 601 | SCS.Second = ICK_Complex_Promotion; |
| 602 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 603 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 604 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 605 | // Integral conversions (C++ 4.7). |
| 606 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 607 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 608 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 609 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 610 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 611 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 612 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 613 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 614 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 615 | SCS.Second = ICK_Complex_Conversion; |
| 616 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 617 | } else if ((FromType->isFloatingType() && |
| 618 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 619 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 621 | ToType->isFloatingType())) { |
| 622 | // Floating-integral conversions (C++ 4.9). |
| 623 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 624 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 625 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 626 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 627 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 628 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 629 | SCS.Second = ICK_Complex_Real; |
| 630 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 631 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 632 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 633 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 634 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 635 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 636 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 637 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 638 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 639 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 640 | } else if (ToType->isBooleanType() && |
| 641 | (FromType->isArithmeticType() || |
| 642 | FromType->isEnumeralType() || |
| 643 | FromType->isPointerType() || |
| 644 | FromType->isBlockPointerType() || |
| 645 | FromType->isMemberPointerType() || |
| 646 | FromType->isNullPtrType())) { |
| 647 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 648 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 649 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 651 | Context.typesAreCompatible(ToType, FromType)) { |
| 652 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 653 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 654 | } else { |
| 655 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 656 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 659 | QualType CanonFrom; |
| 660 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 661 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 662 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 663 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 664 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 665 | CanonFrom = Context.getCanonicalType(FromType); |
| 666 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 667 | } else { |
| 668 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 669 | SCS.Third = ICK_Identity; |
| 670 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 672 | // [...] Any difference in top-level cv-qualification is |
| 673 | // subsumed by the initialization itself and does not constitute |
| 674 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 675 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 677 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 678 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 679 | FromType = ToType; |
| 680 | CanonFrom = CanonTo; |
| 681 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | // If we have not converted the argument type to the parameter type, |
| 685 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 686 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 687 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 689 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 690 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 694 | /// expression From (whose potentially-adjusted type is FromType) to |
| 695 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 696 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 698 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 699 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 700 | if (!To) { |
| 701 | return false; |
| 702 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 703 | |
| 704 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 705 | // unsigned short int can be converted to an rvalue of type int if |
| 706 | // int can represent all the values of the source type; otherwise, |
| 707 | // the source rvalue can be converted to an rvalue of type unsigned |
| 708 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 709 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 710 | if (// We can promote any signed, promotable integer type to an int |
| 711 | (FromType->isSignedIntegerType() || |
| 712 | // We can promote any unsigned integer type whose size is |
| 713 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 715 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 716 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 719 | return To->getKind() == BuiltinType::UInt; |
| 720 | } |
| 721 | |
| 722 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 723 | // can be converted to an rvalue of the first of the following types |
| 724 | // that can represent all the values of its underlying type: int, |
| 725 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 726 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 727 | && ToType->isIntegerType()) { |
| 728 | // Determine whether the type we're converting from is signed or |
| 729 | // unsigned. |
| 730 | bool FromIsSigned; |
| 731 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 732 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 733 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 734 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 735 | } else { |
| 736 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 737 | FromIsSigned = true; |
| 738 | } |
| 739 | |
| 740 | // The types we'll try to promote to, in the appropriate |
| 741 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | QualType PromoteTypes[6] = { |
| 743 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 744 | Context.LongTy, Context.UnsignedLongTy , |
| 745 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 746 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 747 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 748 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 749 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 751 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 752 | // We found the type that we can promote to. If this is the |
| 753 | // type we wanted, we have a promotion. Otherwise, no |
| 754 | // promotion. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 755 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 756 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 762 | // rvalue of type int if int can represent all the values of the |
| 763 | // bit-field; otherwise, it can be converted to unsigned int if |
| 764 | // unsigned int can represent all the values of the bit-field. If |
| 765 | // the bit-field is larger yet, no integral promotion applies to |
| 766 | // it. If the bit-field has an enumerated type, it is treated as any |
| 767 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 768 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 769 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 770 | using llvm::APSInt; |
| 771 | if (From) |
| 772 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 773 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 774 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 775 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 776 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 777 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 779 | // Are we promoting to an int from a bitfield that fits in an int? |
| 780 | if (BitWidth < ToSize || |
| 781 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 782 | return To->getKind() == BuiltinType::Int; |
| 783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 785 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 786 | // that fits into an unsigned int? |
| 787 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 788 | return To->getKind() == BuiltinType::UInt; |
| 789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 791 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 792 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 795 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 796 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 797 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 798 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 799 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 800 | |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 805 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 806 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 808 | /// An rvalue of type float can be converted to an rvalue of type |
| 809 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 810 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 811 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 812 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 813 | ToBuiltin->getKind() == BuiltinType::Double) |
| 814 | return true; |
| 815 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 816 | // C99 6.3.1.5p1: |
| 817 | // When a float is promoted to double or long double, or a |
| 818 | // double is promoted to long double [...]. |
| 819 | if (!getLangOptions().CPlusPlus && |
| 820 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 821 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 822 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 823 | return true; |
| 824 | } |
| 825 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 826 | return false; |
| 827 | } |
| 828 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 829 | /// \brief Determine if a conversion is a complex promotion. |
| 830 | /// |
| 831 | /// A complex promotion is defined as a complex -> complex conversion |
| 832 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 833 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 834 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 835 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 836 | if (!FromComplex) |
| 837 | return false; |
| 838 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 839 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 840 | if (!ToComplex) |
| 841 | return false; |
| 842 | |
| 843 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 844 | ToComplex->getElementType()) || |
| 845 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 846 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 849 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 850 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 851 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 852 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 853 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | static QualType |
| 855 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 856 | QualType ToPointee, QualType ToType, |
| 857 | ASTContext &Context) { |
| 858 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 859 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 860 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
| 862 | // Exact qualifier match -> return the pointer type we're converting to. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 863 | if (CanonToPointee.getQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 864 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 865 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 866 | return ToType; |
| 867 | |
| 868 | // Build a pointer to ToPointee. It has the right qualifiers |
| 869 | // already. |
| 870 | return Context.getPointerType(ToPointee); |
| 871 | } |
| 872 | |
| 873 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 874 | return Context.getPointerType( |
| 875 | Context.getQualifiedType(CanonToPointee.getUnqualifiedType(), Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 879 | bool InOverloadResolution, |
| 880 | ASTContext &Context) { |
| 881 | // Handle value-dependent integral null pointer constants correctly. |
| 882 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 883 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 884 | Expr->getType()->isIntegralType()) |
| 885 | return !InOverloadResolution; |
| 886 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 887 | return Expr->isNullPointerConstant(Context, |
| 888 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 889 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 890 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 892 | /// IsPointerConversion - Determines whether the conversion of the |
| 893 | /// expression From, which has the (possibly adjusted) type FromType, |
| 894 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 895 | /// 4.10). If so, returns true and places the converted type (that |
| 896 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 897 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 898 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 899 | /// This routine also supports conversions to and from block pointers |
| 900 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 901 | /// pointers to interfaces. FIXME: Once we've determined the |
| 902 | /// appropriate overloading rules for Objective-C, we may want to |
| 903 | /// split the Objective-C checks into a different routine; however, |
| 904 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 905 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 906 | /// set if the conversion is an allowed Objective-C conversion that |
| 907 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 908 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 909 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 910 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 912 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 913 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 914 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 915 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 917 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 918 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 919 | ConvertedType = ToType; |
| 920 | return true; |
| 921 | } |
| 922 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 923 | // Blocks: Block pointers can be converted to void*. |
| 924 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 925 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 926 | ConvertedType = ToType; |
| 927 | return true; |
| 928 | } |
| 929 | // Blocks: A null pointer constant can be converted to a block |
| 930 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 932 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 933 | ConvertedType = ToType; |
| 934 | return true; |
| 935 | } |
| 936 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 937 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 938 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 940 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 941 | ConvertedType = ToType; |
| 942 | return true; |
| 943 | } |
| 944 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 945 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 946 | if (!ToTypePtr) |
| 947 | return false; |
| 948 | |
| 949 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 950 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 951 | ConvertedType = ToType; |
| 952 | return true; |
| 953 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 955 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 956 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 957 | if (!FromTypePtr) |
| 958 | return false; |
| 959 | |
| 960 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 961 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 962 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 963 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 964 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 965 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 966 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 968 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 969 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 970 | return true; |
| 971 | } |
| 972 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 973 | // When we're overloading in C, we allow a special kind of pointer |
| 974 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 976 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 978 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 980 | return true; |
| 981 | } |
| 982 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 983 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 985 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 986 | // can be converted to an rvalue of type "pointer to cv B," where |
| 987 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 988 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 989 | // necessitates this conversion is ill-formed. The result of the |
| 990 | // conversion is a pointer to the base class sub-object of the |
| 991 | // derived class object. The null pointer value is converted to |
| 992 | // the null pointer value of the destination type. |
| 993 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 994 | // Note that we do not check for ambiguity or inaccessibility |
| 995 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 996 | if (getLangOptions().CPlusPlus && |
| 997 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 998 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1000 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1001 | ToType, Context); |
| 1002 | return true; |
| 1003 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1004 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
| 1007 | |
| 1008 | /// isObjCPointerConversion - Determines whether this is an |
| 1009 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1010 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1012 | QualType& ConvertedType, |
| 1013 | bool &IncompatibleObjC) { |
| 1014 | if (!getLangOptions().ObjC1) |
| 1015 | return false; |
| 1016 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1017 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1018 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1020 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1021 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1022 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1023 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1024 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1025 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1026 | ConvertedType = ToType; |
| 1027 | return true; |
| 1028 | } |
| 1029 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1031 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1033 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1034 | ConvertedType = ToType; |
| 1035 | return true; |
| 1036 | } |
| 1037 | // Objective C++: We're able to convert from a pointer to an |
| 1038 | // interface to a pointer to a different interface. |
| 1039 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1040 | ConvertedType = ToType; |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
| 1044 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1045 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1046 | // interfaces, which is permitted. However, we're going to |
| 1047 | // complain about it. |
| 1048 | IncompatibleObjC = true; |
| 1049 | ConvertedType = FromType; |
| 1050 | return true; |
| 1051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1053 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1054 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1055 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1056 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1057 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1058 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1059 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1060 | return false; |
| 1061 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1062 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1063 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1064 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1065 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1066 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1067 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1068 | return false; |
| 1069 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1070 | // If we have pointers to pointers, recursively check whether this |
| 1071 | // is an Objective-C conversion. |
| 1072 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1073 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1074 | IncompatibleObjC)) { |
| 1075 | // We always complain about this conversion. |
| 1076 | IncompatibleObjC = true; |
| 1077 | ConvertedType = ToType; |
| 1078 | return true; |
| 1079 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1080 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1081 | // differences in the argument and result types are in Objective-C |
| 1082 | // pointer conversions. If so, we permit the conversion (but |
| 1083 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1085 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1086 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1087 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1088 | if (FromFunctionType && ToFunctionType) { |
| 1089 | // If the function types are exactly the same, this isn't an |
| 1090 | // Objective-C pointer conversion. |
| 1091 | if (Context.getCanonicalType(FromPointeeType) |
| 1092 | == Context.getCanonicalType(ToPointeeType)) |
| 1093 | return false; |
| 1094 | |
| 1095 | // Perform the quick checks that will tell us whether these |
| 1096 | // function types are obviously different. |
| 1097 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1098 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1099 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1100 | return false; |
| 1101 | |
| 1102 | bool HasObjCConversion = false; |
| 1103 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1104 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1105 | // Okay, the types match exactly. Nothing to do. |
| 1106 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1107 | ToFunctionType->getResultType(), |
| 1108 | ConvertedType, IncompatibleObjC)) { |
| 1109 | // Okay, we have an Objective-C pointer conversion. |
| 1110 | HasObjCConversion = true; |
| 1111 | } else { |
| 1112 | // Function types are too different. Abort. |
| 1113 | return false; |
| 1114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1116 | // Check argument types. |
| 1117 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1118 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1119 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1120 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1121 | if (Context.getCanonicalType(FromArgType) |
| 1122 | == Context.getCanonicalType(ToArgType)) { |
| 1123 | // Okay, the types match exactly. Nothing to do. |
| 1124 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1125 | ConvertedType, IncompatibleObjC)) { |
| 1126 | // Okay, we have an Objective-C pointer conversion. |
| 1127 | HasObjCConversion = true; |
| 1128 | } else { |
| 1129 | // Argument types are too different. Abort. |
| 1130 | return false; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | if (HasObjCConversion) { |
| 1135 | // We had an Objective-C conversion. Allow this pointer |
| 1136 | // conversion, but complain about it. |
| 1137 | ConvertedType = ToType; |
| 1138 | IncompatibleObjC = true; |
| 1139 | return true; |
| 1140 | } |
| 1141 | } |
| 1142 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1143 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1146 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1147 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1148 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1149 | /// conversions for which IsPointerConversion has already returned |
| 1150 | /// true. It returns true and produces a diagnostic if there was an |
| 1151 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1152 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
| 1153 | CastExpr::CastKind &Kind) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1154 | QualType FromType = From->getType(); |
| 1155 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1156 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1157 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1158 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1159 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1161 | if (FromPointeeType->isRecordType() && |
| 1162 | ToPointeeType->isRecordType()) { |
| 1163 | // We must have a derived-to-base conversion. Check an |
| 1164 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1165 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1166 | From->getExprLoc(), |
| 1167 | From->getSourceRange())) |
| 1168 | return true; |
| 1169 | |
| 1170 | // The conversion was successful. |
| 1171 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1172 | } |
| 1173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1175 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1177 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1178 | // Objective-C++ conversions are always okay. |
| 1179 | // FIXME: We should have a different class of conversions for the |
| 1180 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1181 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1182 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1183 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1184 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1185 | return false; |
| 1186 | } |
| 1187 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1188 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1189 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1190 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1191 | /// If so, returns true and places the converted type (that might differ from |
| 1192 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1193 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1194 | QualType ToType, |
| 1195 | bool InOverloadResolution, |
| 1196 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1197 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1198 | if (!ToTypePtr) |
| 1199 | return false; |
| 1200 | |
| 1201 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1202 | if (From->isNullPointerConstant(Context, |
| 1203 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1204 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1205 | ConvertedType = ToType; |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1210 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1211 | if (!FromTypePtr) |
| 1212 | return false; |
| 1213 | |
| 1214 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1215 | // where D is derived from B (C++ 4.11p2). |
| 1216 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1217 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1218 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1219 | |
| 1220 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1221 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1222 | ToClass.getTypePtr()); |
| 1223 | return true; |
| 1224 | } |
| 1225 | |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
| 1229 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1230 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1231 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1232 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1233 | /// true and produces a diagnostic if there was an error, or returns false |
| 1234 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1236 | CastExpr::CastKind &Kind) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1237 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1238 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1239 | if (!FromPtrType) { |
| 1240 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1241 | assert(From->isNullPointerConstant(Context, |
| 1242 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1243 | "Expr must be null pointer constant!"); |
| 1244 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1245 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1246 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1247 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1248 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1249 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1250 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1251 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1252 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1253 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1254 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1255 | // FIXME: What about dependent types? |
| 1256 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1257 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1258 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1259 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1260 | /*DetectVirtual=*/true); |
| 1261 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1262 | assert(DerivationOkay && |
| 1263 | "Should not have been called if derivation isn't OK."); |
| 1264 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1265 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1266 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1267 | getUnqualifiedType())) { |
| 1268 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1269 | Paths.clear(); |
| 1270 | Paths.setRecordingPaths(true); |
| 1271 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1272 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1273 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1274 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1275 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1276 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1277 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1278 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1279 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1280 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1281 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1282 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1283 | << FromClass << ToClass << QualType(VBase, 0) |
| 1284 | << From->getSourceRange(); |
| 1285 | return true; |
| 1286 | } |
| 1287 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1288 | // Must be a base to derived member conversion. |
| 1289 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1290 | return false; |
| 1291 | } |
| 1292 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1293 | /// IsQualificationConversion - Determines whether the conversion from |
| 1294 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1295 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | bool |
| 1297 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1298 | FromType = Context.getCanonicalType(FromType); |
| 1299 | ToType = Context.getCanonicalType(ToType); |
| 1300 | |
| 1301 | // If FromType and ToType are the same type, this is not a |
| 1302 | // qualification conversion. |
| 1303 | if (FromType == ToType) |
| 1304 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1306 | // (C++ 4.4p4): |
| 1307 | // A conversion can add cv-qualifiers at levels other than the first |
| 1308 | // in multi-level pointers, subject to the following rules: [...] |
| 1309 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1310 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1311 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1312 | // Within each iteration of the loop, we check the qualifiers to |
| 1313 | // determine if this still looks like a qualification |
| 1314 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1315 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1316 | // until there are no more pointers or pointers-to-members left to |
| 1317 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1318 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1319 | |
| 1320 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1321 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1322 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1323 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1325 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1326 | // every cv for 0 < k < j. |
| 1327 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1328 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1329 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1331 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1332 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1334 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1335 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1336 | |
| 1337 | // We are left with FromType and ToType being the pointee types |
| 1338 | // after unwrapping the original FromType and ToType the same number |
| 1339 | // of types. If we unwrapped any pointers, and if FromType and |
| 1340 | // ToType have the same unqualified type (since we checked |
| 1341 | // qualifiers above), then this is a qualification conversion. |
| 1342 | return UnwrappedAnyPointer && |
| 1343 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1344 | } |
| 1345 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1346 | /// \brief Given a function template or function, extract the function template |
| 1347 | /// declaration (if any) and the underlying function declaration. |
| 1348 | template<typename T> |
| 1349 | static void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function, |
| 1350 | FunctionTemplateDecl *&FunctionTemplate) { |
| 1351 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig); |
| 1352 | if (FunctionTemplate) |
| 1353 | Function = cast<T>(FunctionTemplate->getTemplatedDecl()); |
| 1354 | else |
| 1355 | Function = cast<T>(Orig); |
| 1356 | } |
| 1357 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1358 | /// Determines whether there is a user-defined conversion sequence |
| 1359 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1360 | /// ToType. If such a conversion exists, User will contain the |
| 1361 | /// user-defined conversion sequence that performs such a conversion |
| 1362 | /// and this routine will return true. Otherwise, this routine returns |
| 1363 | /// false and User is unspecified. |
| 1364 | /// |
| 1365 | /// \param AllowConversionFunctions true if the conversion should |
| 1366 | /// consider conversion functions at all. If false, only constructors |
| 1367 | /// will be considered. |
| 1368 | /// |
| 1369 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1370 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1371 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1372 | /// |
| 1373 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1374 | /// for overload resolution. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1375 | Sema::OverloadingResult Sema::IsUserDefinedConversion( |
| 1376 | Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1377 | UserDefinedConversionSequence& User, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 1378 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1379 | bool AllowConversionFunctions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | bool AllowExplicit, bool ForceRValue) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1381 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | if (CXXRecordDecl *ToRecordDecl |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1383 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1384 | // C++ [over.match.ctor]p1: |
| 1385 | // When objects of class type are direct-initialized (8.5), or |
| 1386 | // copy-initialized from an expression of the same or a |
| 1387 | // derived class type (8.5), overload resolution selects the |
| 1388 | // constructor. [...] For copy-initialization, the candidate |
| 1389 | // functions are all the converting constructors (12.3.1) of |
| 1390 | // that class. The argument list is the expression-list within |
| 1391 | // the parentheses of the initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1393 | = Context.DeclarationNames.getCXXConstructorName( |
| 1394 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1395 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1396 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1397 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1398 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1399 | // Find the constructor (which may be a template). |
| 1400 | CXXConstructorDecl *Constructor = 0; |
| 1401 | FunctionTemplateDecl *ConstructorTmpl |
| 1402 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1403 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1405 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1406 | else |
| 1407 | Constructor = cast<CXXConstructorDecl>(*Con); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1409 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1410 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1411 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1413 | 1, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | /*SuppressUserConversions=*/true, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1415 | ForceRValue); |
| 1416 | else |
| 1417 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 1418 | /*SuppressUserConversions=*/true, ForceRValue); |
| 1419 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1420 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1424 | if (!AllowConversionFunctions) { |
| 1425 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1427 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1428 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1429 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1431 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1433 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1434 | // Add all of the conversion functions as candidates. |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1435 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1436 | = FromRecordDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1437 | for (OverloadedFunctionDecl::function_iterator Func |
| 1438 | = Conversions->function_begin(); |
| 1439 | Func != Conversions->function_end(); ++Func) { |
| 1440 | CXXConversionDecl *Conv; |
| 1441 | FunctionTemplateDecl *ConvTemplate; |
| 1442 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 1443 | if (ConvTemplate) |
| 1444 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1445 | else |
| 1446 | Conv = dyn_cast<CXXConversionDecl>(*Func); |
| 1447 | |
| 1448 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1449 | if (ConvTemplate) |
| 1450 | AddTemplateConversionCandidate(ConvTemplate, From, ToType, |
| 1451 | CandidateSet); |
| 1452 | else |
| 1453 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1454 | } |
| 1455 | } |
| 1456 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1457 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1458 | |
| 1459 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1460 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1461 | case OR_Success: |
| 1462 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1464 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1465 | // C++ [over.ics.user]p1: |
| 1466 | // If the user-defined conversion is specified by a |
| 1467 | // constructor (12.3.1), the initial standard conversion |
| 1468 | // sequence converts the source type to the type required by |
| 1469 | // the argument of the constructor. |
| 1470 | // |
| 1471 | // FIXME: What about ellipsis conversions? |
| 1472 | QualType ThisType = Constructor->getThisType(Context); |
| 1473 | User.Before = Best->Conversions[0].Standard; |
| 1474 | User.ConversionFunction = Constructor; |
| 1475 | User.After.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | User.After.FromTypePtr |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1477 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1478 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1479 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1480 | } else if (CXXConversionDecl *Conversion |
| 1481 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1482 | // C++ [over.ics.user]p1: |
| 1483 | // |
| 1484 | // [...] If the user-defined conversion is specified by a |
| 1485 | // conversion function (12.3.2), the initial standard |
| 1486 | // conversion sequence converts the source type to the |
| 1487 | // implicit object parameter of the conversion function. |
| 1488 | User.Before = Best->Conversions[0].Standard; |
| 1489 | User.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
| 1491 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1492 | // The second standard conversion sequence converts the |
| 1493 | // result of the user-defined conversion to the target type |
| 1494 | // for the sequence. Since an implicit conversion sequence |
| 1495 | // is an initialization, the special rules for |
| 1496 | // initialization by user-defined conversion apply when |
| 1497 | // selecting the best user-defined conversion for a |
| 1498 | // user-defined conversion sequence (see 13.3.3 and |
| 1499 | // 13.3.3.1). |
| 1500 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1501 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1502 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1503 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1504 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1505 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1507 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1508 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1509 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1510 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1511 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1512 | |
| 1513 | case OR_Ambiguous: |
| 1514 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 1515 | // ambiguous conversion sequences. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1516 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1519 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1520 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1521 | |
| 1522 | bool |
| 1523 | Sema::DiagnoseAmbiguousUserDefinedConversion(Expr *From, QualType ToType) { |
| 1524 | ImplicitConversionSequence ICS; |
| 1525 | OverloadCandidateSet CandidateSet; |
| 1526 | OverloadingResult OvResult = |
| 1527 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1528 | CandidateSet, true, false, false); |
| 1529 | if (OvResult != OR_Ambiguous) |
| 1530 | return false; |
| 1531 | Diag(From->getSourceRange().getBegin(), |
| 1532 | diag::err_typecheck_ambiguous_condition) |
| 1533 | << From->getType() << ToType << From->getSourceRange(); |
| 1534 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 1535 | return true; |
| 1536 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1537 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1538 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1539 | /// conversion sequences to determine whether one is better than the |
| 1540 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1542 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1543 | const ImplicitConversionSequence& ICS2) |
| 1544 | { |
| 1545 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1546 | // conversion sequences (as defined in 13.3.3.1) |
| 1547 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1548 | // conversion sequence than a user-defined conversion sequence or |
| 1549 | // an ellipsis conversion sequence, and |
| 1550 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1551 | // conversion sequence than an ellipsis conversion sequence |
| 1552 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | // |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1554 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1555 | return ImplicitConversionSequence::Better; |
| 1556 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1557 | return ImplicitConversionSequence::Worse; |
| 1558 | |
| 1559 | // Two implicit conversion sequences of the same form are |
| 1560 | // indistinguishable conversion sequences unless one of the |
| 1561 | // following rules apply: (C++ 13.3.3.2p3): |
| 1562 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1563 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | else if (ICS1.ConversionKind == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1565 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1566 | // User-defined conversion sequence U1 is a better conversion |
| 1567 | // sequence than another user-defined conversion sequence U2 if |
| 1568 | // they contain the same user-defined conversion function or |
| 1569 | // constructor and if the second standard conversion sequence of |
| 1570 | // U1 is better than the second standard conversion sequence of |
| 1571 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1573 | ICS2.UserDefined.ConversionFunction) |
| 1574 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1575 | ICS2.UserDefined.After); |
| 1576 | } |
| 1577 | |
| 1578 | return ImplicitConversionSequence::Indistinguishable; |
| 1579 | } |
| 1580 | |
| 1581 | /// CompareStandardConversionSequences - Compare two standard |
| 1582 | /// conversion sequences to determine whether one is better than the |
| 1583 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1585 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1586 | const StandardConversionSequence& SCS2) |
| 1587 | { |
| 1588 | // Standard conversion sequence S1 is a better conversion sequence |
| 1589 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1590 | |
| 1591 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1592 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1593 | // excluding any Lvalue Transformation; the identity conversion |
| 1594 | // sequence is considered to be a subsequence of any |
| 1595 | // non-identity conversion sequence) or, if not that, |
| 1596 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1597 | // Neither is a proper subsequence of the other. Do nothing. |
| 1598 | ; |
| 1599 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1600 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1602 | SCS1.Third == ICK_Identity)) |
| 1603 | // SCS1 is a proper subsequence of SCS2. |
| 1604 | return ImplicitConversionSequence::Better; |
| 1605 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1606 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1608 | SCS2.Third == ICK_Identity)) |
| 1609 | // SCS2 is a proper subsequence of SCS1. |
| 1610 | return ImplicitConversionSequence::Worse; |
| 1611 | |
| 1612 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1613 | // defined below), or, if not that, |
| 1614 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1615 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1616 | if (Rank1 < Rank2) |
| 1617 | return ImplicitConversionSequence::Better; |
| 1618 | else if (Rank2 < Rank1) |
| 1619 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1620 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1621 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1622 | // are indistinguishable unless one of the following rules |
| 1623 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1625 | // A conversion that is not a conversion of a pointer, or |
| 1626 | // pointer to member, to bool is better than another conversion |
| 1627 | // that is such a conversion. |
| 1628 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1629 | return SCS2.isPointerConversionToBool() |
| 1630 | ? ImplicitConversionSequence::Better |
| 1631 | : ImplicitConversionSequence::Worse; |
| 1632 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1633 | // C++ [over.ics.rank]p4b2: |
| 1634 | // |
| 1635 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1636 | // conversion of B* to A* is better than conversion of B* to |
| 1637 | // void*, and conversion of A* to void* is better than conversion |
| 1638 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1640 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1642 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1643 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1644 | // Exactly one of the conversion sequences is a conversion to |
| 1645 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1646 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1647 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1648 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1649 | // Neither conversion sequence converts to a void pointer; compare |
| 1650 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1651 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1652 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1653 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1654 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1655 | // Both conversion sequences are conversions to void |
| 1656 | // pointers. Compare the source types to determine if there's an |
| 1657 | // inheritance relationship in their sources. |
| 1658 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1659 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1660 | |
| 1661 | // Adjust the types we're converting from via the array-to-pointer |
| 1662 | // conversion, if we need to. |
| 1663 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1664 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1665 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1666 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1667 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1669 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1670 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1671 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1672 | |
| 1673 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1674 | return ImplicitConversionSequence::Better; |
| 1675 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1676 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Objective-C++: If one interface is more specific than the |
| 1679 | // other, it is the better one. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1680 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1681 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1682 | if (FromIface1 && FromIface1) { |
| 1683 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1684 | return ImplicitConversionSequence::Better; |
| 1685 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1686 | return ImplicitConversionSequence::Worse; |
| 1687 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1688 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1689 | |
| 1690 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1691 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1693 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1694 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1695 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1696 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1697 | // C++0x [over.ics.rank]p3b4: |
| 1698 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1699 | // implicit object parameter of a non-static member function declared |
| 1700 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1701 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1702 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1703 | // or if the member function in this case has a ref qualifier. |
| 1704 | // (Of course, we don't have ref qualifiers yet.) |
| 1705 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1706 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1707 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1708 | |
| 1709 | // C++ [over.ics.rank]p3b4: |
| 1710 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1711 | // which the references refer are the same type except for |
| 1712 | // top-level cv-qualifiers, and the type to which the reference |
| 1713 | // initialized by S2 refers is more cv-qualified than the type |
| 1714 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1715 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1716 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1717 | T1 = Context.getCanonicalType(T1); |
| 1718 | T2 = Context.getCanonicalType(T2); |
| 1719 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1720 | if (T2.isMoreQualifiedThan(T1)) |
| 1721 | return ImplicitConversionSequence::Better; |
| 1722 | else if (T1.isMoreQualifiedThan(T2)) |
| 1723 | return ImplicitConversionSequence::Worse; |
| 1724 | } |
| 1725 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1726 | |
| 1727 | return ImplicitConversionSequence::Indistinguishable; |
| 1728 | } |
| 1729 | |
| 1730 | /// CompareQualificationConversions - Compares two standard conversion |
| 1731 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1733 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1734 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1736 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1737 | // -- S1 and S2 differ only in their qualification conversion and |
| 1738 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1739 | // cv-qualification signature of type T1 is a proper subset of |
| 1740 | // the cv-qualification signature of type T2, and S1 is not the |
| 1741 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1742 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1743 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1744 | return ImplicitConversionSequence::Indistinguishable; |
| 1745 | |
| 1746 | // FIXME: the example in the standard doesn't use a qualification |
| 1747 | // conversion (!) |
| 1748 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1749 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1750 | T1 = Context.getCanonicalType(T1); |
| 1751 | T2 = Context.getCanonicalType(T2); |
| 1752 | |
| 1753 | // If the types are the same, we won't learn anything by unwrapped |
| 1754 | // them. |
| 1755 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1756 | return ImplicitConversionSequence::Indistinguishable; |
| 1757 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1759 | = ImplicitConversionSequence::Indistinguishable; |
| 1760 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1761 | // Within each iteration of the loop, we check the qualifiers to |
| 1762 | // determine if this still looks like a qualification |
| 1763 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1764 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1765 | // until there are no more pointers or pointers-to-members left |
| 1766 | // to unwrap. This essentially mimics what |
| 1767 | // IsQualificationConversion does, but here we're checking for a |
| 1768 | // strict subset of qualifiers. |
| 1769 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1770 | // The qualifiers are the same, so this doesn't tell us anything |
| 1771 | // about how the sequences rank. |
| 1772 | ; |
| 1773 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1774 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1775 | if (Result == ImplicitConversionSequence::Worse) |
| 1776 | // Neither has qualifiers that are a subset of the other's |
| 1777 | // qualifiers. |
| 1778 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1780 | Result = ImplicitConversionSequence::Better; |
| 1781 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1782 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1783 | if (Result == ImplicitConversionSequence::Better) |
| 1784 | // Neither has qualifiers that are a subset of the other's |
| 1785 | // qualifiers. |
| 1786 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1788 | Result = ImplicitConversionSequence::Worse; |
| 1789 | } else { |
| 1790 | // Qualifiers are disjoint. |
| 1791 | return ImplicitConversionSequence::Indistinguishable; |
| 1792 | } |
| 1793 | |
| 1794 | // If the types after this point are equivalent, we're done. |
| 1795 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1796 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1799 | // Check that the winning standard conversion sequence isn't using |
| 1800 | // the deprecated string literal array to pointer conversion. |
| 1801 | switch (Result) { |
| 1802 | case ImplicitConversionSequence::Better: |
| 1803 | if (SCS1.Deprecated) |
| 1804 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1805 | break; |
| 1806 | |
| 1807 | case ImplicitConversionSequence::Indistinguishable: |
| 1808 | break; |
| 1809 | |
| 1810 | case ImplicitConversionSequence::Worse: |
| 1811 | if (SCS2.Deprecated) |
| 1812 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1813 | break; |
| 1814 | } |
| 1815 | |
| 1816 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1819 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1820 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1821 | /// various kinds of derived-to-base conversions (C++ |
| 1822 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1823 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1824 | ImplicitConversionSequence::CompareKind |
| 1825 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1826 | const StandardConversionSequence& SCS2) { |
| 1827 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1828 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1829 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1830 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1831 | |
| 1832 | // Adjust the types we're converting from via the array-to-pointer |
| 1833 | // conversion, if we need to. |
| 1834 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1835 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1836 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1837 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1838 | |
| 1839 | // Canonicalize all of the types. |
| 1840 | FromType1 = Context.getCanonicalType(FromType1); |
| 1841 | ToType1 = Context.getCanonicalType(ToType1); |
| 1842 | FromType2 = Context.getCanonicalType(FromType2); |
| 1843 | ToType2 = Context.getCanonicalType(ToType2); |
| 1844 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1845 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1846 | // |
| 1847 | // If class B is derived directly or indirectly from class A and |
| 1848 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1849 | // |
| 1850 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1851 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1852 | |
| 1853 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1855 | SCS2.Second == ICK_Pointer_Conversion && |
| 1856 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1857 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1858 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1860 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1862 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1863 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1864 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1865 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1866 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1867 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1868 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1869 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1870 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 1871 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1873 | // -- 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] | 1874 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1875 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1876 | return ImplicitConversionSequence::Better; |
| 1877 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1878 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1879 | |
| 1880 | if (ToIface1 && ToIface2) { |
| 1881 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1882 | return ImplicitConversionSequence::Better; |
| 1883 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1884 | return ImplicitConversionSequence::Worse; |
| 1885 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1886 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1887 | |
| 1888 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1889 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1890 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1891 | return ImplicitConversionSequence::Better; |
| 1892 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1893 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1895 | if (FromIface1 && FromIface2) { |
| 1896 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1897 | return ImplicitConversionSequence::Better; |
| 1898 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1899 | return ImplicitConversionSequence::Worse; |
| 1900 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1901 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1904 | // Compare based on reference bindings. |
| 1905 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1906 | SCS1.Second == ICK_Derived_To_Base) { |
| 1907 | // -- binding of an expression of type C to a reference of type |
| 1908 | // B& is better than binding an expression of type C to a |
| 1909 | // reference of type A&, |
| 1910 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1911 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1912 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1913 | return ImplicitConversionSequence::Better; |
| 1914 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1915 | return ImplicitConversionSequence::Worse; |
| 1916 | } |
| 1917 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1918 | // -- binding of an expression of type B to a reference of type |
| 1919 | // A& is better than binding an expression of type C to a |
| 1920 | // reference of type A&, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1921 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1922 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1923 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1924 | return ImplicitConversionSequence::Better; |
| 1925 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1926 | return ImplicitConversionSequence::Worse; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1932 | // A::* to C::*, |
| 1933 | |
| 1934 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1935 | // A::* to C::*, and |
| 1936 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1937 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1938 | SCS1.Second == ICK_Derived_To_Base) { |
| 1939 | // -- conversion of C to B is better than conversion of C to A, |
| 1940 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1941 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1942 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1943 | return ImplicitConversionSequence::Better; |
| 1944 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1945 | return ImplicitConversionSequence::Worse; |
| 1946 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1947 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1948 | // -- conversion of B to A is better than conversion of C to A. |
| 1949 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1950 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1951 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1952 | return ImplicitConversionSequence::Better; |
| 1953 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1954 | return ImplicitConversionSequence::Worse; |
| 1955 | } |
| 1956 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1957 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1958 | return ImplicitConversionSequence::Indistinguishable; |
| 1959 | } |
| 1960 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1961 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1962 | /// ToType from the expression From. Return the implicit conversion |
| 1963 | /// sequence required to pass this argument, which may be a bad |
| 1964 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1965 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1966 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 1967 | /// 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] | 1968 | ImplicitConversionSequence |
| 1969 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 1970 | bool SuppressUserConversions, bool ForceRValue, |
| 1971 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1972 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1973 | ImplicitConversionSequence ICS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1975 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1976 | SuppressUserConversions, |
| 1977 | /*AllowExplicit=*/false, |
| 1978 | ForceRValue, |
| 1979 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1980 | return ICS; |
| 1981 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1983 | SuppressUserConversions, |
| 1984 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1985 | ForceRValue, |
| 1986 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1990 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 1991 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 1992 | /// an error, returns false if the initialization succeeded. Elidable should |
| 1993 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 1994 | /// differently in C++0x for this case. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1996 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1997 | if (!getLangOptions().CPlusPlus) { |
| 1998 | // In C, argument passing is the same as performing an assignment. |
| 1999 | QualType FromType = From->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2001 | AssignConvertType ConvTy = |
| 2002 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2003 | if (ConvTy != Compatible && |
| 2004 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2005 | ConvTy = Compatible; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2007 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 2008 | FromType, From, Flavor); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2009 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2011 | if (ToType->isReferenceType()) |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2012 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2013 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2014 | /*SuppressUserConversions=*/false, |
| 2015 | /*AllowExplicit=*/false, |
| 2016 | /*ForceRValue=*/false); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2017 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2018 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 2019 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2020 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2021 | if (!DiagnoseAmbiguousUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2022 | return Diag(From->getSourceRange().getBegin(), |
| 2023 | diag::err_typecheck_convert_incompatible) |
| 2024 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2025 | return true; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2028 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2029 | /// parameter of the given member function (@c Method) from the |
| 2030 | /// expression @p From. |
| 2031 | ImplicitConversionSequence |
| 2032 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 2033 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2034 | QualType ImplicitParamType |
| 2035 | = Context.getCVRQualifiedType(ClassType, Method->getTypeQualifiers()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2036 | |
| 2037 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2038 | // to exit early. |
| 2039 | ImplicitConversionSequence ICS; |
| 2040 | ICS.Standard.setAsIdentityConversion(); |
| 2041 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2042 | |
| 2043 | // We need to have an object of class type. |
| 2044 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2045 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2046 | FromType = PT->getPointeeType(); |
| 2047 | |
| 2048 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2049 | |
| 2050 | // The implicit object parmeter is has the type "reference to cv X", |
| 2051 | // where X is the class of which the function is a member |
| 2052 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2053 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2055 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2056 | // reference binding here, that allows class rvalues to bind to |
| 2057 | // non-constant references. |
| 2058 | |
| 2059 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2060 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2061 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 2062 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 2063 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 2064 | return ICS; |
| 2065 | |
| 2066 | // Check that we have either the same type or a derived type. It |
| 2067 | // affects the conversion rank. |
| 2068 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 2069 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 2070 | ICS.Standard.Second = ICK_Identity; |
| 2071 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2072 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2073 | else |
| 2074 | return ICS; |
| 2075 | |
| 2076 | // Success. Mark this as a reference binding. |
| 2077 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2078 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2079 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2080 | ICS.Standard.ReferenceBinding = true; |
| 2081 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2082 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2083 | return ICS; |
| 2084 | } |
| 2085 | |
| 2086 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2087 | /// the implicit object parameter for the given Method with the given |
| 2088 | /// expression. |
| 2089 | bool |
| 2090 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2091 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2093 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2095 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2096 | FromRecordType = PT->getPointeeType(); |
| 2097 | DestType = Method->getThisType(Context); |
| 2098 | } else { |
| 2099 | FromRecordType = From->getType(); |
| 2100 | DestType = ImplicitParamRecordType; |
| 2101 | } |
| 2102 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | ImplicitConversionSequence ICS |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2104 | = TryObjectArgumentInitialization(From, Method); |
| 2105 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2106 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2107 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2108 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2110 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2111 | CheckDerivedToBaseConversion(FromRecordType, |
| 2112 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2113 | From->getSourceRange().getBegin(), |
| 2114 | From->getSourceRange())) |
| 2115 | return true; |
| 2116 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 116b7d9 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2118 | /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2119 | return false; |
| 2120 | } |
| 2121 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2122 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2123 | /// expression From to bool (C++0x [conv]p3). |
| 2124 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2126 | // FIXME: Are these flags correct? |
| 2127 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2129 | /*ForceRValue=*/false, |
| 2130 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2134 | /// of the expression From to bool (C++0x [conv]p3). |
| 2135 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2136 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2137 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2138 | return false; |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2139 | |
| 2140 | if (!DiagnoseAmbiguousUserDefinedConversion(From, Context.BoolTy)) |
| 2141 | return Diag(From->getSourceRange().getBegin(), |
| 2142 | diag::err_typecheck_bool_condition) |
| 2143 | << From->getType() << From->getSourceRange(); |
| 2144 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2147 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2148 | /// candidate functions, using the given function call arguments. If |
| 2149 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2150 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2151 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2152 | /// hacky way to implement the overloading rules for elidable copy |
| 2153 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2154 | /// |
| 2155 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2156 | /// based on an incomplete set of function arguments. This feature is used by |
| 2157 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | void |
| 2159 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2160 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2161 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2162 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2163 | bool ForceRValue, |
| 2164 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2166 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2167 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2169 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2171 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2173 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2174 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2175 | // If we get here, it's because we're calling a member function |
| 2176 | // that is named without a member access expression (e.g., |
| 2177 | // "this->f") that was either written explicitly or created |
| 2178 | // implicitly. This can happen with a qualified call to a member |
| 2179 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2180 | // object argument (C++ [over.call.func]p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2182 | SuppressUserConversions, ForceRValue); |
| 2183 | return; |
| 2184 | } |
| 2185 | // We treat a constructor like a non-member function, since its object |
| 2186 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2189 | if (!CandidateSet.isNewCandidate(Function)) |
| 2190 | return; |
| 2191 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2192 | // Add this candidate |
| 2193 | CandidateSet.push_back(OverloadCandidate()); |
| 2194 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2195 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2196 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2197 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2198 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2199 | |
| 2200 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2201 | |
| 2202 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2203 | // parameters is viable only if it has an ellipsis in its parameter |
| 2204 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2205 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2206 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2207 | Candidate.Viable = false; |
| 2208 | return; |
| 2209 | } |
| 2210 | |
| 2211 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2212 | // is viable only if the (m+1)st parameter has a default argument |
| 2213 | // (8.3.6). For the purposes of overload resolution, the |
| 2214 | // parameter list is truncated on the right, so that there are |
| 2215 | // exactly m parameters. |
| 2216 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2217 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2218 | // Not enough arguments. |
| 2219 | Candidate.Viable = false; |
| 2220 | return; |
| 2221 | } |
| 2222 | |
| 2223 | // Determine the implicit conversion sequences for each of the |
| 2224 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2225 | Candidate.Conversions.resize(NumArgs); |
| 2226 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2227 | if (ArgIdx < NumArgsInProto) { |
| 2228 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2229 | // exist for each argument an implicit conversion sequence |
| 2230 | // (13.3.3.1) that converts that argument to the corresponding |
| 2231 | // parameter of F. |
| 2232 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | Candidate.Conversions[ArgIdx] |
| 2234 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2235 | SuppressUserConversions, ForceRValue, |
| 2236 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2238 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2239 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2240 | break; |
| 2241 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2242 | } else { |
| 2243 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2244 | // argument for which there is no corresponding parameter is |
| 2245 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2247 | = ImplicitConversionSequence::EllipsisConversion; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2252 | /// \brief Add all of the function declarations in the given function set to |
| 2253 | /// the overload canddiate set. |
| 2254 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2255 | Expr **Args, unsigned NumArgs, |
| 2256 | OverloadCandidateSet& CandidateSet, |
| 2257 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2259 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2260 | F != FEnd; ++F) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2261 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2262 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2263 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
| 2264 | Args[0], Args + 1, NumArgs - 1, |
| 2265 | CandidateSet, SuppressUserConversions); |
| 2266 | else |
| 2267 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2268 | SuppressUserConversions); |
| 2269 | } else { |
| 2270 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2271 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2272 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2273 | AddMethodTemplateCandidate(FunTmpl, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2274 | /*FIXME: explicit args */false, 0, 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2275 | Args[0], Args + 1, NumArgs - 1, |
| 2276 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2277 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2278 | else |
| 2279 | AddTemplateOverloadCandidate(FunTmpl, |
| 2280 | /*FIXME: explicit args */false, 0, 0, |
| 2281 | Args, NumArgs, CandidateSet, |
| 2282 | SuppressUserConversions); |
| 2283 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2284 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2287 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2288 | /// of candidate functions, using the given function call arguments |
| 2289 | /// and the object argument (@c Object). For example, in a call |
| 2290 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2291 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2292 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2293 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2294 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2295 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | void |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2297 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2298 | Expr **Args, unsigned NumArgs, |
| 2299 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | bool SuppressUserConversions, bool ForceRValue) { |
| 2301 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2302 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2303 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2304 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2305 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2306 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2307 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2308 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2309 | if (!CandidateSet.isNewCandidate(Method)) |
| 2310 | return; |
| 2311 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2312 | // Add this candidate |
| 2313 | CandidateSet.push_back(OverloadCandidate()); |
| 2314 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2315 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2316 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2317 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2318 | |
| 2319 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2320 | |
| 2321 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2322 | // parameters is viable only if it has an ellipsis in its parameter |
| 2323 | // list (8.3.5). |
| 2324 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2325 | Candidate.Viable = false; |
| 2326 | return; |
| 2327 | } |
| 2328 | |
| 2329 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2330 | // is viable only if the (m+1)st parameter has a default argument |
| 2331 | // (8.3.6). For the purposes of overload resolution, the |
| 2332 | // parameter list is truncated on the right, so that there are |
| 2333 | // exactly m parameters. |
| 2334 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2335 | if (NumArgs < MinRequiredArgs) { |
| 2336 | // Not enough arguments. |
| 2337 | Candidate.Viable = false; |
| 2338 | return; |
| 2339 | } |
| 2340 | |
| 2341 | Candidate.Viable = true; |
| 2342 | Candidate.Conversions.resize(NumArgs + 1); |
| 2343 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2344 | if (Method->isStatic() || !Object) |
| 2345 | // The implicit object argument is ignored. |
| 2346 | Candidate.IgnoreObjectArgument = true; |
| 2347 | else { |
| 2348 | // Determine the implicit conversion sequence for the object |
| 2349 | // parameter. |
| 2350 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2352 | == ImplicitConversionSequence::BadConversion) { |
| 2353 | Candidate.Viable = false; |
| 2354 | return; |
| 2355 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | // Determine the implicit conversion sequences for each of the |
| 2359 | // arguments. |
| 2360 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2361 | if (ArgIdx < NumArgsInProto) { |
| 2362 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2363 | // exist for each argument an implicit conversion sequence |
| 2364 | // (13.3.3.1) that converts that argument to the corresponding |
| 2365 | // parameter of F. |
| 2366 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | Candidate.Conversions[ArgIdx + 1] |
| 2368 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2369 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2370 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2372 | == ImplicitConversionSequence::BadConversion) { |
| 2373 | Candidate.Viable = false; |
| 2374 | break; |
| 2375 | } |
| 2376 | } else { |
| 2377 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2378 | // argument for which there is no corresponding parameter is |
| 2379 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2381 | = ImplicitConversionSequence::EllipsisConversion; |
| 2382 | } |
| 2383 | } |
| 2384 | } |
| 2385 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2386 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2387 | /// set, using template argument deduction to produce an appropriate member |
| 2388 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2389 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2390 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2391 | bool HasExplicitTemplateArgs, |
| 2392 | const TemplateArgument *ExplicitTemplateArgs, |
| 2393 | unsigned NumExplicitTemplateArgs, |
| 2394 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2395 | OverloadCandidateSet& CandidateSet, |
| 2396 | bool SuppressUserConversions, |
| 2397 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2398 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2399 | return; |
| 2400 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2401 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2403 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | // 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] | 2405 | // candidate functions in the usual way.113) A given name can refer to one |
| 2406 | // or more function templates and also to a set of overloaded non-template |
| 2407 | // functions. In such a case, the candidate functions generated from each |
| 2408 | // function template are combined with the set of non-template candidate |
| 2409 | // functions. |
| 2410 | TemplateDeductionInfo Info(Context); |
| 2411 | FunctionDecl *Specialization = 0; |
| 2412 | if (TemplateDeductionResult Result |
| 2413 | = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs, |
| 2414 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2415 | Args, NumArgs, Specialization, Info)) { |
| 2416 | // FIXME: Record what happened with template argument deduction, so |
| 2417 | // that we can give the user a beautiful diagnostic. |
| 2418 | (void)Result; |
| 2419 | return; |
| 2420 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2422 | // Add the function template specialization produced by template argument |
| 2423 | // deduction as a candidate. |
| 2424 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2426 | "Specialization is not a member function?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2427 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2428 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2429 | } |
| 2430 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2431 | /// \brief Add a C++ function template specialization as a candidate |
| 2432 | /// in the candidate set, using template argument deduction to produce |
| 2433 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2435 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2436 | bool HasExplicitTemplateArgs, |
| 2437 | const TemplateArgument *ExplicitTemplateArgs, |
| 2438 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2439 | Expr **Args, unsigned NumArgs, |
| 2440 | OverloadCandidateSet& CandidateSet, |
| 2441 | bool SuppressUserConversions, |
| 2442 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2443 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2444 | return; |
| 2445 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2446 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2448 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | // 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] | 2450 | // candidate functions in the usual way.113) A given name can refer to one |
| 2451 | // or more function templates and also to a set of overloaded non-template |
| 2452 | // functions. In such a case, the candidate functions generated from each |
| 2453 | // function template are combined with the set of non-template candidate |
| 2454 | // functions. |
| 2455 | TemplateDeductionInfo Info(Context); |
| 2456 | FunctionDecl *Specialization = 0; |
| 2457 | if (TemplateDeductionResult Result |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2458 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2459 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2460 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2461 | // FIXME: Record what happened with template argument deduction, so |
| 2462 | // that we can give the user a beautiful diagnostic. |
| 2463 | (void)Result; |
| 2464 | return; |
| 2465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2467 | // Add the function template specialization produced by template argument |
| 2468 | // deduction as a candidate. |
| 2469 | assert(Specialization && "Missing function template specialization?"); |
| 2470 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2471 | SuppressUserConversions, ForceRValue); |
| 2472 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2474 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2476 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | /// 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] | 2478 | /// (which may or may not be the same type as the type that the |
| 2479 | /// conversion function produces). |
| 2480 | void |
| 2481 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2482 | Expr *From, QualType ToType, |
| 2483 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2484 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2485 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2486 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2487 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2488 | return; |
| 2489 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2490 | // Add this candidate |
| 2491 | CandidateSet.push_back(OverloadCandidate()); |
| 2492 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2493 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2494 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2495 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2496 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2498 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2499 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2500 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2501 | // Determine the implicit conversion sequence for the implicit |
| 2502 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2503 | Candidate.Viable = true; |
| 2504 | Candidate.Conversions.resize(1); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2505 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2506 | // Conversion functions to a different type in the base class is visible in |
| 2507 | // the derived class. So, a derived to base conversion should not participate |
| 2508 | // in overload resolution. |
| 2509 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2510 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2511 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2512 | == ImplicitConversionSequence::BadConversion) { |
| 2513 | Candidate.Viable = false; |
| 2514 | return; |
| 2515 | } |
| 2516 | |
| 2517 | // To determine what the conversion from the result of calling the |
| 2518 | // conversion function to the type we're eventually trying to |
| 2519 | // convert to (ToType), we need to synthesize a call to the |
| 2520 | // conversion function and attempt copy initialization from it. This |
| 2521 | // makes sure that we get the right semantics with respect to |
| 2522 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2523 | // call on the stack and we don't need its arguments to be |
| 2524 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2526 | SourceLocation()); |
| 2527 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2528 | CastExpr::CK_Unknown, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2529 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
| 2531 | // 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] | 2532 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2533 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2534 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2535 | Conversion->getConversionType().getNonReferenceType(), |
| 2536 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | ImplicitConversionSequence ICS = |
| 2538 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2539 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2540 | /*ForceRValue=*/false, |
| 2541 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2543 | switch (ICS.ConversionKind) { |
| 2544 | case ImplicitConversionSequence::StandardConversion: |
| 2545 | Candidate.FinalConversion = ICS.Standard; |
| 2546 | break; |
| 2547 | |
| 2548 | case ImplicitConversionSequence::BadConversion: |
| 2549 | Candidate.Viable = false; |
| 2550 | break; |
| 2551 | |
| 2552 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2554 | "Can only end up with a standard conversion sequence or failure"); |
| 2555 | } |
| 2556 | } |
| 2557 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2558 | /// \brief Adds a conversion function template specialization |
| 2559 | /// candidate to the overload set, using template argument deduction |
| 2560 | /// to deduce the template arguments of the conversion function |
| 2561 | /// template from the type that we are converting to (C++ |
| 2562 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2564 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2565 | Expr *From, QualType ToType, |
| 2566 | OverloadCandidateSet &CandidateSet) { |
| 2567 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2568 | "Only conversion function templates permitted here"); |
| 2569 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2570 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2571 | return; |
| 2572 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2573 | TemplateDeductionInfo Info(Context); |
| 2574 | CXXConversionDecl *Specialization = 0; |
| 2575 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2577 | Specialization, Info)) { |
| 2578 | // FIXME: Record what happened with template argument deduction, so |
| 2579 | // that we can give the user a beautiful diagnostic. |
| 2580 | (void)Result; |
| 2581 | return; |
| 2582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2584 | // Add the conversion function template specialization produced by |
| 2585 | // template argument deduction as a candidate. |
| 2586 | assert(Specialization && "Missing function template specialization?"); |
| 2587 | AddConversionCandidate(Specialization, From, ToType, CandidateSet); |
| 2588 | } |
| 2589 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2590 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2591 | /// converts the given @c Object to a function pointer via the |
| 2592 | /// conversion function @c Conversion, and then attempts to call it |
| 2593 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2594 | /// the type of function that we'll eventually be calling. |
| 2595 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2596 | const FunctionProtoType *Proto, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2597 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2598 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2599 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2600 | return; |
| 2601 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2602 | CandidateSet.push_back(OverloadCandidate()); |
| 2603 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2604 | Candidate.Function = 0; |
| 2605 | Candidate.Surrogate = Conversion; |
| 2606 | Candidate.Viable = true; |
| 2607 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2608 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2609 | Candidate.Conversions.resize(NumArgs + 1); |
| 2610 | |
| 2611 | // Determine the implicit conversion sequence for the implicit |
| 2612 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | ImplicitConversionSequence ObjectInit |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2614 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2615 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2616 | Candidate.Viable = false; |
| 2617 | return; |
| 2618 | } |
| 2619 | |
| 2620 | // The first conversion is actually a user-defined conversion whose |
| 2621 | // first conversion is ObjectInit's standard conversion (which is |
| 2622 | // effectively a reference binding). Record it as such. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2623 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2624 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2625 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2626 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2628 | = Candidate.Conversions[0].UserDefined.Before; |
| 2629 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2630 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2632 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2633 | |
| 2634 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2635 | // parameters is viable only if it has an ellipsis in its parameter |
| 2636 | // list (8.3.5). |
| 2637 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2638 | Candidate.Viable = false; |
| 2639 | return; |
| 2640 | } |
| 2641 | |
| 2642 | // Function types don't have any default arguments, so just check if |
| 2643 | // we have enough arguments. |
| 2644 | if (NumArgs < NumArgsInProto) { |
| 2645 | // Not enough arguments. |
| 2646 | Candidate.Viable = false; |
| 2647 | return; |
| 2648 | } |
| 2649 | |
| 2650 | // Determine the implicit conversion sequences for each of the |
| 2651 | // arguments. |
| 2652 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2653 | if (ArgIdx < NumArgsInProto) { |
| 2654 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2655 | // exist for each argument an implicit conversion sequence |
| 2656 | // (13.3.3.1) that converts that argument to the corresponding |
| 2657 | // parameter of F. |
| 2658 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | Candidate.Conversions[ArgIdx + 1] |
| 2660 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2661 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2662 | /*ForceRValue=*/false, |
| 2663 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2665 | == ImplicitConversionSequence::BadConversion) { |
| 2666 | Candidate.Viable = false; |
| 2667 | break; |
| 2668 | } |
| 2669 | } else { |
| 2670 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2671 | // argument for which there is no corresponding parameter is |
| 2672 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2674 | = ImplicitConversionSequence::EllipsisConversion; |
| 2675 | } |
| 2676 | } |
| 2677 | } |
| 2678 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2679 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2680 | // operator overloading logic over to the scheme used by binary operators, which |
| 2681 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2682 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2683 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2684 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2685 | OverloadCandidateSet& CandidateSet, |
| 2686 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2687 | FunctionSet Functions; |
| 2688 | |
| 2689 | QualType T1 = Args[0]->getType(); |
| 2690 | QualType T2; |
| 2691 | if (NumArgs > 1) |
| 2692 | T2 = Args[1]->getType(); |
| 2693 | |
| 2694 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2695 | if (S) |
| 2696 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2697 | ArgumentDependentLookup(OpName, Args, NumArgs, Functions); |
| 2698 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2699 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 2700 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
| 2701 | } |
| 2702 | |
| 2703 | /// \brief Add overload candidates for overloaded operators that are |
| 2704 | /// member functions. |
| 2705 | /// |
| 2706 | /// Add the overloaded operator candidates that are member functions |
| 2707 | /// for the operator Op that was used in an operator expression such |
| 2708 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2709 | /// CandidateSet will store the added overload candidates. (C++ |
| 2710 | /// [over.match.oper]). |
| 2711 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2712 | SourceLocation OpLoc, |
| 2713 | Expr **Args, unsigned NumArgs, |
| 2714 | OverloadCandidateSet& CandidateSet, |
| 2715 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2716 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2717 | |
| 2718 | // C++ [over.match.oper]p3: |
| 2719 | // For a unary operator @ with an operand of a type whose |
| 2720 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2721 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2722 | // a right operand of a type whose cv-unqualified version is T2, |
| 2723 | // three sets of candidate functions, designated member |
| 2724 | // candidates, non-member candidates and built-in candidates, are |
| 2725 | // constructed as follows: |
| 2726 | QualType T1 = Args[0]->getType(); |
| 2727 | QualType T2; |
| 2728 | if (NumArgs > 1) |
| 2729 | T2 = Args[1]->getType(); |
| 2730 | |
| 2731 | // -- If T1 is a class type, the set of member candidates is the |
| 2732 | // result of the qualified lookup of T1::operator@ |
| 2733 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2734 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2735 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2736 | // Complete the type if it can be completed. Otherwise, we're done. |
| 2737 | if (RequireCompleteType(OpLoc, T1, PartialDiagnostic(0))) |
| 2738 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | |
| 2740 | LookupResult Operators = LookupQualifiedName(T1Rec->getDecl(), OpName, |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2741 | LookupOrdinaryName, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2742 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2743 | OperEnd = Operators.end(); |
| 2744 | Oper != OperEnd; |
| 2745 | ++Oper) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2746 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2747 | Args+1, NumArgs - 1, CandidateSet, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2748 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2749 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2752 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2753 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2754 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2755 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2756 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2757 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2758 | /// (at the beginning of the argument list) that will be contextually |
| 2759 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2761 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2762 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2763 | bool IsAssignmentOperator, |
| 2764 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2765 | // Add this candidate |
| 2766 | CandidateSet.push_back(OverloadCandidate()); |
| 2767 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2768 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2769 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2770 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2771 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2772 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2773 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2774 | |
| 2775 | // Determine the implicit conversion sequences for each of the |
| 2776 | // arguments. |
| 2777 | Candidate.Viable = true; |
| 2778 | Candidate.Conversions.resize(NumArgs); |
| 2779 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2780 | // C++ [over.match.oper]p4: |
| 2781 | // For the built-in assignment operators, conversions of the |
| 2782 | // left operand are restricted as follows: |
| 2783 | // -- no temporaries are introduced to hold the left operand, and |
| 2784 | // -- no user-defined conversions are applied to the left |
| 2785 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2786 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2787 | // |
| 2788 | // We block these conversions by turning off user-defined |
| 2789 | // conversions, since that is the only way that initialization of |
| 2790 | // a reference to a non-class type can occur from something that |
| 2791 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2792 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2794 | "Contextual conversion to bool requires bool type"); |
| 2795 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2796 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | Candidate.Conversions[ArgIdx] |
| 2798 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2799 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2800 | /*ForceRValue=*/false, |
| 2801 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2803 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2804 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2805 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2806 | break; |
| 2807 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2812 | /// candidate operator functions for built-in operators (C++ |
| 2813 | /// [over.built]). The types are separated into pointer types and |
| 2814 | /// enumeration types. |
| 2815 | class BuiltinCandidateTypeSet { |
| 2816 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2817 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2818 | |
| 2819 | /// PointerTypes - The set of pointer types that will be used in the |
| 2820 | /// built-in candidates. |
| 2821 | TypeSet PointerTypes; |
| 2822 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2823 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2824 | /// used in the built-in candidates. |
| 2825 | TypeSet MemberPointerTypes; |
| 2826 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2827 | /// EnumerationTypes - The set of enumeration types that will be |
| 2828 | /// used in the built-in candidates. |
| 2829 | TypeSet EnumerationTypes; |
| 2830 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2831 | /// Sema - The semantic analysis instance where we are building the |
| 2832 | /// candidate type set. |
| 2833 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2835 | /// Context - The AST context in which we will build the type sets. |
| 2836 | ASTContext &Context; |
| 2837 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2838 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| 2839 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2840 | |
| 2841 | public: |
| 2842 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2843 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2844 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2846 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2848 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions, |
| 2849 | bool AllowExplicitConversions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2850 | |
| 2851 | /// pointer_begin - First pointer type found; |
| 2852 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2853 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2854 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2855 | iterator pointer_end() { return PointerTypes.end(); } |
| 2856 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2857 | /// member_pointer_begin - First member pointer type found; |
| 2858 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2859 | |
| 2860 | /// member_pointer_end - Past the last member pointer type found; |
| 2861 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2862 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2863 | /// enumeration_begin - First enumeration type found; |
| 2864 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2865 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2866 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2867 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2868 | }; |
| 2869 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2870 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2871 | /// the set of pointer types along with any more-qualified variants of |
| 2872 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2873 | /// will add "int const *", "int const volatile *", "int const |
| 2874 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2875 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2876 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2877 | /// |
| 2878 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2879 | bool |
| 2880 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2882 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2883 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2884 | return false; |
| 2885 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2886 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 2887 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2888 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2889 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2890 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 2891 | |
| 2892 | // Iterate through all strict supersets of BaseCVR. |
| 2893 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 2894 | if ((CVR | BaseCVR) != CVR) continue; |
| 2895 | |
| 2896 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 2897 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | return true; |
| 2901 | } |
| 2902 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2903 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 2904 | /// to the set of pointer types along with any more-qualified variants of |
| 2905 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2906 | /// will add "int const *", "int const volatile *", "int const |
| 2907 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2908 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2909 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2910 | /// |
| 2911 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2912 | bool |
| 2913 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 2914 | QualType Ty) { |
| 2915 | // Insert this type. |
| 2916 | if (!MemberPointerTypes.insert(Ty)) |
| 2917 | return false; |
| 2918 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2919 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 2920 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2921 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2922 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2923 | const Type *ClassTy = PointerTy->getClass(); |
| 2924 | |
| 2925 | // Iterate through all strict supersets of the pointee type's CVR |
| 2926 | // qualifiers. |
| 2927 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 2928 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 2929 | if ((CVR | BaseCVR) != CVR) continue; |
| 2930 | |
| 2931 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 2932 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | return true; |
| 2936 | } |
| 2937 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2938 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2939 | /// 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] | 2940 | /// primarily interested in pointer types and enumeration types. We also |
| 2941 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2942 | /// AllowUserConversions is true if we should look at the conversion |
| 2943 | /// functions of a class type, and AllowExplicitConversions if we |
| 2944 | /// should also include the explicit conversion functions of a class |
| 2945 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2946 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2947 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2948 | bool AllowUserConversions, |
| 2949 | bool AllowExplicitConversions) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2950 | // Only deal with canonical types. |
| 2951 | Ty = Context.getCanonicalType(Ty); |
| 2952 | |
| 2953 | // Look through reference types; they aren't part of the type of an |
| 2954 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2955 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2956 | Ty = RefTy->getPointeeType(); |
| 2957 | |
| 2958 | // We don't care about qualifiers on the type. |
| 2959 | Ty = Ty.getUnqualifiedType(); |
| 2960 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2961 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2962 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2963 | |
| 2964 | // Insert our type, and its more-qualified variants, into the set |
| 2965 | // of types. |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2966 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2967 | return; |
| 2968 | |
| 2969 | // Add 'cv void*' to our set of types. |
| 2970 | if (!Ty->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | QualType QualVoid |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2972 | = Context.getCVRQualifiedType(Context.VoidTy, |
| 2973 | PointeeTy.getCVRQualifiers()); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2974 | AddPointerWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | // If this is a pointer to a class type, add pointers to its bases |
| 2978 | // (with the same level of cv-qualification as the original |
| 2979 | // derived class, of course). |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2980 | if (const RecordType *PointeeRec = PointeeTy->getAs<RecordType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2981 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 2982 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2983 | Base != ClassDecl->bases_end(); ++Base) { |
| 2984 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2985 | BaseTy = Context.getCVRQualifiedType(BaseTy.getUnqualifiedType(), |
| 2986 | PointeeTy.getCVRQualifiers()); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2987 | |
| 2988 | // Add the pointer type, recursively, so that we get all of |
| 2989 | // the indirect base classes, too. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2990 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2991 | } |
| 2992 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2993 | } else if (Ty->isMemberPointerType()) { |
| 2994 | // Member pointers are far easier, since the pointee can't be converted. |
| 2995 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 2996 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2997 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2998 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2999 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3000 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3001 | if (SemaRef.RequireCompleteType(SourceLocation(), Ty, 0)) { |
| 3002 | // No conversion functions in incomplete types. |
| 3003 | return; |
| 3004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3006 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 3007 | // FIXME: Visit conversion functions in the base classes, too. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3009 | = ClassDecl->getConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3011 | = Conversions->function_begin(); |
| 3012 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3013 | CXXConversionDecl *Conv; |
| 3014 | FunctionTemplateDecl *ConvTemplate; |
| 3015 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 3016 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3017 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3018 | // about which builtin types we can convert to. |
| 3019 | if (ConvTemplate) |
| 3020 | continue; |
| 3021 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3022 | if (AllowExplicitConversions || !Conv->isExplicit()) |
| 3023 | AddTypesConvertedFrom(Conv->getConversionType(), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3024 | } |
| 3025 | } |
| 3026 | } |
| 3027 | } |
| 3028 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3029 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3030 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3031 | /// given type to the candidate set. |
| 3032 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3033 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3035 | unsigned NumArgs, |
| 3036 | OverloadCandidateSet &CandidateSet) { |
| 3037 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3039 | // T& operator=(T&, T) |
| 3040 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3041 | ParamTypes[1] = T; |
| 3042 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3043 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3045 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3046 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3047 | ParamTypes[0] |
| 3048 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3049 | ParamTypes[1] = T; |
| 3050 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3051 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3055 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3056 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3057 | /// on the operator @p Op and the arguments given. For example, if the |
| 3058 | /// operator is a binary '+', this routine might add "int |
| 3059 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3060 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3061 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3062 | Expr **Args, unsigned NumArgs, |
| 3063 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3064 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3065 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3066 | // that the first few of these types are the promoted integral |
| 3067 | // types; these types need to be first. |
| 3068 | // FIXME: What about complex? |
| 3069 | const unsigned FirstIntegralType = 0; |
| 3070 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3072 | LastPromotedIntegralType = 13; |
| 3073 | const unsigned FirstPromotedArithmeticType = 7, |
| 3074 | LastPromotedArithmeticType = 16; |
| 3075 | const unsigned NumArithmeticTypes = 16; |
| 3076 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3078 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3079 | Context.SignedCharTy, Context.ShortTy, |
| 3080 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3081 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3082 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3083 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3084 | }; |
| 3085 | |
| 3086 | // Find all of the types that the arguments can convert to, but only |
| 3087 | // if the operator we're looking at has built-in operator candidates |
| 3088 | // that make use of these types. |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3089 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3090 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3091 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3092 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3093 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3094 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3095 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3096 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3097 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 3098 | true, |
| 3099 | (Op == OO_Exclaim || |
| 3100 | Op == OO_AmpAmp || |
| 3101 | Op == OO_PipePipe)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | bool isComparison = false; |
| 3105 | switch (Op) { |
| 3106 | case OO_None: |
| 3107 | case NUM_OVERLOADED_OPERATORS: |
| 3108 | assert(false && "Expected an overloaded operator"); |
| 3109 | break; |
| 3110 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3111 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3112 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3113 | goto UnaryStar; |
| 3114 | else |
| 3115 | goto BinaryStar; |
| 3116 | break; |
| 3117 | |
| 3118 | case OO_Plus: // '+' is either unary or binary |
| 3119 | if (NumArgs == 1) |
| 3120 | goto UnaryPlus; |
| 3121 | else |
| 3122 | goto BinaryPlus; |
| 3123 | break; |
| 3124 | |
| 3125 | case OO_Minus: // '-' is either unary or binary |
| 3126 | if (NumArgs == 1) |
| 3127 | goto UnaryMinus; |
| 3128 | else |
| 3129 | goto BinaryMinus; |
| 3130 | break; |
| 3131 | |
| 3132 | case OO_Amp: // '&' is either unary or binary |
| 3133 | if (NumArgs == 1) |
| 3134 | goto UnaryAmp; |
| 3135 | else |
| 3136 | goto BinaryAmp; |
| 3137 | |
| 3138 | case OO_PlusPlus: |
| 3139 | case OO_MinusMinus: |
| 3140 | // C++ [over.built]p3: |
| 3141 | // |
| 3142 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3143 | // is either volatile or empty, there exist candidate operator |
| 3144 | // functions of the form |
| 3145 | // |
| 3146 | // VQ T& operator++(VQ T&); |
| 3147 | // T operator++(VQ T&, int); |
| 3148 | // |
| 3149 | // C++ [over.built]p4: |
| 3150 | // |
| 3151 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3152 | // than bool, and VQ is either volatile or empty, there exist |
| 3153 | // candidate operator functions of the form |
| 3154 | // |
| 3155 | // VQ T& operator--(VQ T&); |
| 3156 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3158 | Arith < NumArithmeticTypes; ++Arith) { |
| 3159 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3160 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3161 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Non-volatile version. |
| 3164 | if (NumArgs == 1) |
| 3165 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3166 | else |
| 3167 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3168 | |
| 3169 | // Volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3170 | ParamTypes[0] |
| 3171 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3172 | if (NumArgs == 1) |
| 3173 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3174 | else |
| 3175 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3176 | } |
| 3177 | |
| 3178 | // C++ [over.built]p5: |
| 3179 | // |
| 3180 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3181 | // cv-unqualified object type, and VQ is either volatile or |
| 3182 | // empty, there exist candidate operator functions of the form |
| 3183 | // |
| 3184 | // T*VQ& operator++(T*VQ&); |
| 3185 | // T*VQ& operator--(T*VQ&); |
| 3186 | // T* operator++(T*VQ&, int); |
| 3187 | // T* operator--(T*VQ&, int); |
| 3188 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3189 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3190 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3191 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3192 | continue; |
| 3193 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3194 | QualType ParamTypes[2] = { |
| 3195 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3196 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3198 | // Without volatile |
| 3199 | if (NumArgs == 1) |
| 3200 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3201 | else |
| 3202 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3203 | |
| 3204 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3205 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3206 | ParamTypes[0] |
| 3207 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3208 | if (NumArgs == 1) |
| 3209 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3210 | else |
| 3211 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3212 | } |
| 3213 | } |
| 3214 | break; |
| 3215 | |
| 3216 | UnaryStar: |
| 3217 | // C++ [over.built]p6: |
| 3218 | // For every cv-qualified or cv-unqualified object type T, there |
| 3219 | // exist candidate operator functions of the form |
| 3220 | // |
| 3221 | // T& operator*(T*); |
| 3222 | // |
| 3223 | // C++ [over.built]p7: |
| 3224 | // For every function type T, there exist candidate operator |
| 3225 | // functions of the form |
| 3226 | // T& operator*(T*); |
| 3227 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3228 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3229 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3230 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3232 | &ParamTy, Args, 1, CandidateSet); |
| 3233 | } |
| 3234 | break; |
| 3235 | |
| 3236 | UnaryPlus: |
| 3237 | // C++ [over.built]p8: |
| 3238 | // For every type T, there exist candidate operator functions of |
| 3239 | // the form |
| 3240 | // |
| 3241 | // T* operator+(T*); |
| 3242 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3243 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3244 | QualType ParamTy = *Ptr; |
| 3245 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3248 | // Fall through |
| 3249 | |
| 3250 | UnaryMinus: |
| 3251 | // C++ [over.built]p9: |
| 3252 | // For every promoted arithmetic type T, there exist candidate |
| 3253 | // operator functions of the form |
| 3254 | // |
| 3255 | // T operator+(T); |
| 3256 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3258 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3259 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3260 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3261 | } |
| 3262 | break; |
| 3263 | |
| 3264 | case OO_Tilde: |
| 3265 | // C++ [over.built]p10: |
| 3266 | // For every promoted integral type T, there exist candidate |
| 3267 | // operator functions of the form |
| 3268 | // |
| 3269 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3270 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3271 | Int < LastPromotedIntegralType; ++Int) { |
| 3272 | QualType IntTy = ArithmeticTypes[Int]; |
| 3273 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3274 | } |
| 3275 | break; |
| 3276 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3277 | case OO_New: |
| 3278 | case OO_Delete: |
| 3279 | case OO_Array_New: |
| 3280 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3281 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3282 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3283 | break; |
| 3284 | |
| 3285 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3286 | UnaryAmp: |
| 3287 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3288 | // C++ [over.match.oper]p3: |
| 3289 | // -- For the operator ',', the unary operator '&', or the |
| 3290 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3291 | break; |
| 3292 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3293 | case OO_EqualEqual: |
| 3294 | case OO_ExclaimEqual: |
| 3295 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3296 | // For every pointer to member type T, there exist candidate operator |
| 3297 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3298 | // |
| 3299 | // bool operator==(T,T); |
| 3300 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3302 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3303 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3304 | MemPtr != MemPtrEnd; |
| 3305 | ++MemPtr) { |
| 3306 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3307 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3310 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3312 | case OO_Less: |
| 3313 | case OO_Greater: |
| 3314 | case OO_LessEqual: |
| 3315 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3316 | // C++ [over.built]p15: |
| 3317 | // |
| 3318 | // For every pointer or enumeration type T, there exist |
| 3319 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3321 | // bool operator<(T, T); |
| 3322 | // bool operator>(T, T); |
| 3323 | // bool operator<=(T, T); |
| 3324 | // bool operator>=(T, T); |
| 3325 | // bool operator==(T, T); |
| 3326 | // bool operator!=(T, T); |
| 3327 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3328 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3329 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3330 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3331 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3333 | = CandidateTypes.enumeration_begin(); |
| 3334 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3335 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3336 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3337 | } |
| 3338 | |
| 3339 | // Fall through. |
| 3340 | isComparison = true; |
| 3341 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3342 | BinaryPlus: |
| 3343 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3344 | if (!isComparison) { |
| 3345 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3346 | |
| 3347 | // C++ [over.built]p13: |
| 3348 | // |
| 3349 | // For every cv-qualified or cv-unqualified object type T |
| 3350 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3352 | // T* operator+(T*, ptrdiff_t); |
| 3353 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3354 | // T* operator-(T*, ptrdiff_t); |
| 3355 | // T* operator+(ptrdiff_t, T*); |
| 3356 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3357 | // |
| 3358 | // C++ [over.built]p14: |
| 3359 | // |
| 3360 | // For every T, where T is a pointer to object type, there |
| 3361 | // exist candidate operator functions of the form |
| 3362 | // |
| 3363 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3364 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3365 | = CandidateTypes.pointer_begin(); |
| 3366 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3367 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3368 | |
| 3369 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3370 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3371 | |
| 3372 | if (Op == OO_Plus) { |
| 3373 | // T* operator+(ptrdiff_t, T*); |
| 3374 | ParamTypes[0] = ParamTypes[1]; |
| 3375 | ParamTypes[1] = *Ptr; |
| 3376 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3377 | } else { |
| 3378 | // ptrdiff_t operator-(T, T); |
| 3379 | ParamTypes[1] = *Ptr; |
| 3380 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3381 | Args, 2, CandidateSet); |
| 3382 | } |
| 3383 | } |
| 3384 | } |
| 3385 | // Fall through |
| 3386 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3387 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3388 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3389 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3390 | // C++ [over.built]p12: |
| 3391 | // |
| 3392 | // For every pair of promoted arithmetic types L and R, there |
| 3393 | // exist candidate operator functions of the form |
| 3394 | // |
| 3395 | // LR operator*(L, R); |
| 3396 | // LR operator/(L, R); |
| 3397 | // LR operator+(L, R); |
| 3398 | // LR operator-(L, R); |
| 3399 | // bool operator<(L, R); |
| 3400 | // bool operator>(L, R); |
| 3401 | // bool operator<=(L, R); |
| 3402 | // bool operator>=(L, R); |
| 3403 | // bool operator==(L, R); |
| 3404 | // bool operator!=(L, R); |
| 3405 | // |
| 3406 | // where LR is the result of the usual arithmetic conversions |
| 3407 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3408 | // |
| 3409 | // C++ [over.built]p24: |
| 3410 | // |
| 3411 | // For every pair of promoted arithmetic types L and R, there exist |
| 3412 | // candidate operator functions of the form |
| 3413 | // |
| 3414 | // LR operator?(bool, L, R); |
| 3415 | // |
| 3416 | // where LR is the result of the usual arithmetic conversions |
| 3417 | // between types L and R. |
| 3418 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3420 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3421 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3422 | Right < LastPromotedArithmeticType; ++Right) { |
| 3423 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3424 | QualType Result |
| 3425 | = isComparison |
| 3426 | ? Context.BoolTy |
| 3427 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3428 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3429 | } |
| 3430 | } |
| 3431 | break; |
| 3432 | |
| 3433 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3434 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3435 | case OO_Caret: |
| 3436 | case OO_Pipe: |
| 3437 | case OO_LessLess: |
| 3438 | case OO_GreaterGreater: |
| 3439 | // C++ [over.built]p17: |
| 3440 | // |
| 3441 | // For every pair of promoted integral types L and R, there |
| 3442 | // exist candidate operator functions of the form |
| 3443 | // |
| 3444 | // LR operator%(L, R); |
| 3445 | // LR operator&(L, R); |
| 3446 | // LR operator^(L, R); |
| 3447 | // LR operator|(L, R); |
| 3448 | // L operator<<(L, R); |
| 3449 | // L operator>>(L, R); |
| 3450 | // |
| 3451 | // where LR is the result of the usual arithmetic conversions |
| 3452 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3453 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3454 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3456 | Right < LastPromotedIntegralType; ++Right) { |
| 3457 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3458 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3459 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3460 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3461 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3462 | } |
| 3463 | } |
| 3464 | break; |
| 3465 | |
| 3466 | case OO_Equal: |
| 3467 | // C++ [over.built]p20: |
| 3468 | // |
| 3469 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3470 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3471 | // empty, there exist candidate operator functions of the form |
| 3472 | // |
| 3473 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3474 | for (BuiltinCandidateTypeSet::iterator |
| 3475 | Enum = CandidateTypes.enumeration_begin(), |
| 3476 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3477 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3479 | CandidateSet); |
| 3480 | for (BuiltinCandidateTypeSet::iterator |
| 3481 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3482 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3483 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3484 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3485 | CandidateSet); |
| 3486 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3487 | |
| 3488 | case OO_PlusEqual: |
| 3489 | case OO_MinusEqual: |
| 3490 | // C++ [over.built]p19: |
| 3491 | // |
| 3492 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3493 | // volatile or empty, there exist candidate operator functions |
| 3494 | // of the form |
| 3495 | // |
| 3496 | // T*VQ& operator=(T*VQ&, T*); |
| 3497 | // |
| 3498 | // C++ [over.built]p21: |
| 3499 | // |
| 3500 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3501 | // cv-unqualified object type and VQ is either volatile or |
| 3502 | // empty, there exist candidate operator functions of the form |
| 3503 | // |
| 3504 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3505 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3506 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3507 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3508 | QualType ParamTypes[2]; |
| 3509 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3510 | |
| 3511 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3512 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3513 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3514 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3515 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3516 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3517 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3518 | ParamTypes[0] |
| 3519 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3520 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3521 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3522 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3523 | } |
| 3524 | // Fall through. |
| 3525 | |
| 3526 | case OO_StarEqual: |
| 3527 | case OO_SlashEqual: |
| 3528 | // C++ [over.built]p18: |
| 3529 | // |
| 3530 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3531 | // VQ is either volatile or empty, and R is a promoted |
| 3532 | // arithmetic type, there exist candidate operator functions of |
| 3533 | // the form |
| 3534 | // |
| 3535 | // VQ L& operator=(VQ L&, R); |
| 3536 | // VQ L& operator*=(VQ L&, R); |
| 3537 | // VQ L& operator/=(VQ L&, R); |
| 3538 | // VQ L& operator+=(VQ L&, R); |
| 3539 | // VQ L& operator-=(VQ L&, R); |
| 3540 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3542 | Right < LastPromotedArithmeticType; ++Right) { |
| 3543 | QualType ParamTypes[2]; |
| 3544 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3545 | |
| 3546 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3547 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3548 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3549 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3550 | |
| 3551 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3552 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3553 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3554 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3555 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | break; |
| 3559 | |
| 3560 | case OO_PercentEqual: |
| 3561 | case OO_LessLessEqual: |
| 3562 | case OO_GreaterGreaterEqual: |
| 3563 | case OO_AmpEqual: |
| 3564 | case OO_CaretEqual: |
| 3565 | case OO_PipeEqual: |
| 3566 | // C++ [over.built]p22: |
| 3567 | // |
| 3568 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3569 | // is either volatile or empty, and R is a promoted integral |
| 3570 | // type, there exist candidate operator functions of the form |
| 3571 | // |
| 3572 | // VQ L& operator%=(VQ L&, R); |
| 3573 | // VQ L& operator<<=(VQ L&, R); |
| 3574 | // VQ L& operator>>=(VQ L&, R); |
| 3575 | // VQ L& operator&=(VQ L&, R); |
| 3576 | // VQ L& operator^=(VQ L&, R); |
| 3577 | // VQ L& operator|=(VQ L&, R); |
| 3578 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3580 | Right < LastPromotedIntegralType; ++Right) { |
| 3581 | QualType ParamTypes[2]; |
| 3582 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3583 | |
| 3584 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3585 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3586 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3587 | |
| 3588 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3589 | ParamTypes[0] = ArithmeticTypes[Left]; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3590 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3591 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3592 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3593 | } |
| 3594 | } |
| 3595 | break; |
| 3596 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3597 | case OO_Exclaim: { |
| 3598 | // C++ [over.operator]p23: |
| 3599 | // |
| 3600 | // There also exist candidate operator functions of the form |
| 3601 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3602 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3603 | // bool operator&&(bool, bool); [BELOW] |
| 3604 | // bool operator||(bool, bool); [BELOW] |
| 3605 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3606 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3607 | /*IsAssignmentOperator=*/false, |
| 3608 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3609 | break; |
| 3610 | } |
| 3611 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3612 | case OO_AmpAmp: |
| 3613 | case OO_PipePipe: { |
| 3614 | // C++ [over.operator]p23: |
| 3615 | // |
| 3616 | // There also exist candidate operator functions of the form |
| 3617 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3618 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3619 | // bool operator&&(bool, bool); |
| 3620 | // bool operator||(bool, bool); |
| 3621 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3622 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3623 | /*IsAssignmentOperator=*/false, |
| 3624 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3625 | break; |
| 3626 | } |
| 3627 | |
| 3628 | case OO_Subscript: |
| 3629 | // C++ [over.built]p13: |
| 3630 | // |
| 3631 | // For every cv-qualified or cv-unqualified object type T there |
| 3632 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3634 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3635 | // T& operator[](T*, ptrdiff_t); |
| 3636 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3637 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3638 | // T& operator[](ptrdiff_t, T*); |
| 3639 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3640 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3641 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3642 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3643 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3644 | |
| 3645 | // T& operator[](T*, ptrdiff_t) |
| 3646 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3647 | |
| 3648 | // T& operator[](ptrdiff_t, T*); |
| 3649 | ParamTypes[0] = ParamTypes[1]; |
| 3650 | ParamTypes[1] = *Ptr; |
| 3651 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3652 | } |
| 3653 | break; |
| 3654 | |
| 3655 | case OO_ArrowStar: |
| 3656 | // FIXME: No support for pointer-to-members yet. |
| 3657 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3658 | |
| 3659 | case OO_Conditional: |
| 3660 | // Note that we don't consider the first argument, since it has been |
| 3661 | // contextually converted to bool long ago. The candidates below are |
| 3662 | // therefore added as binary. |
| 3663 | // |
| 3664 | // C++ [over.built]p24: |
| 3665 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3666 | // there exist candidate operator functions of the form |
| 3667 | // |
| 3668 | // T operator?(bool, T, T); |
| 3669 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3670 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3671 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3672 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3673 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3674 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3675 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3676 | CandidateTypes.member_pointer_begin(), |
| 3677 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3678 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3679 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3680 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3681 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3685 | /// \brief Add function candidates found via argument-dependent lookup |
| 3686 | /// to the set of overloading candidates. |
| 3687 | /// |
| 3688 | /// This routine performs argument-dependent name lookup based on the |
| 3689 | /// given function name (which may also be an operator name) and adds |
| 3690 | /// all of the overload candidates found by ADL to the overload |
| 3691 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3693 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3694 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3695 | bool HasExplicitTemplateArgs, |
| 3696 | const TemplateArgument *ExplicitTemplateArgs, |
| 3697 | unsigned NumExplicitTemplateArgs, |
| 3698 | OverloadCandidateSet& CandidateSet, |
| 3699 | bool PartialOverloading) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3700 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3701 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3702 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 3703 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3704 | // Record all of the function candidates that we've already |
| 3705 | // added to the overload set, so that we don't add those same |
| 3706 | // candidates a second time. |
| 3707 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3708 | CandEnd = CandidateSet.end(); |
| 3709 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3710 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3711 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3712 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3713 | Functions.insert(FunTmpl); |
| 3714 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3715 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3716 | // FIXME: Pass in the explicit template arguments? |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3717 | ArgumentDependentLookup(Name, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3718 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3719 | // Erase all of the candidates we already knew about. |
| 3720 | // FIXME: This is suboptimal. Is there a better way? |
| 3721 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3722 | CandEnd = CandidateSet.end(); |
| 3723 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3724 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3725 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3726 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3727 | Functions.erase(FunTmpl); |
| 3728 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3729 | |
| 3730 | // For each of the ADL candidates we found, add it to the overload |
| 3731 | // set. |
| 3732 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3733 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3734 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3735 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
| 3736 | if (HasExplicitTemplateArgs) |
| 3737 | continue; |
| 3738 | |
| 3739 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 3740 | false, false, PartialOverloading); |
| 3741 | } else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3743 | HasExplicitTemplateArgs, |
| 3744 | ExplicitTemplateArgs, |
| 3745 | NumExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3746 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3747 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3750 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3751 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3753 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3755 | // Define viable functions to be better candidates than non-viable |
| 3756 | // functions. |
| 3757 | if (!Cand2.Viable) |
| 3758 | return Cand1.Viable; |
| 3759 | else if (!Cand1.Viable) |
| 3760 | return false; |
| 3761 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3762 | // C++ [over.match.best]p1: |
| 3763 | // |
| 3764 | // -- if F is a static member function, ICS1(F) is defined such |
| 3765 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3766 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3767 | // better nor worse than ICS1(F). |
| 3768 | unsigned StartArg = 0; |
| 3769 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3770 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3771 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3772 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | // A viable function F1 is defined to be a better function than another |
| 3774 | // 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] | 3775 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3776 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3777 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3778 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3779 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3780 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3781 | Cand2.Conversions[ArgIdx])) { |
| 3782 | case ImplicitConversionSequence::Better: |
| 3783 | // Cand1 has a better conversion sequence. |
| 3784 | HasBetterConversion = true; |
| 3785 | break; |
| 3786 | |
| 3787 | case ImplicitConversionSequence::Worse: |
| 3788 | // Cand1 can't be better than Cand2. |
| 3789 | return false; |
| 3790 | |
| 3791 | case ImplicitConversionSequence::Indistinguishable: |
| 3792 | // Do nothing. |
| 3793 | break; |
| 3794 | } |
| 3795 | } |
| 3796 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3798 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3799 | if (HasBetterConversion) |
| 3800 | return true; |
| 3801 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3803 | // specialization, or, if not that, |
| 3804 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 3805 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 3806 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3807 | |
| 3808 | // -- F1 and F2 are function template specializations, and the function |
| 3809 | // template for F1 is more specialized than the template for F2 |
| 3810 | // 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] | 3811 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 3812 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 3813 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3814 | if (FunctionTemplateDecl *BetterTemplate |
| 3815 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 3816 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 3817 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 3818 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3819 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3820 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3821 | // -- the context is an initialization by user-defined conversion |
| 3822 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 3823 | // from the return type of F1 to the destination type (i.e., |
| 3824 | // the type of the entity being initialized) is a better |
| 3825 | // conversion sequence than the standard conversion sequence |
| 3826 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | if (Cand1.Function && Cand2.Function && |
| 3828 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3829 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 3830 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 3831 | Cand2.FinalConversion)) { |
| 3832 | case ImplicitConversionSequence::Better: |
| 3833 | // Cand1 has a better conversion sequence. |
| 3834 | return true; |
| 3835 | |
| 3836 | case ImplicitConversionSequence::Worse: |
| 3837 | // Cand1 can't be better than Cand2. |
| 3838 | return false; |
| 3839 | |
| 3840 | case ImplicitConversionSequence::Indistinguishable: |
| 3841 | // Do nothing |
| 3842 | break; |
| 3843 | } |
| 3844 | } |
| 3845 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3846 | return false; |
| 3847 | } |
| 3848 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3850 | /// within an overload candidate set. |
| 3851 | /// |
| 3852 | /// \param CandidateSet the set of candidate functions. |
| 3853 | /// |
| 3854 | /// \param Loc the location of the function name (or operator symbol) for |
| 3855 | /// which overload resolution occurs. |
| 3856 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3858 | /// function, Best points to the candidate function found. |
| 3859 | /// |
| 3860 | /// \returns The result of overload resolution. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | Sema::OverloadingResult |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3862 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3863 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3864 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3865 | // Find the best viable function. |
| 3866 | Best = CandidateSet.end(); |
| 3867 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3868 | Cand != CandidateSet.end(); ++Cand) { |
| 3869 | if (Cand->Viable) { |
| 3870 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 3871 | Best = Cand; |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | // If we didn't find any viable functions, abort. |
| 3876 | if (Best == CandidateSet.end()) |
| 3877 | return OR_No_Viable_Function; |
| 3878 | |
| 3879 | // Make sure that this function is better than every other viable |
| 3880 | // function. If not, we have an ambiguity. |
| 3881 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3882 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3884 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3885 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 3886 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3887 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3888 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3891 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3892 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3894 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3895 | return OR_Deleted; |
| 3896 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3897 | // C++ [basic.def.odr]p2: |
| 3898 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | // when referred to from a potentially-evaluated expression. [Note: this |
| 3900 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3901 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 3902 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 3903 | if (Best->Function) |
| 3904 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3905 | return OR_Success; |
| 3906 | } |
| 3907 | |
| 3908 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 3909 | /// diagnostic messages containing the candidates in the candidate |
| 3910 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3912 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | bool OnlyViable) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3914 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3915 | LastCand = CandidateSet.end(); |
| 3916 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3917 | if (Cand->Viable || !OnlyViable) { |
| 3918 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3919 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3920 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3921 | // Deleted or "unavailable" function. |
| 3922 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 3923 | << Cand->Function->isDeleted(); |
Douglas Gregor | 1fdd89b | 2009-09-15 20:11:42 +0000 | [diff] [blame] | 3924 | } else if (FunctionTemplateDecl *FunTmpl |
| 3925 | = Cand->Function->getPrimaryTemplate()) { |
| 3926 | // Function template specialization |
| 3927 | // FIXME: Give a better reason! |
| 3928 | Diag(Cand->Function->getLocation(), diag::err_ovl_template_candidate) |
| 3929 | << getTemplateArgumentBindingsText(FunTmpl->getTemplateParameters(), |
| 3930 | *Cand->Function->getTemplateSpecializationArgs()); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3931 | } else { |
| 3932 | // Normal function |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 3933 | bool errReported = false; |
| 3934 | if (!Cand->Viable && Cand->Conversions.size() > 0) { |
| 3935 | for (int i = Cand->Conversions.size()-1; i >= 0; i--) { |
| 3936 | const ImplicitConversionSequence &Conversion = |
| 3937 | Cand->Conversions[i]; |
| 3938 | if ((Conversion.ConversionKind != |
| 3939 | ImplicitConversionSequence::BadConversion) || |
| 3940 | Conversion.ConversionFunctionSet.size() == 0) |
| 3941 | continue; |
| 3942 | Diag(Cand->Function->getLocation(), |
| 3943 | diag::err_ovl_candidate_not_viable) << (i+1); |
| 3944 | errReported = true; |
| 3945 | for (int j = Conversion.ConversionFunctionSet.size()-1; |
| 3946 | j >= 0; j--) { |
| 3947 | FunctionDecl *Func = Conversion.ConversionFunctionSet[j]; |
| 3948 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 3949 | } |
| 3950 | } |
| 3951 | } |
| 3952 | if (!errReported) |
| 3953 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3954 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3955 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3956 | // Desugar the type of the surrogate down to a function type, |
| 3957 | // retaining as many typedefs as possible while still showing |
| 3958 | // the function type (and, therefore, its parameter types). |
| 3959 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3960 | bool isLValueReference = false; |
| 3961 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3962 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3963 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3964 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3965 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3966 | isLValueReference = true; |
| 3967 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3968 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3969 | FnType = FnTypeRef->getPointeeType(); |
| 3970 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3971 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3972 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3973 | FnType = FnTypePtr->getPointeeType(); |
| 3974 | isPointer = true; |
| 3975 | } |
| 3976 | // Desugar down to a function type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3977 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3978 | // Reconstruct the pointer/reference as appropriate. |
| 3979 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3980 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 3981 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3982 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3983 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3984 | << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3985 | } else { |
| 3986 | // FIXME: We need to get the identifier in here |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3987 | // FIXME: Do we want the error message to point at the operator? |
| 3988 | // (built-ins won't have a location) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | QualType FnType |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3990 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 3991 | Cand->BuiltinTypes.ParamTypes, |
| 3992 | Cand->Conversions.size(), |
| 3993 | false, 0); |
| 3994 | |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3995 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3996 | } |
| 3997 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4001 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4002 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4003 | /// expression with overloaded function type and @p ToType is the type |
| 4004 | /// we're trying to resolve to. For example: |
| 4005 | /// |
| 4006 | /// @code |
| 4007 | /// int f(double); |
| 4008 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4009 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4010 | /// int (*pfd)(double) = f; // selects f(double) |
| 4011 | /// @endcode |
| 4012 | /// |
| 4013 | /// This routine returns the resulting FunctionDecl if it could be |
| 4014 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4015 | /// routine will emit diagnostics if there is an error. |
| 4016 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4017 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4018 | bool Complain) { |
| 4019 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4020 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4021 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4022 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4023 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4024 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4025 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4026 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4027 | FunctionType = MemTypePtr->getPointeeType(); |
| 4028 | IsMember = true; |
| 4029 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4030 | |
| 4031 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4032 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4033 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4034 | return 0; |
| 4035 | |
| 4036 | // Find the actual overloaded function declaration. |
| 4037 | OverloadedFunctionDecl *Ovl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4039 | // C++ [over.over]p1: |
| 4040 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4041 | // overloaded function name is ignored (5.1). ] |
| 4042 | Expr *OvlExpr = From->IgnoreParens(); |
| 4043 | |
| 4044 | // C++ [over.over]p1: |
| 4045 | // [...] The overloaded function name can be preceded by the & |
| 4046 | // operator. |
| 4047 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4048 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4049 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4050 | } |
| 4051 | |
| 4052 | // Try to dig out the overloaded function. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4053 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 4054 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4055 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4056 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
| 4057 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4058 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | // If there's no overloaded function declaration or function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4060 | // we're done. |
| 4061 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4062 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4064 | OverloadIterator Fun; |
| 4065 | if (Ovl) |
| 4066 | Fun = Ovl; |
| 4067 | else |
| 4068 | Fun = FunctionTemplate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4070 | // Look through all of the overloaded functions, searching for one |
| 4071 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4072 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4073 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4074 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4075 | // C++ [over.over]p3: |
| 4076 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4077 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4078 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4079 | // type "pointer-to-member-function." |
| 4080 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4081 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4082 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4083 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4085 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4086 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4087 | // static when converting to member pointer. |
| 4088 | if (Method->isStatic() == IsMember) |
| 4089 | continue; |
| 4090 | } else if (IsMember) |
| 4091 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4092 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4093 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | // If the name is a function template, template argument deduction is |
| 4095 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4096 | // resulting template argument list is used to generate a single |
| 4097 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4098 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4099 | // 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] | 4100 | FunctionDecl *Specialization = 0; |
| 4101 | TemplateDeductionInfo Info(Context); |
| 4102 | if (TemplateDeductionResult Result |
| 4103 | = DeduceTemplateArguments(FunctionTemplate, /*FIXME*/false, |
| 4104 | /*FIXME:*/0, /*FIXME:*/0, |
| 4105 | FunctionType, Specialization, Info)) { |
| 4106 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4107 | (void)Result; |
| 4108 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4109 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4110 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4112 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4113 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4114 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4115 | } |
| 4116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4118 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 4119 | // Skip non-static functions when converting to pointer, and static |
| 4120 | // when converting to member pointer. |
| 4121 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4122 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4123 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4124 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4125 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4126 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4127 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4128 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4129 | FoundNonTemplateFunction = true; |
| 4130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4131 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4134 | // If there were 0 or 1 matches, we're done. |
| 4135 | if (Matches.empty()) |
| 4136 | return 0; |
| 4137 | else if (Matches.size() == 1) |
| 4138 | return *Matches.begin(); |
| 4139 | |
| 4140 | // C++ [over.over]p4: |
| 4141 | // If more than one function is selected, [...] |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4142 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4143 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4144 | // [...] and any given function template specialization F1 is |
| 4145 | // eliminated if the set contains a second function template |
| 4146 | // specialization whose function template is more specialized |
| 4147 | // than the function template of F1 according to the partial |
| 4148 | // ordering rules of 14.5.5.2. |
| 4149 | |
| 4150 | // The algorithm specified above is quadratic. We instead use a |
| 4151 | // two-pass algorithm (similar to the one used to identify the |
| 4152 | // best viable function in an overload set) that identifies the |
| 4153 | // best function template (if it exists). |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4154 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
| 4155 | Matches.end()); |
| 4156 | return getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4157 | TPOC_Other, From->getLocStart(), |
| 4158 | PartialDiagnostic(0), |
| 4159 | PartialDiagnostic(diag::err_addr_ovl_ambiguous) |
| 4160 | << TemplateMatches[0]->getDeclName(), |
| 4161 | PartialDiagnostic(diag::err_ovl_template_candidate)); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4164 | // [...] any function template specializations in the set are |
| 4165 | // eliminated if the set also contains a non-template function, [...] |
| 4166 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4167 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4168 | if ((*M)->getPrimaryTemplate() == 0) |
| 4169 | RemainingMatches.push_back(*M); |
| 4170 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4172 | // selected function. |
| 4173 | if (RemainingMatches.size() == 1) |
| 4174 | return RemainingMatches.front(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4175 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4176 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4177 | // returns (even if we issue the diagnostics here). |
| 4178 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4179 | << RemainingMatches[0]->getDeclName(); |
| 4180 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 4181 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4182 | return 0; |
| 4183 | } |
| 4184 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4185 | /// \brief Add a single candidate to the overload set. |
| 4186 | static void AddOverloadedCallCandidate(Sema &S, |
| 4187 | AnyFunctionDecl Callee, |
| 4188 | bool &ArgumentDependentLookup, |
| 4189 | bool HasExplicitTemplateArgs, |
| 4190 | const TemplateArgument *ExplicitTemplateArgs, |
| 4191 | unsigned NumExplicitTemplateArgs, |
| 4192 | Expr **Args, unsigned NumArgs, |
| 4193 | OverloadCandidateSet &CandidateSet, |
| 4194 | bool PartialOverloading) { |
| 4195 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
| 4196 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
| 4197 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 4198 | PartialOverloading); |
| 4199 | |
| 4200 | if (Func->getDeclContext()->isRecord() || |
| 4201 | Func->getDeclContext()->isFunctionOrMethod()) |
| 4202 | ArgumentDependentLookup = false; |
| 4203 | return; |
| 4204 | } |
| 4205 | |
| 4206 | FunctionTemplateDecl *FuncTemplate = cast<FunctionTemplateDecl>(Callee); |
| 4207 | S.AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 4208 | ExplicitTemplateArgs, |
| 4209 | NumExplicitTemplateArgs, |
| 4210 | Args, NumArgs, CandidateSet); |
| 4211 | |
| 4212 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 4213 | ArgumentDependentLookup = false; |
| 4214 | } |
| 4215 | |
| 4216 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 4217 | /// dependent lookup to the given overload set. |
| 4218 | void Sema::AddOverloadedCallCandidates(NamedDecl *Callee, |
| 4219 | DeclarationName &UnqualifiedName, |
| 4220 | bool &ArgumentDependentLookup, |
| 4221 | bool HasExplicitTemplateArgs, |
| 4222 | const TemplateArgument *ExplicitTemplateArgs, |
| 4223 | unsigned NumExplicitTemplateArgs, |
| 4224 | Expr **Args, unsigned NumArgs, |
| 4225 | OverloadCandidateSet &CandidateSet, |
| 4226 | bool PartialOverloading) { |
| 4227 | // Add the functions denoted by Callee to the set of candidate |
| 4228 | // functions. While we're doing so, track whether argument-dependent |
| 4229 | // lookup still applies, per: |
| 4230 | // |
| 4231 | // C++0x [basic.lookup.argdep]p3: |
| 4232 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4233 | // and let Y be the lookup set produced by argument dependent |
| 4234 | // lookup (defined as follows). If X contains |
| 4235 | // |
| 4236 | // -- a declaration of a class member, or |
| 4237 | // |
| 4238 | // -- a block-scope function declaration that is not a |
| 4239 | // using-declaration (FIXME: check for using declaration), or |
| 4240 | // |
| 4241 | // -- a declaration that is neither a function or a function |
| 4242 | // template |
| 4243 | // |
| 4244 | // then Y is empty. |
| 4245 | if (!Callee) { |
| 4246 | // Nothing to do. |
| 4247 | } else if (OverloadedFunctionDecl *Ovl |
| 4248 | = dyn_cast<OverloadedFunctionDecl>(Callee)) { |
| 4249 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4250 | FuncEnd = Ovl->function_end(); |
| 4251 | Func != FuncEnd; ++Func) |
| 4252 | AddOverloadedCallCandidate(*this, *Func, ArgumentDependentLookup, |
| 4253 | HasExplicitTemplateArgs, |
| 4254 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4255 | Args, NumArgs, CandidateSet, |
| 4256 | PartialOverloading); |
| 4257 | } else if (isa<FunctionDecl>(Callee) || isa<FunctionTemplateDecl>(Callee)) |
| 4258 | AddOverloadedCallCandidate(*this, |
| 4259 | AnyFunctionDecl::getFromNamedDecl(Callee), |
| 4260 | ArgumentDependentLookup, |
| 4261 | HasExplicitTemplateArgs, |
| 4262 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 4263 | Args, NumArgs, CandidateSet, |
| 4264 | PartialOverloading); |
| 4265 | // FIXME: assert isa<FunctionDecl> || isa<FunctionTemplateDecl> rather than |
| 4266 | // checking dynamically. |
| 4267 | |
| 4268 | if (Callee) |
| 4269 | UnqualifiedName = Callee->getDeclName(); |
| 4270 | |
| 4271 | if (ArgumentDependentLookup) |
| 4272 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
| 4273 | HasExplicitTemplateArgs, |
| 4274 | ExplicitTemplateArgs, |
| 4275 | NumExplicitTemplateArgs, |
| 4276 | CandidateSet, |
| 4277 | PartialOverloading); |
| 4278 | } |
| 4279 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4280 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4281 | /// (which eventually refers to the declaration Func) and the call |
| 4282 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4283 | /// to a specific function. If overload resolution succeeds, returns |
| 4284 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4285 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4286 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4287 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4288 | DeclarationName UnqualifiedName, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4289 | bool HasExplicitTemplateArgs, |
| 4290 | const TemplateArgument *ExplicitTemplateArgs, |
| 4291 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4292 | SourceLocation LParenLoc, |
| 4293 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | SourceLocation *CommaLocs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4295 | SourceLocation RParenLoc, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4296 | bool &ArgumentDependentLookup) { |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4297 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4298 | |
| 4299 | // Add the functions denoted by Callee to the set of candidate |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4300 | // functions. |
| 4301 | AddOverloadedCallCandidates(Callee, UnqualifiedName, ArgumentDependentLookup, |
| 4302 | HasExplicitTemplateArgs, ExplicitTemplateArgs, |
| 4303 | NumExplicitTemplateArgs, Args, NumArgs, |
| 4304 | CandidateSet); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4305 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4306 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4307 | case OR_Success: |
| 4308 | return Best->Function; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4309 | |
| 4310 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4311 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4312 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4313 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4314 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4315 | break; |
| 4316 | |
| 4317 | case OR_Ambiguous: |
| 4318 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4319 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4320 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4321 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4322 | |
| 4323 | case OR_Deleted: |
| 4324 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4325 | << Best->Function->isDeleted() |
| 4326 | << UnqualifiedName |
| 4327 | << Fn->getSourceRange(); |
| 4328 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4329 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4333 | // return NULL. |
| 4334 | Fn->Destroy(Context); |
| 4335 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4336 | Args[Arg]->Destroy(Context); |
| 4337 | return 0; |
| 4338 | } |
| 4339 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4340 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4341 | /// operator. |
| 4342 | /// |
| 4343 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4344 | /// |
| 4345 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4346 | /// operator. |
| 4347 | /// |
| 4348 | /// \param Functions The set of non-member functions that will be |
| 4349 | /// considered by overload resolution. The caller needs to build this |
| 4350 | /// set based on the context using, e.g., |
| 4351 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4352 | /// set should not contain any member functions; those will be added |
| 4353 | /// by CreateOverloadedUnaryOp(). |
| 4354 | /// |
| 4355 | /// \param input The input argument. |
| 4356 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4357 | unsigned OpcIn, |
| 4358 | FunctionSet &Functions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4360 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4361 | Expr *Input = (Expr *)input.get(); |
| 4362 | |
| 4363 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 4364 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 4365 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4366 | |
| 4367 | Expr *Args[2] = { Input, 0 }; |
| 4368 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4370 | // For post-increment and post-decrement, add the implicit '0' as |
| 4371 | // the second argument, so that we know this is a post-increment or |
| 4372 | // post-decrement. |
| 4373 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 4374 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4375 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4376 | SourceLocation()); |
| 4377 | NumArgs = 2; |
| 4378 | } |
| 4379 | |
| 4380 | if (Input->isTypeDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4382 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4384 | FuncEnd = Functions.end(); |
| 4385 | Func != FuncEnd; ++Func) |
| 4386 | Overloads->addOverload(*Func); |
| 4387 | |
| 4388 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4389 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4390 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4391 | input.release(); |
| 4392 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4393 | &Args[0], NumArgs, |
| 4394 | Context.DependentTy, |
| 4395 | OpLoc)); |
| 4396 | } |
| 4397 | |
| 4398 | // Build an empty overload set. |
| 4399 | OverloadCandidateSet CandidateSet; |
| 4400 | |
| 4401 | // Add the candidates from the given function set. |
| 4402 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4403 | |
| 4404 | // Add operator candidates that are member functions. |
| 4405 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4406 | |
| 4407 | // Add builtin operator candidates. |
| 4408 | AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet); |
| 4409 | |
| 4410 | // Perform overload resolution. |
| 4411 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4412 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4413 | case OR_Success: { |
| 4414 | // We found a built-in operator or an overloaded operator. |
| 4415 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4417 | if (FnDecl) { |
| 4418 | // We matched an overloaded operator. Build a call to that |
| 4419 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4421 | // Convert the arguments. |
| 4422 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4423 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4424 | return ExprError(); |
| 4425 | } else { |
| 4426 | // Convert the arguments. |
| 4427 | if (PerformCopyInitialization(Input, |
| 4428 | FnDecl->getParamDecl(0)->getType(), |
| 4429 | "passing")) |
| 4430 | return ExprError(); |
| 4431 | } |
| 4432 | |
| 4433 | // Determine the result type |
| 4434 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4435 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4436 | ResultTy = ResultTy.getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4437 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4438 | // Build the actual expression node. |
| 4439 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4440 | SourceLocation()); |
| 4441 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4442 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4443 | input.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | |
| 4445 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4446 | &Input, 1, ResultTy, OpLoc); |
| 4447 | return MaybeBindToTemporary(CE); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4448 | } else { |
| 4449 | // We matched a built-in operator. Convert the arguments, then |
| 4450 | // break out so that we will build the appropriate built-in |
| 4451 | // operator node. |
| 4452 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4453 | Best->Conversions[0], "passing")) |
| 4454 | return ExprError(); |
| 4455 | |
| 4456 | break; |
| 4457 | } |
| 4458 | } |
| 4459 | |
| 4460 | case OR_No_Viable_Function: |
| 4461 | // No viable function; fall through to handling this as a |
| 4462 | // built-in operator, which will produce an error message for us. |
| 4463 | break; |
| 4464 | |
| 4465 | case OR_Ambiguous: |
| 4466 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4467 | << UnaryOperator::getOpcodeStr(Opc) |
| 4468 | << Input->getSourceRange(); |
| 4469 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4470 | return ExprError(); |
| 4471 | |
| 4472 | case OR_Deleted: |
| 4473 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4474 | << Best->Function->isDeleted() |
| 4475 | << UnaryOperator::getOpcodeStr(Opc) |
| 4476 | << Input->getSourceRange(); |
| 4477 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4478 | return ExprError(); |
| 4479 | } |
| 4480 | |
| 4481 | // Either we found no viable overloaded operator or we matched a |
| 4482 | // built-in operator. In either case, fall through to trying to |
| 4483 | // build a built-in operation. |
| 4484 | input.release(); |
| 4485 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4486 | } |
| 4487 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4488 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4489 | /// operator. |
| 4490 | /// |
| 4491 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4492 | /// |
| 4493 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4494 | /// operator. |
| 4495 | /// |
| 4496 | /// \param Functions The set of non-member functions that will be |
| 4497 | /// considered by overload resolution. The caller needs to build this |
| 4498 | /// set based on the context using, e.g., |
| 4499 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4500 | /// set should not contain any member functions; those will be added |
| 4501 | /// by CreateOverloadedBinOp(). |
| 4502 | /// |
| 4503 | /// \param LHS Left-hand argument. |
| 4504 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4505 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4506 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | unsigned OpcIn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4508 | FunctionSet &Functions, |
| 4509 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4510 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4511 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4512 | |
| 4513 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4514 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4515 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4516 | |
| 4517 | // If either side is type-dependent, create an appropriate dependent |
| 4518 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4519 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4520 | // .* cannot be overloaded. |
| 4521 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4522 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4523 | Context.DependentTy, OpLoc)); |
| 4524 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4525 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4526 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4527 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4528 | FuncEnd = Functions.end(); |
| 4529 | Func != FuncEnd; ++Func) |
| 4530 | Overloads->addOverload(*Func); |
| 4531 | |
| 4532 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4533 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4534 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4535 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4536 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4537 | Context.DependentTy, |
| 4538 | OpLoc)); |
| 4539 | } |
| 4540 | |
| 4541 | // If this is the .* operator, which is not overloadable, just |
| 4542 | // create a built-in binary operator. |
| 4543 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4544 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4545 | |
| 4546 | // If this is one of the assignment operators, we only perform |
| 4547 | // overload resolution if the left-hand side is a class or |
| 4548 | // enumeration type (C++ [expr.ass]p3). |
| 4549 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4550 | !Args[0]->getType()->isOverloadableType()) |
| 4551 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4552 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4553 | // Build an empty overload set. |
| 4554 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4555 | |
| 4556 | // Add the candidates from the given function set. |
| 4557 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4558 | |
| 4559 | // Add operator candidates that are member functions. |
| 4560 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4561 | |
| 4562 | // Add builtin operator candidates. |
| 4563 | AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet); |
| 4564 | |
| 4565 | // Perform overload resolution. |
| 4566 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4567 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4568 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4569 | // We found a built-in operator or an overloaded operator. |
| 4570 | FunctionDecl *FnDecl = Best->Function; |
| 4571 | |
| 4572 | if (FnDecl) { |
| 4573 | // We matched an overloaded operator. Build a call to that |
| 4574 | // operator. |
| 4575 | |
| 4576 | // Convert the arguments. |
| 4577 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4578 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 4579 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4580 | "passing")) |
| 4581 | return ExprError(); |
| 4582 | } else { |
| 4583 | // Convert the arguments. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4584 | if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4585 | "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4586 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4587 | "passing")) |
| 4588 | return ExprError(); |
| 4589 | } |
| 4590 | |
| 4591 | // Determine the result type |
| 4592 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4593 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4594 | ResultTy = ResultTy.getNonReferenceType(); |
| 4595 | |
| 4596 | // Build the actual expression node. |
| 4597 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4598 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4599 | UsualUnaryConversions(FnExpr); |
| 4600 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4601 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4602 | Args, 2, ResultTy, OpLoc); |
| 4603 | return MaybeBindToTemporary(CE); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4604 | } else { |
| 4605 | // We matched a built-in operator. Convert the arguments, then |
| 4606 | // break out so that we will build the appropriate built-in |
| 4607 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4608 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4609 | Best->Conversions[0], "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4610 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4611 | Best->Conversions[1], "passing")) |
| 4612 | return ExprError(); |
| 4613 | |
| 4614 | break; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | case OR_No_Viable_Function: |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4619 | // For class as left operand for assignment or compound assigment operator |
| 4620 | // do not fall through to handling in built-in, but report that no overloaded |
| 4621 | // assignment operator found |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4622 | if (Args[0]->getType()->isRecordType() && Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4623 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4624 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4625 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4626 | return ExprError(); |
| 4627 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4628 | // No viable function; fall through to handling this as a |
| 4629 | // built-in operator, which will produce an error message for us. |
| 4630 | break; |
| 4631 | |
| 4632 | case OR_Ambiguous: |
| 4633 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4634 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4635 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4636 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4637 | return ExprError(); |
| 4638 | |
| 4639 | case OR_Deleted: |
| 4640 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4641 | << Best->Function->isDeleted() |
| 4642 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4643 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4644 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4645 | return ExprError(); |
| 4646 | } |
| 4647 | |
| 4648 | // Either we found no viable overloaded operator or we matched a |
| 4649 | // built-in operator. In either case, try to build a built-in |
| 4650 | // operation. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4651 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4654 | /// BuildCallToMemberFunction - Build a call to a member |
| 4655 | /// function. MemExpr is the expression that refers to the member |
| 4656 | /// function (and includes the object parameter), Args/NumArgs are the |
| 4657 | /// arguments to the function call (not including the object |
| 4658 | /// parameter). The caller needs to validate that the member |
| 4659 | /// expression refers to a member function or an overloaded member |
| 4660 | /// function. |
| 4661 | Sema::ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 4663 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4664 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 4665 | SourceLocation RParenLoc) { |
| 4666 | // Dig out the member expression. This holds both the object |
| 4667 | // argument and the member function we're referring to. |
| 4668 | MemberExpr *MemExpr = 0; |
| 4669 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 4670 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 4671 | else |
| 4672 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 4673 | assert(MemExpr && "Building member call without member expression"); |
| 4674 | |
| 4675 | // Extract the object argument. |
| 4676 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4677 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4678 | CXXMethodDecl *Method = 0; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4679 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 4680 | isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4681 | // Add overload candidates |
| 4682 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4683 | DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4685 | for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd; |
| 4686 | Func != FuncEnd; ++Func) { |
| 4687 | if ((Method = dyn_cast<CXXMethodDecl>(*Func))) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4688 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4689 | /*SuppressUserConversions=*/false); |
| 4690 | else |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 4691 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func), |
| 4692 | MemExpr->hasExplicitTemplateArgumentList(), |
| 4693 | MemExpr->getTemplateArgs(), |
| 4694 | MemExpr->getNumTemplateArgs(), |
| 4695 | ObjectArg, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4696 | CandidateSet, |
| 4697 | /*SuppressUsedConversions=*/false); |
| 4698 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4699 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4700 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4701 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4702 | case OR_Success: |
| 4703 | Method = cast<CXXMethodDecl>(Best->Function); |
| 4704 | break; |
| 4705 | |
| 4706 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4708 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4709 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4710 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4711 | // FIXME: Leaking incoming expressions! |
| 4712 | return true; |
| 4713 | |
| 4714 | case OR_Ambiguous: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4716 | diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4717 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4718 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4719 | // FIXME: Leaking incoming expressions! |
| 4720 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4721 | |
| 4722 | case OR_Deleted: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4723 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4724 | diag::err_ovl_deleted_member_call) |
| 4725 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4726 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4727 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4728 | // FIXME: Leaking incoming expressions! |
| 4729 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | FixOverloadedFunctionReference(MemExpr, Method); |
| 4733 | } else { |
| 4734 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 4735 | } |
| 4736 | |
| 4737 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4738 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4739 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4741 | Method->getResultType().getNonReferenceType(), |
| 4742 | RParenLoc)); |
| 4743 | |
| 4744 | // Convert the object argument (for a non-static member function call). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | if (!Method->isStatic() && |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4746 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 4747 | return true; |
| 4748 | MemExpr->setBase(ObjectArg); |
| 4749 | |
| 4750 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4751 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4752 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4753 | RParenLoc)) |
| 4754 | return true; |
| 4755 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4756 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4757 | return true; |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 4758 | |
| 4759 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4762 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 4763 | /// type (C++ [over.call.object]), which can end up invoking an |
| 4764 | /// overloaded function call operator (@c operator()) or performing a |
| 4765 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4766 | Sema::ExprResult |
| 4767 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 4768 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4769 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4771 | SourceLocation RParenLoc) { |
| 4772 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4773 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4774 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4775 | // C++ [over.call.object]p1: |
| 4776 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4777 | // 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] | 4778 | // candidate functions includes at least the function call |
| 4779 | // operators of T. The function call operators of T are obtained by |
| 4780 | // ordinary lookup of the name operator() in the context of |
| 4781 | // (E).operator(). |
| 4782 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4783 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4784 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4785 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4786 | Oper != OperEnd; ++Oper) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4788 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4789 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4790 | // C++ [over.call.object]p2: |
| 4791 | // In addition, for each conversion function declared in T of the |
| 4792 | // form |
| 4793 | // |
| 4794 | // operator conversion-type-id () cv-qualifier; |
| 4795 | // |
| 4796 | // where cv-qualifier is the same cv-qualification as, or a |
| 4797 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 4798 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 4799 | // R", or the type "reference to pointer to function of |
| 4800 | // (P1,...,Pn) returning R", or the type "reference to function |
| 4801 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4802 | // is also considered as a candidate function. Similarly, |
| 4803 | // surrogate call functions are added to the set of candidate |
| 4804 | // functions for each conversion function declared in an |
| 4805 | // accessible base class provided the function is not hidden |
| 4806 | // within T by another intervening declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4808 | if (!RequireCompleteType(SourceLocation(), Object->getType(), 0)) { |
| 4809 | // FIXME: Look in base classes for more conversion operators! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4810 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4811 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4813 | Func = Conversions->function_begin(), |
| 4814 | FuncEnd = Conversions->function_end(); |
| 4815 | Func != FuncEnd; ++Func) { |
| 4816 | CXXConversionDecl *Conv; |
| 4817 | FunctionTemplateDecl *ConvTemplate; |
| 4818 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4820 | // Skip over templated conversion functions; they aren't |
| 4821 | // surrogates. |
| 4822 | if (ConvTemplate) |
| 4823 | continue; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4824 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4825 | // Strip the reference type (if any) and then the pointer type (if |
| 4826 | // any) to get down to what might be a function type. |
| 4827 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 4828 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4829 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4830 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4831 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4832 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 4833 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4834 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4835 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4836 | // Perform overload resolution. |
| 4837 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4838 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4839 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4840 | // Overload resolution succeeded; we'll build the appropriate call |
| 4841 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4842 | break; |
| 4843 | |
| 4844 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | Diag(Object->getSourceRange().getBegin(), |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4846 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4847 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4848 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4849 | break; |
| 4850 | |
| 4851 | case OR_Ambiguous: |
| 4852 | Diag(Object->getSourceRange().getBegin(), |
| 4853 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4854 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4855 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4856 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4857 | |
| 4858 | case OR_Deleted: |
| 4859 | Diag(Object->getSourceRange().getBegin(), |
| 4860 | diag::err_ovl_deleted_object_call) |
| 4861 | << Best->Function->isDeleted() |
| 4862 | << Object->getType() << Object->getSourceRange(); |
| 4863 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4864 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4867 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4868 | // We had an error; delete all of the subexpressions and return |
| 4869 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4870 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4871 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4872 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4873 | return true; |
| 4874 | } |
| 4875 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4876 | if (Best->Function == 0) { |
| 4877 | // Since there is no function declaration, this is one of the |
| 4878 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4880 | = cast<CXXConversionDecl>( |
| 4881 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 4882 | |
| 4883 | // We selected one of the surrogate functions that converts the |
| 4884 | // object parameter to a function pointer. Perform the conversion |
| 4885 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame^] | 4886 | |
| 4887 | // Create an implicit member expr to refer to the conversion operator. |
| 4888 | MemberExpr *ME = |
| 4889 | new (Context) MemberExpr(Object, /*IsArrow=*/false, Conv, |
| 4890 | SourceLocation(), Conv->getType()); |
| 4891 | QualType ResultType = Conv->getConversionType().getNonReferenceType(); |
| 4892 | CXXMemberCallExpr *CE = |
| 4893 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, |
| 4894 | ResultType, |
| 4895 | SourceLocation()); |
| 4896 | |
| 4897 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4898 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 4899 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 4903 | // that calls this method, using Object for the implicit object |
| 4904 | // parameter and passing along the remaining arguments. |
| 4905 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4906 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4907 | |
| 4908 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4909 | unsigned NumArgsToCheck = NumArgs; |
| 4910 | |
| 4911 | // Build the full argument list for the method call (the |
| 4912 | // implicit object parameter is placed at the beginning of the |
| 4913 | // list). |
| 4914 | Expr **MethodArgs; |
| 4915 | if (NumArgs < NumArgsInProto) { |
| 4916 | NumArgsToCheck = NumArgsInProto; |
| 4917 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 4918 | } else { |
| 4919 | MethodArgs = new Expr*[NumArgs + 1]; |
| 4920 | } |
| 4921 | MethodArgs[0] = Object; |
| 4922 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4923 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | |
| 4925 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4926 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4927 | UsualUnaryConversions(NewFn); |
| 4928 | |
| 4929 | // Once we've built TheCall, all of the expressions are properly |
| 4930 | // owned. |
| 4931 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4932 | ExprOwningPtr<CXXOperatorCallExpr> |
| 4933 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4934 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4935 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4936 | delete [] MethodArgs; |
| 4937 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4938 | // We may have default arguments. If so, we need to allocate more |
| 4939 | // slots in the call for them. |
| 4940 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4941 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4942 | else if (NumArgs > NumArgsInProto) |
| 4943 | NumArgsToCheck = NumArgsInProto; |
| 4944 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4945 | bool IsError = false; |
| 4946 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4947 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4948 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4949 | TheCall->setArg(0, Object); |
| 4950 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4951 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4952 | // Check the argument types. |
| 4953 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4954 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4955 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4956 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4958 | // Pass the argument. |
| 4959 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4960 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4961 | } else { |
Anders Carlsson | f1480ee | 2009-08-14 18:30:22 +0000 | [diff] [blame] | 4962 | Arg = CXXDefaultArgExpr::Create(Context, Method->getParamDecl(i)); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4963 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4964 | |
| 4965 | TheCall->setArg(i + 1, Arg); |
| 4966 | } |
| 4967 | |
| 4968 | // If this is a variadic call, handle args passed through "...". |
| 4969 | if (Proto->isVariadic()) { |
| 4970 | // Promote the arguments (C99 6.5.2.2p7). |
| 4971 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 4972 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4973 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4974 | TheCall->setArg(i + 1, Arg); |
| 4975 | } |
| 4976 | } |
| 4977 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4978 | if (IsError) return true; |
| 4979 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4980 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4981 | return true; |
| 4982 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 4983 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4986 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4987 | /// (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] | 4988 | /// @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] | 4989 | Sema::OwningExprResult |
| 4990 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 4991 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4992 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4993 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4994 | // C++ [over.ref]p1: |
| 4995 | // |
| 4996 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 4997 | // for a class object x of type T if T::operator->() exists and if |
| 4998 | // the operator is selected as the best match function by the |
| 4999 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5000 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 5001 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5002 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5003 | |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5004 | LookupResult R = LookupQualifiedName(BaseRecord->getDecl(), OpName, |
| 5005 | LookupOrdinaryName); |
| 5006 | |
| 5007 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 5008 | Oper != OperEnd; ++Oper) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 5009 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5010 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5011 | |
| 5012 | // Perform overload resolution. |
| 5013 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5014 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5015 | case OR_Success: |
| 5016 | // Overload resolution succeeded; we'll build the call below. |
| 5017 | break; |
| 5018 | |
| 5019 | case OR_No_Viable_Function: |
| 5020 | if (CandidateSet.empty()) |
| 5021 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5022 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5023 | else |
| 5024 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5025 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5026 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5027 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5028 | |
| 5029 | case OR_Ambiguous: |
| 5030 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5031 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5032 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5033 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5034 | |
| 5035 | case OR_Deleted: |
| 5036 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5037 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5038 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5039 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5040 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | // Convert the object parameter. |
| 5044 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5045 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5046 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 5047 | |
| 5048 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5049 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5050 | |
| 5051 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5052 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 5053 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5054 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5055 | Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5056 | Method->getResultType().getNonReferenceType(), |
| 5057 | OpLoc); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5058 | return Owned(Base); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5061 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 5062 | /// a C++ overloaded function (possibly with some parentheses and |
| 5063 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 5064 | /// to the function declaration Fn, so patch up the expression E to |
| 5065 | /// refer (possibly indirectly) to Fn. |
| 5066 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 5067 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 5068 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 5069 | E->setType(PE->getSubExpr()->getType()); |
| 5070 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5071 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5072 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5073 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5074 | if (Method->isStatic()) { |
| 5075 | // Do nothing: static member functions aren't any different |
| 5076 | // from non-member functions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5077 | } else if (QualifiedDeclRefExpr *DRE |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5078 | = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) { |
| 5079 | // We have taken the address of a pointer to member |
| 5080 | // function. Perform the computation here so that we get the |
| 5081 | // appropriate pointer to member type. |
| 5082 | DRE->setDecl(Fn); |
| 5083 | DRE->setType(Fn->getType()); |
| 5084 | QualType ClassType |
| 5085 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5086 | E->setType(Context.getMemberPointerType(Fn->getType(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5087 | ClassType.getTypePtr())); |
| 5088 | return; |
| 5089 | } |
| 5090 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5091 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5092 | E->setType(Context.getPointerType(UnOp->getSubExpr()->getType())); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5093 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5094 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5095 | isa<FunctionTemplateDecl>(DR->getDecl())) && |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5096 | "Expected overloaded function or function template"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5097 | DR->setDecl(Fn); |
| 5098 | E->setType(Fn->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5099 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 5100 | MemExpr->setMemberDecl(Fn); |
| 5101 | E->setType(Fn->getType()); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5102 | } else { |
| 5103 | assert(false && "Invalid reference to overloaded function"); |
| 5104 | } |
| 5105 | } |
| 5106 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5107 | } // end namespace clang |