Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 16 | #include "SemaInit.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 28 | #include <cstdio> |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 29 | |
| 30 | namespace clang { |
| 31 | |
| 32 | /// GetConversionCategory - Retrieve the implicit conversion |
| 33 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 35 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 36 | static const ImplicitConversionCategory |
| 37 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 38 | ICC_Identity, |
| 39 | ICC_Lvalue_Transformation, |
| 40 | ICC_Lvalue_Transformation, |
| 41 | ICC_Lvalue_Transformation, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 42 | ICC_Identity, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 43 | ICC_Qualification_Adjustment, |
| 44 | ICC_Promotion, |
| 45 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 46 | ICC_Promotion, |
| 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 49 | ICC_Conversion, |
| 50 | ICC_Conversion, |
| 51 | ICC_Conversion, |
| 52 | ICC_Conversion, |
| 53 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 54 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 55 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 56 | ICC_Conversion |
| 57 | }; |
| 58 | return Category[(int)Kind]; |
| 59 | } |
| 60 | |
| 61 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 62 | /// corresponding to the given implicit conversion kind. |
| 63 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 64 | static const ImplicitConversionRank |
| 65 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Exact_Match, |
| 70 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 71 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 72 | ICR_Promotion, |
| 73 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 74 | ICR_Promotion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
| 79 | ICR_Conversion, |
| 80 | ICR_Conversion, |
| 81 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 82 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 83 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 84 | ICR_Conversion |
| 85 | }; |
| 86 | return Rank[(int)Kind]; |
| 87 | } |
| 88 | |
| 89 | /// GetImplicitConversionName - Return the name of this kind of |
| 90 | /// implicit conversion. |
| 91 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 92 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 93 | "No conversion", |
| 94 | "Lvalue-to-rvalue", |
| 95 | "Array-to-pointer", |
| 96 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 97 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | "Qualification", |
| 99 | "Integral promotion", |
| 100 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 101 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 102 | "Integral conversion", |
| 103 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 104 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | "Floating-integral conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 106 | "Complex-real conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 107 | "Pointer conversion", |
| 108 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 109 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 110 | "Compatible-types conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 111 | "Derived-to-base conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 112 | }; |
| 113 | return Name[Kind]; |
| 114 | } |
| 115 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 116 | /// StandardConversionSequence - Set the standard conversion |
| 117 | /// sequence to the identity conversion. |
| 118 | void StandardConversionSequence::setAsIdentityConversion() { |
| 119 | First = ICK_Identity; |
| 120 | Second = ICK_Identity; |
| 121 | Third = ICK_Identity; |
| 122 | Deprecated = false; |
| 123 | ReferenceBinding = false; |
| 124 | DirectBinding = false; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 125 | RRefBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 126 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 129 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 130 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 131 | /// implicit conversions. |
| 132 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 133 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 134 | if (GetConversionRank(First) > Rank) |
| 135 | Rank = GetConversionRank(First); |
| 136 | if (GetConversionRank(Second) > Rank) |
| 137 | Rank = GetConversionRank(Second); |
| 138 | if (GetConversionRank(Third) > Rank) |
| 139 | Rank = GetConversionRank(Third); |
| 140 | return Rank; |
| 141 | } |
| 142 | |
| 143 | /// isPointerConversionToBool - Determines whether this conversion is |
| 144 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 146 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 148 | // Note that FromType has not necessarily been transformed by the |
| 149 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 150 | // check for their presence as well as checking whether FromType is |
| 151 | // a pointer. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 152 | if (getToType()->isBooleanType() && |
| 153 | (getFromType()->isPointerType() || getFromType()->isBlockPointerType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 154 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 155 | return true; |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 160 | /// isPointerConversionToVoidPointer - Determines whether this |
| 161 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 162 | /// used as part of the ranking of standard conversion sequences (C++ |
| 163 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 165 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 167 | QualType FromType = getFromType(); |
| 168 | QualType ToType = getToType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 169 | |
| 170 | // Note that FromType has not necessarily been transformed by the |
| 171 | // array-to-pointer implicit conversion, so check for its presence |
| 172 | // and redo the conversion to get a pointer. |
| 173 | if (First == ICK_Array_To_Pointer) |
| 174 | FromType = Context.getArrayDecayedType(FromType); |
| 175 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 176 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 177 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 178 | return ToPtrType->getPointeeType()->isVoidType(); |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 183 | /// DebugPrint - Print this standard conversion sequence to standard |
| 184 | /// error. Useful for debugging overloading issues. |
| 185 | void StandardConversionSequence::DebugPrint() const { |
| 186 | bool PrintedSomething = false; |
| 187 | if (First != ICK_Identity) { |
| 188 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 189 | PrintedSomething = true; |
| 190 | } |
| 191 | |
| 192 | if (Second != ICK_Identity) { |
| 193 | if (PrintedSomething) { |
| 194 | fprintf(stderr, " -> "); |
| 195 | } |
| 196 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 197 | |
| 198 | if (CopyConstructor) { |
| 199 | fprintf(stderr, " (by copy constructor)"); |
| 200 | } else if (DirectBinding) { |
| 201 | fprintf(stderr, " (direct reference binding)"); |
| 202 | } else if (ReferenceBinding) { |
| 203 | fprintf(stderr, " (reference binding)"); |
| 204 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 205 | PrintedSomething = true; |
| 206 | } |
| 207 | |
| 208 | if (Third != ICK_Identity) { |
| 209 | if (PrintedSomething) { |
| 210 | fprintf(stderr, " -> "); |
| 211 | } |
| 212 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 213 | PrintedSomething = true; |
| 214 | } |
| 215 | |
| 216 | if (!PrintedSomething) { |
| 217 | fprintf(stderr, "No conversions required"); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 222 | /// error. Useful for debugging overloading issues. |
| 223 | void UserDefinedConversionSequence::DebugPrint() const { |
| 224 | if (Before.First || Before.Second || Before.Third) { |
| 225 | Before.DebugPrint(); |
| 226 | fprintf(stderr, " -> "); |
| 227 | } |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 228 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 229 | if (After.First || After.Second || After.Third) { |
| 230 | fprintf(stderr, " -> "); |
| 231 | After.DebugPrint(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 236 | /// error. Useful for debugging overloading issues. |
| 237 | void ImplicitConversionSequence::DebugPrint() const { |
| 238 | switch (ConversionKind) { |
| 239 | case StandardConversion: |
| 240 | fprintf(stderr, "Standard conversion: "); |
| 241 | Standard.DebugPrint(); |
| 242 | break; |
| 243 | case UserDefinedConversion: |
| 244 | fprintf(stderr, "User-defined conversion: "); |
| 245 | UserDefined.DebugPrint(); |
| 246 | break; |
| 247 | case EllipsisConversion: |
| 248 | fprintf(stderr, "Ellipsis conversion"); |
| 249 | break; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 250 | case AmbiguousConversion: |
| 251 | fprintf(stderr, "Ambiguous conversion"); |
| 252 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 253 | case BadConversion: |
| 254 | fprintf(stderr, "Bad conversion"); |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | fprintf(stderr, "\n"); |
| 259 | } |
| 260 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 261 | void AmbiguousConversionSequence::construct() { |
| 262 | new (&conversions()) ConversionSet(); |
| 263 | } |
| 264 | |
| 265 | void AmbiguousConversionSequence::destruct() { |
| 266 | conversions().~ConversionSet(); |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 271 | FromTypePtr = O.FromTypePtr; |
| 272 | ToTypePtr = O.ToTypePtr; |
| 273 | new (&conversions()) ConversionSet(O.conversions()); |
| 274 | } |
| 275 | |
| 276 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 277 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 278 | // overload of the declarations in Old. This routine returns false if |
| 279 | // New and Old cannot be overloaded, e.g., if New has the same |
| 280 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 281 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 282 | // it does return false, MatchedDecl will point to the decl that New |
| 283 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 284 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 285 | // |
| 286 | // Example: Given the following input: |
| 287 | // |
| 288 | // void f(int, float); // #1 |
| 289 | // void f(int, int); // #2 |
| 290 | // int f(int, int); // #3 |
| 291 | // |
| 292 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 294 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 295 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 296 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 297 | // (since they have different signatures), so this routine returns |
| 298 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 299 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 300 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 301 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 302 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 303 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 304 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 305 | // point to the FunctionDecl for #2. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 306 | Sema::OverloadKind |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 307 | Sema::CheckOverload(FunctionDecl *New, const LookupResult &Old, |
| 308 | NamedDecl *&Match) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 309 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 310 | I != E; ++I) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 311 | NamedDecl *OldD = (*I)->getUnderlyingDecl(); |
| 312 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 313 | if (!IsOverload(New, OldT->getTemplatedDecl())) { |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 314 | Match = *I; |
| 315 | return Ovl_Match; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 316 | } |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 317 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 318 | if (!IsOverload(New, OldF)) { |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 319 | Match = *I; |
| 320 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 321 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 322 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 323 | // We can overload with these, which can show up when doing |
| 324 | // redeclaration checks for UsingDecls. |
| 325 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 326 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 327 | // Optimistically assume that an unresolved using decl will |
| 328 | // overload; if it doesn't, we'll have to diagnose during |
| 329 | // template instantiation. |
| 330 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 331 | // (C++ 13p1): |
| 332 | // Only function declarations can be overloaded; object and type |
| 333 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 334 | Match = *I; |
| 335 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 336 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 337 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 338 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 339 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old) { |
| 343 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 344 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 345 | |
| 346 | // C++ [temp.fct]p2: |
| 347 | // A function template can be overloaded with other function templates |
| 348 | // and with normal (non-template) functions. |
| 349 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 350 | return true; |
| 351 | |
| 352 | // Is the function New an overload of the function Old? |
| 353 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 354 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 355 | |
| 356 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 357 | // determine whether they are overloads. If we find any mismatch |
| 358 | // in the signature, they are overloads. |
| 359 | |
| 360 | // If either of these functions is a K&R-style function (no |
| 361 | // prototype), then we consider them to have matching signatures. |
| 362 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 363 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 364 | return false; |
| 365 | |
| 366 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 367 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 368 | |
| 369 | // The signature of a function includes the types of its |
| 370 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 371 | // of the ellipsis; see C++ DR 357). |
| 372 | if (OldQType != NewQType && |
| 373 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 374 | OldType->isVariadic() != NewType->isVariadic() || |
| 375 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 376 | NewType->arg_type_begin()))) |
| 377 | return true; |
| 378 | |
| 379 | // C++ [temp.over.link]p4: |
| 380 | // The signature of a function template consists of its function |
| 381 | // signature, its return type and its template parameter list. The names |
| 382 | // of the template parameters are significant only for establishing the |
| 383 | // relationship between the template parameters and the rest of the |
| 384 | // signature. |
| 385 | // |
| 386 | // We check the return type and template parameter lists for function |
| 387 | // templates first; the remaining checks follow. |
| 388 | if (NewTemplate && |
| 389 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 390 | OldTemplate->getTemplateParameters(), |
| 391 | false, TPL_TemplateMatch) || |
| 392 | OldType->getResultType() != NewType->getResultType())) |
| 393 | return true; |
| 394 | |
| 395 | // If the function is a class member, its signature includes the |
| 396 | // cv-qualifiers (if any) on the function itself. |
| 397 | // |
| 398 | // As part of this, also check whether one of the member functions |
| 399 | // is static, in which case they are not overloads (C++ |
| 400 | // 13.1p2). While not part of the definition of the signature, |
| 401 | // this check is important to determine whether these functions |
| 402 | // can be overloaded. |
| 403 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 404 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 405 | if (OldMethod && NewMethod && |
| 406 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 407 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 408 | return true; |
| 409 | |
| 410 | // The signatures match; this is not an overload. |
| 411 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 414 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 415 | /// from the given expression (Expr) to the given type (ToType). This |
| 416 | /// function returns an implicit conversion sequence that can be used |
| 417 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 418 | /// |
| 419 | /// void f(float f); |
| 420 | /// void g(int i) { f(i); } |
| 421 | /// |
| 422 | /// this routine would produce an implicit conversion sequence to |
| 423 | /// describe the initialization of f from i, which will be a standard |
| 424 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 425 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 426 | // |
| 427 | /// Note that this routine only determines how the conversion can be |
| 428 | /// performed; it does not actually perform the conversion. As such, |
| 429 | /// it will not produce any diagnostics if no conversion is available, |
| 430 | /// but will instead return an implicit conversion sequence of kind |
| 431 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 432 | /// |
| 433 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 434 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 435 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 436 | /// permitted. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 437 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 438 | /// no matter its actual lvalueness. |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 439 | /// If @p UserCast, the implicit conversion is being done for a user-specified |
| 440 | /// cast. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 441 | ImplicitConversionSequence |
Anders Carlsson | 5ec4abf | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 442 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 443 | bool SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 444 | bool AllowExplicit, bool ForceRValue, |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 445 | bool InOverloadResolution, |
| 446 | bool UserCast) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 447 | ImplicitConversionSequence ICS; |
Fariborz Jahanian | 19c7328 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 448 | OverloadCandidateSet Conversions; |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 449 | OverloadingResult UserDefResult = OR_Success; |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 450 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 451 | ICS.setStandard(); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 452 | else if (getLangOptions().CPlusPlus && |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 453 | (UserDefResult = IsUserDefinedConversion(From, ToType, |
| 454 | ICS.UserDefined, |
Fariborz Jahanian | 19c7328 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 455 | Conversions, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 456 | !SuppressUserConversions, AllowExplicit, |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 457 | ForceRValue, UserCast)) == OR_Success) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 458 | ICS.setUserDefined(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 459 | // C++ [over.ics.user]p4: |
| 460 | // A conversion of an expression of class type to the same class |
| 461 | // type is given Exact Match rank, and a conversion of an |
| 462 | // expression of class type to a base class of that type is |
| 463 | // given Conversion rank, in spite of the fact that a copy |
| 464 | // constructor (i.e., a user-defined conversion function) is |
| 465 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 467 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | QualType FromCanon |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 469 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 470 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 471 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 4141d5b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 472 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 473 | // Turn this into a "standard" conversion sequence, so that it |
| 474 | // gets ranked with standard conversion sequences. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 475 | ICS.setStandard(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 476 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 477 | ICS.Standard.setFromType(From->getType()); |
| 478 | ICS.Standard.setToType(ToType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 479 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 480 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 481 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 482 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 483 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 484 | |
| 485 | // C++ [over.best.ics]p4: |
| 486 | // However, when considering the argument of a user-defined |
| 487 | // conversion function that is a candidate by 13.3.1.3 when |
| 488 | // invoked for the copying of the temporary in the second step |
| 489 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 490 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 491 | // ellipsis conversion sequences are allowed. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 492 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 493 | ICS.setBad(); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 494 | ICS.Bad.init(BadConversionSequence::suppressed_user, From, ToType); |
| 495 | } |
John McCall | e8c8cd2 | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 496 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 497 | ICS.setAmbiguous(); |
| 498 | ICS.Ambiguous.setFromType(From->getType()); |
| 499 | ICS.Ambiguous.setToType(ToType); |
| 500 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 501 | Cand != Conversions.end(); ++Cand) |
| 502 | if (Cand->Viable) |
| 503 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 504 | } else { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 505 | ICS.setBad(); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 506 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 507 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 508 | |
| 509 | return ICS; |
| 510 | } |
| 511 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 512 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 513 | /// conversion that strips "noreturn" off the nested function type. |
| 514 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 515 | QualType ToType, QualType &ResultTy) { |
| 516 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 517 | return false; |
| 518 | |
| 519 | // Strip the noreturn off the type we're converting from; noreturn can |
| 520 | // safely be removed. |
| 521 | FromType = Context.getNoReturnType(FromType, false); |
| 522 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 523 | return false; |
| 524 | |
| 525 | ResultTy = FromType; |
| 526 | return true; |
| 527 | } |
| 528 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 529 | /// IsStandardConversion - Determines whether there is a standard |
| 530 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 531 | /// expression From to the type ToType. Standard conversion sequences |
| 532 | /// only consider non-class types; for conversions that involve class |
| 533 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 534 | /// contain the standard conversion sequence required to perform this |
| 535 | /// conversion and this routine will return true. Otherwise, this |
| 536 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | bool |
| 538 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 539 | bool InOverloadResolution, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | StandardConversionSequence &SCS) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 541 | QualType FromType = From->getType(); |
| 542 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 543 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 544 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 545 | SCS.Deprecated = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 546 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 547 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 548 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 550 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 552 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 553 | if (getLangOptions().CPlusPlus) |
| 554 | return false; |
| 555 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 559 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 560 | // array-to-pointer conversion, or function-to-pointer conversion |
| 561 | // (C++ 4p1). |
| 562 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 564 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 565 | // converted to an rvalue. |
| 566 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 568 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 569 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 570 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 571 | |
| 572 | // If T is a non-class type, the type of the rvalue is the |
| 573 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 574 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 575 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 576 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 577 | } else if (FromType->isArrayType()) { |
| 578 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 579 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 580 | |
| 581 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 582 | // bound of T" can be converted to an rvalue of type "pointer to |
| 583 | // T" (C++ 4.2p1). |
| 584 | FromType = Context.getArrayDecayedType(FromType); |
| 585 | |
| 586 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 587 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 588 | SCS.Deprecated = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 589 | |
| 590 | // For the purpose of ranking in overload resolution |
| 591 | // (13.3.3.1.1), this conversion is considered an |
| 592 | // array-to-pointer conversion followed by a qualification |
| 593 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 594 | SCS.Second = ICK_Identity; |
| 595 | SCS.Third = ICK_Qualification; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 596 | SCS.setToType(ToType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 597 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 599 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 600 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 601 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 602 | |
| 603 | // An lvalue of function type T can be converted to an rvalue of |
| 604 | // type "pointer to T." The result is a pointer to the |
| 605 | // function. (C++ 4.3p1). |
| 606 | FromType = Context.getPointerType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | } else if (FunctionDecl *Fn |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 608 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 609 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 610 | SCS.First = ICK_Function_To_Pointer; |
| 611 | |
| 612 | // We were able to resolve the address of the overloaded function, |
| 613 | // so we can convert to the type of that function. |
| 614 | FromType = Fn->getType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 615 | if (ToType->isLValueReferenceType()) |
| 616 | FromType = Context.getLValueReferenceType(FromType); |
| 617 | else if (ToType->isRValueReferenceType()) |
| 618 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 619 | else if (ToType->isMemberPointerType()) { |
| 620 | // Resolve address only succeeds if both sides are member pointers, |
| 621 | // but it doesn't have to be the same class. See DR 247. |
| 622 | // Note that this means that the type of &Derived::fn can be |
| 623 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 624 | // base class, even if it was brought into the derived class via a |
| 625 | // using declaration. The standard isn't clear on this issue at all. |
| 626 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 627 | FromType = Context.getMemberPointerType(FromType, |
| 628 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 629 | } else |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 630 | FromType = Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 631 | } else { |
| 632 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 633 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // The second conversion can be an integral promotion, floating |
| 637 | // point promotion, integral conversion, floating point conversion, |
| 638 | // floating-integral conversion, pointer conversion, |
| 639 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 640 | // For overloading in C, this can also be a "compatible-type" |
| 641 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 642 | bool IncompatibleObjC = false; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 643 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 644 | // The unqualified versions of the types are the same: there's no |
| 645 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 646 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 647 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 649 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 650 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 651 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 652 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 653 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 654 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 655 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 656 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 657 | SCS.Second = ICK_Complex_Promotion; |
| 658 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 659 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 660 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 661 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 662 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 663 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 664 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 665 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 666 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 667 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 668 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 669 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 670 | SCS.Second = ICK_Complex_Conversion; |
| 671 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 672 | } else if ((FromType->isFloatingType() && |
| 673 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 674 | !ToType->isEnumeralType())) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 676 | ToType->isFloatingType())) { |
| 677 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 678 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 679 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 680 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 681 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 682 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 683 | SCS.Second = ICK_Complex_Real; |
| 684 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 685 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 686 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 687 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 688 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 689 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 690 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 691 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 692 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 693 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 694 | } else if (ToType->isBooleanType() && |
| 695 | (FromType->isArithmeticType() || |
| 696 | FromType->isEnumeralType() || |
Fariborz Jahanian | 8811885 | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 697 | FromType->isAnyPointerType() || |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 698 | FromType->isBlockPointerType() || |
| 699 | FromType->isMemberPointerType() || |
| 700 | FromType->isNullPtrType())) { |
| 701 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 702 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 703 | FromType = Context.BoolTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 705 | Context.typesAreCompatible(ToType, FromType)) { |
| 706 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 707 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 708 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 709 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 710 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 711 | } else { |
| 712 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 713 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 716 | QualType CanonFrom; |
| 717 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 718 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 719 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 720 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 721 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 722 | CanonFrom = Context.getCanonicalType(FromType); |
| 723 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 724 | } else { |
| 725 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 726 | SCS.Third = ICK_Identity; |
| 727 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 729 | // [...] Any difference in top-level cv-qualification is |
| 730 | // subsumed by the initialization itself and does not constitute |
| 731 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 732 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 734 | if (CanonFrom.getLocalUnqualifiedType() |
| 735 | == CanonTo.getLocalUnqualifiedType() && |
| 736 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 737 | FromType = ToType; |
| 738 | CanonFrom = CanonTo; |
| 739 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | // If we have not converted the argument type to the parameter type, |
| 743 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 744 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 745 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 746 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 747 | SCS.setToType(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 748 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 752 | /// expression From (whose potentially-adjusted type is FromType) to |
| 753 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 754 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 756 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 757 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 758 | if (!To) { |
| 759 | return false; |
| 760 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 761 | |
| 762 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 763 | // unsigned short int can be converted to an rvalue of type int if |
| 764 | // int can represent all the values of the source type; otherwise, |
| 765 | // the source rvalue can be converted to an rvalue of type unsigned |
| 766 | // int (C++ 4.5p1). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 767 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 768 | if (// We can promote any signed, promotable integer type to an int |
| 769 | (FromType->isSignedIntegerType() || |
| 770 | // We can promote any unsigned integer type whose size is |
| 771 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 773 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 774 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 777 | return To->getKind() == BuiltinType::UInt; |
| 778 | } |
| 779 | |
| 780 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 781 | // can be converted to an rvalue of the first of the following types |
| 782 | // that can represent all the values of its underlying type: int, |
| 783 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 784 | |
| 785 | // We pre-calculate the promotion type for enum types. |
| 786 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 787 | if (ToType->isIntegerType()) |
| 788 | return Context.hasSameUnqualifiedType(ToType, |
| 789 | FromEnumType->getDecl()->getPromotionType()); |
| 790 | |
| 791 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 792 | // Determine whether the type we're converting from is signed or |
| 793 | // unsigned. |
| 794 | bool FromIsSigned; |
| 795 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 796 | |
| 797 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 798 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 799 | |
| 800 | // The types we'll try to promote to, in the appropriate |
| 801 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | QualType PromoteTypes[6] = { |
| 803 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 804 | Context.LongTy, Context.UnsignedLongTy , |
| 805 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 806 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 807 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 808 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 809 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 811 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 812 | // We found the type that we can promote to. If this is the |
| 813 | // type we wanted, we have a promotion. Otherwise, no |
| 814 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 815 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 821 | // rvalue of type int if int can represent all the values of the |
| 822 | // bit-field; otherwise, it can be converted to unsigned int if |
| 823 | // unsigned int can represent all the values of the bit-field. If |
| 824 | // the bit-field is larger yet, no integral promotion applies to |
| 825 | // it. If the bit-field has an enumerated type, it is treated as any |
| 826 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 827 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 828 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 829 | using llvm::APSInt; |
| 830 | if (From) |
| 831 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 832 | APSInt BitWidth; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 833 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 834 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 835 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 836 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 838 | // Are we promoting to an int from a bitfield that fits in an int? |
| 839 | if (BitWidth < ToSize || |
| 840 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 841 | return To->getKind() == BuiltinType::Int; |
| 842 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 844 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 845 | // that fits into an unsigned int? |
| 846 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 847 | return To->getKind() == BuiltinType::UInt; |
| 848 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 850 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 851 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 852 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 854 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 855 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 856 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 857 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 858 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 859 | |
| 860 | return false; |
| 861 | } |
| 862 | |
| 863 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 864 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 865 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 867 | /// An rvalue of type float can be converted to an rvalue of type |
| 868 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 869 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 870 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 871 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 872 | ToBuiltin->getKind() == BuiltinType::Double) |
| 873 | return true; |
| 874 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 875 | // C99 6.3.1.5p1: |
| 876 | // When a float is promoted to double or long double, or a |
| 877 | // double is promoted to long double [...]. |
| 878 | if (!getLangOptions().CPlusPlus && |
| 879 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 880 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 881 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 882 | return true; |
| 883 | } |
| 884 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 885 | return false; |
| 886 | } |
| 887 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 888 | /// \brief Determine if a conversion is a complex promotion. |
| 889 | /// |
| 890 | /// A complex promotion is defined as a complex -> complex conversion |
| 891 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 892 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 893 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 894 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 895 | if (!FromComplex) |
| 896 | return false; |
| 897 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 898 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 899 | if (!ToComplex) |
| 900 | return false; |
| 901 | |
| 902 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 903 | ToComplex->getElementType()) || |
| 904 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 905 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 908 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 909 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 910 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 911 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 912 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | static QualType |
| 914 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 915 | QualType ToPointee, QualType ToType, |
| 916 | ASTContext &Context) { |
| 917 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 918 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 919 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
| 921 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 922 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 923 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 924 | if (!ToType.isNull()) |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 925 | return ToType; |
| 926 | |
| 927 | // Build a pointer to ToPointee. It has the right qualifiers |
| 928 | // already. |
| 929 | return Context.getPointerType(ToPointee); |
| 930 | } |
| 931 | |
| 932 | // Just build a canonical type that has the right qualifiers. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 933 | return Context.getPointerType( |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 934 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 935 | Quals)); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 938 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 939 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 940 | /// not have the right set of qualifiers. |
| 941 | static QualType |
| 942 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 943 | QualType ToType, |
| 944 | ASTContext &Context) { |
| 945 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 946 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 947 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 948 | |
| 949 | // Exact qualifier match -> return the pointer type we're converting to. |
| 950 | if (CanonToType.getLocalQualifiers() == Quals) |
| 951 | return ToType; |
| 952 | |
| 953 | // Just build a canonical type that has the right qualifiers. |
| 954 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 955 | } |
| 956 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 958 | bool InOverloadResolution, |
| 959 | ASTContext &Context) { |
| 960 | // Handle value-dependent integral null pointer constants correctly. |
| 961 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 962 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 963 | Expr->getType()->isIntegralType()) |
| 964 | return !InOverloadResolution; |
| 965 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 966 | return Expr->isNullPointerConstant(Context, |
| 967 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 968 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 969 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 971 | /// IsPointerConversion - Determines whether the conversion of the |
| 972 | /// expression From, which has the (possibly adjusted) type FromType, |
| 973 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 974 | /// 4.10). If so, returns true and places the converted type (that |
| 975 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 976 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 977 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 978 | /// This routine also supports conversions to and from block pointers |
| 979 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 980 | /// pointers to interfaces. FIXME: Once we've determined the |
| 981 | /// appropriate overloading rules for Objective-C, we may want to |
| 982 | /// split the Objective-C checks into a different routine; however, |
| 983 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 984 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 985 | /// set if the conversion is an allowed Objective-C conversion that |
| 986 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 987 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 988 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 989 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 990 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 991 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 992 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 993 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 994 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 996 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 997 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 998 | ConvertedType = ToType; |
| 999 | return true; |
| 1000 | } |
| 1001 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1002 | // Blocks: Block pointers can be converted to void*. |
| 1003 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1004 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1005 | ConvertedType = ToType; |
| 1006 | return true; |
| 1007 | } |
| 1008 | // Blocks: A null pointer constant can be converted to a block |
| 1009 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1011 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1012 | ConvertedType = ToType; |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1016 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1017 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1019 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1020 | ConvertedType = ToType; |
| 1021 | return true; |
| 1022 | } |
| 1023 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1024 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1025 | if (!ToTypePtr) |
| 1026 | return false; |
| 1027 | |
| 1028 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1029 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1030 | ConvertedType = ToType; |
| 1031 | return true; |
| 1032 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1033 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1034 | // Beyond this point, both types need to be pointers |
| 1035 | // , including objective-c pointers. |
| 1036 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1037 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1038 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1039 | ToType, Context); |
| 1040 | return true; |
| 1041 | |
| 1042 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1043 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1044 | if (!FromTypePtr) |
| 1045 | return false; |
| 1046 | |
| 1047 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1049 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1050 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1051 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1052 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1054 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1055 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1056 | return true; |
| 1057 | } |
| 1058 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1059 | // When we're overloading in C, we allow a special kind of pointer |
| 1060 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1062 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1064 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
| 1068 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1069 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1071 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1072 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1073 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1074 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1075 | // necessitates this conversion is ill-formed. The result of the |
| 1076 | // conversion is a pointer to the base class sub-object of the |
| 1077 | // derived class object. The null pointer value is converted to |
| 1078 | // the null pointer value of the destination type. |
| 1079 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1080 | // Note that we do not check for ambiguity or inaccessibility |
| 1081 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1082 | if (getLangOptions().CPlusPlus && |
| 1083 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1084 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1085 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1087 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1088 | ToType, Context); |
| 1089 | return true; |
| 1090 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1091 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
| 1094 | |
| 1095 | /// isObjCPointerConversion - Determines whether this is an |
| 1096 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1097 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1099 | QualType& ConvertedType, |
| 1100 | bool &IncompatibleObjC) { |
| 1101 | if (!getLangOptions().ObjC1) |
| 1102 | return false; |
| 1103 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1104 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1105 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1107 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1108 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1109 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1110 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1111 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1112 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1113 | ConvertedType = ToType; |
| 1114 | return true; |
| 1115 | } |
| 1116 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1118 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1120 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1121 | ConvertedType = ToType; |
| 1122 | return true; |
| 1123 | } |
| 1124 | // Objective C++: We're able to convert from a pointer to an |
| 1125 | // interface to a pointer to a different interface. |
| 1126 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1127 | ConvertedType = ToType; |
| 1128 | return true; |
| 1129 | } |
| 1130 | |
| 1131 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1132 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1133 | // interfaces, which is permitted. However, we're going to |
| 1134 | // complain about it. |
| 1135 | IncompatibleObjC = true; |
| 1136 | ConvertedType = FromType; |
| 1137 | return true; |
| 1138 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1140 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1141 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1142 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1143 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1144 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1145 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1146 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1147 | return false; |
| 1148 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1149 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1150 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1151 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1152 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1153 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1154 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1155 | return false; |
| 1156 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1157 | // If we have pointers to pointers, recursively check whether this |
| 1158 | // is an Objective-C conversion. |
| 1159 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1160 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1161 | IncompatibleObjC)) { |
| 1162 | // We always complain about this conversion. |
| 1163 | IncompatibleObjC = true; |
| 1164 | ConvertedType = ToType; |
| 1165 | return true; |
| 1166 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1167 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1168 | // differences in the argument and result types are in Objective-C |
| 1169 | // pointer conversions. If so, we permit the conversion (but |
| 1170 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1172 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1173 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1174 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1175 | if (FromFunctionType && ToFunctionType) { |
| 1176 | // If the function types are exactly the same, this isn't an |
| 1177 | // Objective-C pointer conversion. |
| 1178 | if (Context.getCanonicalType(FromPointeeType) |
| 1179 | == Context.getCanonicalType(ToPointeeType)) |
| 1180 | return false; |
| 1181 | |
| 1182 | // Perform the quick checks that will tell us whether these |
| 1183 | // function types are obviously different. |
| 1184 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1185 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1186 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1187 | return false; |
| 1188 | |
| 1189 | bool HasObjCConversion = false; |
| 1190 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1191 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1192 | // Okay, the types match exactly. Nothing to do. |
| 1193 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1194 | ToFunctionType->getResultType(), |
| 1195 | ConvertedType, IncompatibleObjC)) { |
| 1196 | // Okay, we have an Objective-C pointer conversion. |
| 1197 | HasObjCConversion = true; |
| 1198 | } else { |
| 1199 | // Function types are too different. Abort. |
| 1200 | return false; |
| 1201 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1203 | // Check argument types. |
| 1204 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1205 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1206 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1207 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1208 | if (Context.getCanonicalType(FromArgType) |
| 1209 | == Context.getCanonicalType(ToArgType)) { |
| 1210 | // Okay, the types match exactly. Nothing to do. |
| 1211 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1212 | ConvertedType, IncompatibleObjC)) { |
| 1213 | // Okay, we have an Objective-C pointer conversion. |
| 1214 | HasObjCConversion = true; |
| 1215 | } else { |
| 1216 | // Argument types are too different. Abort. |
| 1217 | return false; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | if (HasObjCConversion) { |
| 1222 | // We had an Objective-C conversion. Allow this pointer |
| 1223 | // conversion, but complain about it. |
| 1224 | ConvertedType = ToType; |
| 1225 | IncompatibleObjC = true; |
| 1226 | return true; |
| 1227 | } |
| 1228 | } |
| 1229 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1230 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1233 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1234 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1235 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1236 | /// conversions for which IsPointerConversion has already returned |
| 1237 | /// true. It returns true and produces a diagnostic if there was an |
| 1238 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1239 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1240 | CastExpr::CastKind &Kind, |
| 1241 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1242 | QualType FromType = From->getType(); |
| 1243 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1244 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1245 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1246 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1247 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1248 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1249 | if (FromPointeeType->isRecordType() && |
| 1250 | ToPointeeType->isRecordType()) { |
| 1251 | // We must have a derived-to-base conversion. Check an |
| 1252 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1253 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1254 | From->getExprLoc(), |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1255 | From->getSourceRange(), |
| 1256 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1257 | return true; |
| 1258 | |
| 1259 | // The conversion was successful. |
| 1260 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1264 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1266 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1267 | // Objective-C++ conversions are always okay. |
| 1268 | // FIXME: We should have a different class of conversions for the |
| 1269 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1270 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1271 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1272 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1273 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1274 | return false; |
| 1275 | } |
| 1276 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1277 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1278 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1279 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1280 | /// If so, returns true and places the converted type (that might differ from |
| 1281 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1282 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1283 | QualType ToType, |
| 1284 | bool InOverloadResolution, |
| 1285 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1286 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1287 | if (!ToTypePtr) |
| 1288 | return false; |
| 1289 | |
| 1290 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1291 | if (From->isNullPointerConstant(Context, |
| 1292 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1293 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1294 | ConvertedType = ToType; |
| 1295 | return true; |
| 1296 | } |
| 1297 | |
| 1298 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1299 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1300 | if (!FromTypePtr) |
| 1301 | return false; |
| 1302 | |
| 1303 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1304 | // where D is derived from B (C++ 4.11p2). |
| 1305 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1306 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1307 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1308 | |
| 1309 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1310 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1311 | ToClass.getTypePtr()); |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
| 1315 | return false; |
| 1316 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1317 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1318 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1319 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1320 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1321 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1322 | /// true and produces a diagnostic if there was an error, or returns false |
| 1323 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1325 | CastExpr::CastKind &Kind, |
| 1326 | bool IgnoreBaseAccess) { |
| 1327 | (void)IgnoreBaseAccess; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1328 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1329 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1330 | if (!FromPtrType) { |
| 1331 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1332 | assert(From->isNullPointerConstant(Context, |
| 1333 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1334 | "Expr must be null pointer constant!"); |
| 1335 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1336 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1337 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1338 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1339 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1340 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1341 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1342 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1343 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1344 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1345 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1346 | // FIXME: What about dependent types? |
| 1347 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1348 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1350 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1351 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1352 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1353 | assert(DerivationOkay && |
| 1354 | "Should not have been called if derivation isn't OK."); |
| 1355 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1356 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1357 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1358 | getUnqualifiedType())) { |
| 1359 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1360 | Paths.clear(); |
| 1361 | Paths.setRecordingPaths(true); |
| 1362 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1363 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1364 | (void)StillOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1365 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1366 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1367 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1368 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1369 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1370 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1371 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1372 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1373 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1374 | << FromClass << ToClass << QualType(VBase, 0) |
| 1375 | << From->getSourceRange(); |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1379 | // Must be a base to derived member conversion. |
| 1380 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1381 | return false; |
| 1382 | } |
| 1383 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1384 | /// IsQualificationConversion - Determines whether the conversion from |
| 1385 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1386 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | bool |
| 1388 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1389 | FromType = Context.getCanonicalType(FromType); |
| 1390 | ToType = Context.getCanonicalType(ToType); |
| 1391 | |
| 1392 | // If FromType and ToType are the same type, this is not a |
| 1393 | // qualification conversion. |
| 1394 | if (FromType == ToType) |
| 1395 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1396 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1397 | // (C++ 4.4p4): |
| 1398 | // A conversion can add cv-qualifiers at levels other than the first |
| 1399 | // in multi-level pointers, subject to the following rules: [...] |
| 1400 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1401 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1402 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1403 | // Within each iteration of the loop, we check the qualifiers to |
| 1404 | // determine if this still looks like a qualification |
| 1405 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1406 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1407 | // until there are no more pointers or pointers-to-members left to |
| 1408 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1409 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1410 | |
| 1411 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1412 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1413 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1414 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1416 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1417 | // every cv for 0 < k < j. |
| 1418 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1419 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1420 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1422 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1423 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1425 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1426 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1427 | |
| 1428 | // We are left with FromType and ToType being the pointee types |
| 1429 | // after unwrapping the original FromType and ToType the same number |
| 1430 | // of types. If we unwrapped any pointers, and if FromType and |
| 1431 | // ToType have the same unqualified type (since we checked |
| 1432 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1433 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1436 | /// Determines whether there is a user-defined conversion sequence |
| 1437 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1438 | /// ToType. If such a conversion exists, User will contain the |
| 1439 | /// user-defined conversion sequence that performs such a conversion |
| 1440 | /// and this routine will return true. Otherwise, this routine returns |
| 1441 | /// false and User is unspecified. |
| 1442 | /// |
| 1443 | /// \param AllowConversionFunctions true if the conversion should |
| 1444 | /// consider conversion functions at all. If false, only constructors |
| 1445 | /// will be considered. |
| 1446 | /// |
| 1447 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1448 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1449 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1450 | /// |
| 1451 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1452 | /// for overload resolution. |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1453 | /// \param UserCast true if looking for user defined conversion for a static |
| 1454 | /// cast. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1455 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1456 | UserDefinedConversionSequence& User, |
| 1457 | OverloadCandidateSet& CandidateSet, |
| 1458 | bool AllowConversionFunctions, |
| 1459 | bool AllowExplicit, |
| 1460 | bool ForceRValue, |
| 1461 | bool UserCast) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1462 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1463 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1464 | // We're not going to find any constructors. |
| 1465 | } else if (CXXRecordDecl *ToRecordDecl |
| 1466 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1467 | // C++ [over.match.ctor]p1: |
| 1468 | // When objects of class type are direct-initialized (8.5), or |
| 1469 | // copy-initialized from an expression of the same or a |
| 1470 | // derived class type (8.5), overload resolution selects the |
| 1471 | // constructor. [...] For copy-initialization, the candidate |
| 1472 | // functions are all the converting constructors (12.3.1) of |
| 1473 | // that class. The argument list is the expression-list within |
| 1474 | // the parentheses of the initializer. |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1475 | bool SuppressUserConversions = !UserCast; |
| 1476 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1477 | IsDerivedFrom(From->getType(), ToType)) { |
| 1478 | SuppressUserConversions = false; |
| 1479 | AllowConversionFunctions = false; |
| 1480 | } |
| 1481 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1483 | = Context.DeclarationNames.getCXXConstructorName( |
| 1484 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1485 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1487 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1488 | Con != ConEnd; ++Con) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1489 | // Find the constructor (which may be a template). |
| 1490 | CXXConstructorDecl *Constructor = 0; |
| 1491 | FunctionTemplateDecl *ConstructorTmpl |
| 1492 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1493 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1495 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1496 | else |
| 1497 | Constructor = cast<CXXConstructorDecl>(*Con); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1498 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1499 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1500 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1501 | if (ConstructorTmpl) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1502 | AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0, |
| 1503 | &From, 1, CandidateSet, |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1504 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1505 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1506 | // Allow one user-defined conversion when user specifies a |
| 1507 | // From->ToType conversion via an static cast (c-style, etc). |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1508 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1509 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1510 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1511 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1515 | if (!AllowConversionFunctions) { |
| 1516 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1518 | PDiag(0) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1519 | << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1520 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | } else if (const RecordType *FromRecordType |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1522 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1524 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1525 | // Add all of the conversion functions as candidates. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1526 | const UnresolvedSet *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1527 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1528 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 1529 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1530 | NamedDecl *D = *I; |
| 1531 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1532 | if (isa<UsingShadowDecl>(D)) |
| 1533 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1534 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1535 | CXXConversionDecl *Conv; |
| 1536 | FunctionTemplateDecl *ConvTemplate; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1537 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(*I))) |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1538 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1539 | else |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1540 | Conv = dyn_cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1541 | |
| 1542 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1543 | if (ConvTemplate) |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1544 | AddTemplateConversionCandidate(ConvTemplate, ActingContext, |
| 1545 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1546 | else |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1547 | AddConversionCandidate(Conv, ActingContext, From, ToType, |
| 1548 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1552 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1553 | |
| 1554 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1555 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1556 | case OR_Success: |
| 1557 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1559 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1560 | // C++ [over.ics.user]p1: |
| 1561 | // If the user-defined conversion is specified by a |
| 1562 | // constructor (12.3.1), the initial standard conversion |
| 1563 | // sequence converts the source type to the type required by |
| 1564 | // the argument of the constructor. |
| 1565 | // |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1566 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1567 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1568 | User.EllipsisConversion = true; |
| 1569 | else { |
| 1570 | User.Before = Best->Conversions[0].Standard; |
| 1571 | User.EllipsisConversion = false; |
| 1572 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1573 | User.ConversionFunction = Constructor; |
| 1574 | User.After.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1575 | User.After.setFromType( |
| 1576 | ThisType->getAs<PointerType>()->getPointeeType()); |
| 1577 | User.After.setToType(ToType); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1578 | return OR_Success; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1579 | } else if (CXXConversionDecl *Conversion |
| 1580 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1581 | // C++ [over.ics.user]p1: |
| 1582 | // |
| 1583 | // [...] If the user-defined conversion is specified by a |
| 1584 | // conversion function (12.3.2), the initial standard |
| 1585 | // conversion sequence converts the source type to the |
| 1586 | // implicit object parameter of the conversion function. |
| 1587 | User.Before = Best->Conversions[0].Standard; |
| 1588 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1589 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | |
| 1591 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1592 | // The second standard conversion sequence converts the |
| 1593 | // result of the user-defined conversion to the target type |
| 1594 | // for the sequence. Since an implicit conversion sequence |
| 1595 | // is an initialization, the special rules for |
| 1596 | // initialization by user-defined conversion apply when |
| 1597 | // selecting the best user-defined conversion for a |
| 1598 | // user-defined conversion sequence (see 13.3.3 and |
| 1599 | // 13.3.3.1). |
| 1600 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1601 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1602 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1603 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1604 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1605 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1607 | case OR_No_Viable_Function: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1608 | return OR_No_Viable_Function; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1609 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1610 | // No conversion here! We're done. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1611 | return OR_Deleted; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1612 | |
| 1613 | case OR_Ambiguous: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1614 | return OR_Ambiguous; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1617 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1618 | } |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1619 | |
| 1620 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1621 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1622 | ImplicitConversionSequence ICS; |
| 1623 | OverloadCandidateSet CandidateSet; |
| 1624 | OverloadingResult OvResult = |
| 1625 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1626 | CandidateSet, true, false, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1627 | if (OvResult == OR_Ambiguous) |
| 1628 | Diag(From->getSourceRange().getBegin(), |
| 1629 | diag::err_typecheck_ambiguous_condition) |
| 1630 | << From->getType() << ToType << From->getSourceRange(); |
| 1631 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1632 | Diag(From->getSourceRange().getBegin(), |
| 1633 | diag::err_typecheck_nonviable_condition) |
| 1634 | << From->getType() << ToType << From->getSourceRange(); |
| 1635 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1636 | return false; |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1637 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1640 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1641 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1642 | /// conversion sequences to determine whether one is better than the |
| 1643 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1645 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1646 | const ImplicitConversionSequence& ICS2) |
| 1647 | { |
| 1648 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1649 | // conversion sequences (as defined in 13.3.3.1) |
| 1650 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1651 | // conversion sequence than a user-defined conversion sequence or |
| 1652 | // an ellipsis conversion sequence, and |
| 1653 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1654 | // conversion sequence than an ellipsis conversion sequence |
| 1655 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1657 | // C++0x [over.best.ics]p10: |
| 1658 | // For the purpose of ranking implicit conversion sequences as |
| 1659 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1660 | // treated as a user-defined sequence that is indistinguishable |
| 1661 | // from any other user-defined conversion sequence. |
| 1662 | if (ICS1.getKind() < ICS2.getKind()) { |
| 1663 | if (!(ICS1.isUserDefined() && ICS2.isAmbiguous())) |
| 1664 | return ImplicitConversionSequence::Better; |
| 1665 | } else if (ICS2.getKind() < ICS1.getKind()) { |
| 1666 | if (!(ICS2.isUserDefined() && ICS1.isAmbiguous())) |
| 1667 | return ImplicitConversionSequence::Worse; |
| 1668 | } |
| 1669 | |
| 1670 | if (ICS1.isAmbiguous() || ICS2.isAmbiguous()) |
| 1671 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Two implicit conversion sequences of the same form are |
| 1674 | // indistinguishable conversion sequences unless one of the |
| 1675 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1676 | if (ICS1.isStandard()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1677 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1678 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1679 | // User-defined conversion sequence U1 is a better conversion |
| 1680 | // sequence than another user-defined conversion sequence U2 if |
| 1681 | // they contain the same user-defined conversion function or |
| 1682 | // constructor and if the second standard conversion sequence of |
| 1683 | // U1 is better than the second standard conversion sequence of |
| 1684 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1686 | ICS2.UserDefined.ConversionFunction) |
| 1687 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1688 | ICS2.UserDefined.After); |
| 1689 | } |
| 1690 | |
| 1691 | return ImplicitConversionSequence::Indistinguishable; |
| 1692 | } |
| 1693 | |
| 1694 | /// CompareStandardConversionSequences - Compare two standard |
| 1695 | /// conversion sequences to determine whether one is better than the |
| 1696 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1698 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1699 | const StandardConversionSequence& SCS2) |
| 1700 | { |
| 1701 | // Standard conversion sequence S1 is a better conversion sequence |
| 1702 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1703 | |
| 1704 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1705 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1706 | // excluding any Lvalue Transformation; the identity conversion |
| 1707 | // sequence is considered to be a subsequence of any |
| 1708 | // non-identity conversion sequence) or, if not that, |
| 1709 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1710 | // Neither is a proper subsequence of the other. Do nothing. |
| 1711 | ; |
| 1712 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1713 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1715 | SCS1.Third == ICK_Identity)) |
| 1716 | // SCS1 is a proper subsequence of SCS2. |
| 1717 | return ImplicitConversionSequence::Better; |
| 1718 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1719 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1721 | SCS2.Third == ICK_Identity)) |
| 1722 | // SCS2 is a proper subsequence of SCS1. |
| 1723 | return ImplicitConversionSequence::Worse; |
| 1724 | |
| 1725 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1726 | // defined below), or, if not that, |
| 1727 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1728 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1729 | if (Rank1 < Rank2) |
| 1730 | return ImplicitConversionSequence::Better; |
| 1731 | else if (Rank2 < Rank1) |
| 1732 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1733 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1734 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1735 | // are indistinguishable unless one of the following rules |
| 1736 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1738 | // A conversion that is not a conversion of a pointer, or |
| 1739 | // pointer to member, to bool is better than another conversion |
| 1740 | // that is such a conversion. |
| 1741 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1742 | return SCS2.isPointerConversionToBool() |
| 1743 | ? ImplicitConversionSequence::Better |
| 1744 | : ImplicitConversionSequence::Worse; |
| 1745 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1746 | // C++ [over.ics.rank]p4b2: |
| 1747 | // |
| 1748 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1749 | // conversion of B* to A* is better than conversion of B* to |
| 1750 | // void*, and conversion of A* to void* is better than conversion |
| 1751 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1753 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1755 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1756 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1757 | // Exactly one of the conversion sequences is a conversion to |
| 1758 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1759 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1760 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1761 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1762 | // Neither conversion sequence converts to a void pointer; compare |
| 1763 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1764 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1765 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1766 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1767 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1768 | // Both conversion sequences are conversions to void |
| 1769 | // pointers. Compare the source types to determine if there's an |
| 1770 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1771 | QualType FromType1 = SCS1.getFromType(); |
| 1772 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1773 | |
| 1774 | // Adjust the types we're converting from via the array-to-pointer |
| 1775 | // conversion, if we need to. |
| 1776 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1777 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1778 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1779 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1780 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1781 | QualType FromPointee1 |
| 1782 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1783 | QualType FromPointee2 |
| 1784 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1785 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1786 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1787 | return ImplicitConversionSequence::Better; |
| 1788 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1789 | return ImplicitConversionSequence::Worse; |
| 1790 | |
| 1791 | // Objective-C++: If one interface is more specific than the |
| 1792 | // other, it is the better one. |
| 1793 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1794 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1795 | if (FromIface1 && FromIface1) { |
| 1796 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1797 | return ImplicitConversionSequence::Better; |
| 1798 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1799 | return ImplicitConversionSequence::Worse; |
| 1800 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1801 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1802 | |
| 1803 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1804 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1806 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1807 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1809 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1810 | // C++0x [over.ics.rank]p3b4: |
| 1811 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1812 | // implicit object parameter of a non-static member function declared |
| 1813 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1814 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1815 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1816 | // or if the member function in this case has a ref qualifier. |
| 1817 | // (Of course, we don't have ref qualifiers yet.) |
| 1818 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1819 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1820 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1821 | |
| 1822 | // C++ [over.ics.rank]p3b4: |
| 1823 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1824 | // which the references refer are the same type except for |
| 1825 | // top-level cv-qualifiers, and the type to which the reference |
| 1826 | // initialized by S2 refers is more cv-qualified than the type |
| 1827 | // to which the reference initialized by S1 refers. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1828 | QualType T1 = SCS1.getToType(); |
| 1829 | QualType T2 = SCS2.getToType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1830 | T1 = Context.getCanonicalType(T1); |
| 1831 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1832 | Qualifiers T1Quals, T2Quals; |
| 1833 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1834 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1835 | if (UnqualT1 == UnqualT2) { |
| 1836 | // If the type is an array type, promote the element qualifiers to the type |
| 1837 | // for comparison. |
| 1838 | if (isa<ArrayType>(T1) && T1Quals) |
| 1839 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1840 | if (isa<ArrayType>(T2) && T2Quals) |
| 1841 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1842 | if (T2.isMoreQualifiedThan(T1)) |
| 1843 | return ImplicitConversionSequence::Better; |
| 1844 | else if (T1.isMoreQualifiedThan(T2)) |
| 1845 | return ImplicitConversionSequence::Worse; |
| 1846 | } |
| 1847 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1848 | |
| 1849 | return ImplicitConversionSequence::Indistinguishable; |
| 1850 | } |
| 1851 | |
| 1852 | /// CompareQualificationConversions - Compares two standard conversion |
| 1853 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1855 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1856 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1858 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1859 | // -- S1 and S2 differ only in their qualification conversion and |
| 1860 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1861 | // cv-qualification signature of type T1 is a proper subset of |
| 1862 | // the cv-qualification signature of type T2, and S1 is not the |
| 1863 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1864 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1865 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1866 | return ImplicitConversionSequence::Indistinguishable; |
| 1867 | |
| 1868 | // FIXME: the example in the standard doesn't use a qualification |
| 1869 | // conversion (!) |
| 1870 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1871 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1872 | T1 = Context.getCanonicalType(T1); |
| 1873 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1874 | Qualifiers T1Quals, T2Quals; |
| 1875 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1876 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1877 | |
| 1878 | // If the types are the same, we won't learn anything by unwrapped |
| 1879 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1880 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1881 | return ImplicitConversionSequence::Indistinguishable; |
| 1882 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1883 | // If the type is an array type, promote the element qualifiers to the type |
| 1884 | // for comparison. |
| 1885 | if (isa<ArrayType>(T1) && T1Quals) |
| 1886 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1887 | if (isa<ArrayType>(T2) && T2Quals) |
| 1888 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1889 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1891 | = ImplicitConversionSequence::Indistinguishable; |
| 1892 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1893 | // Within each iteration of the loop, we check the qualifiers to |
| 1894 | // determine if this still looks like a qualification |
| 1895 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1896 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1897 | // until there are no more pointers or pointers-to-members left |
| 1898 | // to unwrap. This essentially mimics what |
| 1899 | // IsQualificationConversion does, but here we're checking for a |
| 1900 | // strict subset of qualifiers. |
| 1901 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1902 | // The qualifiers are the same, so this doesn't tell us anything |
| 1903 | // about how the sequences rank. |
| 1904 | ; |
| 1905 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1906 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1907 | if (Result == ImplicitConversionSequence::Worse) |
| 1908 | // Neither has qualifiers that are a subset of the other's |
| 1909 | // qualifiers. |
| 1910 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1912 | Result = ImplicitConversionSequence::Better; |
| 1913 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1914 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1915 | if (Result == ImplicitConversionSequence::Better) |
| 1916 | // Neither has qualifiers that are a subset of the other's |
| 1917 | // qualifiers. |
| 1918 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1920 | Result = ImplicitConversionSequence::Worse; |
| 1921 | } else { |
| 1922 | // Qualifiers are disjoint. |
| 1923 | return ImplicitConversionSequence::Indistinguishable; |
| 1924 | } |
| 1925 | |
| 1926 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1927 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1928 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1931 | // Check that the winning standard conversion sequence isn't using |
| 1932 | // the deprecated string literal array to pointer conversion. |
| 1933 | switch (Result) { |
| 1934 | case ImplicitConversionSequence::Better: |
| 1935 | if (SCS1.Deprecated) |
| 1936 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1937 | break; |
| 1938 | |
| 1939 | case ImplicitConversionSequence::Indistinguishable: |
| 1940 | break; |
| 1941 | |
| 1942 | case ImplicitConversionSequence::Worse: |
| 1943 | if (SCS2.Deprecated) |
| 1944 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1945 | break; |
| 1946 | } |
| 1947 | |
| 1948 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1951 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1952 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1953 | /// various kinds of derived-to-base conversions (C++ |
| 1954 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1955 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1956 | ImplicitConversionSequence::CompareKind |
| 1957 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1958 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1959 | QualType FromType1 = SCS1.getFromType(); |
| 1960 | QualType ToType1 = SCS1.getToType(); |
| 1961 | QualType FromType2 = SCS2.getFromType(); |
| 1962 | QualType ToType2 = SCS2.getToType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1963 | |
| 1964 | // Adjust the types we're converting from via the array-to-pointer |
| 1965 | // conversion, if we need to. |
| 1966 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1967 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1968 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1969 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1970 | |
| 1971 | // Canonicalize all of the types. |
| 1972 | FromType1 = Context.getCanonicalType(FromType1); |
| 1973 | ToType1 = Context.getCanonicalType(ToType1); |
| 1974 | FromType2 = Context.getCanonicalType(FromType2); |
| 1975 | ToType2 = Context.getCanonicalType(ToType2); |
| 1976 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1977 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1978 | // |
| 1979 | // If class B is derived directly or indirectly from class A and |
| 1980 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1981 | // |
| 1982 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1983 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1984 | |
| 1985 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1987 | SCS2.Second == ICK_Pointer_Conversion && |
| 1988 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1989 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1990 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1992 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1994 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1995 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1996 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1997 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1998 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1999 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2000 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2001 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2002 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2003 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2005 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2006 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2007 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2008 | return ImplicitConversionSequence::Better; |
| 2009 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2010 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2011 | |
| 2012 | if (ToIface1 && ToIface2) { |
| 2013 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2014 | return ImplicitConversionSequence::Better; |
| 2015 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2016 | return ImplicitConversionSequence::Worse; |
| 2017 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2018 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2019 | |
| 2020 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2021 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2022 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2023 | return ImplicitConversionSequence::Better; |
| 2024 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2025 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2027 | if (FromIface1 && FromIface2) { |
| 2028 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2029 | return ImplicitConversionSequence::Better; |
| 2030 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2031 | return ImplicitConversionSequence::Worse; |
| 2032 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2033 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2036 | // Compare based on reference bindings. |
| 2037 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 2038 | SCS1.Second == ICK_Derived_To_Base) { |
| 2039 | // -- binding of an expression of type C to a reference of type |
| 2040 | // B& is better than binding an expression of type C to a |
| 2041 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2042 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2043 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2044 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2045 | return ImplicitConversionSequence::Better; |
| 2046 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2047 | return ImplicitConversionSequence::Worse; |
| 2048 | } |
| 2049 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2050 | // -- binding of an expression of type B to a reference of type |
| 2051 | // A& is better than binding an expression of type C to a |
| 2052 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2053 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2054 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2055 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2056 | return ImplicitConversionSequence::Better; |
| 2057 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2058 | return ImplicitConversionSequence::Worse; |
| 2059 | } |
| 2060 | } |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2061 | |
| 2062 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2063 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2064 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2065 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2066 | const MemberPointerType * FromMemPointer1 = |
| 2067 | FromType1->getAs<MemberPointerType>(); |
| 2068 | const MemberPointerType * ToMemPointer1 = |
| 2069 | ToType1->getAs<MemberPointerType>(); |
| 2070 | const MemberPointerType * FromMemPointer2 = |
| 2071 | FromType2->getAs<MemberPointerType>(); |
| 2072 | const MemberPointerType * ToMemPointer2 = |
| 2073 | ToType2->getAs<MemberPointerType>(); |
| 2074 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2075 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2076 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2077 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2078 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2079 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2080 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2081 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2082 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2083 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2084 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2085 | return ImplicitConversionSequence::Worse; |
| 2086 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2087 | return ImplicitConversionSequence::Better; |
| 2088 | } |
| 2089 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2090 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2091 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2092 | return ImplicitConversionSequence::Better; |
| 2093 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2094 | return ImplicitConversionSequence::Worse; |
| 2095 | } |
| 2096 | } |
| 2097 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2098 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 2099 | SCS1.Second == ICK_Derived_To_Base) { |
| 2100 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2101 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2102 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2103 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2104 | return ImplicitConversionSequence::Better; |
| 2105 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2106 | return ImplicitConversionSequence::Worse; |
| 2107 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2108 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2109 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2110 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2111 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2112 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2113 | return ImplicitConversionSequence::Better; |
| 2114 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2115 | return ImplicitConversionSequence::Worse; |
| 2116 | } |
| 2117 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2118 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2119 | return ImplicitConversionSequence::Indistinguishable; |
| 2120 | } |
| 2121 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2122 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2123 | /// ToType from the expression From. Return the implicit conversion |
| 2124 | /// sequence required to pass this argument, which may be a bad |
| 2125 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2126 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2127 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2128 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | ImplicitConversionSequence |
| 2130 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2131 | bool SuppressUserConversions, bool ForceRValue, |
| 2132 | bool InOverloadResolution) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2133 | if (ToType->isReferenceType()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2134 | ImplicitConversionSequence ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2135 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2137 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2138 | SuppressUserConversions, |
| 2139 | /*AllowExplicit=*/false, |
| 2140 | ForceRValue, |
| 2141 | &ICS); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2142 | return ICS; |
| 2143 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2145 | SuppressUserConversions, |
| 2146 | /*AllowExplicit=*/false, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2147 | ForceRValue, |
| 2148 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2152 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2153 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2154 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2155 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2156 | /// differently in C++0x for this case. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2158 | AssignmentAction Action, bool Elidable) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2159 | if (!getLangOptions().CPlusPlus) { |
| 2160 | // In C, argument passing is the same as performing an assignment. |
| 2161 | QualType FromType = From->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2163 | AssignConvertType ConvTy = |
| 2164 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2165 | if (ConvTy != Compatible && |
| 2166 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2167 | ConvTy = Compatible; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2169 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2170 | FromType, From, Action); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2171 | } |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2172 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2173 | if (ToType->isReferenceType()) |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2174 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2175 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2176 | /*SuppressUserConversions=*/false, |
| 2177 | /*AllowExplicit=*/false, |
| 2178 | /*ForceRValue=*/false); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2179 | |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2180 | if (!PerformImplicitConversion(From, ToType, Action, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2181 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2182 | return false; |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2183 | if (!DiagnoseMultipleUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2184 | return Diag(From->getSourceRange().getBegin(), |
| 2185 | diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2186 | << ToType << From->getType() << Action << From->getSourceRange(); |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2187 | return true; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2190 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2191 | /// parameter of the given member function (@c Method) from the |
| 2192 | /// expression @p From. |
| 2193 | ImplicitConversionSequence |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2194 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2195 | CXXMethodDecl *Method, |
| 2196 | CXXRecordDecl *ActingContext) { |
| 2197 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2198 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2199 | // const volatile object. |
| 2200 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2201 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2202 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2203 | |
| 2204 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2205 | // to exit early. |
| 2206 | ImplicitConversionSequence ICS; |
| 2207 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2208 | ICS.setBad(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2209 | |
| 2210 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2211 | QualType FromType = OrigFromType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2212 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2213 | FromType = PT->getPointeeType(); |
| 2214 | |
| 2215 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2216 | |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2217 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2218 | // where X is the class of which the function is a member |
| 2219 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2220 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2222 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2223 | // reference binding here, that allows class rvalues to bind to |
| 2224 | // non-constant references. |
| 2225 | |
| 2226 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2227 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2228 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2229 | if (ImplicitParamType.getCVRQualifiers() |
| 2230 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2231 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2232 | ICS.Bad.init(BadConversionSequence::bad_qualifiers, |
| 2233 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2234 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2235 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2236 | |
| 2237 | // Check that we have either the same type or a derived type. It |
| 2238 | // affects the conversion rank. |
| 2239 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2240 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2241 | ICS.Standard.Second = ICK_Identity; |
| 2242 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2243 | ICS.Standard.Second = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2244 | else { |
| 2245 | ICS.Bad.init(BadConversionSequence::unrelated_class, FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2246 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2247 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2248 | |
| 2249 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2250 | ICS.setStandard(); |
| 2251 | ICS.Standard.setFromType(FromType); |
| 2252 | ICS.Standard.setToType(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2253 | ICS.Standard.ReferenceBinding = true; |
| 2254 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2255 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2256 | return ICS; |
| 2257 | } |
| 2258 | |
| 2259 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2260 | /// the implicit object parameter for the given Method with the given |
| 2261 | /// expression. |
| 2262 | bool |
| 2263 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2264 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2266 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2268 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2269 | FromRecordType = PT->getPointeeType(); |
| 2270 | DestType = Method->getThisType(Context); |
| 2271 | } else { |
| 2272 | FromRecordType = From->getType(); |
| 2273 | DestType = ImplicitParamRecordType; |
| 2274 | } |
| 2275 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2276 | // Note that we always use the true parent context when performing |
| 2277 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | ImplicitConversionSequence ICS |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2279 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2280 | Method->getParent()); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2281 | if (ICS.isBad()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2282 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2283 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2284 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2286 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2287 | CheckDerivedToBaseConversion(FromRecordType, |
| 2288 | ImplicitParamRecordType, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2289 | From->getSourceRange().getBegin(), |
| 2290 | From->getSourceRange())) |
| 2291 | return true; |
| 2292 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 4f4aab2 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2294 | /*isLvalue=*/true); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2295 | return false; |
| 2296 | } |
| 2297 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2298 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2299 | /// expression From to bool (C++0x [conv]p3). |
| 2300 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2302 | // FIXME: Are these flags correct? |
| 2303 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2305 | /*ForceRValue=*/false, |
| 2306 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2310 | /// of the expression From to bool (C++0x [conv]p3). |
| 2311 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2312 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2313 | if (!ICS.isBad()) |
| 2314 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2315 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2316 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2317 | return Diag(From->getSourceRange().getBegin(), |
| 2318 | diag::err_typecheck_bool_condition) |
| 2319 | << From->getType() << From->getSourceRange(); |
| 2320 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2323 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2324 | /// candidate functions, using the given function call arguments. If |
| 2325 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2326 | /// conversions via constructors or conversion operators. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2327 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2328 | /// hacky way to implement the overloading rules for elidable copy |
| 2329 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2330 | /// |
| 2331 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2332 | /// based on an incomplete set of function arguments. This feature is used by |
| 2333 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | void |
| 2335 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2336 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2337 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2338 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2339 | bool ForceRValue, |
| 2340 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2342 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2343 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2345 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2347 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2348 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2349 | // If we get here, it's because we're calling a member function |
| 2350 | // that is named without a member access expression (e.g., |
| 2351 | // "this->f") that was either written explicitly or created |
| 2352 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2353 | // function, e.g., X::f(). We use an empty type for the implied |
| 2354 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2355 | // is irrelevant. |
| 2356 | AddMethodCandidate(Method, Method->getParent(), |
| 2357 | QualType(), Args, NumArgs, CandidateSet, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2358 | SuppressUserConversions, ForceRValue); |
| 2359 | return; |
| 2360 | } |
| 2361 | // We treat a constructor like a non-member function, since its object |
| 2362 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2365 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2366 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2368 | // Overload resolution is always an unevaluated context. |
| 2369 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2370 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2371 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2372 | // C++ [class.copy]p3: |
| 2373 | // A member function template is never instantiated to perform the copy |
| 2374 | // of a class object to an object of its class type. |
| 2375 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2376 | if (NumArgs == 1 && |
| 2377 | Constructor->isCopyConstructorLikeSpecialization() && |
| 2378 | Context.hasSameUnqualifiedType(ClassType, Args[0]->getType())) |
| 2379 | return; |
| 2380 | } |
| 2381 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2382 | // Add this candidate |
| 2383 | CandidateSet.push_back(OverloadCandidate()); |
| 2384 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2385 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2386 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2387 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2388 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2389 | |
| 2390 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2391 | |
| 2392 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2393 | // parameters is viable only if it has an ellipsis in its parameter |
| 2394 | // list (8.3.5). |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2395 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2396 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2397 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2398 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2399 | return; |
| 2400 | } |
| 2401 | |
| 2402 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2403 | // is viable only if the (m+1)st parameter has a default argument |
| 2404 | // (8.3.6). For the purposes of overload resolution, the |
| 2405 | // parameter list is truncated on the right, so that there are |
| 2406 | // exactly m parameters. |
| 2407 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2408 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2409 | // Not enough arguments. |
| 2410 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2411 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
| 2414 | |
| 2415 | // Determine the implicit conversion sequences for each of the |
| 2416 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2417 | Candidate.Conversions.resize(NumArgs); |
| 2418 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2419 | if (ArgIdx < NumArgsInProto) { |
| 2420 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2421 | // exist for each argument an implicit conversion sequence |
| 2422 | // (13.3.3.1) that converts that argument to the corresponding |
| 2423 | // parameter of F. |
| 2424 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | Candidate.Conversions[ArgIdx] |
| 2426 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2427 | SuppressUserConversions, ForceRValue, |
| 2428 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2429 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2430 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2431 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2432 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2433 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2434 | } else { |
| 2435 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2436 | // argument for which there is no corresponding parameter is |
| 2437 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2438 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2443 | /// \brief Add all of the function declarations in the given function set to |
| 2444 | /// the overload canddiate set. |
| 2445 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2446 | Expr **Args, unsigned NumArgs, |
| 2447 | OverloadCandidateSet& CandidateSet, |
| 2448 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2450 | FEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2451 | F != FEnd; ++F) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2452 | // FIXME: using declarations |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2453 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2454 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2455 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2456 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2457 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2458 | CandidateSet, SuppressUserConversions); |
| 2459 | else |
| 2460 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2461 | SuppressUserConversions); |
| 2462 | } else { |
| 2463 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2464 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2465 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2466 | AddMethodTemplateCandidate(FunTmpl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2467 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2468 | /*FIXME: explicit args */ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2469 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2470 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2471 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2472 | else |
| 2473 | AddTemplateOverloadCandidate(FunTmpl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2474 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2475 | Args, NumArgs, CandidateSet, |
| 2476 | SuppressUserConversions); |
| 2477 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2478 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2481 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2482 | /// method) as a method candidate to the given overload set. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2483 | void Sema::AddMethodCandidate(NamedDecl *Decl, |
| 2484 | QualType ObjectType, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2485 | Expr **Args, unsigned NumArgs, |
| 2486 | OverloadCandidateSet& CandidateSet, |
| 2487 | bool SuppressUserConversions, bool ForceRValue) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2488 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2489 | |
| 2490 | if (isa<UsingShadowDecl>(Decl)) |
| 2491 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2492 | |
| 2493 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2494 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2495 | "Expected a member function template"); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2496 | AddMethodTemplateCandidate(TD, ActingContext, /*ExplicitArgs*/ 0, |
| 2497 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2498 | CandidateSet, |
| 2499 | SuppressUserConversions, |
| 2500 | ForceRValue); |
| 2501 | } else { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2502 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), ActingContext, |
| 2503 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2504 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2505 | } |
| 2506 | } |
| 2507 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2508 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2509 | /// of candidate functions, using the given function call arguments |
| 2510 | /// and the object argument (@c Object). For example, in a call |
| 2511 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2512 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2513 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2514 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2515 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2516 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 | void |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2518 | Sema::AddMethodCandidate(CXXMethodDecl *Method, CXXRecordDecl *ActingContext, |
| 2519 | QualType ObjectType, Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2520 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | bool SuppressUserConversions, bool ForceRValue) { |
| 2522 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2523 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2524 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2525 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2526 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2528 | if (!CandidateSet.isNewCandidate(Method)) |
| 2529 | return; |
| 2530 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2531 | // Overload resolution is always an unevaluated context. |
| 2532 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2533 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2534 | // Add this candidate |
| 2535 | CandidateSet.push_back(OverloadCandidate()); |
| 2536 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2537 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2538 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2539 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2540 | |
| 2541 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2542 | |
| 2543 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2544 | // parameters is viable only if it has an ellipsis in its parameter |
| 2545 | // list (8.3.5). |
| 2546 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2547 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2548 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2549 | return; |
| 2550 | } |
| 2551 | |
| 2552 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2553 | // is viable only if the (m+1)st parameter has a default argument |
| 2554 | // (8.3.6). For the purposes of overload resolution, the |
| 2555 | // parameter list is truncated on the right, so that there are |
| 2556 | // exactly m parameters. |
| 2557 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2558 | if (NumArgs < MinRequiredArgs) { |
| 2559 | // Not enough arguments. |
| 2560 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2561 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2562 | return; |
| 2563 | } |
| 2564 | |
| 2565 | Candidate.Viable = true; |
| 2566 | Candidate.Conversions.resize(NumArgs + 1); |
| 2567 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2568 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2569 | // The implicit object argument is ignored. |
| 2570 | Candidate.IgnoreObjectArgument = true; |
| 2571 | else { |
| 2572 | // Determine the implicit conversion sequence for the object |
| 2573 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2574 | Candidate.Conversions[0] |
| 2575 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2576 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2577 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2578 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | // Determine the implicit conversion sequences for each of the |
| 2584 | // arguments. |
| 2585 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2586 | if (ArgIdx < NumArgsInProto) { |
| 2587 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2588 | // exist for each argument an implicit conversion sequence |
| 2589 | // (13.3.3.1) that converts that argument to the corresponding |
| 2590 | // parameter of F. |
| 2591 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | Candidate.Conversions[ArgIdx + 1] |
| 2593 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2594 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2595 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2596 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2597 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2598 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2599 | break; |
| 2600 | } |
| 2601 | } else { |
| 2602 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2603 | // argument for which there is no corresponding parameter is |
| 2604 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2605 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2610 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2611 | /// set, using template argument deduction to produce an appropriate member |
| 2612 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2614 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2615 | CXXRecordDecl *ActingContext, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2616 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2617 | QualType ObjectType, |
| 2618 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2619 | OverloadCandidateSet& CandidateSet, |
| 2620 | bool SuppressUserConversions, |
| 2621 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2622 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2623 | return; |
| 2624 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2625 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2627 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2629 | // candidate functions in the usual way.113) A given name can refer to one |
| 2630 | // or more function templates and also to a set of overloaded non-template |
| 2631 | // functions. In such a case, the candidate functions generated from each |
| 2632 | // function template are combined with the set of non-template candidate |
| 2633 | // functions. |
| 2634 | TemplateDeductionInfo Info(Context); |
| 2635 | FunctionDecl *Specialization = 0; |
| 2636 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2637 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2638 | Args, NumArgs, Specialization, Info)) { |
| 2639 | // FIXME: Record what happened with template argument deduction, so |
| 2640 | // that we can give the user a beautiful diagnostic. |
| 2641 | (void)Result; |
| 2642 | return; |
| 2643 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2645 | // Add the function template specialization produced by template argument |
| 2646 | // deduction as a candidate. |
| 2647 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2649 | "Specialization is not a member function?"); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2650 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), ActingContext, |
| 2651 | ObjectType, Args, NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2652 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2653 | } |
| 2654 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2655 | /// \brief Add a C++ function template specialization as a candidate |
| 2656 | /// in the candidate set, using template argument deduction to produce |
| 2657 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2659 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2660 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2661 | Expr **Args, unsigned NumArgs, |
| 2662 | OverloadCandidateSet& CandidateSet, |
| 2663 | bool SuppressUserConversions, |
| 2664 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2665 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2666 | return; |
| 2667 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2668 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2670 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2672 | // candidate functions in the usual way.113) A given name can refer to one |
| 2673 | // or more function templates and also to a set of overloaded non-template |
| 2674 | // functions. In such a case, the candidate functions generated from each |
| 2675 | // function template are combined with the set of non-template candidate |
| 2676 | // functions. |
| 2677 | TemplateDeductionInfo Info(Context); |
| 2678 | FunctionDecl *Specialization = 0; |
| 2679 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2680 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2681 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2682 | // FIXME: Record what happened with template argument deduction, so |
| 2683 | // that we can give the user a beautiful diagnostic. |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2684 | (void) Result; |
| 2685 | |
| 2686 | CandidateSet.push_back(OverloadCandidate()); |
| 2687 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 2688 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 2689 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2690 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2691 | Candidate.IsSurrogate = false; |
| 2692 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2693 | return; |
| 2694 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2696 | // Add the function template specialization produced by template argument |
| 2697 | // deduction as a candidate. |
| 2698 | assert(Specialization && "Missing function template specialization?"); |
| 2699 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2700 | SuppressUserConversions, ForceRValue); |
| 2701 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2703 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2705 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2706 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2707 | /// (which may or may not be the same type as the type that the |
| 2708 | /// conversion function produces). |
| 2709 | void |
| 2710 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2711 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2712 | Expr *From, QualType ToType, |
| 2713 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2714 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2715 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2716 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2717 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2718 | return; |
| 2719 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2720 | // Overload resolution is always an unevaluated context. |
| 2721 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2722 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2723 | // Add this candidate |
| 2724 | CandidateSet.push_back(OverloadCandidate()); |
| 2725 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2726 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2727 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2728 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2729 | Candidate.FinalConversion.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2730 | Candidate.FinalConversion.setFromType(Conversion->getConversionType()); |
| 2731 | Candidate.FinalConversion.setToType(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2733 | // Determine the implicit conversion sequence for the implicit |
| 2734 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2735 | Candidate.Viable = true; |
| 2736 | Candidate.Conversions.resize(1); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2737 | Candidate.Conversions[0] |
| 2738 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 2739 | ActingContext); |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2740 | // Conversion functions to a different type in the base class is visible in |
| 2741 | // the derived class. So, a derived to base conversion should not participate |
| 2742 | // in overload resolution. |
| 2743 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2744 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2745 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2746 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2747 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2748 | return; |
| 2749 | } |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2750 | |
| 2751 | // We won't go through a user-define type conversion function to convert a |
| 2752 | // derived to base as such conversions are given Conversion Rank. They only |
| 2753 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2754 | QualType FromCanon |
| 2755 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2756 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2757 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2758 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2759 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2760 | return; |
| 2761 | } |
| 2762 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2763 | |
| 2764 | // To determine what the conversion from the result of calling the |
| 2765 | // conversion function to the type we're eventually trying to |
| 2766 | // convert to (ToType), we need to synthesize a call to the |
| 2767 | // conversion function and attempt copy initialization from it. This |
| 2768 | // makes sure that we get the right semantics with respect to |
| 2769 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2770 | // call on the stack and we don't need its arguments to be |
| 2771 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2773 | From->getLocStart()); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2774 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2775 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2776 | &ConversionRef, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2777 | |
| 2778 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2779 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2780 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2782 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2783 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2784 | ImplicitConversionSequence ICS = |
| 2785 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2786 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2787 | /*ForceRValue=*/false, |
| 2788 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2790 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2791 | case ImplicitConversionSequence::StandardConversion: |
| 2792 | Candidate.FinalConversion = ICS.Standard; |
| 2793 | break; |
| 2794 | |
| 2795 | case ImplicitConversionSequence::BadConversion: |
| 2796 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2797 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2798 | break; |
| 2799 | |
| 2800 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2802 | "Can only end up with a standard conversion sequence or failure"); |
| 2803 | } |
| 2804 | } |
| 2805 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2806 | /// \brief Adds a conversion function template specialization |
| 2807 | /// candidate to the overload set, using template argument deduction |
| 2808 | /// to deduce the template arguments of the conversion function |
| 2809 | /// template from the type that we are converting to (C++ |
| 2810 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2812 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2813 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2814 | Expr *From, QualType ToType, |
| 2815 | OverloadCandidateSet &CandidateSet) { |
| 2816 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2817 | "Only conversion function templates permitted here"); |
| 2818 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2819 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2820 | return; |
| 2821 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2822 | TemplateDeductionInfo Info(Context); |
| 2823 | CXXConversionDecl *Specialization = 0; |
| 2824 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2826 | Specialization, Info)) { |
| 2827 | // FIXME: Record what happened with template argument deduction, so |
| 2828 | // that we can give the user a beautiful diagnostic. |
| 2829 | (void)Result; |
| 2830 | return; |
| 2831 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2833 | // Add the conversion function template specialization produced by |
| 2834 | // template argument deduction as a candidate. |
| 2835 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2836 | AddConversionCandidate(Specialization, ActingDC, From, ToType, CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2839 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2840 | /// converts the given @c Object to a function pointer via the |
| 2841 | /// conversion function @c Conversion, and then attempts to call it |
| 2842 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2843 | /// the type of function that we'll eventually be calling. |
| 2844 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2845 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2846 | const FunctionProtoType *Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2847 | QualType ObjectType, |
| 2848 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2849 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2850 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2851 | return; |
| 2852 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2853 | // Overload resolution is always an unevaluated context. |
| 2854 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2855 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2856 | CandidateSet.push_back(OverloadCandidate()); |
| 2857 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2858 | Candidate.Function = 0; |
| 2859 | Candidate.Surrogate = Conversion; |
| 2860 | Candidate.Viable = true; |
| 2861 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2862 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2863 | Candidate.Conversions.resize(NumArgs + 1); |
| 2864 | |
| 2865 | // Determine the implicit conversion sequence for the implicit |
| 2866 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | ImplicitConversionSequence ObjectInit |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2868 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2869 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2870 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2871 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2872 | return; |
| 2873 | } |
| 2874 | |
| 2875 | // The first conversion is actually a user-defined conversion whose |
| 2876 | // first conversion is ObjectInit's standard conversion (which is |
| 2877 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2878 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2879 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2880 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2881 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2883 | = Candidate.Conversions[0].UserDefined.Before; |
| 2884 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2885 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2887 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2888 | |
| 2889 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2890 | // parameters is viable only if it has an ellipsis in its parameter |
| 2891 | // list (8.3.5). |
| 2892 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2893 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2894 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | } |
| 2897 | |
| 2898 | // Function types don't have any default arguments, so just check if |
| 2899 | // we have enough arguments. |
| 2900 | if (NumArgs < NumArgsInProto) { |
| 2901 | // Not enough arguments. |
| 2902 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2903 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2904 | return; |
| 2905 | } |
| 2906 | |
| 2907 | // Determine the implicit conversion sequences for each of the |
| 2908 | // arguments. |
| 2909 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2910 | if (ArgIdx < NumArgsInProto) { |
| 2911 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2912 | // exist for each argument an implicit conversion sequence |
| 2913 | // (13.3.3.1) that converts that argument to the corresponding |
| 2914 | // parameter of F. |
| 2915 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | Candidate.Conversions[ArgIdx + 1] |
| 2917 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2918 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2919 | /*ForceRValue=*/false, |
| 2920 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2921 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2922 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2923 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2924 | break; |
| 2925 | } |
| 2926 | } else { |
| 2927 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2928 | // argument for which there is no corresponding parameter is |
| 2929 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2930 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | } |
| 2934 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2935 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2936 | // operator overloading logic over to the scheme used by binary operators, which |
| 2937 | // works for template instantiation. |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2938 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2939 | SourceLocation OpLoc, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2940 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2941 | OverloadCandidateSet& CandidateSet, |
| 2942 | SourceRange OpRange) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2943 | FunctionSet Functions; |
| 2944 | |
| 2945 | QualType T1 = Args[0]->getType(); |
| 2946 | QualType T2; |
| 2947 | if (NumArgs > 1) |
| 2948 | T2 = Args[1]->getType(); |
| 2949 | |
| 2950 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2951 | if (S) |
| 2952 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2953 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, NumArgs, Functions); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2954 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2955 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2956 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | /// \brief Add overload candidates for overloaded operators that are |
| 2960 | /// member functions. |
| 2961 | /// |
| 2962 | /// Add the overloaded operator candidates that are member functions |
| 2963 | /// for the operator Op that was used in an operator expression such |
| 2964 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2965 | /// CandidateSet will store the added overload candidates. (C++ |
| 2966 | /// [over.match.oper]). |
| 2967 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2968 | SourceLocation OpLoc, |
| 2969 | Expr **Args, unsigned NumArgs, |
| 2970 | OverloadCandidateSet& CandidateSet, |
| 2971 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2972 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2973 | |
| 2974 | // C++ [over.match.oper]p3: |
| 2975 | // For a unary operator @ with an operand of a type whose |
| 2976 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2977 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2978 | // a right operand of a type whose cv-unqualified version is T2, |
| 2979 | // three sets of candidate functions, designated member |
| 2980 | // candidates, non-member candidates and built-in candidates, are |
| 2981 | // constructed as follows: |
| 2982 | QualType T1 = Args[0]->getType(); |
| 2983 | QualType T2; |
| 2984 | if (NumArgs > 1) |
| 2985 | T2 = Args[1]->getType(); |
| 2986 | |
| 2987 | // -- If T1 is a class type, the set of member candidates is the |
| 2988 | // result of the qualified lookup of T1::operator@ |
| 2989 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2990 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2991 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2992 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2993 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2994 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2996 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 2997 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 2998 | Operators.suppressDiagnostics(); |
| 2999 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3001 | OperEnd = Operators.end(); |
| 3002 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3003 | ++Oper) |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3004 | AddMethodCandidate(*Oper, Args[0]->getType(), |
| 3005 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3006 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3007 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3010 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3011 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3012 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3013 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3014 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3015 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3016 | /// (at the beginning of the argument list) that will be contextually |
| 3017 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3018 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3019 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3020 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3021 | bool IsAssignmentOperator, |
| 3022 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3023 | // Overload resolution is always an unevaluated context. |
| 3024 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3025 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3026 | // Add this candidate |
| 3027 | CandidateSet.push_back(OverloadCandidate()); |
| 3028 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 3029 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3030 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3031 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3032 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3033 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3034 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3035 | |
| 3036 | // Determine the implicit conversion sequences for each of the |
| 3037 | // arguments. |
| 3038 | Candidate.Viable = true; |
| 3039 | Candidate.Conversions.resize(NumArgs); |
| 3040 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3041 | // C++ [over.match.oper]p4: |
| 3042 | // For the built-in assignment operators, conversions of the |
| 3043 | // left operand are restricted as follows: |
| 3044 | // -- no temporaries are introduced to hold the left operand, and |
| 3045 | // -- no user-defined conversions are applied to the left |
| 3046 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3047 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3048 | // |
| 3049 | // We block these conversions by turning off user-defined |
| 3050 | // conversions, since that is the only way that initialization of |
| 3051 | // a reference to a non-class type can occur from something that |
| 3052 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3053 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3055 | "Contextual conversion to bool requires bool type"); |
| 3056 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3057 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | Candidate.Conversions[ArgIdx] |
| 3059 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3060 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3061 | /*ForceRValue=*/false, |
| 3062 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3063 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3064 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3065 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3066 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3067 | break; |
| 3068 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3073 | /// candidate operator functions for built-in operators (C++ |
| 3074 | /// [over.built]). The types are separated into pointer types and |
| 3075 | /// enumeration types. |
| 3076 | class BuiltinCandidateTypeSet { |
| 3077 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3078 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3079 | |
| 3080 | /// PointerTypes - The set of pointer types that will be used in the |
| 3081 | /// built-in candidates. |
| 3082 | TypeSet PointerTypes; |
| 3083 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3084 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3085 | /// used in the built-in candidates. |
| 3086 | TypeSet MemberPointerTypes; |
| 3087 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3088 | /// EnumerationTypes - The set of enumeration types that will be |
| 3089 | /// used in the built-in candidates. |
| 3090 | TypeSet EnumerationTypes; |
| 3091 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3092 | /// Sema - The semantic analysis instance where we are building the |
| 3093 | /// candidate type set. |
| 3094 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3096 | /// Context - The AST context in which we will build the type sets. |
| 3097 | ASTContext &Context; |
| 3098 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3099 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3100 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3101 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3102 | |
| 3103 | public: |
| 3104 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3105 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3106 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3108 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3109 | |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3110 | void AddTypesConvertedFrom(QualType Ty, |
| 3111 | SourceLocation Loc, |
| 3112 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3113 | bool AllowExplicitConversions, |
| 3114 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3115 | |
| 3116 | /// pointer_begin - First pointer type found; |
| 3117 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3118 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3119 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3120 | iterator pointer_end() { return PointerTypes.end(); } |
| 3121 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3122 | /// member_pointer_begin - First member pointer type found; |
| 3123 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3124 | |
| 3125 | /// member_pointer_end - Past the last member pointer type found; |
| 3126 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3127 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3128 | /// enumeration_begin - First enumeration type found; |
| 3129 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3130 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3131 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3132 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3133 | }; |
| 3134 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3135 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3136 | /// the set of pointer types along with any more-qualified variants of |
| 3137 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3138 | /// will add "int const *", "int const volatile *", "int const |
| 3139 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3140 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3141 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3142 | /// |
| 3143 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3144 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3145 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3146 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3147 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3148 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3149 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3150 | return false; |
| 3151 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3152 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3153 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3154 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3155 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3156 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3157 | // (the qualifier would sink to the element type), and for another, the |
| 3158 | // only overload situation where it matters is subscript or pointer +- int, |
| 3159 | // and those shouldn't have qualifier variants anyway. |
| 3160 | if (PointeeTy->isArrayType()) |
| 3161 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3162 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3163 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3164 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3165 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3166 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3167 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3168 | // Iterate through all strict supersets of BaseCVR. |
| 3169 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3170 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3171 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3172 | // in the types. |
| 3173 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3174 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3175 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3176 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | return true; |
| 3180 | } |
| 3181 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3182 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3183 | /// to the set of pointer types along with any more-qualified variants of |
| 3184 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3185 | /// will add "int const *", "int const volatile *", "int const |
| 3186 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3187 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3188 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3189 | /// |
| 3190 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3191 | bool |
| 3192 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3193 | QualType Ty) { |
| 3194 | // Insert this type. |
| 3195 | if (!MemberPointerTypes.insert(Ty)) |
| 3196 | return false; |
| 3197 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3198 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3199 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3200 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3201 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3202 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3203 | // (the qualifier would sink to the element type), and for another, the |
| 3204 | // only overload situation where it matters is subscript or pointer +- int, |
| 3205 | // and those shouldn't have qualifier variants anyway. |
| 3206 | if (PointeeTy->isArrayType()) |
| 3207 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3208 | const Type *ClassTy = PointerTy->getClass(); |
| 3209 | |
| 3210 | // Iterate through all strict supersets of the pointee type's CVR |
| 3211 | // qualifiers. |
| 3212 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3213 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3214 | if ((CVR | BaseCVR) != CVR) continue; |
| 3215 | |
| 3216 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3217 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | return true; |
| 3221 | } |
| 3222 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3223 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3224 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3225 | /// primarily interested in pointer types and enumeration types. We also |
| 3226 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3227 | /// AllowUserConversions is true if we should look at the conversion |
| 3228 | /// functions of a class type, and AllowExplicitConversions if we |
| 3229 | /// should also include the explicit conversion functions of a class |
| 3230 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3232 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3233 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3234 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3235 | bool AllowExplicitConversions, |
| 3236 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3237 | // Only deal with canonical types. |
| 3238 | Ty = Context.getCanonicalType(Ty); |
| 3239 | |
| 3240 | // Look through reference types; they aren't part of the type of an |
| 3241 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3242 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3243 | Ty = RefTy->getPointeeType(); |
| 3244 | |
| 3245 | // We don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3246 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3247 | |
Sebastian Redl | 65ae200 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3248 | // If we're dealing with an array type, decay to the pointer. |
| 3249 | if (Ty->isArrayType()) |
| 3250 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3251 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3252 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3253 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3254 | |
| 3255 | // Insert our type, and its more-qualified variants, into the set |
| 3256 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3257 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3258 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3259 | } else if (Ty->isMemberPointerType()) { |
| 3260 | // Member pointers are far easier, since the pointee can't be converted. |
| 3261 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3262 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3263 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3264 | EnumerationTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3265 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3266 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3267 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3268 | // No conversion functions in incomplete types. |
| 3269 | return; |
| 3270 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3272 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3273 | const UnresolvedSet *Conversions |
Fariborz Jahanian | ae01f78 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3274 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3275 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 3276 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3277 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3279 | // about which builtin types we can convert to. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3280 | if (isa<FunctionTemplateDecl>(*I)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3281 | continue; |
| 3282 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3283 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3284 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3285 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3286 | VisibleQuals); |
| 3287 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | } |
| 3291 | } |
| 3292 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3293 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3294 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3295 | /// given type to the candidate set. |
| 3296 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3297 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3299 | unsigned NumArgs, |
| 3300 | OverloadCandidateSet &CandidateSet) { |
| 3301 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3303 | // T& operator=(T&, T) |
| 3304 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3305 | ParamTypes[1] = T; |
| 3306 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3307 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3308 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3309 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3310 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3311 | ParamTypes[0] |
| 3312 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3313 | ParamTypes[1] = T; |
| 3314 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3316 | } |
| 3317 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3319 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3320 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3321 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3322 | Qualifiers VRQuals; |
| 3323 | const RecordType *TyRec; |
| 3324 | if (const MemberPointerType *RHSMPType = |
| 3325 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3326 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3327 | else |
| 3328 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3329 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3330 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3331 | VRQuals.addVolatile(); |
| 3332 | VRQuals.addRestrict(); |
| 3333 | return VRQuals; |
| 3334 | } |
| 3335 | |
| 3336 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3337 | const UnresolvedSet *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3338 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3339 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3340 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 3341 | E = Conversions->end(); I != E; ++I) { |
| 3342 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*I)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3343 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3344 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3345 | CanTy = ResTypeRef->getPointeeType(); |
| 3346 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3347 | // as see them. |
| 3348 | bool done = false; |
| 3349 | while (!done) { |
| 3350 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3351 | CanTy = ResTypePtr->getPointeeType(); |
| 3352 | else if (const MemberPointerType *ResTypeMPtr = |
| 3353 | CanTy->getAs<MemberPointerType>()) |
| 3354 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3355 | else |
| 3356 | done = true; |
| 3357 | if (CanTy.isVolatileQualified()) |
| 3358 | VRQuals.addVolatile(); |
| 3359 | if (CanTy.isRestrictQualified()) |
| 3360 | VRQuals.addRestrict(); |
| 3361 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3362 | return VRQuals; |
| 3363 | } |
| 3364 | } |
| 3365 | } |
| 3366 | return VRQuals; |
| 3367 | } |
| 3368 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3369 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3370 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3371 | /// on the operator @p Op and the arguments given. For example, if the |
| 3372 | /// operator is a binary '+', this routine might add "int |
| 3373 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3374 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3375 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3376 | SourceLocation OpLoc, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3377 | Expr **Args, unsigned NumArgs, |
| 3378 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3379 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3380 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3381 | // that the first few of these types are the promoted integral |
| 3382 | // types; these types need to be first. |
| 3383 | // FIXME: What about complex? |
| 3384 | const unsigned FirstIntegralType = 0; |
| 3385 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3387 | LastPromotedIntegralType = 13; |
| 3388 | const unsigned FirstPromotedArithmeticType = 7, |
| 3389 | LastPromotedArithmeticType = 16; |
| 3390 | const unsigned NumArithmeticTypes = 16; |
| 3391 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3393 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3394 | Context.SignedCharTy, Context.ShortTy, |
| 3395 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3396 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3397 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3398 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3399 | }; |
Douglas Gregor | b8440a7 | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3400 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3401 | "Invalid first promoted integral type"); |
| 3402 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3403 | == Context.UnsignedLongLongTy && |
| 3404 | "Invalid last promoted integral type"); |
| 3405 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3406 | "Invalid first promoted arithmetic type"); |
| 3407 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3408 | == Context.LongDoubleTy && |
| 3409 | "Invalid last promoted arithmetic type"); |
| 3410 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3411 | // Find all of the types that the arguments can convert to, but only |
| 3412 | // if the operator we're looking at has built-in operator candidates |
| 3413 | // that make use of these types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3414 | Qualifiers VisibleTypeConversionsQuals; |
| 3415 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3416 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3417 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3418 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3419 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3420 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3421 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3422 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3423 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3424 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3425 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3426 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3427 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3428 | OpLoc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3429 | true, |
| 3430 | (Op == OO_Exclaim || |
| 3431 | Op == OO_AmpAmp || |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3432 | Op == OO_PipePipe), |
| 3433 | VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | bool isComparison = false; |
| 3437 | switch (Op) { |
| 3438 | case OO_None: |
| 3439 | case NUM_OVERLOADED_OPERATORS: |
| 3440 | assert(false && "Expected an overloaded operator"); |
| 3441 | break; |
| 3442 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3443 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3445 | goto UnaryStar; |
| 3446 | else |
| 3447 | goto BinaryStar; |
| 3448 | break; |
| 3449 | |
| 3450 | case OO_Plus: // '+' is either unary or binary |
| 3451 | if (NumArgs == 1) |
| 3452 | goto UnaryPlus; |
| 3453 | else |
| 3454 | goto BinaryPlus; |
| 3455 | break; |
| 3456 | |
| 3457 | case OO_Minus: // '-' is either unary or binary |
| 3458 | if (NumArgs == 1) |
| 3459 | goto UnaryMinus; |
| 3460 | else |
| 3461 | goto BinaryMinus; |
| 3462 | break; |
| 3463 | |
| 3464 | case OO_Amp: // '&' is either unary or binary |
| 3465 | if (NumArgs == 1) |
| 3466 | goto UnaryAmp; |
| 3467 | else |
| 3468 | goto BinaryAmp; |
| 3469 | |
| 3470 | case OO_PlusPlus: |
| 3471 | case OO_MinusMinus: |
| 3472 | // C++ [over.built]p3: |
| 3473 | // |
| 3474 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3475 | // is either volatile or empty, there exist candidate operator |
| 3476 | // functions of the form |
| 3477 | // |
| 3478 | // VQ T& operator++(VQ T&); |
| 3479 | // T operator++(VQ T&, int); |
| 3480 | // |
| 3481 | // C++ [over.built]p4: |
| 3482 | // |
| 3483 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3484 | // than bool, and VQ is either volatile or empty, there exist |
| 3485 | // candidate operator functions of the form |
| 3486 | // |
| 3487 | // VQ T& operator--(VQ T&); |
| 3488 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3490 | Arith < NumArithmeticTypes; ++Arith) { |
| 3491 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3492 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3493 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3494 | |
| 3495 | // Non-volatile version. |
| 3496 | if (NumArgs == 1) |
| 3497 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3498 | else |
| 3499 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3500 | // heuristic to reduce number of builtin candidates in the set. |
| 3501 | // Add volatile version only if there are conversions to a volatile type. |
| 3502 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3503 | // Volatile version |
| 3504 | ParamTypes[0] |
| 3505 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3506 | if (NumArgs == 1) |
| 3507 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3508 | else |
| 3509 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3510 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | // C++ [over.built]p5: |
| 3514 | // |
| 3515 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3516 | // cv-unqualified object type, and VQ is either volatile or |
| 3517 | // empty, there exist candidate operator functions of the form |
| 3518 | // |
| 3519 | // T*VQ& operator++(T*VQ&); |
| 3520 | // T*VQ& operator--(T*VQ&); |
| 3521 | // T* operator++(T*VQ&, int); |
| 3522 | // T* operator--(T*VQ&, int); |
| 3523 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3524 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3525 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3526 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3527 | continue; |
| 3528 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | QualType ParamTypes[2] = { |
| 3530 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3531 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3533 | // Without volatile |
| 3534 | if (NumArgs == 1) |
| 3535 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3536 | else |
| 3537 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3538 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3539 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3540 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3541 | // With volatile |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3542 | ParamTypes[0] |
| 3543 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3544 | if (NumArgs == 1) |
| 3545 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3546 | else |
| 3547 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3548 | } |
| 3549 | } |
| 3550 | break; |
| 3551 | |
| 3552 | UnaryStar: |
| 3553 | // C++ [over.built]p6: |
| 3554 | // For every cv-qualified or cv-unqualified object type T, there |
| 3555 | // exist candidate operator functions of the form |
| 3556 | // |
| 3557 | // T& operator*(T*); |
| 3558 | // |
| 3559 | // C++ [over.built]p7: |
| 3560 | // For every function type T, there exist candidate operator |
| 3561 | // functions of the form |
| 3562 | // T& operator*(T*); |
| 3563 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3564 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3565 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3566 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3567 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3568 | &ParamTy, Args, 1, CandidateSet); |
| 3569 | } |
| 3570 | break; |
| 3571 | |
| 3572 | UnaryPlus: |
| 3573 | // C++ [over.built]p8: |
| 3574 | // For every type T, there exist candidate operator functions of |
| 3575 | // the form |
| 3576 | // |
| 3577 | // T* operator+(T*); |
| 3578 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3579 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3580 | QualType ParamTy = *Ptr; |
| 3581 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3582 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3583 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3584 | // Fall through |
| 3585 | |
| 3586 | UnaryMinus: |
| 3587 | // C++ [over.built]p9: |
| 3588 | // For every promoted arithmetic type T, there exist candidate |
| 3589 | // operator functions of the form |
| 3590 | // |
| 3591 | // T operator+(T); |
| 3592 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3593 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3594 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3595 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3596 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3597 | } |
| 3598 | break; |
| 3599 | |
| 3600 | case OO_Tilde: |
| 3601 | // C++ [over.built]p10: |
| 3602 | // For every promoted integral type T, there exist candidate |
| 3603 | // operator functions of the form |
| 3604 | // |
| 3605 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3607 | Int < LastPromotedIntegralType; ++Int) { |
| 3608 | QualType IntTy = ArithmeticTypes[Int]; |
| 3609 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3610 | } |
| 3611 | break; |
| 3612 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3613 | case OO_New: |
| 3614 | case OO_Delete: |
| 3615 | case OO_Array_New: |
| 3616 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3617 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3618 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3619 | break; |
| 3620 | |
| 3621 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3622 | UnaryAmp: |
| 3623 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3624 | // C++ [over.match.oper]p3: |
| 3625 | // -- For the operator ',', the unary operator '&', or the |
| 3626 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3627 | break; |
| 3628 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3629 | case OO_EqualEqual: |
| 3630 | case OO_ExclaimEqual: |
| 3631 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | // For every pointer to member type T, there exist candidate operator |
| 3633 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3634 | // |
| 3635 | // bool operator==(T,T); |
| 3636 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3637 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3638 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3639 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3640 | MemPtr != MemPtrEnd; |
| 3641 | ++MemPtr) { |
| 3642 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3643 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3644 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3646 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3647 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3648 | case OO_Less: |
| 3649 | case OO_Greater: |
| 3650 | case OO_LessEqual: |
| 3651 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3652 | // C++ [over.built]p15: |
| 3653 | // |
| 3654 | // For every pointer or enumeration type T, there exist |
| 3655 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3657 | // bool operator<(T, T); |
| 3658 | // bool operator>(T, T); |
| 3659 | // bool operator<=(T, T); |
| 3660 | // bool operator>=(T, T); |
| 3661 | // bool operator==(T, T); |
| 3662 | // bool operator!=(T, T); |
| 3663 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3664 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3665 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3666 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3667 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3669 | = CandidateTypes.enumeration_begin(); |
| 3670 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3671 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3672 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3673 | } |
| 3674 | |
| 3675 | // Fall through. |
| 3676 | isComparison = true; |
| 3677 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3678 | BinaryPlus: |
| 3679 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3680 | if (!isComparison) { |
| 3681 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3682 | |
| 3683 | // C++ [over.built]p13: |
| 3684 | // |
| 3685 | // For every cv-qualified or cv-unqualified object type T |
| 3686 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3688 | // T* operator+(T*, ptrdiff_t); |
| 3689 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3690 | // T* operator-(T*, ptrdiff_t); |
| 3691 | // T* operator+(ptrdiff_t, T*); |
| 3692 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3693 | // |
| 3694 | // C++ [over.built]p14: |
| 3695 | // |
| 3696 | // For every T, where T is a pointer to object type, there |
| 3697 | // exist candidate operator functions of the form |
| 3698 | // |
| 3699 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3701 | = CandidateTypes.pointer_begin(); |
| 3702 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3703 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3704 | |
| 3705 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3706 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3707 | |
| 3708 | if (Op == OO_Plus) { |
| 3709 | // T* operator+(ptrdiff_t, T*); |
| 3710 | ParamTypes[0] = ParamTypes[1]; |
| 3711 | ParamTypes[1] = *Ptr; |
| 3712 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3713 | } else { |
| 3714 | // ptrdiff_t operator-(T, T); |
| 3715 | ParamTypes[1] = *Ptr; |
| 3716 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3717 | Args, 2, CandidateSet); |
| 3718 | } |
| 3719 | } |
| 3720 | } |
| 3721 | // Fall through |
| 3722 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3723 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3724 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3725 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3726 | // C++ [over.built]p12: |
| 3727 | // |
| 3728 | // For every pair of promoted arithmetic types L and R, there |
| 3729 | // exist candidate operator functions of the form |
| 3730 | // |
| 3731 | // LR operator*(L, R); |
| 3732 | // LR operator/(L, R); |
| 3733 | // LR operator+(L, R); |
| 3734 | // LR operator-(L, R); |
| 3735 | // bool operator<(L, R); |
| 3736 | // bool operator>(L, R); |
| 3737 | // bool operator<=(L, R); |
| 3738 | // bool operator>=(L, R); |
| 3739 | // bool operator==(L, R); |
| 3740 | // bool operator!=(L, R); |
| 3741 | // |
| 3742 | // where LR is the result of the usual arithmetic conversions |
| 3743 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3744 | // |
| 3745 | // C++ [over.built]p24: |
| 3746 | // |
| 3747 | // For every pair of promoted arithmetic types L and R, there exist |
| 3748 | // candidate operator functions of the form |
| 3749 | // |
| 3750 | // LR operator?(bool, L, R); |
| 3751 | // |
| 3752 | // where LR is the result of the usual arithmetic conversions |
| 3753 | // between types L and R. |
| 3754 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3756 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3758 | Right < LastPromotedArithmeticType; ++Right) { |
| 3759 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3760 | QualType Result |
| 3761 | = isComparison |
| 3762 | ? Context.BoolTy |
| 3763 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3764 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3765 | } |
| 3766 | } |
| 3767 | break; |
| 3768 | |
| 3769 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3770 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3771 | case OO_Caret: |
| 3772 | case OO_Pipe: |
| 3773 | case OO_LessLess: |
| 3774 | case OO_GreaterGreater: |
| 3775 | // C++ [over.built]p17: |
| 3776 | // |
| 3777 | // For every pair of promoted integral types L and R, there |
| 3778 | // exist candidate operator functions of the form |
| 3779 | // |
| 3780 | // LR operator%(L, R); |
| 3781 | // LR operator&(L, R); |
| 3782 | // LR operator^(L, R); |
| 3783 | // LR operator|(L, R); |
| 3784 | // L operator<<(L, R); |
| 3785 | // L operator>>(L, R); |
| 3786 | // |
| 3787 | // where LR is the result of the usual arithmetic conversions |
| 3788 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3790 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3792 | Right < LastPromotedIntegralType; ++Right) { |
| 3793 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3794 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3795 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3796 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3797 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3798 | } |
| 3799 | } |
| 3800 | break; |
| 3801 | |
| 3802 | case OO_Equal: |
| 3803 | // C++ [over.built]p20: |
| 3804 | // |
| 3805 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3806 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3807 | // empty, there exist candidate operator functions of the form |
| 3808 | // |
| 3809 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3810 | for (BuiltinCandidateTypeSet::iterator |
| 3811 | Enum = CandidateTypes.enumeration_begin(), |
| 3812 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3813 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3815 | CandidateSet); |
| 3816 | for (BuiltinCandidateTypeSet::iterator |
| 3817 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3818 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3819 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3820 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3821 | CandidateSet); |
| 3822 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3823 | |
| 3824 | case OO_PlusEqual: |
| 3825 | case OO_MinusEqual: |
| 3826 | // C++ [over.built]p19: |
| 3827 | // |
| 3828 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3829 | // volatile or empty, there exist candidate operator functions |
| 3830 | // of the form |
| 3831 | // |
| 3832 | // T*VQ& operator=(T*VQ&, T*); |
| 3833 | // |
| 3834 | // C++ [over.built]p21: |
| 3835 | // |
| 3836 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3837 | // cv-unqualified object type and VQ is either volatile or |
| 3838 | // empty, there exist candidate operator functions of the form |
| 3839 | // |
| 3840 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3841 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3842 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3843 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3844 | QualType ParamTypes[2]; |
| 3845 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3846 | |
| 3847 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3848 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3849 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3850 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3851 | |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3852 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3853 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3854 | // volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3855 | ParamTypes[0] |
| 3856 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3857 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3858 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3859 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3860 | } |
| 3861 | // Fall through. |
| 3862 | |
| 3863 | case OO_StarEqual: |
| 3864 | case OO_SlashEqual: |
| 3865 | // C++ [over.built]p18: |
| 3866 | // |
| 3867 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3868 | // VQ is either volatile or empty, and R is a promoted |
| 3869 | // arithmetic type, there exist candidate operator functions of |
| 3870 | // the form |
| 3871 | // |
| 3872 | // VQ L& operator=(VQ L&, R); |
| 3873 | // VQ L& operator*=(VQ L&, R); |
| 3874 | // VQ L& operator/=(VQ L&, R); |
| 3875 | // VQ L& operator+=(VQ L&, R); |
| 3876 | // VQ L& operator-=(VQ L&, R); |
| 3877 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3879 | Right < LastPromotedArithmeticType; ++Right) { |
| 3880 | QualType ParamTypes[2]; |
| 3881 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3882 | |
| 3883 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3884 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3885 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3886 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3887 | |
| 3888 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3889 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3890 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3891 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3892 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3893 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3894 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3895 | } |
| 3896 | } |
| 3897 | break; |
| 3898 | |
| 3899 | case OO_PercentEqual: |
| 3900 | case OO_LessLessEqual: |
| 3901 | case OO_GreaterGreaterEqual: |
| 3902 | case OO_AmpEqual: |
| 3903 | case OO_CaretEqual: |
| 3904 | case OO_PipeEqual: |
| 3905 | // C++ [over.built]p22: |
| 3906 | // |
| 3907 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3908 | // is either volatile or empty, and R is a promoted integral |
| 3909 | // type, there exist candidate operator functions of the form |
| 3910 | // |
| 3911 | // VQ L& operator%=(VQ L&, R); |
| 3912 | // VQ L& operator<<=(VQ L&, R); |
| 3913 | // VQ L& operator>>=(VQ L&, R); |
| 3914 | // VQ L& operator&=(VQ L&, R); |
| 3915 | // VQ L& operator^=(VQ L&, R); |
| 3916 | // VQ L& operator|=(VQ L&, R); |
| 3917 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3919 | Right < LastPromotedIntegralType; ++Right) { |
| 3920 | QualType ParamTypes[2]; |
| 3921 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3922 | |
| 3923 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3924 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3925 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a4a9334 | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 3926 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3927 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3928 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3929 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 3930 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3931 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3932 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3933 | } |
| 3934 | } |
| 3935 | break; |
| 3936 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3937 | case OO_Exclaim: { |
| 3938 | // C++ [over.operator]p23: |
| 3939 | // |
| 3940 | // There also exist candidate operator functions of the form |
| 3941 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3942 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3943 | // bool operator&&(bool, bool); [BELOW] |
| 3944 | // bool operator||(bool, bool); [BELOW] |
| 3945 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3946 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3947 | /*IsAssignmentOperator=*/false, |
| 3948 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3949 | break; |
| 3950 | } |
| 3951 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3952 | case OO_AmpAmp: |
| 3953 | case OO_PipePipe: { |
| 3954 | // C++ [over.operator]p23: |
| 3955 | // |
| 3956 | // There also exist candidate operator functions of the form |
| 3957 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3958 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3959 | // bool operator&&(bool, bool); |
| 3960 | // bool operator||(bool, bool); |
| 3961 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3962 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3963 | /*IsAssignmentOperator=*/false, |
| 3964 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3965 | break; |
| 3966 | } |
| 3967 | |
| 3968 | case OO_Subscript: |
| 3969 | // C++ [over.built]p13: |
| 3970 | // |
| 3971 | // For every cv-qualified or cv-unqualified object type T there |
| 3972 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3974 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3975 | // T& operator[](T*, ptrdiff_t); |
| 3976 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3977 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3978 | // T& operator[](ptrdiff_t, T*); |
| 3979 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3980 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3981 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3982 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3983 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3984 | |
| 3985 | // T& operator[](T*, ptrdiff_t) |
| 3986 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3987 | |
| 3988 | // T& operator[](ptrdiff_t, T*); |
| 3989 | ParamTypes[0] = ParamTypes[1]; |
| 3990 | ParamTypes[1] = *Ptr; |
| 3991 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3992 | } |
| 3993 | break; |
| 3994 | |
| 3995 | case OO_ArrowStar: |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3996 | // C++ [over.built]p11: |
| 3997 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 3998 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 3999 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4000 | // there exist candidate operator functions of the form |
| 4001 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4002 | // where CV12 is the union of CV1 and CV2. |
| 4003 | { |
| 4004 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4005 | CandidateTypes.pointer_begin(); |
| 4006 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4007 | QualType C1Ty = (*Ptr); |
| 4008 | QualType C1; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4009 | QualifierCollector Q1; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4010 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4011 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4012 | if (!isa<RecordType>(C1)) |
| 4013 | continue; |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4014 | // heuristic to reduce number of builtin candidates in the set. |
| 4015 | // Add volatile/restrict version only if there are conversions to a |
| 4016 | // volatile/restrict type. |
| 4017 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4018 | continue; |
| 4019 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4020 | continue; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4021 | } |
| 4022 | for (BuiltinCandidateTypeSet::iterator |
| 4023 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4024 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4025 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4026 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4027 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 12df37c | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4028 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4029 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4030 | break; |
| 4031 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4032 | // build CV12 T& |
| 4033 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4034 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4035 | T.isVolatileQualified()) |
| 4036 | continue; |
| 4037 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4038 | T.isRestrictQualified()) |
| 4039 | continue; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4040 | T = Q1.apply(T); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4041 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4042 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4043 | } |
| 4044 | } |
| 4045 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4046 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4047 | |
| 4048 | case OO_Conditional: |
| 4049 | // Note that we don't consider the first argument, since it has been |
| 4050 | // contextually converted to bool long ago. The candidates below are |
| 4051 | // therefore added as binary. |
| 4052 | // |
| 4053 | // C++ [over.built]p24: |
| 4054 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4055 | // there exist candidate operator functions of the form |
| 4056 | // |
| 4057 | // T operator?(bool, T, T); |
| 4058 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4059 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4060 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4061 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4062 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4063 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4064 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4065 | CandidateTypes.member_pointer_begin(), |
| 4066 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4067 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4068 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4069 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4070 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4074 | /// \brief Add function candidates found via argument-dependent lookup |
| 4075 | /// to the set of overloading candidates. |
| 4076 | /// |
| 4077 | /// This routine performs argument-dependent name lookup based on the |
| 4078 | /// given function name (which may also be an operator name) and adds |
| 4079 | /// all of the overload candidates found by ADL to the overload |
| 4080 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4081 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4082 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 4083 | Expr **Args, unsigned NumArgs, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4084 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4085 | OverloadCandidateSet& CandidateSet, |
| 4086 | bool PartialOverloading) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4087 | FunctionSet Functions; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4088 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4089 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 4090 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4091 | // Record all of the function candidates that we've already |
| 4092 | // added to the overload set, so that we don't add those same |
| 4093 | // candidates a second time. |
| 4094 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4095 | CandEnd = CandidateSet.end(); |
| 4096 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4097 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4098 | Functions.insert(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4099 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4100 | Functions.insert(FunTmpl); |
| 4101 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4102 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4103 | // FIXME: Pass in the explicit template arguments? |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 4104 | ArgumentDependentLookup(Name, /*Operator*/false, Args, NumArgs, Functions); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4105 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4106 | // Erase all of the candidates we already knew about. |
| 4107 | // FIXME: This is suboptimal. Is there a better way? |
| 4108 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4109 | CandEnd = CandidateSet.end(); |
| 4110 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4111 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4112 | Functions.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4113 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4114 | Functions.erase(FunTmpl); |
| 4115 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4116 | |
| 4117 | // For each of the ADL candidates we found, add it to the overload |
| 4118 | // set. |
| 4119 | for (FunctionSet::iterator Func = Functions.begin(), |
| 4120 | FuncEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4121 | Func != FuncEnd; ++Func) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4122 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4123 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4124 | continue; |
| 4125 | |
| 4126 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 4127 | false, false, PartialOverloading); |
| 4128 | } else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4129 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4130 | ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4131 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4132 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4135 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4136 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4137 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4138 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4140 | // Define viable functions to be better candidates than non-viable |
| 4141 | // functions. |
| 4142 | if (!Cand2.Viable) |
| 4143 | return Cand1.Viable; |
| 4144 | else if (!Cand1.Viable) |
| 4145 | return false; |
| 4146 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4147 | // C++ [over.match.best]p1: |
| 4148 | // |
| 4149 | // -- if F is a static member function, ICS1(F) is defined such |
| 4150 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4151 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4152 | // better nor worse than ICS1(F). |
| 4153 | unsigned StartArg = 0; |
| 4154 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4155 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4156 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4157 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4158 | // A viable function F1 is defined to be a better function than another |
| 4159 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4160 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4161 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4162 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4163 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4164 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4165 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4166 | Cand2.Conversions[ArgIdx])) { |
| 4167 | case ImplicitConversionSequence::Better: |
| 4168 | // Cand1 has a better conversion sequence. |
| 4169 | HasBetterConversion = true; |
| 4170 | break; |
| 4171 | |
| 4172 | case ImplicitConversionSequence::Worse: |
| 4173 | // Cand1 can't be better than Cand2. |
| 4174 | return false; |
| 4175 | |
| 4176 | case ImplicitConversionSequence::Indistinguishable: |
| 4177 | // Do nothing. |
| 4178 | break; |
| 4179 | } |
| 4180 | } |
| 4181 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4182 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4183 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4184 | if (HasBetterConversion) |
| 4185 | return true; |
| 4186 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4187 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4188 | // specialization, or, if not that, |
| 4189 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4190 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4191 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | |
| 4193 | // -- F1 and F2 are function template specializations, and the function |
| 4194 | // template for F1 is more specialized than the template for F2 |
| 4195 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4196 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4197 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4198 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4199 | if (FunctionTemplateDecl *BetterTemplate |
| 4200 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4201 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 6010da0 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4202 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4203 | : TPOC_Call)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4204 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4205 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4206 | // -- the context is an initialization by user-defined conversion |
| 4207 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4208 | // from the return type of F1 to the destination type (i.e., |
| 4209 | // the type of the entity being initialized) is a better |
| 4210 | // conversion sequence than the standard conversion sequence |
| 4211 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | if (Cand1.Function && Cand2.Function && |
| 4213 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4214 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4215 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4216 | Cand2.FinalConversion)) { |
| 4217 | case ImplicitConversionSequence::Better: |
| 4218 | // Cand1 has a better conversion sequence. |
| 4219 | return true; |
| 4220 | |
| 4221 | case ImplicitConversionSequence::Worse: |
| 4222 | // Cand1 can't be better than Cand2. |
| 4223 | return false; |
| 4224 | |
| 4225 | case ImplicitConversionSequence::Indistinguishable: |
| 4226 | // Do nothing |
| 4227 | break; |
| 4228 | } |
| 4229 | } |
| 4230 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4231 | return false; |
| 4232 | } |
| 4233 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4234 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4235 | /// within an overload candidate set. |
| 4236 | /// |
| 4237 | /// \param CandidateSet the set of candidate functions. |
| 4238 | /// |
| 4239 | /// \param Loc the location of the function name (or operator symbol) for |
| 4240 | /// which overload resolution occurs. |
| 4241 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4243 | /// function, Best points to the candidate function found. |
| 4244 | /// |
| 4245 | /// \returns The result of overload resolution. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4246 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4247 | SourceLocation Loc, |
| 4248 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4249 | // Find the best viable function. |
| 4250 | Best = CandidateSet.end(); |
| 4251 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4252 | Cand != CandidateSet.end(); ++Cand) { |
| 4253 | if (Cand->Viable) { |
| 4254 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 4255 | Best = Cand; |
| 4256 | } |
| 4257 | } |
| 4258 | |
| 4259 | // If we didn't find any viable functions, abort. |
| 4260 | if (Best == CandidateSet.end()) |
| 4261 | return OR_No_Viable_Function; |
| 4262 | |
| 4263 | // Make sure that this function is better than every other viable |
| 4264 | // function. If not, we have an ambiguity. |
| 4265 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4266 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4267 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4268 | Cand != Best && |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4269 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 4270 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4271 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4272 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4273 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4275 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4276 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4277 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4278 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4279 | return OR_Deleted; |
| 4280 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4281 | // C++ [basic.def.odr]p2: |
| 4282 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4284 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4285 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4286 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4287 | if (Best->Function) |
| 4288 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4289 | return OR_Success; |
| 4290 | } |
| 4291 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4292 | namespace { |
| 4293 | |
| 4294 | enum OverloadCandidateKind { |
| 4295 | oc_function, |
| 4296 | oc_method, |
| 4297 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4298 | oc_function_template, |
| 4299 | oc_method_template, |
| 4300 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4301 | oc_implicit_default_constructor, |
| 4302 | oc_implicit_copy_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4303 | oc_implicit_copy_assignment |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4304 | }; |
| 4305 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4306 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4307 | FunctionDecl *Fn, |
| 4308 | std::string &Description) { |
| 4309 | bool isTemplate = false; |
| 4310 | |
| 4311 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4312 | isTemplate = true; |
| 4313 | Description = S.getTemplateArgumentBindingsText( |
| 4314 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4315 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4316 | |
| 4317 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4318 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4319 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4320 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4321 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4322 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4326 | // This actually gets spelled 'candidate function' for now, but |
| 4327 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4328 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4329 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4330 | |
| 4331 | assert(Meth->isCopyAssignment() |
| 4332 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4333 | return oc_implicit_copy_assignment; |
| 4334 | } |
| 4335 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4336 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | } // end anonymous namespace |
| 4340 | |
| 4341 | // Notes the location of an overload candidate. |
| 4342 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4343 | std::string FnDesc; |
| 4344 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4345 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4346 | << (unsigned) K << FnDesc; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4349 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4350 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4351 | /// target types of the conversion. |
| 4352 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4353 | SourceLocation CaretLoc, |
| 4354 | const PartialDiagnostic &PDiag) { |
| 4355 | Diag(CaretLoc, PDiag) |
| 4356 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4357 | for (AmbiguousConversionSequence::const_iterator |
| 4358 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4359 | NoteOverloadCandidate(*I); |
| 4360 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4363 | namespace { |
| 4364 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4365 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4366 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4367 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4368 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4369 | FunctionDecl *Fn = Cand->Function; |
| 4370 | |
| 4371 | // There's a conversion slot for the object argument if this is a |
| 4372 | // non-constructor method. Note that 'I' corresponds the |
| 4373 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4374 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4375 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4376 | if (I == 0) |
| 4377 | isObjectArgument = true; |
| 4378 | else |
| 4379 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4382 | std::string FnDesc; |
| 4383 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4384 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4385 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 4386 | QualType FromTy = Conv.Bad.getFromType(); |
| 4387 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4388 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4389 | // Do some hand-waving analysis to see if the non-viability is due to a |
| 4390 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 4391 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 4392 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 4393 | CToTy = RT->getPointeeType(); |
| 4394 | else { |
| 4395 | // TODO: detect and diagnose the full richness of const mismatches. |
| 4396 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 4397 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 4398 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 4399 | } |
| 4400 | |
| 4401 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 4402 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 4403 | // It is dumb that we have to do this here. |
| 4404 | while (isa<ArrayType>(CFromTy)) |
| 4405 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4406 | while (isa<ArrayType>(CToTy)) |
| 4407 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4408 | |
| 4409 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 4410 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 4411 | |
| 4412 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 4413 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 4414 | << (unsigned) FnKind << FnDesc |
| 4415 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4416 | << FromTy |
| 4417 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 4418 | << (unsigned) isObjectArgument << I+1; |
| 4419 | return; |
| 4420 | } |
| 4421 | |
| 4422 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4423 | assert(CVR && "unexpected qualifiers mismatch"); |
| 4424 | |
| 4425 | if (isObjectArgument) { |
| 4426 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 4427 | << (unsigned) FnKind << FnDesc |
| 4428 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4429 | << FromTy << (CVR - 1); |
| 4430 | } else { |
| 4431 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 4432 | << (unsigned) FnKind << FnDesc |
| 4433 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4434 | << FromTy << (CVR - 1) << I+1; |
| 4435 | } |
| 4436 | return; |
| 4437 | } |
| 4438 | |
| 4439 | // TODO: specialize more based on the kind of mismatch |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4440 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 4441 | << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4442 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | a1709fd | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 4443 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 4447 | unsigned NumFormalArgs) { |
| 4448 | // TODO: treat calls to a missing default constructor as a special case |
| 4449 | |
| 4450 | FunctionDecl *Fn = Cand->Function; |
| 4451 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 4452 | |
| 4453 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 4454 | |
| 4455 | // at least / at most / exactly |
| 4456 | unsigned mode, modeCount; |
| 4457 | if (NumFormalArgs < MinParams) { |
| 4458 | assert(Cand->FailureKind == ovl_fail_too_few_arguments); |
| 4459 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 4460 | mode = 0; // "at least" |
| 4461 | else |
| 4462 | mode = 2; // "exactly" |
| 4463 | modeCount = MinParams; |
| 4464 | } else { |
| 4465 | assert(Cand->FailureKind == ovl_fail_too_many_arguments); |
| 4466 | if (MinParams != FnTy->getNumArgs()) |
| 4467 | mode = 1; // "at most" |
| 4468 | else |
| 4469 | mode = 2; // "exactly" |
| 4470 | modeCount = FnTy->getNumArgs(); |
| 4471 | } |
| 4472 | |
| 4473 | std::string Description; |
| 4474 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 4475 | |
| 4476 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 4477 | << (unsigned) FnKind << Description << mode << modeCount << NumFormalArgs; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 4481 | Expr **Args, unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4482 | FunctionDecl *Fn = Cand->Function; |
| 4483 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4484 | // Note deleted candidates, but only if they're viable. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4485 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4486 | std::string FnDesc; |
| 4487 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4488 | |
| 4489 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4490 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4491 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4494 | // We don't really have anything else to say about viable candidates. |
| 4495 | if (Cand->Viable) { |
| 4496 | S.NoteOverloadCandidate(Fn); |
| 4497 | return; |
| 4498 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4499 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4500 | switch (Cand->FailureKind) { |
| 4501 | case ovl_fail_too_many_arguments: |
| 4502 | case ovl_fail_too_few_arguments: |
| 4503 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4504 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4505 | case ovl_fail_bad_deduction: |
| 4506 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4507 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4508 | case ovl_fail_bad_conversion: |
| 4509 | for (unsigned I = 0, N = Cand->Conversions.size(); I != N; ++I) |
| 4510 | if (Cand->Conversions[I].isBad()) |
| 4511 | return DiagnoseBadConversion(S, Cand, I); |
| 4512 | |
| 4513 | // FIXME: this currently happens when we're called from SemaInit |
| 4514 | // when user-conversion overload fails. Figure out how to handle |
| 4515 | // those conditions and diagnose them well. |
| 4516 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4517 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4521 | // Desugar the type of the surrogate down to a function type, |
| 4522 | // retaining as many typedefs as possible while still showing |
| 4523 | // the function type (and, therefore, its parameter types). |
| 4524 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4525 | bool isLValueReference = false; |
| 4526 | bool isRValueReference = false; |
| 4527 | bool isPointer = false; |
| 4528 | if (const LValueReferenceType *FnTypeRef = |
| 4529 | FnType->getAs<LValueReferenceType>()) { |
| 4530 | FnType = FnTypeRef->getPointeeType(); |
| 4531 | isLValueReference = true; |
| 4532 | } else if (const RValueReferenceType *FnTypeRef = |
| 4533 | FnType->getAs<RValueReferenceType>()) { |
| 4534 | FnType = FnTypeRef->getPointeeType(); |
| 4535 | isRValueReference = true; |
| 4536 | } |
| 4537 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 4538 | FnType = FnTypePtr->getPointeeType(); |
| 4539 | isPointer = true; |
| 4540 | } |
| 4541 | // Desugar down to a function type. |
| 4542 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 4543 | // Reconstruct the pointer/reference as appropriate. |
| 4544 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 4545 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 4546 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 4547 | |
| 4548 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 4549 | << FnType; |
| 4550 | } |
| 4551 | |
| 4552 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 4553 | const char *Opc, |
| 4554 | SourceLocation OpLoc, |
| 4555 | OverloadCandidate *Cand) { |
| 4556 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 4557 | std::string TypeStr("operator"); |
| 4558 | TypeStr += Opc; |
| 4559 | TypeStr += "("; |
| 4560 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4561 | if (Cand->Conversions.size() == 1) { |
| 4562 | TypeStr += ")"; |
| 4563 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 4564 | } else { |
| 4565 | TypeStr += ", "; |
| 4566 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4567 | TypeStr += ")"; |
| 4568 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 4573 | OverloadCandidate *Cand) { |
| 4574 | unsigned NoOperands = Cand->Conversions.size(); |
| 4575 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 4576 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4577 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 4578 | if (!ICS.isAmbiguous()) continue; |
| 4579 | |
| 4580 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
| 4581 | PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame^] | 4585 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 4586 | if (Cand->Function) |
| 4587 | return Cand->Function->getLocation(); |
| 4588 | if (Cand->Surrogate) |
| 4589 | return Cand->Surrogate->getLocation(); |
| 4590 | return SourceLocation(); |
| 4591 | } |
| 4592 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4593 | struct CompareOverloadCandidatesForDisplay { |
| 4594 | Sema &S; |
| 4595 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4596 | |
| 4597 | bool operator()(const OverloadCandidate *L, |
| 4598 | const OverloadCandidate *R) { |
| 4599 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4600 | if (L->Viable) { |
| 4601 | if (!R->Viable) return true; |
| 4602 | |
| 4603 | // TODO: introduce a tri-valued comparison for overload |
| 4604 | // candidates. Would be more worthwhile if we had a sort |
| 4605 | // that could exploit it. |
| 4606 | if (S.isBetterOverloadCandidate(*L, *R)) return true; |
| 4607 | if (S.isBetterOverloadCandidate(*R, *L)) return false; |
| 4608 | } else if (R->Viable) |
| 4609 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4610 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame^] | 4611 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4612 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame^] | 4613 | // Criteria by which we can sort non-viable candidates: |
| 4614 | if (!L->Viable) { |
| 4615 | // 1. Arity mismatches come after other candidates. |
| 4616 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 4617 | L->FailureKind == ovl_fail_too_few_arguments) |
| 4618 | return false; |
| 4619 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 4620 | R->FailureKind == ovl_fail_too_few_arguments) |
| 4621 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4622 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame^] | 4623 | // TODO: others? |
| 4624 | } |
| 4625 | |
| 4626 | // Sort everything else by location. |
| 4627 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 4628 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 4629 | |
| 4630 | // Put candidates without locations (e.g. builtins) at the end. |
| 4631 | if (LLoc.isInvalid()) return false; |
| 4632 | if (RLoc.isInvalid()) return true; |
| 4633 | |
| 4634 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4635 | } |
| 4636 | }; |
| 4637 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4638 | } // end anonymous namespace |
| 4639 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4640 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4641 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4642 | /// set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4644 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4645 | OverloadCandidateDisplayKind OCD, |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 4646 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4647 | const char *Opc, |
Fariborz Jahanian | 29f9d39 | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4648 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4649 | // Sort the candidates by viability and position. Sorting directly would |
| 4650 | // be prohibitive, so we make a set of pointers and sort those. |
| 4651 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 4652 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 4653 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4654 | LastCand = CandidateSet.end(); |
| 4655 | Cand != LastCand; ++Cand) |
| 4656 | if (Cand->Viable || OCD == OCD_AllCandidates) |
| 4657 | Cands.push_back(Cand); |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4658 | std::sort(Cands.begin(), Cands.end(), |
| 4659 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4660 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4661 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4662 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4663 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 4664 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 4665 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4666 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4667 | if (Cand->Function) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4668 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4669 | else if (Cand->IsSurrogate) |
| 4670 | NoteSurrogateCandidate(*this, Cand); |
| 4671 | |
| 4672 | // This a builtin candidate. We do not, in general, want to list |
| 4673 | // every possible builtin candidate. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4674 | else if (Cand->Viable) { |
| 4675 | // Generally we only see ambiguities including viable builtin |
| 4676 | // operators if overload resolution got screwed up by an |
| 4677 | // ambiguous user-defined conversion. |
| 4678 | // |
| 4679 | // FIXME: It's quite possible for different conversions to see |
| 4680 | // different ambiguities, though. |
| 4681 | if (!ReportedAmbiguousConversions) { |
| 4682 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 4683 | ReportedAmbiguousConversions = true; |
| 4684 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4685 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4686 | // If this is a viable builtin, print it. |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4687 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4688 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4689 | } |
| 4690 | } |
| 4691 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4692 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4693 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4694 | /// expression with overloaded function type and @p ToType is the type |
| 4695 | /// we're trying to resolve to. For example: |
| 4696 | /// |
| 4697 | /// @code |
| 4698 | /// int f(double); |
| 4699 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4701 | /// int (*pfd)(double) = f; // selects f(double) |
| 4702 | /// @endcode |
| 4703 | /// |
| 4704 | /// This routine returns the resulting FunctionDecl if it could be |
| 4705 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4706 | /// routine will emit diagnostics if there is an error. |
| 4707 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4708 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4709 | bool Complain) { |
| 4710 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4711 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4712 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4713 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4714 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4715 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4716 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4717 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4718 | FunctionType = MemTypePtr->getPointeeType(); |
| 4719 | IsMember = true; |
| 4720 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4721 | |
| 4722 | // We only look at pointers or references to functions. |
Douglas Gregor | 6b6ba8b | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4723 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4724 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4725 | return 0; |
| 4726 | |
| 4727 | // Find the actual overloaded function declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4728 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4729 | // C++ [over.over]p1: |
| 4730 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4731 | // overloaded function name is ignored (5.1). ] |
| 4732 | Expr *OvlExpr = From->IgnoreParens(); |
| 4733 | |
| 4734 | // C++ [over.over]p1: |
| 4735 | // [...] The overloaded function name can be preceded by the & |
| 4736 | // operator. |
| 4737 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4738 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4739 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4740 | } |
| 4741 | |
Anders Carlsson | b68b028 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4742 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4743 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4744 | |
| 4745 | llvm::SmallVector<NamedDecl*,8> Fns; |
Anders Carlsson | b68b028 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4746 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4747 | // Look into the overloaded expression. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4748 | if (UnresolvedLookupExpr *UL |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4749 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 4750 | Fns.append(UL->decls_begin(), UL->decls_end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4751 | if (UL->hasExplicitTemplateArgs()) { |
| 4752 | HasExplicitTemplateArgs = true; |
| 4753 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4754 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4755 | } else if (UnresolvedMemberExpr *ME |
| 4756 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 4757 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 4758 | if (ME->hasExplicitTemplateArgs()) { |
| 4759 | HasExplicitTemplateArgs = true; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4760 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4761 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4762 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4764 | // If we didn't actually find anything, we're done. |
| 4765 | if (Fns.empty()) |
| 4766 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4767 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4768 | // Look through all of the overloaded functions, searching for one |
| 4769 | // whose type matches exactly. |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4770 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4771 | bool FoundNonTemplateFunction = false; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4772 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 4773 | E = Fns.end(); I != E; ++I) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4774 | // Look through any using declarations to find the underlying function. |
| 4775 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 4776 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4777 | // C++ [over.over]p3: |
| 4778 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4779 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4780 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4781 | // type "pointer-to-member-function." |
| 4782 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4783 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4784 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4785 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4786 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4787 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4789 | // static when converting to member pointer. |
| 4790 | if (Method->isStatic() == IsMember) |
| 4791 | continue; |
| 4792 | } else if (IsMember) |
| 4793 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4794 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4795 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | // If the name is a function template, template argument deduction is |
| 4797 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4798 | // resulting template argument list is used to generate a single |
| 4799 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4800 | // overloaded functions considered. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4801 | // FIXME: We don't really want to build the specialization here, do we? |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4802 | FunctionDecl *Specialization = 0; |
| 4803 | TemplateDeductionInfo Info(Context); |
| 4804 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4805 | = DeduceTemplateArguments(FunctionTemplate, |
| 4806 | (HasExplicitTemplateArgs ? &ExplicitTemplateArgs : 0), |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4807 | FunctionType, Specialization, Info)) { |
| 4808 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4809 | (void)Result; |
| 4810 | } else { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4811 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4812 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4813 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4814 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4815 | Matches.insert( |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4816 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4817 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4818 | |
| 4819 | continue; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4820 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4821 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4822 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4823 | // Skip non-static functions when converting to pointer, and static |
| 4824 | // when converting to member pointer. |
| 4825 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4826 | continue; |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4827 | |
| 4828 | // If we have explicit template arguments, skip non-templates. |
| 4829 | if (HasExplicitTemplateArgs) |
| 4830 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4831 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4832 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4833 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4834 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 4835 | QualType ResultTy; |
| 4836 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 4837 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 4838 | ResultTy)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4839 | Matches.insert(cast<FunctionDecl>(FunDecl->getCanonicalDecl())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4840 | FoundNonTemplateFunction = true; |
| 4841 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4845 | // If there were 0 or 1 matches, we're done. |
| 4846 | if (Matches.empty()) |
| 4847 | return 0; |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4848 | else if (Matches.size() == 1) { |
| 4849 | FunctionDecl *Result = *Matches.begin(); |
| 4850 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4851 | return Result; |
| 4852 | } |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4853 | |
| 4854 | // C++ [over.over]p4: |
| 4855 | // If more than one function is selected, [...] |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4856 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4857 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4858 | // [...] and any given function template specialization F1 is |
| 4859 | // eliminated if the set contains a second function template |
| 4860 | // specialization whose function template is more specialized |
| 4861 | // than the function template of F1 according to the partial |
| 4862 | // ordering rules of 14.5.5.2. |
| 4863 | |
| 4864 | // The algorithm specified above is quadratic. We instead use a |
| 4865 | // two-pass algorithm (similar to the one used to identify the |
| 4866 | // best viable function in an overload set) that identifies the |
| 4867 | // best function template (if it exists). |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4868 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4869 | Matches.end()); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4870 | FunctionDecl *Result = |
| 4871 | getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4872 | TPOC_Other, From->getLocStart(), |
| 4873 | PDiag(), |
| 4874 | PDiag(diag::err_addr_ovl_ambiguous) |
| 4875 | << TemplateMatches[0]->getDeclName(), |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4876 | PDiag(diag::note_ovl_candidate) |
| 4877 | << (unsigned) oc_function_template); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4878 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4879 | return Result; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4880 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4881 | |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4882 | // [...] any function template specializations in the set are |
| 4883 | // eliminated if the set also contains a non-template function, [...] |
| 4884 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4885 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4886 | if ((*M)->getPrimaryTemplate() == 0) |
| 4887 | RemainingMatches.push_back(*M); |
| 4888 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4889 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4890 | // selected function. |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4891 | if (RemainingMatches.size() == 1) { |
| 4892 | FunctionDecl *Result = RemainingMatches.front(); |
| 4893 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4894 | return Result; |
| 4895 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4896 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4897 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4898 | // returns (even if we issue the diagnostics here). |
| 4899 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4900 | << RemainingMatches[0]->getDeclName(); |
| 4901 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4902 | NoteOverloadCandidate(RemainingMatches[I]); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4903 | return 0; |
| 4904 | } |
| 4905 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4906 | /// \brief Given an expression that refers to an overloaded function, try to |
| 4907 | /// resolve that overloaded function expression down to a single function. |
| 4908 | /// |
| 4909 | /// This routine can only resolve template-ids that refer to a single function |
| 4910 | /// template, where that template-id refers to a single template whose template |
| 4911 | /// arguments are either provided by the template-id or have defaults, |
| 4912 | /// as described in C++0x [temp.arg.explicit]p3. |
| 4913 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 4914 | // C++ [over.over]p1: |
| 4915 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4916 | // overloaded function name is ignored (5.1). ] |
| 4917 | Expr *OvlExpr = From->IgnoreParens(); |
| 4918 | |
| 4919 | // C++ [over.over]p1: |
| 4920 | // [...] The overloaded function name can be preceded by the & |
| 4921 | // operator. |
| 4922 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4923 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4924 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4925 | } |
| 4926 | |
| 4927 | bool HasExplicitTemplateArgs = false; |
| 4928 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 4929 | |
| 4930 | llvm::SmallVector<NamedDecl*,8> Fns; |
| 4931 | |
| 4932 | // Look into the overloaded expression. |
| 4933 | if (UnresolvedLookupExpr *UL |
| 4934 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 4935 | Fns.append(UL->decls_begin(), UL->decls_end()); |
| 4936 | if (UL->hasExplicitTemplateArgs()) { |
| 4937 | HasExplicitTemplateArgs = true; |
| 4938 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4939 | } |
| 4940 | } else if (UnresolvedMemberExpr *ME |
| 4941 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 4942 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 4943 | if (ME->hasExplicitTemplateArgs()) { |
| 4944 | HasExplicitTemplateArgs = true; |
| 4945 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | // If we didn't actually find any template-ids, we're done. |
| 4950 | if (Fns.empty() || !HasExplicitTemplateArgs) |
| 4951 | return 0; |
| 4952 | |
| 4953 | // Look through all of the overloaded functions, searching for one |
| 4954 | // whose type matches exactly. |
| 4955 | FunctionDecl *Matched = 0; |
| 4956 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 4957 | E = Fns.end(); I != E; ++I) { |
| 4958 | // C++0x [temp.arg.explicit]p3: |
| 4959 | // [...] In contexts where deduction is done and fails, or in contexts |
| 4960 | // where deduction is not done, if a template argument list is |
| 4961 | // specified and it, along with any default template arguments, |
| 4962 | // identifies a single function template specialization, then the |
| 4963 | // template-id is an lvalue for the function template specialization. |
| 4964 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 4965 | |
| 4966 | // C++ [over.over]p2: |
| 4967 | // If the name is a function template, template argument deduction is |
| 4968 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4969 | // resulting template argument list is used to generate a single |
| 4970 | // function template specialization, which is added to the set of |
| 4971 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4972 | FunctionDecl *Specialization = 0; |
| 4973 | TemplateDeductionInfo Info(Context); |
| 4974 | if (TemplateDeductionResult Result |
| 4975 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 4976 | Specialization, Info)) { |
| 4977 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4978 | (void)Result; |
| 4979 | continue; |
| 4980 | } |
| 4981 | |
| 4982 | // Multiple matches; we can't resolve to a single declaration. |
| 4983 | if (Matched) |
| 4984 | return 0; |
| 4985 | |
| 4986 | Matched = Specialization; |
| 4987 | } |
| 4988 | |
| 4989 | return Matched; |
| 4990 | } |
| 4991 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4992 | /// \brief Add a single candidate to the overload set. |
| 4993 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4994 | NamedDecl *Callee, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4995 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4996 | Expr **Args, unsigned NumArgs, |
| 4997 | OverloadCandidateSet &CandidateSet, |
| 4998 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4999 | if (isa<UsingShadowDecl>(Callee)) |
| 5000 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5001 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5002 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5003 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5004 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 5005 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5006 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
| 5009 | if (FunctionTemplateDecl *FuncTemplate |
| 5010 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5011 | S.AddTemplateOverloadCandidate(FuncTemplate, ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5012 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5013 | return; |
| 5014 | } |
| 5015 | |
| 5016 | assert(false && "unhandled case in overloaded call candidate"); |
| 5017 | |
| 5018 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
| 5021 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5022 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5023 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5024 | Expr **Args, unsigned NumArgs, |
| 5025 | OverloadCandidateSet &CandidateSet, |
| 5026 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5027 | |
| 5028 | #ifndef NDEBUG |
| 5029 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5030 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5031 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5032 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5033 | // and let Y be the lookup set produced by argument dependent |
| 5034 | // lookup (defined as follows). If X contains |
| 5035 | // |
| 5036 | // -- a declaration of a class member, or |
| 5037 | // |
| 5038 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5039 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5040 | // |
| 5041 | // -- a declaration that is neither a function or a function |
| 5042 | // template |
| 5043 | // |
| 5044 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5045 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5046 | if (ULE->requiresADL()) { |
| 5047 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5048 | E = ULE->decls_end(); I != E; ++I) { |
| 5049 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5050 | assert(isa<UsingShadowDecl>(*I) || |
| 5051 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5052 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5053 | } |
| 5054 | } |
| 5055 | #endif |
| 5056 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5057 | // It would be nice to avoid this copy. |
| 5058 | TemplateArgumentListInfo TABuffer; |
| 5059 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5060 | if (ULE->hasExplicitTemplateArgs()) { |
| 5061 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5062 | ExplicitTemplateArgs = &TABuffer; |
| 5063 | } |
| 5064 | |
| 5065 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5066 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5067 | AddOverloadedCallCandidate(*this, *I, ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5068 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5069 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5070 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5071 | if (ULE->requiresADL()) |
| 5072 | AddArgumentDependentLookupCandidates(ULE->getName(), Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5073 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5074 | CandidateSet, |
| 5075 | PartialOverloading); |
| 5076 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5077 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5078 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5079 | Expr **Args, unsigned NumArgs) { |
| 5080 | Fn->Destroy(SemaRef.Context); |
| 5081 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5082 | Args[Arg]->Destroy(SemaRef.Context); |
| 5083 | return SemaRef.ExprError(); |
| 5084 | } |
| 5085 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5086 | /// Attempts to recover from a call where no functions were found. |
| 5087 | /// |
| 5088 | /// Returns true if new candidates were found. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5089 | static Sema::OwningExprResult |
| 5090 | BuildRecoveryCallExpr(Sema &SemaRef, Expr *Fn, |
| 5091 | UnresolvedLookupExpr *ULE, |
| 5092 | SourceLocation LParenLoc, |
| 5093 | Expr **Args, unsigned NumArgs, |
| 5094 | SourceLocation *CommaLocs, |
| 5095 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5096 | |
| 5097 | CXXScopeSpec SS; |
| 5098 | if (ULE->getQualifier()) { |
| 5099 | SS.setScopeRep(ULE->getQualifier()); |
| 5100 | SS.setRange(ULE->getQualifierRange()); |
| 5101 | } |
| 5102 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5103 | TemplateArgumentListInfo TABuffer; |
| 5104 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5105 | if (ULE->hasExplicitTemplateArgs()) { |
| 5106 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5107 | ExplicitTemplateArgs = &TABuffer; |
| 5108 | } |
| 5109 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5110 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5111 | Sema::LookupOrdinaryName); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 5112 | if (SemaRef.DiagnoseEmptyLookup(/*Scope=*/0, SS, R)) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5113 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5114 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5115 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5116 | |
| 5117 | // Build an implicit member call if appropriate. Just drop the |
| 5118 | // casts and such from the call, we don't really care. |
| 5119 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5120 | if ((*R.begin())->isCXXClassMember()) |
| 5121 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5122 | else if (ExplicitTemplateArgs) |
| 5123 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5124 | else |
| 5125 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 5126 | |
| 5127 | if (NewFn.isInvalid()) |
| 5128 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5129 | |
| 5130 | Fn->Destroy(SemaRef.Context); |
| 5131 | |
| 5132 | // This shouldn't cause an infinite loop because we're giving it |
| 5133 | // an expression with non-empty lookup results, which should never |
| 5134 | // end up here. |
| 5135 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 5136 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 5137 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5138 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5140 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5141 | /// (which eventually refers to the declaration Func) and the call |
| 5142 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 5143 | /// to a specific function. If overload resolution succeeds, returns |
| 5144 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 5145 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5146 | /// arguments and Fn, and returns NULL. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5147 | Sema::OwningExprResult |
| 5148 | Sema::BuildOverloadedCallExpr(Expr *Fn, UnresolvedLookupExpr *ULE, |
| 5149 | SourceLocation LParenLoc, |
| 5150 | Expr **Args, unsigned NumArgs, |
| 5151 | SourceLocation *CommaLocs, |
| 5152 | SourceLocation RParenLoc) { |
| 5153 | #ifndef NDEBUG |
| 5154 | if (ULE->requiresADL()) { |
| 5155 | // To do ADL, we must have found an unqualified name. |
| 5156 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 5157 | |
| 5158 | // We don't perform ADL for implicit declarations of builtins. |
| 5159 | // Verify that this was correctly set up. |
| 5160 | FunctionDecl *F; |
| 5161 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5162 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5163 | F->getBuiltinID() && F->isImplicit()) |
| 5164 | assert(0 && "performing ADL for builtin"); |
| 5165 | |
| 5166 | // We don't perform ADL in C. |
| 5167 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5168 | } |
| 5169 | #endif |
| 5170 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5171 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 5172 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5173 | // Add the functions denoted by the callee to the set of candidate |
| 5174 | // functions, including those from argument-dependent lookup. |
| 5175 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5176 | |
| 5177 | // If we found nothing, try to recover. |
| 5178 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5179 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5180 | if (CandidateSet.empty()) |
| 5181 | return BuildRecoveryCallExpr(*this, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5182 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5183 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5184 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5185 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5186 | case OR_Success: { |
| 5187 | FunctionDecl *FDecl = Best->Function; |
| 5188 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 5189 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5190 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5191 | |
| 5192 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5193 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5194 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5195 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5196 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5197 | break; |
| 5198 | |
| 5199 | case OR_Ambiguous: |
| 5200 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5201 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5202 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5203 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5204 | |
| 5205 | case OR_Deleted: |
| 5206 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5207 | << Best->Function->isDeleted() |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5208 | << ULE->getName() |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5209 | << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5210 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5211 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5215 | // return NULL. |
| 5216 | Fn->Destroy(Context); |
| 5217 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5218 | Args[Arg]->Destroy(Context); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5219 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5222 | static bool IsOverloaded(const Sema::FunctionSet &Functions) { |
| 5223 | return Functions.size() > 1 || |
| 5224 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5225 | } |
| 5226 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5227 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5228 | /// operator. |
| 5229 | /// |
| 5230 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5231 | /// |
| 5232 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5233 | /// operator. |
| 5234 | /// |
| 5235 | /// \param Functions The set of non-member functions that will be |
| 5236 | /// considered by overload resolution. The caller needs to build this |
| 5237 | /// set based on the context using, e.g., |
| 5238 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5239 | /// set should not contain any member functions; those will be added |
| 5240 | /// by CreateOverloadedUnaryOp(). |
| 5241 | /// |
| 5242 | /// \param input The input argument. |
| 5243 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 5244 | unsigned OpcIn, |
| 5245 | FunctionSet &Functions, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5246 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5247 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5248 | Expr *Input = (Expr *)input.get(); |
| 5249 | |
| 5250 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5251 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5252 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5253 | |
| 5254 | Expr *Args[2] = { Input, 0 }; |
| 5255 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5256 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5257 | // For post-increment and post-decrement, add the implicit '0' as |
| 5258 | // the second argument, so that we know this is a post-increment or |
| 5259 | // post-decrement. |
| 5260 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5261 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5262 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5263 | SourceLocation()); |
| 5264 | NumArgs = 2; |
| 5265 | } |
| 5266 | |
| 5267 | if (Input->isTypeDependent()) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5268 | UnresolvedLookupExpr *Fn |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5269 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5270 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5271 | /*ADL*/ true, IsOverloaded(Functions)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5272 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5273 | FuncEnd = Functions.end(); |
| 5274 | Func != FuncEnd; ++Func) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5275 | Fn->addDecl(*Func); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5276 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5277 | input.release(); |
| 5278 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5279 | &Args[0], NumArgs, |
| 5280 | Context.DependentTy, |
| 5281 | OpLoc)); |
| 5282 | } |
| 5283 | |
| 5284 | // Build an empty overload set. |
| 5285 | OverloadCandidateSet CandidateSet; |
| 5286 | |
| 5287 | // Add the candidates from the given function set. |
| 5288 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 5289 | |
| 5290 | // Add operator candidates that are member functions. |
| 5291 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5292 | |
| 5293 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5294 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5295 | |
| 5296 | // Perform overload resolution. |
| 5297 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5298 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5299 | case OR_Success: { |
| 5300 | // We found a built-in operator or an overloaded operator. |
| 5301 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5302 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5303 | if (FnDecl) { |
| 5304 | // We matched an overloaded operator. Build a call to that |
| 5305 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5306 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5307 | // Convert the arguments. |
| 5308 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5309 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 5310 | return ExprError(); |
| 5311 | } else { |
| 5312 | // Convert the arguments. |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5313 | OwningExprResult InputInit |
| 5314 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5315 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5316 | SourceLocation(), |
| 5317 | move(input)); |
| 5318 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5319 | return ExprError(); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5320 | |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5321 | input = move(InputInit); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5322 | Input = (Expr *)input.get(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | // Determine the result type |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5326 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5328 | // Build the actual expression node. |
| 5329 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5330 | SourceLocation()); |
| 5331 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5332 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5333 | input.release(); |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5334 | Args[0] = Input; |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5335 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5336 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5337 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5338 | |
| 5339 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5340 | FnDecl)) |
| 5341 | return ExprError(); |
| 5342 | |
| 5343 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5344 | } else { |
| 5345 | // We matched a built-in operator. Convert the arguments, then |
| 5346 | // break out so that we will build the appropriate built-in |
| 5347 | // operator node. |
| 5348 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5349 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5350 | return ExprError(); |
| 5351 | |
| 5352 | break; |
| 5353 | } |
| 5354 | } |
| 5355 | |
| 5356 | case OR_No_Viable_Function: |
| 5357 | // No viable function; fall through to handling this as a |
| 5358 | // built-in operator, which will produce an error message for us. |
| 5359 | break; |
| 5360 | |
| 5361 | case OR_Ambiguous: |
| 5362 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5363 | << UnaryOperator::getOpcodeStr(Opc) |
| 5364 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5365 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5366 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5367 | return ExprError(); |
| 5368 | |
| 5369 | case OR_Deleted: |
| 5370 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5371 | << Best->Function->isDeleted() |
| 5372 | << UnaryOperator::getOpcodeStr(Opc) |
| 5373 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5374 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5375 | return ExprError(); |
| 5376 | } |
| 5377 | |
| 5378 | // Either we found no viable overloaded operator or we matched a |
| 5379 | // built-in operator. In either case, fall through to trying to |
| 5380 | // build a built-in operation. |
| 5381 | input.release(); |
| 5382 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5383 | } |
| 5384 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5385 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5386 | /// operator. |
| 5387 | /// |
| 5388 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5389 | /// |
| 5390 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5391 | /// operator. |
| 5392 | /// |
| 5393 | /// \param Functions The set of non-member functions that will be |
| 5394 | /// considered by overload resolution. The caller needs to build this |
| 5395 | /// set based on the context using, e.g., |
| 5396 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5397 | /// set should not contain any member functions; those will be added |
| 5398 | /// by CreateOverloadedBinOp(). |
| 5399 | /// |
| 5400 | /// \param LHS Left-hand argument. |
| 5401 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5402 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5403 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | unsigned OpcIn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5405 | FunctionSet &Functions, |
| 5406 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5407 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5408 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5409 | |
| 5410 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5411 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5412 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5413 | |
| 5414 | // If either side is type-dependent, create an appropriate dependent |
| 5415 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5416 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5417 | if (Functions.empty()) { |
| 5418 | // If there are no functions to store, just build a dependent |
| 5419 | // BinaryOperator or CompoundAssignment. |
| 5420 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5421 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5422 | Context.DependentTy, OpLoc)); |
| 5423 | |
| 5424 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5425 | Context.DependentTy, |
| 5426 | Context.DependentTy, |
| 5427 | Context.DependentTy, |
| 5428 | OpLoc)); |
| 5429 | } |
| 5430 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5431 | UnresolvedLookupExpr *Fn |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5432 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5433 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5434 | /* ADL */ true, IsOverloaded(Functions)); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5435 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5436 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5437 | FuncEnd = Functions.end(); |
| 5438 | Func != FuncEnd; ++Func) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5439 | Fn->addDecl(*Func); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5441 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5443 | Context.DependentTy, |
| 5444 | OpLoc)); |
| 5445 | } |
| 5446 | |
| 5447 | // If this is the .* operator, which is not overloadable, just |
| 5448 | // create a built-in binary operator. |
| 5449 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5450 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5451 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 5452 | // If this is the assignment operator, we only perform overload resolution |
| 5453 | // if the left-hand side is a class or enumeration type. This is actually |
| 5454 | // a hack. The standard requires that we do overload resolution between the |
| 5455 | // various built-in candidates, but as DR507 points out, this can lead to |
| 5456 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 5457 | // Note that we go the traditional code path for compound assignment forms. |
| 5458 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5459 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5460 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5461 | // Build an empty overload set. |
| 5462 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5463 | |
| 5464 | // Add the candidates from the given function set. |
| 5465 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 5466 | |
| 5467 | // Add operator candidates that are member functions. |
| 5468 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5469 | |
| 5470 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5471 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5472 | |
| 5473 | // Perform overload resolution. |
| 5474 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5475 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5476 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5477 | // We found a built-in operator or an overloaded operator. |
| 5478 | FunctionDecl *FnDecl = Best->Function; |
| 5479 | |
| 5480 | if (FnDecl) { |
| 5481 | // We matched an overloaded operator. Build a call to that |
| 5482 | // operator. |
| 5483 | |
| 5484 | // Convert the arguments. |
| 5485 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5486 | OwningExprResult Arg1 |
| 5487 | = PerformCopyInitialization( |
| 5488 | InitializedEntity::InitializeParameter( |
| 5489 | FnDecl->getParamDecl(0)), |
| 5490 | SourceLocation(), |
| 5491 | Owned(Args[1])); |
| 5492 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5493 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5494 | |
| 5495 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
| 5496 | return ExprError(); |
| 5497 | |
| 5498 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5499 | } else { |
| 5500 | // Convert the arguments. |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5501 | OwningExprResult Arg0 |
| 5502 | = PerformCopyInitialization( |
| 5503 | InitializedEntity::InitializeParameter( |
| 5504 | FnDecl->getParamDecl(0)), |
| 5505 | SourceLocation(), |
| 5506 | Owned(Args[0])); |
| 5507 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5508 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5509 | |
| 5510 | OwningExprResult Arg1 |
| 5511 | = PerformCopyInitialization( |
| 5512 | InitializedEntity::InitializeParameter( |
| 5513 | FnDecl->getParamDecl(1)), |
| 5514 | SourceLocation(), |
| 5515 | Owned(Args[1])); |
| 5516 | if (Arg1.isInvalid()) |
| 5517 | return ExprError(); |
| 5518 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 5519 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5520 | } |
| 5521 | |
| 5522 | // Determine the result type |
| 5523 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5524 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5525 | ResultTy = ResultTy.getNonReferenceType(); |
| 5526 | |
| 5527 | // Build the actual expression node. |
| 5528 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 5529 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5530 | UsualUnaryConversions(FnExpr); |
| 5531 | |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5532 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5533 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5534 | Args, 2, ResultTy, |
| 5535 | OpLoc)); |
| 5536 | |
| 5537 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5538 | FnDecl)) |
| 5539 | return ExprError(); |
| 5540 | |
| 5541 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5542 | } else { |
| 5543 | // We matched a built-in operator. Convert the arguments, then |
| 5544 | // break out so that we will build the appropriate built-in |
| 5545 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5546 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5547 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5548 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5549 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5550 | return ExprError(); |
| 5551 | |
| 5552 | break; |
| 5553 | } |
| 5554 | } |
| 5555 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5556 | case OR_No_Viable_Function: { |
| 5557 | // C++ [over.match.oper]p9: |
| 5558 | // If the operator is the operator , [...] and there are no |
| 5559 | // viable functions, then the operator is assumed to be the |
| 5560 | // built-in operator and interpreted according to clause 5. |
| 5561 | if (Opc == BinaryOperator::Comma) |
| 5562 | break; |
| 5563 | |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5564 | // For class as left operand for assignment or compound assigment operator |
| 5565 | // do not fall through to handling in built-in, but report that no overloaded |
| 5566 | // assignment operator found |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5567 | OwningExprResult Result = ExprError(); |
| 5568 | if (Args[0]->getType()->isRecordType() && |
| 5569 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5570 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 5571 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5572 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5573 | } else { |
| 5574 | // No viable function; try to create a built-in operation, which will |
| 5575 | // produce an error. Then, show the non-viable candidates. |
| 5576 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5577 | } |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5578 | assert(Result.isInvalid() && |
| 5579 | "C++ binary operator overloading is missing candidates!"); |
| 5580 | if (Result.isInvalid()) |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5581 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5582 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5583 | return move(Result); |
| 5584 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5585 | |
| 5586 | case OR_Ambiguous: |
| 5587 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5588 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5589 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5590 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5591 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5592 | return ExprError(); |
| 5593 | |
| 5594 | case OR_Deleted: |
| 5595 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5596 | << Best->Function->isDeleted() |
| 5597 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5598 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5599 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5600 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5601 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5602 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5603 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5604 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5607 | Action::OwningExprResult |
| 5608 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 5609 | SourceLocation RLoc, |
| 5610 | ExprArg Base, ExprArg Idx) { |
| 5611 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 5612 | static_cast<Expr*>(Idx.get()) }; |
| 5613 | DeclarationName OpName = |
| 5614 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 5615 | |
| 5616 | // If either side is type-dependent, create an appropriate dependent |
| 5617 | // expression. |
| 5618 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5619 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5620 | UnresolvedLookupExpr *Fn |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5621 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5622 | 0, SourceRange(), OpName, LLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5623 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5624 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5625 | |
| 5626 | Base.release(); |
| 5627 | Idx.release(); |
| 5628 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5629 | Args, 2, |
| 5630 | Context.DependentTy, |
| 5631 | RLoc)); |
| 5632 | } |
| 5633 | |
| 5634 | // Build an empty overload set. |
| 5635 | OverloadCandidateSet CandidateSet; |
| 5636 | |
| 5637 | // Subscript can only be overloaded as a member function. |
| 5638 | |
| 5639 | // Add operator candidates that are member functions. |
| 5640 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5641 | |
| 5642 | // Add builtin operator candidates. |
| 5643 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5644 | |
| 5645 | // Perform overload resolution. |
| 5646 | OverloadCandidateSet::iterator Best; |
| 5647 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5648 | case OR_Success: { |
| 5649 | // We found a built-in operator or an overloaded operator. |
| 5650 | FunctionDecl *FnDecl = Best->Function; |
| 5651 | |
| 5652 | if (FnDecl) { |
| 5653 | // We matched an overloaded operator. Build a call to that |
| 5654 | // operator. |
| 5655 | |
| 5656 | // Convert the arguments. |
| 5657 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5658 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 5659 | PerformCopyInitialization(Args[1], |
| 5660 | FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5661 | AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5662 | return ExprError(); |
| 5663 | |
| 5664 | // Determine the result type |
| 5665 | QualType ResultTy |
| 5666 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5667 | ResultTy = ResultTy.getNonReferenceType(); |
| 5668 | |
| 5669 | // Build the actual expression node. |
| 5670 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5671 | LLoc); |
| 5672 | UsualUnaryConversions(FnExpr); |
| 5673 | |
| 5674 | Base.release(); |
| 5675 | Idx.release(); |
| 5676 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5677 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5678 | FnExpr, Args, 2, |
| 5679 | ResultTy, RLoc)); |
| 5680 | |
| 5681 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5682 | FnDecl)) |
| 5683 | return ExprError(); |
| 5684 | |
| 5685 | return MaybeBindToTemporary(TheCall.release()); |
| 5686 | } else { |
| 5687 | // We matched a built-in operator. Convert the arguments, then |
| 5688 | // break out so that we will build the appropriate built-in |
| 5689 | // operator node. |
| 5690 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5691 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5692 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5693 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5694 | return ExprError(); |
| 5695 | |
| 5696 | break; |
| 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5701 | if (CandidateSet.empty()) |
| 5702 | Diag(LLoc, diag::err_ovl_no_oper) |
| 5703 | << Args[0]->getType() << /*subscript*/ 0 |
| 5704 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5705 | else |
| 5706 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 5707 | << Args[0]->getType() |
| 5708 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5709 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5710 | "[]", LLoc); |
| 5711 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | case OR_Ambiguous: |
| 5715 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5716 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5717 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5718 | "[]", LLoc); |
| 5719 | return ExprError(); |
| 5720 | |
| 5721 | case OR_Deleted: |
| 5722 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5723 | << Best->Function->isDeleted() << "[]" |
| 5724 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5725 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5726 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5727 | return ExprError(); |
| 5728 | } |
| 5729 | |
| 5730 | // We matched a built-in operator; build it. |
| 5731 | Base.release(); |
| 5732 | Idx.release(); |
| 5733 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 5734 | Owned(Args[1]), RLoc); |
| 5735 | } |
| 5736 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5737 | /// BuildCallToMemberFunction - Build a call to a member |
| 5738 | /// function. MemExpr is the expression that refers to the member |
| 5739 | /// function (and includes the object parameter), Args/NumArgs are the |
| 5740 | /// arguments to the function call (not including the object |
| 5741 | /// parameter). The caller needs to validate that the member |
| 5742 | /// expression refers to a member function or an overloaded member |
| 5743 | /// function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5744 | Sema::OwningExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5745 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 5746 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5747 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 5748 | SourceLocation RParenLoc) { |
| 5749 | // Dig out the member expression. This holds both the object |
| 5750 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5751 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 5752 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5753 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5754 | CXXMethodDecl *Method = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5755 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 5756 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5757 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 5758 | } else { |
| 5759 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5760 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5761 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5762 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5763 | // Add overload candidates |
| 5764 | OverloadCandidateSet CandidateSet; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5765 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5766 | // FIXME: avoid copy. |
| 5767 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 5768 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 5769 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 5770 | TemplateArgs = &TemplateArgsBuffer; |
| 5771 | } |
| 5772 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5773 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 5774 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 5775 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5776 | NamedDecl *Func = *I; |
| 5777 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 5778 | if (isa<UsingShadowDecl>(Func)) |
| 5779 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 5780 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5781 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5782 | // If explicit template arguments were provided, we can't call a |
| 5783 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5784 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5785 | continue; |
| 5786 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5787 | AddMethodCandidate(Method, ActingDC, ObjectType, Args, NumArgs, |
| 5788 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5789 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5790 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5791 | ActingDC, TemplateArgs, |
| 5792 | ObjectType, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5793 | CandidateSet, |
| 5794 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5795 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5797 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5798 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 5799 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5800 | OverloadCandidateSet::iterator Best; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5801 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5802 | case OR_Success: |
| 5803 | Method = cast<CXXMethodDecl>(Best->Function); |
| 5804 | break; |
| 5805 | |
| 5806 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5807 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5808 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5809 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5810 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5811 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5812 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5813 | |
| 5814 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5815 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5816 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5817 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5818 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5819 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5820 | |
| 5821 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5822 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5823 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5824 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5825 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5826 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5827 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 5830 | MemExprE = FixOverloadedFunctionReference(MemExprE, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5831 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5832 | // If overload resolution picked a static member, build a |
| 5833 | // non-member call based on that function. |
| 5834 | if (Method->isStatic()) { |
| 5835 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 5836 | Args, NumArgs, RParenLoc); |
| 5837 | } |
| 5838 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5839 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5843 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5844 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5845 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5846 | Method->getResultType().getNonReferenceType(), |
| 5847 | RParenLoc)); |
| 5848 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5849 | // Check for a valid return type. |
| 5850 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 5851 | TheCall.get(), Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5852 | return ExprError(); |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5853 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5854 | // Convert the object argument (for a non-static member function call). |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5855 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5856 | if (!Method->isStatic() && |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5857 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5858 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5859 | MemExpr->setBase(ObjectArg); |
| 5860 | |
| 5861 | // Convert the rest of the arguments |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5862 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5863 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5864 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5865 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5866 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5867 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5868 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 5869 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5870 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5871 | } |
| 5872 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5873 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 5874 | /// type (C++ [over.call.object]), which can end up invoking an |
| 5875 | /// overloaded function call operator (@c operator()) or performing a |
| 5876 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5877 | Sema::ExprResult |
| 5878 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 5879 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5880 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5881 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5882 | SourceLocation RParenLoc) { |
| 5883 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5884 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5885 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5886 | // C++ [over.call.object]p1: |
| 5887 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5888 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5889 | // candidate functions includes at least the function call |
| 5890 | // operators of T. The function call operators of T are obtained by |
| 5891 | // ordinary lookup of the name operator() in the context of |
| 5892 | // (E).operator(). |
| 5893 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5894 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5895 | |
| 5896 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 5897 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 5898 | << Object->getSourceRange())) |
| 5899 | return true; |
| 5900 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5901 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 5902 | LookupQualifiedName(R, Record->getDecl()); |
| 5903 | R.suppressDiagnostics(); |
| 5904 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5905 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5906 | Oper != OperEnd; ++Oper) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5907 | AddMethodCandidate(*Oper, Object->getType(), Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5908 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5909 | } |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5910 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5911 | // C++ [over.call.object]p2: |
| 5912 | // In addition, for each conversion function declared in T of the |
| 5913 | // form |
| 5914 | // |
| 5915 | // operator conversion-type-id () cv-qualifier; |
| 5916 | // |
| 5917 | // where cv-qualifier is the same cv-qualification as, or a |
| 5918 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 5919 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 5920 | // R", or the type "reference to pointer to function of |
| 5921 | // (P1,...,Pn) returning R", or the type "reference to function |
| 5922 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5923 | // is also considered as a candidate function. Similarly, |
| 5924 | // surrogate call functions are added to the set of candidate |
| 5925 | // functions for each conversion function declared in an |
| 5926 | // accessible base class provided the function is not hidden |
| 5927 | // within T by another intervening declaration. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5928 | const UnresolvedSet *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 5929 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5930 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 5931 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5932 | NamedDecl *D = *I; |
| 5933 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5934 | if (isa<UsingShadowDecl>(D)) |
| 5935 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5936 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5937 | // Skip over templated conversion functions; they aren't |
| 5938 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5939 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5940 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5941 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5942 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5943 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5944 | // Strip the reference type (if any) and then the pointer type (if |
| 5945 | // any) to get down to what might be a function type. |
| 5946 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 5947 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5948 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5949 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5950 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5951 | AddSurrogateCandidate(Conv, ActingContext, Proto, |
| 5952 | Object->getType(), Args, NumArgs, |
| 5953 | CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5954 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5955 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5956 | // Perform overload resolution. |
| 5957 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5958 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5959 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5960 | // Overload resolution succeeded; we'll build the appropriate call |
| 5961 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5962 | break; |
| 5963 | |
| 5964 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5965 | if (CandidateSet.empty()) |
| 5966 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 5967 | << Object->getType() << /*call*/ 1 |
| 5968 | << Object->getSourceRange(); |
| 5969 | else |
| 5970 | Diag(Object->getSourceRange().getBegin(), |
| 5971 | diag::err_ovl_no_viable_object_call) |
| 5972 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5973 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5974 | break; |
| 5975 | |
| 5976 | case OR_Ambiguous: |
| 5977 | Diag(Object->getSourceRange().getBegin(), |
| 5978 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5979 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5980 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5981 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5982 | |
| 5983 | case OR_Deleted: |
| 5984 | Diag(Object->getSourceRange().getBegin(), |
| 5985 | diag::err_ovl_deleted_object_call) |
| 5986 | << Best->Function->isDeleted() |
| 5987 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5988 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5989 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5990 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5991 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5992 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5993 | // We had an error; delete all of the subexpressions and return |
| 5994 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5995 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5996 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5997 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5998 | return true; |
| 5999 | } |
| 6000 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6001 | if (Best->Function == 0) { |
| 6002 | // Since there is no function declaration, this is one of the |
| 6003 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6004 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6005 | = cast<CXXConversionDecl>( |
| 6006 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6007 | |
| 6008 | // We selected one of the surrogate functions that converts the |
| 6009 | // object parameter to a function pointer. Perform the conversion |
| 6010 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6011 | |
| 6012 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6013 | // and then call it. |
Eli Friedman | a958a01 | 2009-12-09 04:52:43 +0000 | [diff] [blame] | 6014 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Conv); |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6015 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6016 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6017 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 6018 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6019 | } |
| 6020 | |
| 6021 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6022 | // that calls this method, using Object for the implicit object |
| 6023 | // parameter and passing along the remaining arguments. |
| 6024 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6025 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6026 | |
| 6027 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6028 | unsigned NumArgsToCheck = NumArgs; |
| 6029 | |
| 6030 | // Build the full argument list for the method call (the |
| 6031 | // implicit object parameter is placed at the beginning of the |
| 6032 | // list). |
| 6033 | Expr **MethodArgs; |
| 6034 | if (NumArgs < NumArgsInProto) { |
| 6035 | NumArgsToCheck = NumArgsInProto; |
| 6036 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6037 | } else { |
| 6038 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6039 | } |
| 6040 | MethodArgs[0] = Object; |
| 6041 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6042 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6043 | |
| 6044 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6045 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6046 | UsualUnaryConversions(NewFn); |
| 6047 | |
| 6048 | // Once we've built TheCall, all of the expressions are properly |
| 6049 | // owned. |
| 6050 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6051 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6052 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6053 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6054 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6055 | delete [] MethodArgs; |
| 6056 | |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 6057 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6058 | Method)) |
| 6059 | return true; |
| 6060 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6061 | // We may have default arguments. If so, we need to allocate more |
| 6062 | // slots in the call for them. |
| 6063 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6064 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6065 | else if (NumArgs > NumArgsInProto) |
| 6066 | NumArgsToCheck = NumArgsInProto; |
| 6067 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6068 | bool IsError = false; |
| 6069 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6070 | // Initialize the implicit object parameter. |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6071 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6072 | TheCall->setArg(0, Object); |
| 6073 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6074 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6075 | // Check the argument types. |
| 6076 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6077 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6078 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6079 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6080 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6081 | // Pass the argument. |
| 6082 | QualType ProtoArgType = Proto->getArgType(i); |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6083 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, AA_Passing); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6084 | } else { |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 6085 | OwningExprResult DefArg |
| 6086 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 6087 | if (DefArg.isInvalid()) { |
| 6088 | IsError = true; |
| 6089 | break; |
| 6090 | } |
| 6091 | |
| 6092 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6093 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6094 | |
| 6095 | TheCall->setArg(i + 1, Arg); |
| 6096 | } |
| 6097 | |
| 6098 | // If this is a variadic call, handle args passed through "...". |
| 6099 | if (Proto->isVariadic()) { |
| 6100 | // Promote the arguments (C99 6.5.2.2p7). |
| 6101 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 6102 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6103 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6104 | TheCall->setArg(i + 1, Arg); |
| 6105 | } |
| 6106 | } |
| 6107 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6108 | if (IsError) return true; |
| 6109 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6110 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6111 | return true; |
| 6112 | |
Anders Carlsson | 1c83deb | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 6113 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6114 | } |
| 6115 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6116 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6117 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6118 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6119 | Sema::OwningExprResult |
| 6120 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 6121 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6122 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6123 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6124 | // C++ [over.ref]p1: |
| 6125 | // |
| 6126 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 6127 | // for a class object x of type T if T::operator->() exists and if |
| 6128 | // the operator is selected as the best match function by the |
| 6129 | // overload resolution mechanism (13.3). |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6130 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 6131 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6132 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6133 | |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 6134 | if (RequireCompleteType(Base->getLocStart(), Base->getType(), |
| 6135 | PDiag(diag::err_typecheck_incomplete_tag) |
| 6136 | << Base->getSourceRange())) |
| 6137 | return ExprError(); |
| 6138 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6139 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6140 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 6141 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6142 | |
| 6143 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6144 | Oper != OperEnd; ++Oper) { |
| 6145 | NamedDecl *D = *Oper; |
| 6146 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6147 | if (isa<UsingShadowDecl>(D)) |
| 6148 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6149 | |
| 6150 | AddMethodCandidate(cast<CXXMethodDecl>(D), ActingContext, |
| 6151 | Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6152 | /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6153 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6154 | |
| 6155 | // Perform overload resolution. |
| 6156 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6157 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6158 | case OR_Success: |
| 6159 | // Overload resolution succeeded; we'll build the call below. |
| 6160 | break; |
| 6161 | |
| 6162 | case OR_No_Viable_Function: |
| 6163 | if (CandidateSet.empty()) |
| 6164 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6165 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6166 | else |
| 6167 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6168 | << "operator->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6169 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6170 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6171 | |
| 6172 | case OR_Ambiguous: |
| 6173 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6174 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6175 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6176 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6177 | |
| 6178 | case OR_Deleted: |
| 6179 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6180 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6181 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6182 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6183 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | // Convert the object parameter. |
| 6187 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6188 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6189 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6190 | |
| 6191 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6192 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6193 | |
| 6194 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6195 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6196 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6197 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6198 | |
| 6199 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6200 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6201 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6202 | &Base, 1, ResultTy, OpLoc)); |
| 6203 | |
| 6204 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6205 | Method)) |
| 6206 | return ExprError(); |
| 6207 | return move(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6210 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6211 | /// a C++ overloaded function (possibly with some parentheses and |
| 6212 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6213 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6214 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 6215 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6216 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6217 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 6218 | if (SubExpr == PE->getSubExpr()) |
| 6219 | return PE->Retain(); |
| 6220 | |
| 6221 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6222 | } |
| 6223 | |
| 6224 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 6225 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6226 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6227 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6228 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6229 | if (SubExpr == ICE->getSubExpr()) |
| 6230 | return ICE->Retain(); |
| 6231 | |
| 6232 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6233 | ICE->getCastKind(), |
| 6234 | SubExpr, |
| 6235 | ICE->isLvalueCast()); |
| 6236 | } |
| 6237 | |
| 6238 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6239 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6240 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6241 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6242 | if (Method->isStatic()) { |
| 6243 | // Do nothing: static member functions aren't any different |
| 6244 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6245 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6246 | // Fix the sub expression, which really has to be an |
| 6247 | // UnresolvedLookupExpr holding an overloaded member function |
| 6248 | // or template. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6249 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6250 | if (SubExpr == UnOp->getSubExpr()) |
| 6251 | return UnOp->Retain(); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6252 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6253 | assert(isa<DeclRefExpr>(SubExpr) |
| 6254 | && "fixed to something other than a decl ref"); |
| 6255 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6256 | && "fixed to a member ref with no nested name qualifier"); |
| 6257 | |
| 6258 | // We have taken the address of a pointer to member |
| 6259 | // function. Perform the computation here so that we get the |
| 6260 | // appropriate pointer to member type. |
| 6261 | QualType ClassType |
| 6262 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6263 | QualType MemPtrType |
| 6264 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6265 | |
| 6266 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6267 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6268 | } |
| 6269 | } |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6270 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6271 | if (SubExpr == UnOp->getSubExpr()) |
| 6272 | return UnOp->Retain(); |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6274 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6275 | Context.getPointerType(SubExpr->getType()), |
| 6276 | UnOp->getOperatorLoc()); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6277 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6278 | |
| 6279 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6280 | // FIXME: avoid copy. |
| 6281 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6282 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6283 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6284 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6287 | return DeclRefExpr::Create(Context, |
| 6288 | ULE->getQualifier(), |
| 6289 | ULE->getQualifierRange(), |
| 6290 | Fn, |
| 6291 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6292 | Fn->getType(), |
| 6293 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6296 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6297 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6298 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6299 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6300 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6301 | TemplateArgs = &TemplateArgsBuffer; |
| 6302 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6303 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6304 | Expr *Base; |
| 6305 | |
| 6306 | // If we're filling in |
| 6307 | if (MemExpr->isImplicitAccess()) { |
| 6308 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6309 | return DeclRefExpr::Create(Context, |
| 6310 | MemExpr->getQualifier(), |
| 6311 | MemExpr->getQualifierRange(), |
| 6312 | Fn, |
| 6313 | MemExpr->getMemberLoc(), |
| 6314 | Fn->getType(), |
| 6315 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 6316 | } else { |
| 6317 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6318 | if (MemExpr->getQualifier()) |
| 6319 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6320 | Base = new (Context) CXXThisExpr(Loc, |
| 6321 | MemExpr->getBaseType(), |
| 6322 | /*isImplicit=*/true); |
| 6323 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6324 | } else |
| 6325 | Base = MemExpr->getBase()->Retain(); |
| 6326 | |
| 6327 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6328 | MemExpr->isArrow(), |
| 6329 | MemExpr->getQualifier(), |
| 6330 | MemExpr->getQualifierRange(), |
| 6331 | Fn, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6332 | MemExpr->getMemberLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6333 | TemplateArgs, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6334 | Fn->getType()); |
| 6335 | } |
| 6336 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6337 | assert(false && "Invalid reference to overloaded function"); |
| 6338 | return E->Retain(); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6341 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
| 6342 | FunctionDecl *Fn) { |
| 6343 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Fn)); |
| 6344 | } |
| 6345 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6346 | } // end namespace clang |