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 | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 492 | if (SuppressUserConversions && ICS.isUserDefined()) |
| 493 | ICS.setBad(); |
| 494 | } else if (UserDefResult == OR_Ambiguous) { |
| 495 | ICS.setAmbiguous(); |
| 496 | ICS.Ambiguous.setFromType(From->getType()); |
| 497 | ICS.Ambiguous.setToType(ToType); |
| 498 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 499 | Cand != Conversions.end(); ++Cand) |
| 500 | if (Cand->Viable) |
| 501 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 502 | } else { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 503 | ICS.setBad(); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 504 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 505 | |
| 506 | return ICS; |
| 507 | } |
| 508 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 509 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 510 | /// conversion that strips "noreturn" off the nested function type. |
| 511 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 512 | QualType ToType, QualType &ResultTy) { |
| 513 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 514 | return false; |
| 515 | |
| 516 | // Strip the noreturn off the type we're converting from; noreturn can |
| 517 | // safely be removed. |
| 518 | FromType = Context.getNoReturnType(FromType, false); |
| 519 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 520 | return false; |
| 521 | |
| 522 | ResultTy = FromType; |
| 523 | return true; |
| 524 | } |
| 525 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 526 | /// IsStandardConversion - Determines whether there is a standard |
| 527 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 528 | /// expression From to the type ToType. Standard conversion sequences |
| 529 | /// only consider non-class types; for conversions that involve class |
| 530 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 531 | /// contain the standard conversion sequence required to perform this |
| 532 | /// conversion and this routine will return true. Otherwise, this |
| 533 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | bool |
| 535 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 536 | bool InOverloadResolution, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | StandardConversionSequence &SCS) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 538 | QualType FromType = From->getType(); |
| 539 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 540 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 541 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 542 | SCS.Deprecated = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 543 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 544 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 545 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 547 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 549 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 550 | if (getLangOptions().CPlusPlus) |
| 551 | return false; |
| 552 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 556 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 557 | // array-to-pointer conversion, or function-to-pointer conversion |
| 558 | // (C++ 4p1). |
| 559 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 560 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 561 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 562 | // converted to an rvalue. |
| 563 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 565 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 566 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 567 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 568 | |
| 569 | // If T is a non-class type, the type of the rvalue is the |
| 570 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 571 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 572 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 573 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 574 | } else if (FromType->isArrayType()) { |
| 575 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 576 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 577 | |
| 578 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 579 | // bound of T" can be converted to an rvalue of type "pointer to |
| 580 | // T" (C++ 4.2p1). |
| 581 | FromType = Context.getArrayDecayedType(FromType); |
| 582 | |
| 583 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 584 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 585 | SCS.Deprecated = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 586 | |
| 587 | // For the purpose of ranking in overload resolution |
| 588 | // (13.3.3.1.1), this conversion is considered an |
| 589 | // array-to-pointer conversion followed by a qualification |
| 590 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 591 | SCS.Second = ICK_Identity; |
| 592 | SCS.Third = ICK_Qualification; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 593 | SCS.setToType(ToType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 594 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 595 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 596 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 597 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 598 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 599 | |
| 600 | // An lvalue of function type T can be converted to an rvalue of |
| 601 | // type "pointer to T." The result is a pointer to the |
| 602 | // function. (C++ 4.3p1). |
| 603 | FromType = Context.getPointerType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | } else if (FunctionDecl *Fn |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 605 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 606 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 607 | SCS.First = ICK_Function_To_Pointer; |
| 608 | |
| 609 | // We were able to resolve the address of the overloaded function, |
| 610 | // so we can convert to the type of that function. |
| 611 | FromType = Fn->getType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 612 | if (ToType->isLValueReferenceType()) |
| 613 | FromType = Context.getLValueReferenceType(FromType); |
| 614 | else if (ToType->isRValueReferenceType()) |
| 615 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 616 | else if (ToType->isMemberPointerType()) { |
| 617 | // Resolve address only succeeds if both sides are member pointers, |
| 618 | // but it doesn't have to be the same class. See DR 247. |
| 619 | // Note that this means that the type of &Derived::fn can be |
| 620 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 621 | // base class, even if it was brought into the derived class via a |
| 622 | // using declaration. The standard isn't clear on this issue at all. |
| 623 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 624 | FromType = Context.getMemberPointerType(FromType, |
| 625 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 626 | } else |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 627 | FromType = Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 628 | } else { |
| 629 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 630 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // The second conversion can be an integral promotion, floating |
| 634 | // point promotion, integral conversion, floating point conversion, |
| 635 | // floating-integral conversion, pointer conversion, |
| 636 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 637 | // For overloading in C, this can also be a "compatible-type" |
| 638 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 639 | bool IncompatibleObjC = false; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 640 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 641 | // The unqualified versions of the types are the same: there's no |
| 642 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 643 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 644 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 646 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 647 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 648 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 649 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 650 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 651 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 652 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 653 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 654 | SCS.Second = ICK_Complex_Promotion; |
| 655 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 656 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 657 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 658 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 659 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 660 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 661 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 662 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 663 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 664 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 665 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 666 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 667 | SCS.Second = ICK_Complex_Conversion; |
| 668 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 669 | } else if ((FromType->isFloatingType() && |
| 670 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 671 | !ToType->isEnumeralType())) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 673 | ToType->isFloatingType())) { |
| 674 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 675 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 676 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 677 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 678 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 679 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 680 | SCS.Second = ICK_Complex_Real; |
| 681 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 682 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 683 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 684 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 685 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 686 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 687 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 688 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 689 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 690 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 691 | } else if (ToType->isBooleanType() && |
| 692 | (FromType->isArithmeticType() || |
| 693 | FromType->isEnumeralType() || |
Fariborz Jahanian | 8811885 | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 694 | FromType->isAnyPointerType() || |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 695 | FromType->isBlockPointerType() || |
| 696 | FromType->isMemberPointerType() || |
| 697 | FromType->isNullPtrType())) { |
| 698 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 699 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 700 | FromType = Context.BoolTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 702 | Context.typesAreCompatible(ToType, FromType)) { |
| 703 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 704 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 705 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 706 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 707 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 708 | } else { |
| 709 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 710 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 713 | QualType CanonFrom; |
| 714 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 715 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 716 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 717 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 718 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 719 | CanonFrom = Context.getCanonicalType(FromType); |
| 720 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 721 | } else { |
| 722 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 723 | SCS.Third = ICK_Identity; |
| 724 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 726 | // [...] Any difference in top-level cv-qualification is |
| 727 | // subsumed by the initialization itself and does not constitute |
| 728 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 729 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 731 | if (CanonFrom.getLocalUnqualifiedType() |
| 732 | == CanonTo.getLocalUnqualifiedType() && |
| 733 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 734 | FromType = ToType; |
| 735 | CanonFrom = CanonTo; |
| 736 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // If we have not converted the argument type to the parameter type, |
| 740 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 741 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 742 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 743 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 744 | SCS.setToType(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 745 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 749 | /// expression From (whose potentially-adjusted type is FromType) to |
| 750 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 751 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 753 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 754 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 755 | if (!To) { |
| 756 | return false; |
| 757 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 758 | |
| 759 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 760 | // unsigned short int can be converted to an rvalue of type int if |
| 761 | // int can represent all the values of the source type; otherwise, |
| 762 | // the source rvalue can be converted to an rvalue of type unsigned |
| 763 | // int (C++ 4.5p1). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 764 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 765 | if (// We can promote any signed, promotable integer type to an int |
| 766 | (FromType->isSignedIntegerType() || |
| 767 | // We can promote any unsigned integer type whose size is |
| 768 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 770 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 771 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 774 | return To->getKind() == BuiltinType::UInt; |
| 775 | } |
| 776 | |
| 777 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 778 | // can be converted to an rvalue of the first of the following types |
| 779 | // that can represent all the values of its underlying type: int, |
| 780 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 781 | |
| 782 | // We pre-calculate the promotion type for enum types. |
| 783 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 784 | if (ToType->isIntegerType()) |
| 785 | return Context.hasSameUnqualifiedType(ToType, |
| 786 | FromEnumType->getDecl()->getPromotionType()); |
| 787 | |
| 788 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 789 | // Determine whether the type we're converting from is signed or |
| 790 | // unsigned. |
| 791 | bool FromIsSigned; |
| 792 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 793 | |
| 794 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 795 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 796 | |
| 797 | // The types we'll try to promote to, in the appropriate |
| 798 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | QualType PromoteTypes[6] = { |
| 800 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 801 | Context.LongTy, Context.UnsignedLongTy , |
| 802 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 803 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 804 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 805 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 806 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 808 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 809 | // We found the type that we can promote to. If this is the |
| 810 | // type we wanted, we have a promotion. Otherwise, no |
| 811 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 812 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 818 | // rvalue of type int if int can represent all the values of the |
| 819 | // bit-field; otherwise, it can be converted to unsigned int if |
| 820 | // unsigned int can represent all the values of the bit-field. If |
| 821 | // the bit-field is larger yet, no integral promotion applies to |
| 822 | // it. If the bit-field has an enumerated type, it is treated as any |
| 823 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 824 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 825 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 826 | using llvm::APSInt; |
| 827 | if (From) |
| 828 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 829 | APSInt BitWidth; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 830 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 831 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 832 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 833 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 835 | // Are we promoting to an int from a bitfield that fits in an int? |
| 836 | if (BitWidth < ToSize || |
| 837 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 838 | return To->getKind() == BuiltinType::Int; |
| 839 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 841 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 842 | // that fits into an unsigned int? |
| 843 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 844 | return To->getKind() == BuiltinType::UInt; |
| 845 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 847 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 848 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 849 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 851 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 852 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 853 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 854 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 855 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 856 | |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 861 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 862 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 864 | /// An rvalue of type float can be converted to an rvalue of type |
| 865 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 866 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 867 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 868 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 869 | ToBuiltin->getKind() == BuiltinType::Double) |
| 870 | return true; |
| 871 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 872 | // C99 6.3.1.5p1: |
| 873 | // When a float is promoted to double or long double, or a |
| 874 | // double is promoted to long double [...]. |
| 875 | if (!getLangOptions().CPlusPlus && |
| 876 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 877 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 878 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 879 | return true; |
| 880 | } |
| 881 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 882 | return false; |
| 883 | } |
| 884 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 885 | /// \brief Determine if a conversion is a complex promotion. |
| 886 | /// |
| 887 | /// A complex promotion is defined as a complex -> complex conversion |
| 888 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 889 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 890 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 891 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 892 | if (!FromComplex) |
| 893 | return false; |
| 894 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 895 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 896 | if (!ToComplex) |
| 897 | return false; |
| 898 | |
| 899 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 900 | ToComplex->getElementType()) || |
| 901 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 902 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 905 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 906 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 907 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 908 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 909 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | static QualType |
| 911 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 912 | QualType ToPointee, QualType ToType, |
| 913 | ASTContext &Context) { |
| 914 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 915 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 916 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | |
| 918 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 919 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 920 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 921 | if (!ToType.isNull()) |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 922 | return ToType; |
| 923 | |
| 924 | // Build a pointer to ToPointee. It has the right qualifiers |
| 925 | // already. |
| 926 | return Context.getPointerType(ToPointee); |
| 927 | } |
| 928 | |
| 929 | // Just build a canonical type that has the right qualifiers. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 930 | return Context.getPointerType( |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 931 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 932 | Quals)); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 935 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 936 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 937 | /// not have the right set of qualifiers. |
| 938 | static QualType |
| 939 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 940 | QualType ToType, |
| 941 | ASTContext &Context) { |
| 942 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 943 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 944 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 945 | |
| 946 | // Exact qualifier match -> return the pointer type we're converting to. |
| 947 | if (CanonToType.getLocalQualifiers() == Quals) |
| 948 | return ToType; |
| 949 | |
| 950 | // Just build a canonical type that has the right qualifiers. |
| 951 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 952 | } |
| 953 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 955 | bool InOverloadResolution, |
| 956 | ASTContext &Context) { |
| 957 | // Handle value-dependent integral null pointer constants correctly. |
| 958 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 959 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 960 | Expr->getType()->isIntegralType()) |
| 961 | return !InOverloadResolution; |
| 962 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 963 | return Expr->isNullPointerConstant(Context, |
| 964 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 965 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 966 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 968 | /// IsPointerConversion - Determines whether the conversion of the |
| 969 | /// expression From, which has the (possibly adjusted) type FromType, |
| 970 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 971 | /// 4.10). If so, returns true and places the converted type (that |
| 972 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 973 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 974 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 975 | /// This routine also supports conversions to and from block pointers |
| 976 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 977 | /// pointers to interfaces. FIXME: Once we've determined the |
| 978 | /// appropriate overloading rules for Objective-C, we may want to |
| 979 | /// split the Objective-C checks into a different routine; however, |
| 980 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 981 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 982 | /// set if the conversion is an allowed Objective-C conversion that |
| 983 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 984 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 985 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 986 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 988 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 989 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 990 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 991 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 993 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 994 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 995 | ConvertedType = ToType; |
| 996 | return true; |
| 997 | } |
| 998 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 999 | // Blocks: Block pointers can be converted to void*. |
| 1000 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1001 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1002 | ConvertedType = ToType; |
| 1003 | return true; |
| 1004 | } |
| 1005 | // Blocks: A null pointer constant can be converted to a block |
| 1006 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1008 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1009 | ConvertedType = ToType; |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1013 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1014 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1016 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1017 | ConvertedType = ToType; |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1021 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1022 | if (!ToTypePtr) |
| 1023 | return false; |
| 1024 | |
| 1025 | // 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] | 1026 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1027 | ConvertedType = ToType; |
| 1028 | return true; |
| 1029 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1030 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1031 | // Beyond this point, both types need to be pointers |
| 1032 | // , including objective-c pointers. |
| 1033 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1034 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1035 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1036 | ToType, Context); |
| 1037 | return true; |
| 1038 | |
| 1039 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1040 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1041 | if (!FromTypePtr) |
| 1042 | return false; |
| 1043 | |
| 1044 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1045 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1046 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1047 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1048 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1049 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1051 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1052 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1053 | return true; |
| 1054 | } |
| 1055 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1056 | // When we're overloading in C, we allow a special kind of pointer |
| 1057 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1059 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1061 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1063 | return true; |
| 1064 | } |
| 1065 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1066 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1068 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1069 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1070 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1071 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1072 | // necessitates this conversion is ill-formed. The result of the |
| 1073 | // conversion is a pointer to the base class sub-object of the |
| 1074 | // derived class object. The null pointer value is converted to |
| 1075 | // the null pointer value of the destination type. |
| 1076 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1077 | // Note that we do not check for ambiguity or inaccessibility |
| 1078 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1079 | if (getLangOptions().CPlusPlus && |
| 1080 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1081 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1082 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1084 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1085 | ToType, Context); |
| 1086 | return true; |
| 1087 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1089 | return false; |
| 1090 | } |
| 1091 | |
| 1092 | /// isObjCPointerConversion - Determines whether this is an |
| 1093 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1094 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1096 | QualType& ConvertedType, |
| 1097 | bool &IncompatibleObjC) { |
| 1098 | if (!getLangOptions().ObjC1) |
| 1099 | return false; |
| 1100 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1101 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1102 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1104 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1105 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1106 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1107 | // 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] | 1108 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1109 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1110 | ConvertedType = ToType; |
| 1111 | return true; |
| 1112 | } |
| 1113 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1115 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1117 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1118 | ConvertedType = ToType; |
| 1119 | return true; |
| 1120 | } |
| 1121 | // Objective C++: We're able to convert from a pointer to an |
| 1122 | // interface to a pointer to a different interface. |
| 1123 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1124 | ConvertedType = ToType; |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
| 1128 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1129 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1130 | // interfaces, which is permitted. However, we're going to |
| 1131 | // complain about it. |
| 1132 | IncompatibleObjC = true; |
| 1133 | ConvertedType = FromType; |
| 1134 | return true; |
| 1135 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1137 | // 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] | 1138 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1139 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1140 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1141 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1142 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1143 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1144 | return false; |
| 1145 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1146 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1147 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1148 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1149 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1150 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1151 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1152 | return false; |
| 1153 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1154 | // If we have pointers to pointers, recursively check whether this |
| 1155 | // is an Objective-C conversion. |
| 1156 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1157 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1158 | IncompatibleObjC)) { |
| 1159 | // We always complain about this conversion. |
| 1160 | IncompatibleObjC = true; |
| 1161 | ConvertedType = ToType; |
| 1162 | return true; |
| 1163 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1164 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1165 | // differences in the argument and result types are in Objective-C |
| 1166 | // pointer conversions. If so, we permit the conversion (but |
| 1167 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1169 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1170 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1171 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1172 | if (FromFunctionType && ToFunctionType) { |
| 1173 | // If the function types are exactly the same, this isn't an |
| 1174 | // Objective-C pointer conversion. |
| 1175 | if (Context.getCanonicalType(FromPointeeType) |
| 1176 | == Context.getCanonicalType(ToPointeeType)) |
| 1177 | return false; |
| 1178 | |
| 1179 | // Perform the quick checks that will tell us whether these |
| 1180 | // function types are obviously different. |
| 1181 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1182 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1183 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1184 | return false; |
| 1185 | |
| 1186 | bool HasObjCConversion = false; |
| 1187 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1188 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1189 | // Okay, the types match exactly. Nothing to do. |
| 1190 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1191 | ToFunctionType->getResultType(), |
| 1192 | ConvertedType, IncompatibleObjC)) { |
| 1193 | // Okay, we have an Objective-C pointer conversion. |
| 1194 | HasObjCConversion = true; |
| 1195 | } else { |
| 1196 | // Function types are too different. Abort. |
| 1197 | return false; |
| 1198 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1200 | // Check argument types. |
| 1201 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1202 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1203 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1204 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1205 | if (Context.getCanonicalType(FromArgType) |
| 1206 | == Context.getCanonicalType(ToArgType)) { |
| 1207 | // Okay, the types match exactly. Nothing to do. |
| 1208 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1209 | ConvertedType, IncompatibleObjC)) { |
| 1210 | // Okay, we have an Objective-C pointer conversion. |
| 1211 | HasObjCConversion = true; |
| 1212 | } else { |
| 1213 | // Argument types are too different. Abort. |
| 1214 | return false; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | if (HasObjCConversion) { |
| 1219 | // We had an Objective-C conversion. Allow this pointer |
| 1220 | // conversion, but complain about it. |
| 1221 | ConvertedType = ToType; |
| 1222 | IncompatibleObjC = true; |
| 1223 | return true; |
| 1224 | } |
| 1225 | } |
| 1226 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1227 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1230 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1231 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1232 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1233 | /// conversions for which IsPointerConversion has already returned |
| 1234 | /// true. It returns true and produces a diagnostic if there was an |
| 1235 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1236 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1237 | CastExpr::CastKind &Kind, |
| 1238 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1239 | QualType FromType = From->getType(); |
| 1240 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1241 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1242 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1243 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1244 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1246 | if (FromPointeeType->isRecordType() && |
| 1247 | ToPointeeType->isRecordType()) { |
| 1248 | // We must have a derived-to-base conversion. Check an |
| 1249 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1250 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1251 | From->getExprLoc(), |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1252 | From->getSourceRange(), |
| 1253 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1254 | return true; |
| 1255 | |
| 1256 | // The conversion was successful. |
| 1257 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1260 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1261 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1263 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1264 | // Objective-C++ conversions are always okay. |
| 1265 | // FIXME: We should have a different class of conversions for the |
| 1266 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1267 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1268 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1269 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1270 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1271 | return false; |
| 1272 | } |
| 1273 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1274 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1275 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1276 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1277 | /// If so, returns true and places the converted type (that might differ from |
| 1278 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1279 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1280 | QualType ToType, |
| 1281 | bool InOverloadResolution, |
| 1282 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1283 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1284 | if (!ToTypePtr) |
| 1285 | return false; |
| 1286 | |
| 1287 | // 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] | 1288 | if (From->isNullPointerConstant(Context, |
| 1289 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1290 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1291 | ConvertedType = ToType; |
| 1292 | return true; |
| 1293 | } |
| 1294 | |
| 1295 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1296 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1297 | if (!FromTypePtr) |
| 1298 | return false; |
| 1299 | |
| 1300 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1301 | // where D is derived from B (C++ 4.11p2). |
| 1302 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1303 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1304 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1305 | |
| 1306 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1307 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1308 | ToClass.getTypePtr()); |
| 1309 | return true; |
| 1310 | } |
| 1311 | |
| 1312 | return false; |
| 1313 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1314 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1315 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1316 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1317 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1318 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1319 | /// true and produces a diagnostic if there was an error, or returns false |
| 1320 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1322 | CastExpr::CastKind &Kind, |
| 1323 | bool IgnoreBaseAccess) { |
| 1324 | (void)IgnoreBaseAccess; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1325 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1326 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1327 | if (!FromPtrType) { |
| 1328 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1329 | assert(From->isNullPointerConstant(Context, |
| 1330 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1331 | "Expr must be null pointer constant!"); |
| 1332 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1333 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1334 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1335 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1336 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1337 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1338 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1339 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1340 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1341 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
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 | // FIXME: What about dependent types? |
| 1344 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1345 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1347 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1348 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1349 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1350 | assert(DerivationOkay && |
| 1351 | "Should not have been called if derivation isn't OK."); |
| 1352 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1353 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1354 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1355 | getUnqualifiedType())) { |
| 1356 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1357 | Paths.clear(); |
| 1358 | Paths.setRecordingPaths(true); |
| 1359 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1360 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1361 | (void)StillOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1362 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1363 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1364 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1365 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1366 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1367 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1368 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1369 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1370 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1371 | << FromClass << ToClass << QualType(VBase, 0) |
| 1372 | << From->getSourceRange(); |
| 1373 | return true; |
| 1374 | } |
| 1375 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1376 | // Must be a base to derived member conversion. |
| 1377 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1378 | return false; |
| 1379 | } |
| 1380 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1381 | /// IsQualificationConversion - Determines whether the conversion from |
| 1382 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1383 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | bool |
| 1385 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1386 | FromType = Context.getCanonicalType(FromType); |
| 1387 | ToType = Context.getCanonicalType(ToType); |
| 1388 | |
| 1389 | // If FromType and ToType are the same type, this is not a |
| 1390 | // qualification conversion. |
| 1391 | if (FromType == ToType) |
| 1392 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1393 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1394 | // (C++ 4.4p4): |
| 1395 | // A conversion can add cv-qualifiers at levels other than the first |
| 1396 | // in multi-level pointers, subject to the following rules: [...] |
| 1397 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1398 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1399 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1400 | // Within each iteration of the loop, we check the qualifiers to |
| 1401 | // determine if this still looks like a qualification |
| 1402 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1403 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1404 | // until there are no more pointers or pointers-to-members left to |
| 1405 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1406 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1407 | |
| 1408 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1409 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1410 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1411 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1413 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1414 | // every cv for 0 < k < j. |
| 1415 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1416 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1417 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1419 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1420 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1422 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1423 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1424 | |
| 1425 | // We are left with FromType and ToType being the pointee types |
| 1426 | // after unwrapping the original FromType and ToType the same number |
| 1427 | // of types. If we unwrapped any pointers, and if FromType and |
| 1428 | // ToType have the same unqualified type (since we checked |
| 1429 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1430 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1433 | /// Determines whether there is a user-defined conversion sequence |
| 1434 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1435 | /// ToType. If such a conversion exists, User will contain the |
| 1436 | /// user-defined conversion sequence that performs such a conversion |
| 1437 | /// and this routine will return true. Otherwise, this routine returns |
| 1438 | /// false and User is unspecified. |
| 1439 | /// |
| 1440 | /// \param AllowConversionFunctions true if the conversion should |
| 1441 | /// consider conversion functions at all. If false, only constructors |
| 1442 | /// will be considered. |
| 1443 | /// |
| 1444 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1445 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1446 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1447 | /// |
| 1448 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1449 | /// for overload resolution. |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1450 | /// \param UserCast true if looking for user defined conversion for a static |
| 1451 | /// cast. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1452 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1453 | UserDefinedConversionSequence& User, |
| 1454 | OverloadCandidateSet& CandidateSet, |
| 1455 | bool AllowConversionFunctions, |
| 1456 | bool AllowExplicit, |
| 1457 | bool ForceRValue, |
| 1458 | bool UserCast) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1459 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1460 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1461 | // We're not going to find any constructors. |
| 1462 | } else if (CXXRecordDecl *ToRecordDecl |
| 1463 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1464 | // C++ [over.match.ctor]p1: |
| 1465 | // When objects of class type are direct-initialized (8.5), or |
| 1466 | // copy-initialized from an expression of the same or a |
| 1467 | // derived class type (8.5), overload resolution selects the |
| 1468 | // constructor. [...] For copy-initialization, the candidate |
| 1469 | // functions are all the converting constructors (12.3.1) of |
| 1470 | // that class. The argument list is the expression-list within |
| 1471 | // the parentheses of the initializer. |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1472 | bool SuppressUserConversions = !UserCast; |
| 1473 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1474 | IsDerivedFrom(From->getType(), ToType)) { |
| 1475 | SuppressUserConversions = false; |
| 1476 | AllowConversionFunctions = false; |
| 1477 | } |
| 1478 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1480 | = Context.DeclarationNames.getCXXConstructorName( |
| 1481 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1482 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1484 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1485 | Con != ConEnd; ++Con) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1486 | // Find the constructor (which may be a template). |
| 1487 | CXXConstructorDecl *Constructor = 0; |
| 1488 | FunctionTemplateDecl *ConstructorTmpl |
| 1489 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1490 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1492 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1493 | else |
| 1494 | Constructor = cast<CXXConstructorDecl>(*Con); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1495 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1496 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1497 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1498 | if (ConstructorTmpl) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1499 | AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0, |
| 1500 | &From, 1, CandidateSet, |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1501 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1502 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1503 | // Allow one user-defined conversion when user specifies a |
| 1504 | // From->ToType conversion via an static cast (c-style, etc). |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1505 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1506 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1507 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1508 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1512 | if (!AllowConversionFunctions) { |
| 1513 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1515 | PDiag(0) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1516 | << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1517 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | } else if (const RecordType *FromRecordType |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1519 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1521 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1522 | // Add all of the conversion functions as candidates. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1523 | const UnresolvedSet *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1524 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1525 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 1526 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1527 | NamedDecl *D = *I; |
| 1528 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1529 | if (isa<UsingShadowDecl>(D)) |
| 1530 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1531 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1532 | CXXConversionDecl *Conv; |
| 1533 | FunctionTemplateDecl *ConvTemplate; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1534 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(*I))) |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1535 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1536 | else |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1537 | Conv = dyn_cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1540 | if (ConvTemplate) |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1541 | AddTemplateConversionCandidate(ConvTemplate, ActingContext, |
| 1542 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1543 | else |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1544 | AddConversionCandidate(Conv, ActingContext, From, ToType, |
| 1545 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1549 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1550 | |
| 1551 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1552 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1553 | case OR_Success: |
| 1554 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1556 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1557 | // C++ [over.ics.user]p1: |
| 1558 | // If the user-defined conversion is specified by a |
| 1559 | // constructor (12.3.1), the initial standard conversion |
| 1560 | // sequence converts the source type to the type required by |
| 1561 | // the argument of the constructor. |
| 1562 | // |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1563 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1564 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1565 | User.EllipsisConversion = true; |
| 1566 | else { |
| 1567 | User.Before = Best->Conversions[0].Standard; |
| 1568 | User.EllipsisConversion = false; |
| 1569 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1570 | User.ConversionFunction = Constructor; |
| 1571 | User.After.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1572 | User.After.setFromType( |
| 1573 | ThisType->getAs<PointerType>()->getPointeeType()); |
| 1574 | User.After.setToType(ToType); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1575 | return OR_Success; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1576 | } else if (CXXConversionDecl *Conversion |
| 1577 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1578 | // C++ [over.ics.user]p1: |
| 1579 | // |
| 1580 | // [...] If the user-defined conversion is specified by a |
| 1581 | // conversion function (12.3.2), the initial standard |
| 1582 | // conversion sequence converts the source type to the |
| 1583 | // implicit object parameter of the conversion function. |
| 1584 | User.Before = Best->Conversions[0].Standard; |
| 1585 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1586 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
| 1588 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1589 | // The second standard conversion sequence converts the |
| 1590 | // result of the user-defined conversion to the target type |
| 1591 | // for the sequence. Since an implicit conversion sequence |
| 1592 | // is an initialization, the special rules for |
| 1593 | // initialization by user-defined conversion apply when |
| 1594 | // selecting the best user-defined conversion for a |
| 1595 | // user-defined conversion sequence (see 13.3.3 and |
| 1596 | // 13.3.3.1). |
| 1597 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1598 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1599 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1600 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1601 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1602 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1604 | case OR_No_Viable_Function: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1605 | return OR_No_Viable_Function; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1606 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1607 | // No conversion here! We're done. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1608 | return OR_Deleted; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1609 | |
| 1610 | case OR_Ambiguous: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1611 | return OR_Ambiguous; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1614 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1615 | } |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1616 | |
| 1617 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1618 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1619 | ImplicitConversionSequence ICS; |
| 1620 | OverloadCandidateSet CandidateSet; |
| 1621 | OverloadingResult OvResult = |
| 1622 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1623 | CandidateSet, true, false, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1624 | if (OvResult == OR_Ambiguous) |
| 1625 | Diag(From->getSourceRange().getBegin(), |
| 1626 | diag::err_typecheck_ambiguous_condition) |
| 1627 | << From->getType() << ToType << From->getSourceRange(); |
| 1628 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1629 | Diag(From->getSourceRange().getBegin(), |
| 1630 | diag::err_typecheck_nonviable_condition) |
| 1631 | << From->getType() << ToType << From->getSourceRange(); |
| 1632 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1633 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 1634 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1635 | return true; |
| 1636 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1637 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1638 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1639 | /// conversion sequences to determine whether one is better than the |
| 1640 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1642 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1643 | const ImplicitConversionSequence& ICS2) |
| 1644 | { |
| 1645 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1646 | // conversion sequences (as defined in 13.3.3.1) |
| 1647 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1648 | // conversion sequence than a user-defined conversion sequence or |
| 1649 | // an ellipsis conversion sequence, and |
| 1650 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1651 | // conversion sequence than an ellipsis conversion sequence |
| 1652 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1654 | // C++0x [over.best.ics]p10: |
| 1655 | // For the purpose of ranking implicit conversion sequences as |
| 1656 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1657 | // treated as a user-defined sequence that is indistinguishable |
| 1658 | // from any other user-defined conversion sequence. |
| 1659 | if (ICS1.getKind() < ICS2.getKind()) { |
| 1660 | if (!(ICS1.isUserDefined() && ICS2.isAmbiguous())) |
| 1661 | return ImplicitConversionSequence::Better; |
| 1662 | } else if (ICS2.getKind() < ICS1.getKind()) { |
| 1663 | if (!(ICS2.isUserDefined() && ICS1.isAmbiguous())) |
| 1664 | return ImplicitConversionSequence::Worse; |
| 1665 | } |
| 1666 | |
| 1667 | if (ICS1.isAmbiguous() || ICS2.isAmbiguous()) |
| 1668 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1669 | |
| 1670 | // Two implicit conversion sequences of the same form are |
| 1671 | // indistinguishable conversion sequences unless one of the |
| 1672 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1673 | if (ICS1.isStandard()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1674 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1675 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1676 | // User-defined conversion sequence U1 is a better conversion |
| 1677 | // sequence than another user-defined conversion sequence U2 if |
| 1678 | // they contain the same user-defined conversion function or |
| 1679 | // constructor and if the second standard conversion sequence of |
| 1680 | // U1 is better than the second standard conversion sequence of |
| 1681 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1683 | ICS2.UserDefined.ConversionFunction) |
| 1684 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1685 | ICS2.UserDefined.After); |
| 1686 | } |
| 1687 | |
| 1688 | return ImplicitConversionSequence::Indistinguishable; |
| 1689 | } |
| 1690 | |
| 1691 | /// CompareStandardConversionSequences - Compare two standard |
| 1692 | /// conversion sequences to determine whether one is better than the |
| 1693 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1695 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1696 | const StandardConversionSequence& SCS2) |
| 1697 | { |
| 1698 | // Standard conversion sequence S1 is a better conversion sequence |
| 1699 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1700 | |
| 1701 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1702 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1703 | // excluding any Lvalue Transformation; the identity conversion |
| 1704 | // sequence is considered to be a subsequence of any |
| 1705 | // non-identity conversion sequence) or, if not that, |
| 1706 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1707 | // Neither is a proper subsequence of the other. Do nothing. |
| 1708 | ; |
| 1709 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1710 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1712 | SCS1.Third == ICK_Identity)) |
| 1713 | // SCS1 is a proper subsequence of SCS2. |
| 1714 | return ImplicitConversionSequence::Better; |
| 1715 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1716 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1718 | SCS2.Third == ICK_Identity)) |
| 1719 | // SCS2 is a proper subsequence of SCS1. |
| 1720 | return ImplicitConversionSequence::Worse; |
| 1721 | |
| 1722 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1723 | // defined below), or, if not that, |
| 1724 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1725 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1726 | if (Rank1 < Rank2) |
| 1727 | return ImplicitConversionSequence::Better; |
| 1728 | else if (Rank2 < Rank1) |
| 1729 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1730 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1731 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1732 | // are indistinguishable unless one of the following rules |
| 1733 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1735 | // A conversion that is not a conversion of a pointer, or |
| 1736 | // pointer to member, to bool is better than another conversion |
| 1737 | // that is such a conversion. |
| 1738 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1739 | return SCS2.isPointerConversionToBool() |
| 1740 | ? ImplicitConversionSequence::Better |
| 1741 | : ImplicitConversionSequence::Worse; |
| 1742 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1743 | // C++ [over.ics.rank]p4b2: |
| 1744 | // |
| 1745 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1746 | // conversion of B* to A* is better than conversion of B* to |
| 1747 | // void*, and conversion of A* to void* is better than conversion |
| 1748 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1750 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1752 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1753 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1754 | // Exactly one of the conversion sequences is a conversion to |
| 1755 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1756 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1757 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1758 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1759 | // Neither conversion sequence converts to a void pointer; compare |
| 1760 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1761 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1762 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1763 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1764 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1765 | // Both conversion sequences are conversions to void |
| 1766 | // pointers. Compare the source types to determine if there's an |
| 1767 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1768 | QualType FromType1 = SCS1.getFromType(); |
| 1769 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Adjust the types we're converting from via the array-to-pointer |
| 1772 | // conversion, if we need to. |
| 1773 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1774 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1775 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1776 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1777 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1778 | QualType FromPointee1 |
| 1779 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1780 | QualType FromPointee2 |
| 1781 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1782 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1783 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1784 | return ImplicitConversionSequence::Better; |
| 1785 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1786 | return ImplicitConversionSequence::Worse; |
| 1787 | |
| 1788 | // Objective-C++: If one interface is more specific than the |
| 1789 | // other, it is the better one. |
| 1790 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1791 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1792 | if (FromIface1 && FromIface1) { |
| 1793 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1794 | return ImplicitConversionSequence::Better; |
| 1795 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1796 | return ImplicitConversionSequence::Worse; |
| 1797 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1798 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1799 | |
| 1800 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1801 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1803 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1804 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1805 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1806 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1807 | // C++0x [over.ics.rank]p3b4: |
| 1808 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1809 | // implicit object parameter of a non-static member function declared |
| 1810 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1811 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1812 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1813 | // or if the member function in this case has a ref qualifier. |
| 1814 | // (Of course, we don't have ref qualifiers yet.) |
| 1815 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1816 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1817 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1818 | |
| 1819 | // C++ [over.ics.rank]p3b4: |
| 1820 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1821 | // which the references refer are the same type except for |
| 1822 | // top-level cv-qualifiers, and the type to which the reference |
| 1823 | // initialized by S2 refers is more cv-qualified than the type |
| 1824 | // to which the reference initialized by S1 refers. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1825 | QualType T1 = SCS1.getToType(); |
| 1826 | QualType T2 = SCS2.getToType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1827 | T1 = Context.getCanonicalType(T1); |
| 1828 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1829 | Qualifiers T1Quals, T2Quals; |
| 1830 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1831 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1832 | if (UnqualT1 == UnqualT2) { |
| 1833 | // If the type is an array type, promote the element qualifiers to the type |
| 1834 | // for comparison. |
| 1835 | if (isa<ArrayType>(T1) && T1Quals) |
| 1836 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1837 | if (isa<ArrayType>(T2) && T2Quals) |
| 1838 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1839 | if (T2.isMoreQualifiedThan(T1)) |
| 1840 | return ImplicitConversionSequence::Better; |
| 1841 | else if (T1.isMoreQualifiedThan(T2)) |
| 1842 | return ImplicitConversionSequence::Worse; |
| 1843 | } |
| 1844 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1845 | |
| 1846 | return ImplicitConversionSequence::Indistinguishable; |
| 1847 | } |
| 1848 | |
| 1849 | /// CompareQualificationConversions - Compares two standard conversion |
| 1850 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1852 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1853 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1855 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1856 | // -- S1 and S2 differ only in their qualification conversion and |
| 1857 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1858 | // cv-qualification signature of type T1 is a proper subset of |
| 1859 | // the cv-qualification signature of type T2, and S1 is not the |
| 1860 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1861 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1862 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1863 | return ImplicitConversionSequence::Indistinguishable; |
| 1864 | |
| 1865 | // FIXME: the example in the standard doesn't use a qualification |
| 1866 | // conversion (!) |
| 1867 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1868 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1869 | T1 = Context.getCanonicalType(T1); |
| 1870 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1871 | Qualifiers T1Quals, T2Quals; |
| 1872 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1873 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1874 | |
| 1875 | // If the types are the same, we won't learn anything by unwrapped |
| 1876 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1877 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1878 | return ImplicitConversionSequence::Indistinguishable; |
| 1879 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1880 | // If the type is an array type, promote the element qualifiers to the type |
| 1881 | // for comparison. |
| 1882 | if (isa<ArrayType>(T1) && T1Quals) |
| 1883 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1884 | if (isa<ArrayType>(T2) && T2Quals) |
| 1885 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1886 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1888 | = ImplicitConversionSequence::Indistinguishable; |
| 1889 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1890 | // Within each iteration of the loop, we check the qualifiers to |
| 1891 | // determine if this still looks like a qualification |
| 1892 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1893 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1894 | // until there are no more pointers or pointers-to-members left |
| 1895 | // to unwrap. This essentially mimics what |
| 1896 | // IsQualificationConversion does, but here we're checking for a |
| 1897 | // strict subset of qualifiers. |
| 1898 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1899 | // The qualifiers are the same, so this doesn't tell us anything |
| 1900 | // about how the sequences rank. |
| 1901 | ; |
| 1902 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1903 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1904 | if (Result == ImplicitConversionSequence::Worse) |
| 1905 | // Neither has qualifiers that are a subset of the other's |
| 1906 | // qualifiers. |
| 1907 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1909 | Result = ImplicitConversionSequence::Better; |
| 1910 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1911 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1912 | if (Result == ImplicitConversionSequence::Better) |
| 1913 | // Neither has qualifiers that are a subset of the other's |
| 1914 | // qualifiers. |
| 1915 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1917 | Result = ImplicitConversionSequence::Worse; |
| 1918 | } else { |
| 1919 | // Qualifiers are disjoint. |
| 1920 | return ImplicitConversionSequence::Indistinguishable; |
| 1921 | } |
| 1922 | |
| 1923 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1924 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1925 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1928 | // Check that the winning standard conversion sequence isn't using |
| 1929 | // the deprecated string literal array to pointer conversion. |
| 1930 | switch (Result) { |
| 1931 | case ImplicitConversionSequence::Better: |
| 1932 | if (SCS1.Deprecated) |
| 1933 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1934 | break; |
| 1935 | |
| 1936 | case ImplicitConversionSequence::Indistinguishable: |
| 1937 | break; |
| 1938 | |
| 1939 | case ImplicitConversionSequence::Worse: |
| 1940 | if (SCS2.Deprecated) |
| 1941 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1942 | break; |
| 1943 | } |
| 1944 | |
| 1945 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1948 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1949 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1950 | /// various kinds of derived-to-base conversions (C++ |
| 1951 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1952 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1953 | ImplicitConversionSequence::CompareKind |
| 1954 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1955 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1956 | QualType FromType1 = SCS1.getFromType(); |
| 1957 | QualType ToType1 = SCS1.getToType(); |
| 1958 | QualType FromType2 = SCS2.getFromType(); |
| 1959 | QualType ToType2 = SCS2.getToType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1960 | |
| 1961 | // Adjust the types we're converting from via the array-to-pointer |
| 1962 | // conversion, if we need to. |
| 1963 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1964 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1965 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1966 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1967 | |
| 1968 | // Canonicalize all of the types. |
| 1969 | FromType1 = Context.getCanonicalType(FromType1); |
| 1970 | ToType1 = Context.getCanonicalType(ToType1); |
| 1971 | FromType2 = Context.getCanonicalType(FromType2); |
| 1972 | ToType2 = Context.getCanonicalType(ToType2); |
| 1973 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1974 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1975 | // |
| 1976 | // If class B is derived directly or indirectly from class A and |
| 1977 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1978 | // |
| 1979 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1980 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1981 | |
| 1982 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1984 | SCS2.Second == ICK_Pointer_Conversion && |
| 1985 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1986 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1987 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1989 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1991 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1992 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1993 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1994 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1995 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1996 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1997 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1998 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1999 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2000 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2002 | // -- 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] | 2003 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2004 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2005 | return ImplicitConversionSequence::Better; |
| 2006 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2007 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2008 | |
| 2009 | if (ToIface1 && ToIface2) { |
| 2010 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2011 | return ImplicitConversionSequence::Better; |
| 2012 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2013 | return ImplicitConversionSequence::Worse; |
| 2014 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2015 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2016 | |
| 2017 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2018 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2019 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2020 | return ImplicitConversionSequence::Better; |
| 2021 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2022 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2024 | if (FromIface1 && FromIface2) { |
| 2025 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2026 | return ImplicitConversionSequence::Better; |
| 2027 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2028 | return ImplicitConversionSequence::Worse; |
| 2029 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2030 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2033 | // Compare based on reference bindings. |
| 2034 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 2035 | SCS1.Second == ICK_Derived_To_Base) { |
| 2036 | // -- binding of an expression of type C to a reference of type |
| 2037 | // B& is better than binding an expression of type C to a |
| 2038 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2039 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2040 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2041 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2042 | return ImplicitConversionSequence::Better; |
| 2043 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2044 | return ImplicitConversionSequence::Worse; |
| 2045 | } |
| 2046 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2047 | // -- binding of an expression of type B to a reference of type |
| 2048 | // A& is better than binding an expression of type C to a |
| 2049 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2050 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2051 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2052 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2053 | return ImplicitConversionSequence::Better; |
| 2054 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2055 | return ImplicitConversionSequence::Worse; |
| 2056 | } |
| 2057 | } |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2060 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2061 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2062 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2063 | const MemberPointerType * FromMemPointer1 = |
| 2064 | FromType1->getAs<MemberPointerType>(); |
| 2065 | const MemberPointerType * ToMemPointer1 = |
| 2066 | ToType1->getAs<MemberPointerType>(); |
| 2067 | const MemberPointerType * FromMemPointer2 = |
| 2068 | FromType2->getAs<MemberPointerType>(); |
| 2069 | const MemberPointerType * ToMemPointer2 = |
| 2070 | ToType2->getAs<MemberPointerType>(); |
| 2071 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2072 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2073 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2074 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2075 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2076 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2077 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2078 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2079 | // 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] | 2080 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2081 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2082 | return ImplicitConversionSequence::Worse; |
| 2083 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2084 | return ImplicitConversionSequence::Better; |
| 2085 | } |
| 2086 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2087 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2088 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2089 | return ImplicitConversionSequence::Better; |
| 2090 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2091 | return ImplicitConversionSequence::Worse; |
| 2092 | } |
| 2093 | } |
| 2094 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2095 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 2096 | SCS1.Second == ICK_Derived_To_Base) { |
| 2097 | // -- 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] | 2098 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2099 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2100 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2101 | return ImplicitConversionSequence::Better; |
| 2102 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2103 | return ImplicitConversionSequence::Worse; |
| 2104 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2105 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2106 | // -- 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] | 2107 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2108 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2109 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2110 | return ImplicitConversionSequence::Better; |
| 2111 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2112 | return ImplicitConversionSequence::Worse; |
| 2113 | } |
| 2114 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2116 | return ImplicitConversionSequence::Indistinguishable; |
| 2117 | } |
| 2118 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2119 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2120 | /// ToType from the expression From. Return the implicit conversion |
| 2121 | /// sequence required to pass this argument, which may be a bad |
| 2122 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2123 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2124 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2125 | /// 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] | 2126 | ImplicitConversionSequence |
| 2127 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2128 | bool SuppressUserConversions, bool ForceRValue, |
| 2129 | bool InOverloadResolution) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2130 | if (ToType->isReferenceType()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2131 | ImplicitConversionSequence ICS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2133 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2134 | SuppressUserConversions, |
| 2135 | /*AllowExplicit=*/false, |
| 2136 | ForceRValue, |
| 2137 | &ICS); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2138 | return ICS; |
| 2139 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2141 | SuppressUserConversions, |
| 2142 | /*AllowExplicit=*/false, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2143 | ForceRValue, |
| 2144 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2148 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2149 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2150 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2151 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2152 | /// differently in C++0x for this case. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2154 | AssignmentAction Action, bool Elidable) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2155 | if (!getLangOptions().CPlusPlus) { |
| 2156 | // In C, argument passing is the same as performing an assignment. |
| 2157 | QualType FromType = From->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2159 | AssignConvertType ConvTy = |
| 2160 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2161 | if (ConvTy != Compatible && |
| 2162 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2163 | ConvTy = Compatible; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2165 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2166 | FromType, From, Action); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2167 | } |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2169 | if (ToType->isReferenceType()) |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2170 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2171 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2172 | /*SuppressUserConversions=*/false, |
| 2173 | /*AllowExplicit=*/false, |
| 2174 | /*ForceRValue=*/false); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2175 | |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2176 | if (!PerformImplicitConversion(From, ToType, Action, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2177 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2178 | return false; |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2179 | if (!DiagnoseMultipleUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2180 | return Diag(From->getSourceRange().getBegin(), |
| 2181 | diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2182 | << ToType << From->getType() << Action << From->getSourceRange(); |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2183 | return true; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2186 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2187 | /// parameter of the given member function (@c Method) from the |
| 2188 | /// expression @p From. |
| 2189 | ImplicitConversionSequence |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2190 | Sema::TryObjectArgumentInitialization(QualType FromType, |
| 2191 | CXXMethodDecl *Method, |
| 2192 | CXXRecordDecl *ActingContext) { |
| 2193 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2194 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2195 | // const volatile object. |
| 2196 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2197 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2198 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2199 | |
| 2200 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2201 | // to exit early. |
| 2202 | ImplicitConversionSequence ICS; |
| 2203 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2204 | ICS.setBad(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2205 | |
| 2206 | // We need to have an object of class type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2207 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2208 | FromType = PT->getPointeeType(); |
| 2209 | |
| 2210 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2211 | |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2212 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2213 | // where X is the class of which the function is a member |
| 2214 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2215 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2217 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2218 | // reference binding here, that allows class rvalues to bind to |
| 2219 | // non-constant references. |
| 2220 | |
| 2221 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2222 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2223 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2224 | if (ImplicitParamType.getCVRQualifiers() |
| 2225 | != FromTypeCanon.getLocalCVRQualifiers() && |
Douglas Gregor | 01df946 | 2009-11-05 00:07:36 +0000 | [diff] [blame] | 2226 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2227 | return ICS; |
| 2228 | |
| 2229 | // Check that we have either the same type or a derived type. It |
| 2230 | // affects the conversion rank. |
| 2231 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2232 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2233 | ICS.Standard.Second = ICK_Identity; |
| 2234 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2235 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2236 | else |
| 2237 | return ICS; |
| 2238 | |
| 2239 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2240 | ICS.setStandard(); |
| 2241 | ICS.Standard.setFromType(FromType); |
| 2242 | ICS.Standard.setToType(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2243 | ICS.Standard.ReferenceBinding = true; |
| 2244 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2245 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2246 | return ICS; |
| 2247 | } |
| 2248 | |
| 2249 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2250 | /// the implicit object parameter for the given Method with the given |
| 2251 | /// expression. |
| 2252 | bool |
| 2253 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2254 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2256 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2258 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2259 | FromRecordType = PT->getPointeeType(); |
| 2260 | DestType = Method->getThisType(Context); |
| 2261 | } else { |
| 2262 | FromRecordType = From->getType(); |
| 2263 | DestType = ImplicitParamRecordType; |
| 2264 | } |
| 2265 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2266 | // Note that we always use the true parent context when performing |
| 2267 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | ImplicitConversionSequence ICS |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2269 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2270 | Method->getParent()); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2271 | if (ICS.isBad()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2272 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2273 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2274 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2276 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2277 | CheckDerivedToBaseConversion(FromRecordType, |
| 2278 | ImplicitParamRecordType, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2279 | From->getSourceRange().getBegin(), |
| 2280 | From->getSourceRange())) |
| 2281 | return true; |
| 2282 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 4f4aab2 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2284 | /*isLvalue=*/true); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2285 | return false; |
| 2286 | } |
| 2287 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2288 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2289 | /// expression From to bool (C++0x [conv]p3). |
| 2290 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2292 | // FIXME: Are these flags correct? |
| 2293 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2295 | /*ForceRValue=*/false, |
| 2296 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2300 | /// of the expression From to bool (C++0x [conv]p3). |
| 2301 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2302 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2303 | if (!ICS.isBad()) |
| 2304 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2305 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2306 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2307 | return Diag(From->getSourceRange().getBegin(), |
| 2308 | diag::err_typecheck_bool_condition) |
| 2309 | << From->getType() << From->getSourceRange(); |
| 2310 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2313 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2314 | /// candidate functions, using the given function call arguments. If |
| 2315 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2316 | /// conversions via constructors or conversion operators. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2317 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2318 | /// hacky way to implement the overloading rules for elidable copy |
| 2319 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2320 | /// |
| 2321 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2322 | /// based on an incomplete set of function arguments. This feature is used by |
| 2323 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | void |
| 2325 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2326 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2327 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2328 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2329 | bool ForceRValue, |
| 2330 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2331 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2332 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2333 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2335 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2337 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2338 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2339 | // If we get here, it's because we're calling a member function |
| 2340 | // that is named without a member access expression (e.g., |
| 2341 | // "this->f") that was either written explicitly or created |
| 2342 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2343 | // function, e.g., X::f(). We use an empty type for the implied |
| 2344 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2345 | // is irrelevant. |
| 2346 | AddMethodCandidate(Method, Method->getParent(), |
| 2347 | QualType(), Args, NumArgs, CandidateSet, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2348 | SuppressUserConversions, ForceRValue); |
| 2349 | return; |
| 2350 | } |
| 2351 | // We treat a constructor like a non-member function, since its object |
| 2352 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2355 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2356 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2358 | // Overload resolution is always an unevaluated context. |
| 2359 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2360 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2361 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2362 | // C++ [class.copy]p3: |
| 2363 | // A member function template is never instantiated to perform the copy |
| 2364 | // of a class object to an object of its class type. |
| 2365 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2366 | if (NumArgs == 1 && |
| 2367 | Constructor->isCopyConstructorLikeSpecialization() && |
| 2368 | Context.hasSameUnqualifiedType(ClassType, Args[0]->getType())) |
| 2369 | return; |
| 2370 | } |
| 2371 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2372 | // Add this candidate |
| 2373 | CandidateSet.push_back(OverloadCandidate()); |
| 2374 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2375 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2376 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2377 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2378 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2379 | |
| 2380 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2381 | |
| 2382 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2383 | // parameters is viable only if it has an ellipsis in its parameter |
| 2384 | // list (8.3.5). |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2385 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2386 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2387 | Candidate.Viable = false; |
| 2388 | return; |
| 2389 | } |
| 2390 | |
| 2391 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2392 | // is viable only if the (m+1)st parameter has a default argument |
| 2393 | // (8.3.6). For the purposes of overload resolution, the |
| 2394 | // parameter list is truncated on the right, so that there are |
| 2395 | // exactly m parameters. |
| 2396 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2397 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2398 | // Not enough arguments. |
| 2399 | Candidate.Viable = false; |
| 2400 | return; |
| 2401 | } |
| 2402 | |
| 2403 | // Determine the implicit conversion sequences for each of the |
| 2404 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2405 | Candidate.Conversions.resize(NumArgs); |
| 2406 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2407 | if (ArgIdx < NumArgsInProto) { |
| 2408 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2409 | // exist for each argument an implicit conversion sequence |
| 2410 | // (13.3.3.1) that converts that argument to the corresponding |
| 2411 | // parameter of F. |
| 2412 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | Candidate.Conversions[ArgIdx] |
| 2414 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2415 | SuppressUserConversions, ForceRValue, |
| 2416 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2417 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2418 | Candidate.Viable = false; |
| 2419 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2420 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2421 | } else { |
| 2422 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2423 | // argument for which there is no corresponding parameter is |
| 2424 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2425 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2430 | /// \brief Add all of the function declarations in the given function set to |
| 2431 | /// the overload canddiate set. |
| 2432 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2433 | Expr **Args, unsigned NumArgs, |
| 2434 | OverloadCandidateSet& CandidateSet, |
| 2435 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2437 | FEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2438 | F != FEnd; ++F) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2439 | // FIXME: using declarations |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2440 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2441 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2442 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2443 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2444 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2445 | CandidateSet, SuppressUserConversions); |
| 2446 | else |
| 2447 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2448 | SuppressUserConversions); |
| 2449 | } else { |
| 2450 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2451 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2452 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2453 | AddMethodTemplateCandidate(FunTmpl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2454 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2455 | /*FIXME: explicit args */ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2456 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2457 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2458 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2459 | else |
| 2460 | AddTemplateOverloadCandidate(FunTmpl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2461 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2462 | Args, NumArgs, CandidateSet, |
| 2463 | SuppressUserConversions); |
| 2464 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2465 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2468 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2469 | /// method) as a method candidate to the given overload set. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2470 | void Sema::AddMethodCandidate(NamedDecl *Decl, |
| 2471 | QualType ObjectType, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2472 | Expr **Args, unsigned NumArgs, |
| 2473 | OverloadCandidateSet& CandidateSet, |
| 2474 | bool SuppressUserConversions, bool ForceRValue) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2475 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2476 | |
| 2477 | if (isa<UsingShadowDecl>(Decl)) |
| 2478 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2479 | |
| 2480 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2481 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2482 | "Expected a member function template"); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2483 | AddMethodTemplateCandidate(TD, ActingContext, /*ExplicitArgs*/ 0, |
| 2484 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2485 | CandidateSet, |
| 2486 | SuppressUserConversions, |
| 2487 | ForceRValue); |
| 2488 | } else { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2489 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), ActingContext, |
| 2490 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2491 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2492 | } |
| 2493 | } |
| 2494 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2495 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2496 | /// of candidate functions, using the given function call arguments |
| 2497 | /// and the object argument (@c Object). For example, in a call |
| 2498 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2499 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2500 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2501 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2502 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2503 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | void |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2505 | Sema::AddMethodCandidate(CXXMethodDecl *Method, CXXRecordDecl *ActingContext, |
| 2506 | QualType ObjectType, Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2507 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2508 | bool SuppressUserConversions, bool ForceRValue) { |
| 2509 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2510 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2511 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2512 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2513 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2514 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2515 | if (!CandidateSet.isNewCandidate(Method)) |
| 2516 | return; |
| 2517 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2518 | // Overload resolution is always an unevaluated context. |
| 2519 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2520 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2521 | // Add this candidate |
| 2522 | CandidateSet.push_back(OverloadCandidate()); |
| 2523 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2524 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2525 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2526 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2527 | |
| 2528 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2529 | |
| 2530 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2531 | // parameters is viable only if it has an ellipsis in its parameter |
| 2532 | // list (8.3.5). |
| 2533 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2534 | Candidate.Viable = false; |
| 2535 | return; |
| 2536 | } |
| 2537 | |
| 2538 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2539 | // is viable only if the (m+1)st parameter has a default argument |
| 2540 | // (8.3.6). For the purposes of overload resolution, the |
| 2541 | // parameter list is truncated on the right, so that there are |
| 2542 | // exactly m parameters. |
| 2543 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2544 | if (NumArgs < MinRequiredArgs) { |
| 2545 | // Not enough arguments. |
| 2546 | Candidate.Viable = false; |
| 2547 | return; |
| 2548 | } |
| 2549 | |
| 2550 | Candidate.Viable = true; |
| 2551 | Candidate.Conversions.resize(NumArgs + 1); |
| 2552 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2553 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2554 | // The implicit object argument is ignored. |
| 2555 | Candidate.IgnoreObjectArgument = true; |
| 2556 | else { |
| 2557 | // Determine the implicit conversion sequence for the object |
| 2558 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2559 | Candidate.Conversions[0] |
| 2560 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2561 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2562 | Candidate.Viable = false; |
| 2563 | return; |
| 2564 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | // Determine the implicit conversion sequences for each of the |
| 2568 | // arguments. |
| 2569 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2570 | if (ArgIdx < NumArgsInProto) { |
| 2571 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2572 | // exist for each argument an implicit conversion sequence |
| 2573 | // (13.3.3.1) that converts that argument to the corresponding |
| 2574 | // parameter of F. |
| 2575 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | Candidate.Conversions[ArgIdx + 1] |
| 2577 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2578 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2579 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2580 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2581 | Candidate.Viable = false; |
| 2582 | break; |
| 2583 | } |
| 2584 | } else { |
| 2585 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2586 | // argument for which there is no corresponding parameter is |
| 2587 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2588 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | } |
| 2592 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2593 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2594 | /// set, using template argument deduction to produce an appropriate member |
| 2595 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2597 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2598 | CXXRecordDecl *ActingContext, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2599 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2600 | QualType ObjectType, |
| 2601 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2602 | OverloadCandidateSet& CandidateSet, |
| 2603 | bool SuppressUserConversions, |
| 2604 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2605 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2606 | return; |
| 2607 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2608 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2610 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | // 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] | 2612 | // candidate functions in the usual way.113) A given name can refer to one |
| 2613 | // or more function templates and also to a set of overloaded non-template |
| 2614 | // functions. In such a case, the candidate functions generated from each |
| 2615 | // function template are combined with the set of non-template candidate |
| 2616 | // functions. |
| 2617 | TemplateDeductionInfo Info(Context); |
| 2618 | FunctionDecl *Specialization = 0; |
| 2619 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2620 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2621 | Args, NumArgs, Specialization, Info)) { |
| 2622 | // FIXME: Record what happened with template argument deduction, so |
| 2623 | // that we can give the user a beautiful diagnostic. |
| 2624 | (void)Result; |
| 2625 | return; |
| 2626 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2628 | // Add the function template specialization produced by template argument |
| 2629 | // deduction as a candidate. |
| 2630 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2632 | "Specialization is not a member function?"); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2633 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), ActingContext, |
| 2634 | ObjectType, Args, NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2635 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2636 | } |
| 2637 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2638 | /// \brief Add a C++ function template specialization as a candidate |
| 2639 | /// in the candidate set, using template argument deduction to produce |
| 2640 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2641 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2642 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2643 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2644 | Expr **Args, unsigned NumArgs, |
| 2645 | OverloadCandidateSet& CandidateSet, |
| 2646 | bool SuppressUserConversions, |
| 2647 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2648 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2649 | return; |
| 2650 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2651 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2653 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | // 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] | 2655 | // candidate functions in the usual way.113) A given name can refer to one |
| 2656 | // or more function templates and also to a set of overloaded non-template |
| 2657 | // functions. In such a case, the candidate functions generated from each |
| 2658 | // function template are combined with the set of non-template candidate |
| 2659 | // functions. |
| 2660 | TemplateDeductionInfo Info(Context); |
| 2661 | FunctionDecl *Specialization = 0; |
| 2662 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2663 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2664 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2665 | // FIXME: Record what happened with template argument deduction, so |
| 2666 | // that we can give the user a beautiful diagnostic. |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2667 | (void) Result; |
| 2668 | |
| 2669 | CandidateSet.push_back(OverloadCandidate()); |
| 2670 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 2671 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 2672 | Candidate.Viable = false; |
| 2673 | Candidate.IsSurrogate = false; |
| 2674 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2675 | return; |
| 2676 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2678 | // Add the function template specialization produced by template argument |
| 2679 | // deduction as a candidate. |
| 2680 | assert(Specialization && "Missing function template specialization?"); |
| 2681 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2682 | SuppressUserConversions, ForceRValue); |
| 2683 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2685 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2687 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2688 | /// 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] | 2689 | /// (which may or may not be the same type as the type that the |
| 2690 | /// conversion function produces). |
| 2691 | void |
| 2692 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2693 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2694 | Expr *From, QualType ToType, |
| 2695 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2696 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2697 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2698 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2699 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2700 | return; |
| 2701 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2702 | // Overload resolution is always an unevaluated context. |
| 2703 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2704 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2705 | // Add this candidate |
| 2706 | CandidateSet.push_back(OverloadCandidate()); |
| 2707 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2708 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2709 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2710 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2711 | Candidate.FinalConversion.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2712 | Candidate.FinalConversion.setFromType(Conversion->getConversionType()); |
| 2713 | Candidate.FinalConversion.setToType(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2714 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2715 | // Determine the implicit conversion sequence for the implicit |
| 2716 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2717 | Candidate.Viable = true; |
| 2718 | Candidate.Conversions.resize(1); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2719 | Candidate.Conversions[0] |
| 2720 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 2721 | ActingContext); |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2722 | // Conversion functions to a different type in the base class is visible in |
| 2723 | // the derived class. So, a derived to base conversion should not participate |
| 2724 | // in overload resolution. |
| 2725 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2726 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2727 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2728 | Candidate.Viable = false; |
| 2729 | return; |
| 2730 | } |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2731 | |
| 2732 | // We won't go through a user-define type conversion function to convert a |
| 2733 | // derived to base as such conversions are given Conversion Rank. They only |
| 2734 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2735 | QualType FromCanon |
| 2736 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2737 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2738 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2739 | Candidate.Viable = false; |
| 2740 | return; |
| 2741 | } |
| 2742 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2743 | |
| 2744 | // To determine what the conversion from the result of calling the |
| 2745 | // conversion function to the type we're eventually trying to |
| 2746 | // convert to (ToType), we need to synthesize a call to the |
| 2747 | // conversion function and attempt copy initialization from it. This |
| 2748 | // makes sure that we get the right semantics with respect to |
| 2749 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2750 | // call on the stack and we don't need its arguments to be |
| 2751 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2753 | From->getLocStart()); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2754 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2755 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2756 | &ConversionRef, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | |
| 2758 | // 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] | 2759 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2760 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2762 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2763 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | ImplicitConversionSequence ICS = |
| 2765 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2766 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2767 | /*ForceRValue=*/false, |
| 2768 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2770 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2771 | case ImplicitConversionSequence::StandardConversion: |
| 2772 | Candidate.FinalConversion = ICS.Standard; |
| 2773 | break; |
| 2774 | |
| 2775 | case ImplicitConversionSequence::BadConversion: |
| 2776 | Candidate.Viable = false; |
| 2777 | break; |
| 2778 | |
| 2779 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2781 | "Can only end up with a standard conversion sequence or failure"); |
| 2782 | } |
| 2783 | } |
| 2784 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2785 | /// \brief Adds a conversion function template specialization |
| 2786 | /// candidate to the overload set, using template argument deduction |
| 2787 | /// to deduce the template arguments of the conversion function |
| 2788 | /// template from the type that we are converting to (C++ |
| 2789 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2791 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2792 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2793 | Expr *From, QualType ToType, |
| 2794 | OverloadCandidateSet &CandidateSet) { |
| 2795 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2796 | "Only conversion function templates permitted here"); |
| 2797 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2798 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2799 | return; |
| 2800 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2801 | TemplateDeductionInfo Info(Context); |
| 2802 | CXXConversionDecl *Specialization = 0; |
| 2803 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2805 | Specialization, Info)) { |
| 2806 | // FIXME: Record what happened with template argument deduction, so |
| 2807 | // that we can give the user a beautiful diagnostic. |
| 2808 | (void)Result; |
| 2809 | return; |
| 2810 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2812 | // Add the conversion function template specialization produced by |
| 2813 | // template argument deduction as a candidate. |
| 2814 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2815 | AddConversionCandidate(Specialization, ActingDC, From, ToType, CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2818 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2819 | /// converts the given @c Object to a function pointer via the |
| 2820 | /// conversion function @c Conversion, and then attempts to call it |
| 2821 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2822 | /// the type of function that we'll eventually be calling. |
| 2823 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2824 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2825 | const FunctionProtoType *Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2826 | QualType ObjectType, |
| 2827 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2828 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2829 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2830 | return; |
| 2831 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2832 | // Overload resolution is always an unevaluated context. |
| 2833 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2834 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2835 | CandidateSet.push_back(OverloadCandidate()); |
| 2836 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2837 | Candidate.Function = 0; |
| 2838 | Candidate.Surrogate = Conversion; |
| 2839 | Candidate.Viable = true; |
| 2840 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2841 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2842 | Candidate.Conversions.resize(NumArgs + 1); |
| 2843 | |
| 2844 | // Determine the implicit conversion sequence for the implicit |
| 2845 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | ImplicitConversionSequence ObjectInit |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2847 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2848 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2849 | Candidate.Viable = false; |
| 2850 | return; |
| 2851 | } |
| 2852 | |
| 2853 | // The first conversion is actually a user-defined conversion whose |
| 2854 | // first conversion is ObjectInit's standard conversion (which is |
| 2855 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2856 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2857 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2858 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2859 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2861 | = Candidate.Conversions[0].UserDefined.Before; |
| 2862 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2863 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2865 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2866 | |
| 2867 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2868 | // parameters is viable only if it has an ellipsis in its parameter |
| 2869 | // list (8.3.5). |
| 2870 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2871 | Candidate.Viable = false; |
| 2872 | return; |
| 2873 | } |
| 2874 | |
| 2875 | // Function types don't have any default arguments, so just check if |
| 2876 | // we have enough arguments. |
| 2877 | if (NumArgs < NumArgsInProto) { |
| 2878 | // Not enough arguments. |
| 2879 | Candidate.Viable = false; |
| 2880 | return; |
| 2881 | } |
| 2882 | |
| 2883 | // Determine the implicit conversion sequences for each of the |
| 2884 | // arguments. |
| 2885 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2886 | if (ArgIdx < NumArgsInProto) { |
| 2887 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2888 | // exist for each argument an implicit conversion sequence |
| 2889 | // (13.3.3.1) that converts that argument to the corresponding |
| 2890 | // parameter of F. |
| 2891 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | Candidate.Conversions[ArgIdx + 1] |
| 2893 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2894 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2895 | /*ForceRValue=*/false, |
| 2896 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2897 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2898 | Candidate.Viable = false; |
| 2899 | break; |
| 2900 | } |
| 2901 | } else { |
| 2902 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2903 | // argument for which there is no corresponding parameter is |
| 2904 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2905 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2906 | } |
| 2907 | } |
| 2908 | } |
| 2909 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2910 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2911 | // operator overloading logic over to the scheme used by binary operators, which |
| 2912 | // works for template instantiation. |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2913 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2914 | SourceLocation OpLoc, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2915 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2916 | OverloadCandidateSet& CandidateSet, |
| 2917 | SourceRange OpRange) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2918 | FunctionSet Functions; |
| 2919 | |
| 2920 | QualType T1 = Args[0]->getType(); |
| 2921 | QualType T2; |
| 2922 | if (NumArgs > 1) |
| 2923 | T2 = Args[1]->getType(); |
| 2924 | |
| 2925 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2926 | if (S) |
| 2927 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2928 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, NumArgs, Functions); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2929 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2930 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2931 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | /// \brief Add overload candidates for overloaded operators that are |
| 2935 | /// member functions. |
| 2936 | /// |
| 2937 | /// Add the overloaded operator candidates that are member functions |
| 2938 | /// for the operator Op that was used in an operator expression such |
| 2939 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2940 | /// CandidateSet will store the added overload candidates. (C++ |
| 2941 | /// [over.match.oper]). |
| 2942 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2943 | SourceLocation OpLoc, |
| 2944 | Expr **Args, unsigned NumArgs, |
| 2945 | OverloadCandidateSet& CandidateSet, |
| 2946 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2947 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2948 | |
| 2949 | // C++ [over.match.oper]p3: |
| 2950 | // For a unary operator @ with an operand of a type whose |
| 2951 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2952 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2953 | // a right operand of a type whose cv-unqualified version is T2, |
| 2954 | // three sets of candidate functions, designated member |
| 2955 | // candidates, non-member candidates and built-in candidates, are |
| 2956 | // constructed as follows: |
| 2957 | QualType T1 = Args[0]->getType(); |
| 2958 | QualType T2; |
| 2959 | if (NumArgs > 1) |
| 2960 | T2 = Args[1]->getType(); |
| 2961 | |
| 2962 | // -- If T1 is a class type, the set of member candidates is the |
| 2963 | // result of the qualified lookup of T1::operator@ |
| 2964 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2965 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2966 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2967 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2968 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2969 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2971 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 2972 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 2973 | Operators.suppressDiagnostics(); |
| 2974 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2976 | OperEnd = Operators.end(); |
| 2977 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2978 | ++Oper) |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2979 | AddMethodCandidate(*Oper, Args[0]->getType(), |
| 2980 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2981 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2982 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2985 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2986 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2987 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2988 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2989 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2990 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2991 | /// (at the beginning of the argument list) that will be contextually |
| 2992 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2993 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2994 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2995 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2996 | bool IsAssignmentOperator, |
| 2997 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2998 | // Overload resolution is always an unevaluated context. |
| 2999 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3000 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3001 | // Add this candidate |
| 3002 | CandidateSet.push_back(OverloadCandidate()); |
| 3003 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 3004 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3005 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3006 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3007 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3008 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3009 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3010 | |
| 3011 | // Determine the implicit conversion sequences for each of the |
| 3012 | // arguments. |
| 3013 | Candidate.Viable = true; |
| 3014 | Candidate.Conversions.resize(NumArgs); |
| 3015 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3016 | // C++ [over.match.oper]p4: |
| 3017 | // For the built-in assignment operators, conversions of the |
| 3018 | // left operand are restricted as follows: |
| 3019 | // -- no temporaries are introduced to hold the left operand, and |
| 3020 | // -- no user-defined conversions are applied to the left |
| 3021 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3023 | // |
| 3024 | // We block these conversions by turning off user-defined |
| 3025 | // conversions, since that is the only way that initialization of |
| 3026 | // a reference to a non-class type can occur from something that |
| 3027 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3028 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3030 | "Contextual conversion to bool requires bool type"); |
| 3031 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3032 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | Candidate.Conversions[ArgIdx] |
| 3034 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3035 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3036 | /*ForceRValue=*/false, |
| 3037 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3038 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3039 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3040 | Candidate.Viable = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3041 | break; |
| 3042 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3047 | /// candidate operator functions for built-in operators (C++ |
| 3048 | /// [over.built]). The types are separated into pointer types and |
| 3049 | /// enumeration types. |
| 3050 | class BuiltinCandidateTypeSet { |
| 3051 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3052 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3053 | |
| 3054 | /// PointerTypes - The set of pointer types that will be used in the |
| 3055 | /// built-in candidates. |
| 3056 | TypeSet PointerTypes; |
| 3057 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3058 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3059 | /// used in the built-in candidates. |
| 3060 | TypeSet MemberPointerTypes; |
| 3061 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3062 | /// EnumerationTypes - The set of enumeration types that will be |
| 3063 | /// used in the built-in candidates. |
| 3064 | TypeSet EnumerationTypes; |
| 3065 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3066 | /// Sema - The semantic analysis instance where we are building the |
| 3067 | /// candidate type set. |
| 3068 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3070 | /// Context - The AST context in which we will build the type sets. |
| 3071 | ASTContext &Context; |
| 3072 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3073 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3074 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3075 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3076 | |
| 3077 | public: |
| 3078 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3079 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3080 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3082 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3083 | |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3084 | void AddTypesConvertedFrom(QualType Ty, |
| 3085 | SourceLocation Loc, |
| 3086 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3087 | bool AllowExplicitConversions, |
| 3088 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3089 | |
| 3090 | /// pointer_begin - First pointer type found; |
| 3091 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3092 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3093 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3094 | iterator pointer_end() { return PointerTypes.end(); } |
| 3095 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3096 | /// member_pointer_begin - First member pointer type found; |
| 3097 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3098 | |
| 3099 | /// member_pointer_end - Past the last member pointer type found; |
| 3100 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3101 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3102 | /// enumeration_begin - First enumeration type found; |
| 3103 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3104 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3105 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3106 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3107 | }; |
| 3108 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3109 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3110 | /// the set of pointer types along with any more-qualified variants of |
| 3111 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3112 | /// will add "int const *", "int const volatile *", "int const |
| 3113 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3114 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3115 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3116 | /// |
| 3117 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3118 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3119 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3120 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3121 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3122 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3123 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3124 | return false; |
| 3125 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3126 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3127 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3128 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3129 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3130 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3131 | // (the qualifier would sink to the element type), and for another, the |
| 3132 | // only overload situation where it matters is subscript or pointer +- int, |
| 3133 | // and those shouldn't have qualifier variants anyway. |
| 3134 | if (PointeeTy->isArrayType()) |
| 3135 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3136 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3137 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3138 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3139 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3140 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3141 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3142 | // Iterate through all strict supersets of BaseCVR. |
| 3143 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3144 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3145 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3146 | // in the types. |
| 3147 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3148 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3149 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3150 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | return true; |
| 3154 | } |
| 3155 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3156 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3157 | /// to the set of pointer types along with any more-qualified variants of |
| 3158 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3159 | /// will add "int const *", "int const volatile *", "int const |
| 3160 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3161 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3162 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3163 | /// |
| 3164 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3165 | bool |
| 3166 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3167 | QualType Ty) { |
| 3168 | // Insert this type. |
| 3169 | if (!MemberPointerTypes.insert(Ty)) |
| 3170 | return false; |
| 3171 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3172 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3173 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3174 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3175 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3176 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3177 | // (the qualifier would sink to the element type), and for another, the |
| 3178 | // only overload situation where it matters is subscript or pointer +- int, |
| 3179 | // and those shouldn't have qualifier variants anyway. |
| 3180 | if (PointeeTy->isArrayType()) |
| 3181 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3182 | const Type *ClassTy = PointerTy->getClass(); |
| 3183 | |
| 3184 | // Iterate through all strict supersets of the pointee type's CVR |
| 3185 | // qualifiers. |
| 3186 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3187 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3188 | if ((CVR | BaseCVR) != CVR) continue; |
| 3189 | |
| 3190 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3191 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | return true; |
| 3195 | } |
| 3196 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3197 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3198 | /// 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] | 3199 | /// primarily interested in pointer types and enumeration types. We also |
| 3200 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3201 | /// AllowUserConversions is true if we should look at the conversion |
| 3202 | /// functions of a class type, and AllowExplicitConversions if we |
| 3203 | /// should also include the explicit conversion functions of a class |
| 3204 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3206 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3207 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3208 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3209 | bool AllowExplicitConversions, |
| 3210 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3211 | // Only deal with canonical types. |
| 3212 | Ty = Context.getCanonicalType(Ty); |
| 3213 | |
| 3214 | // Look through reference types; they aren't part of the type of an |
| 3215 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3216 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3217 | Ty = RefTy->getPointeeType(); |
| 3218 | |
| 3219 | // We don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3220 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3221 | |
Sebastian Redl | 65ae200 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3222 | // If we're dealing with an array type, decay to the pointer. |
| 3223 | if (Ty->isArrayType()) |
| 3224 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3225 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3226 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3227 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3228 | |
| 3229 | // Insert our type, and its more-qualified variants, into the set |
| 3230 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3231 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3232 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3233 | } else if (Ty->isMemberPointerType()) { |
| 3234 | // Member pointers are far easier, since the pointee can't be converted. |
| 3235 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3236 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3237 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3238 | EnumerationTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3239 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3240 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3241 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3242 | // No conversion functions in incomplete types. |
| 3243 | return; |
| 3244 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3246 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3247 | const UnresolvedSet *Conversions |
Fariborz Jahanian | ae01f78 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3248 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3249 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 3250 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3251 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3253 | // about which builtin types we can convert to. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3254 | if (isa<FunctionTemplateDecl>(*I)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3255 | continue; |
| 3256 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3257 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3258 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3259 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3260 | VisibleQuals); |
| 3261 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3262 | } |
| 3263 | } |
| 3264 | } |
| 3265 | } |
| 3266 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3267 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3268 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3269 | /// given type to the candidate set. |
| 3270 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3271 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3273 | unsigned NumArgs, |
| 3274 | OverloadCandidateSet &CandidateSet) { |
| 3275 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3277 | // T& operator=(T&, T) |
| 3278 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3279 | ParamTypes[1] = T; |
| 3280 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3281 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3283 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3284 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3285 | ParamTypes[0] |
| 3286 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3287 | ParamTypes[1] = T; |
| 3288 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3290 | } |
| 3291 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3293 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3294 | /// 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] | 3295 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3296 | Qualifiers VRQuals; |
| 3297 | const RecordType *TyRec; |
| 3298 | if (const MemberPointerType *RHSMPType = |
| 3299 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3300 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3301 | else |
| 3302 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3303 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3304 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3305 | VRQuals.addVolatile(); |
| 3306 | VRQuals.addRestrict(); |
| 3307 | return VRQuals; |
| 3308 | } |
| 3309 | |
| 3310 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3311 | const UnresolvedSet *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3312 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3313 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3314 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 3315 | E = Conversions->end(); I != E; ++I) { |
| 3316 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*I)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3317 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3318 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3319 | CanTy = ResTypeRef->getPointeeType(); |
| 3320 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3321 | // as see them. |
| 3322 | bool done = false; |
| 3323 | while (!done) { |
| 3324 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3325 | CanTy = ResTypePtr->getPointeeType(); |
| 3326 | else if (const MemberPointerType *ResTypeMPtr = |
| 3327 | CanTy->getAs<MemberPointerType>()) |
| 3328 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3329 | else |
| 3330 | done = true; |
| 3331 | if (CanTy.isVolatileQualified()) |
| 3332 | VRQuals.addVolatile(); |
| 3333 | if (CanTy.isRestrictQualified()) |
| 3334 | VRQuals.addRestrict(); |
| 3335 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3336 | return VRQuals; |
| 3337 | } |
| 3338 | } |
| 3339 | } |
| 3340 | return VRQuals; |
| 3341 | } |
| 3342 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3343 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3344 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3345 | /// on the operator @p Op and the arguments given. For example, if the |
| 3346 | /// operator is a binary '+', this routine might add "int |
| 3347 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3348 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3350 | SourceLocation OpLoc, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3351 | Expr **Args, unsigned NumArgs, |
| 3352 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3353 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3354 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3355 | // that the first few of these types are the promoted integral |
| 3356 | // types; these types need to be first. |
| 3357 | // FIXME: What about complex? |
| 3358 | const unsigned FirstIntegralType = 0; |
| 3359 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3360 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3361 | LastPromotedIntegralType = 13; |
| 3362 | const unsigned FirstPromotedArithmeticType = 7, |
| 3363 | LastPromotedArithmeticType = 16; |
| 3364 | const unsigned NumArithmeticTypes = 16; |
| 3365 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3366 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3367 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3368 | Context.SignedCharTy, Context.ShortTy, |
| 3369 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3370 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3371 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3372 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3373 | }; |
Douglas Gregor | b8440a7 | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3374 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3375 | "Invalid first promoted integral type"); |
| 3376 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3377 | == Context.UnsignedLongLongTy && |
| 3378 | "Invalid last promoted integral type"); |
| 3379 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3380 | "Invalid first promoted arithmetic type"); |
| 3381 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3382 | == Context.LongDoubleTy && |
| 3383 | "Invalid last promoted arithmetic type"); |
| 3384 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3385 | // Find all of the types that the arguments can convert to, but only |
| 3386 | // if the operator we're looking at has built-in operator candidates |
| 3387 | // that make use of these types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3388 | Qualifiers VisibleTypeConversionsQuals; |
| 3389 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3390 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3391 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3392 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3393 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3394 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3395 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3396 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3397 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3398 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3399 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3400 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3401 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3402 | OpLoc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3403 | true, |
| 3404 | (Op == OO_Exclaim || |
| 3405 | Op == OO_AmpAmp || |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3406 | Op == OO_PipePipe), |
| 3407 | VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | bool isComparison = false; |
| 3411 | switch (Op) { |
| 3412 | case OO_None: |
| 3413 | case NUM_OVERLOADED_OPERATORS: |
| 3414 | assert(false && "Expected an overloaded operator"); |
| 3415 | break; |
| 3416 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3417 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3419 | goto UnaryStar; |
| 3420 | else |
| 3421 | goto BinaryStar; |
| 3422 | break; |
| 3423 | |
| 3424 | case OO_Plus: // '+' is either unary or binary |
| 3425 | if (NumArgs == 1) |
| 3426 | goto UnaryPlus; |
| 3427 | else |
| 3428 | goto BinaryPlus; |
| 3429 | break; |
| 3430 | |
| 3431 | case OO_Minus: // '-' is either unary or binary |
| 3432 | if (NumArgs == 1) |
| 3433 | goto UnaryMinus; |
| 3434 | else |
| 3435 | goto BinaryMinus; |
| 3436 | break; |
| 3437 | |
| 3438 | case OO_Amp: // '&' is either unary or binary |
| 3439 | if (NumArgs == 1) |
| 3440 | goto UnaryAmp; |
| 3441 | else |
| 3442 | goto BinaryAmp; |
| 3443 | |
| 3444 | case OO_PlusPlus: |
| 3445 | case OO_MinusMinus: |
| 3446 | // C++ [over.built]p3: |
| 3447 | // |
| 3448 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3449 | // is either volatile or empty, there exist candidate operator |
| 3450 | // functions of the form |
| 3451 | // |
| 3452 | // VQ T& operator++(VQ T&); |
| 3453 | // T operator++(VQ T&, int); |
| 3454 | // |
| 3455 | // C++ [over.built]p4: |
| 3456 | // |
| 3457 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3458 | // than bool, and VQ is either volatile or empty, there exist |
| 3459 | // candidate operator functions of the form |
| 3460 | // |
| 3461 | // VQ T& operator--(VQ T&); |
| 3462 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3464 | Arith < NumArithmeticTypes; ++Arith) { |
| 3465 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3467 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3468 | |
| 3469 | // Non-volatile version. |
| 3470 | if (NumArgs == 1) |
| 3471 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3472 | else |
| 3473 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3474 | // heuristic to reduce number of builtin candidates in the set. |
| 3475 | // Add volatile version only if there are conversions to a volatile type. |
| 3476 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3477 | // Volatile version |
| 3478 | ParamTypes[0] |
| 3479 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3480 | if (NumArgs == 1) |
| 3481 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3482 | else |
| 3483 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3484 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | // C++ [over.built]p5: |
| 3488 | // |
| 3489 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3490 | // cv-unqualified object type, and VQ is either volatile or |
| 3491 | // empty, there exist candidate operator functions of the form |
| 3492 | // |
| 3493 | // T*VQ& operator++(T*VQ&); |
| 3494 | // T*VQ& operator--(T*VQ&); |
| 3495 | // T* operator++(T*VQ&, int); |
| 3496 | // T* operator--(T*VQ&, int); |
| 3497 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3498 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3499 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3500 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3501 | continue; |
| 3502 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | QualType ParamTypes[2] = { |
| 3504 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3505 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3507 | // Without volatile |
| 3508 | if (NumArgs == 1) |
| 3509 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3510 | else |
| 3511 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3512 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3513 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3514 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3515 | // With volatile |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3516 | ParamTypes[0] |
| 3517 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3518 | if (NumArgs == 1) |
| 3519 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3520 | else |
| 3521 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3522 | } |
| 3523 | } |
| 3524 | break; |
| 3525 | |
| 3526 | UnaryStar: |
| 3527 | // C++ [over.built]p6: |
| 3528 | // For every cv-qualified or cv-unqualified object type T, there |
| 3529 | // exist candidate operator functions of the form |
| 3530 | // |
| 3531 | // T& operator*(T*); |
| 3532 | // |
| 3533 | // C++ [over.built]p7: |
| 3534 | // For every function type T, there exist candidate operator |
| 3535 | // functions of the form |
| 3536 | // T& operator*(T*); |
| 3537 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3538 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3539 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3540 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3542 | &ParamTy, Args, 1, CandidateSet); |
| 3543 | } |
| 3544 | break; |
| 3545 | |
| 3546 | UnaryPlus: |
| 3547 | // C++ [over.built]p8: |
| 3548 | // For every type T, there exist candidate operator functions of |
| 3549 | // the form |
| 3550 | // |
| 3551 | // T* operator+(T*); |
| 3552 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3553 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3554 | QualType ParamTy = *Ptr; |
| 3555 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3556 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3558 | // Fall through |
| 3559 | |
| 3560 | UnaryMinus: |
| 3561 | // C++ [over.built]p9: |
| 3562 | // For every promoted arithmetic type T, there exist candidate |
| 3563 | // operator functions of the form |
| 3564 | // |
| 3565 | // T operator+(T); |
| 3566 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3567 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3568 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3569 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3570 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3571 | } |
| 3572 | break; |
| 3573 | |
| 3574 | case OO_Tilde: |
| 3575 | // C++ [over.built]p10: |
| 3576 | // For every promoted integral type T, there exist candidate |
| 3577 | // operator functions of the form |
| 3578 | // |
| 3579 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3580 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3581 | Int < LastPromotedIntegralType; ++Int) { |
| 3582 | QualType IntTy = ArithmeticTypes[Int]; |
| 3583 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3584 | } |
| 3585 | break; |
| 3586 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3587 | case OO_New: |
| 3588 | case OO_Delete: |
| 3589 | case OO_Array_New: |
| 3590 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3591 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3592 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3593 | break; |
| 3594 | |
| 3595 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3596 | UnaryAmp: |
| 3597 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3598 | // C++ [over.match.oper]p3: |
| 3599 | // -- For the operator ',', the unary operator '&', or the |
| 3600 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3601 | break; |
| 3602 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3603 | case OO_EqualEqual: |
| 3604 | case OO_ExclaimEqual: |
| 3605 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | // For every pointer to member type T, there exist candidate operator |
| 3607 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3608 | // |
| 3609 | // bool operator==(T,T); |
| 3610 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3612 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3613 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3614 | MemPtr != MemPtrEnd; |
| 3615 | ++MemPtr) { |
| 3616 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3617 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3618 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3620 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3621 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3622 | case OO_Less: |
| 3623 | case OO_Greater: |
| 3624 | case OO_LessEqual: |
| 3625 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3626 | // C++ [over.built]p15: |
| 3627 | // |
| 3628 | // For every pointer or enumeration type T, there exist |
| 3629 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3630 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3631 | // bool operator<(T, T); |
| 3632 | // bool operator>(T, T); |
| 3633 | // bool operator<=(T, T); |
| 3634 | // bool operator>=(T, T); |
| 3635 | // bool operator==(T, T); |
| 3636 | // bool operator!=(T, T); |
| 3637 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3638 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3639 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3640 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3641 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3642 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3643 | = CandidateTypes.enumeration_begin(); |
| 3644 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3645 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3646 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3647 | } |
| 3648 | |
| 3649 | // Fall through. |
| 3650 | isComparison = true; |
| 3651 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3652 | BinaryPlus: |
| 3653 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3654 | if (!isComparison) { |
| 3655 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3656 | |
| 3657 | // C++ [over.built]p13: |
| 3658 | // |
| 3659 | // For every cv-qualified or cv-unqualified object type T |
| 3660 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3661 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3662 | // T* operator+(T*, ptrdiff_t); |
| 3663 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3664 | // T* operator-(T*, ptrdiff_t); |
| 3665 | // T* operator+(ptrdiff_t, T*); |
| 3666 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3667 | // |
| 3668 | // C++ [over.built]p14: |
| 3669 | // |
| 3670 | // For every T, where T is a pointer to object type, there |
| 3671 | // exist candidate operator functions of the form |
| 3672 | // |
| 3673 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3675 | = CandidateTypes.pointer_begin(); |
| 3676 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3677 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3678 | |
| 3679 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3680 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3681 | |
| 3682 | if (Op == OO_Plus) { |
| 3683 | // T* operator+(ptrdiff_t, T*); |
| 3684 | ParamTypes[0] = ParamTypes[1]; |
| 3685 | ParamTypes[1] = *Ptr; |
| 3686 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3687 | } else { |
| 3688 | // ptrdiff_t operator-(T, T); |
| 3689 | ParamTypes[1] = *Ptr; |
| 3690 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3691 | Args, 2, CandidateSet); |
| 3692 | } |
| 3693 | } |
| 3694 | } |
| 3695 | // Fall through |
| 3696 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3697 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3698 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3699 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3700 | // C++ [over.built]p12: |
| 3701 | // |
| 3702 | // For every pair of promoted arithmetic types L and R, there |
| 3703 | // exist candidate operator functions of the form |
| 3704 | // |
| 3705 | // LR operator*(L, R); |
| 3706 | // LR operator/(L, R); |
| 3707 | // LR operator+(L, R); |
| 3708 | // LR operator-(L, R); |
| 3709 | // bool operator<(L, R); |
| 3710 | // bool operator>(L, R); |
| 3711 | // bool operator<=(L, R); |
| 3712 | // bool operator>=(L, R); |
| 3713 | // bool operator==(L, R); |
| 3714 | // bool operator!=(L, R); |
| 3715 | // |
| 3716 | // where LR is the result of the usual arithmetic conversions |
| 3717 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3718 | // |
| 3719 | // C++ [over.built]p24: |
| 3720 | // |
| 3721 | // For every pair of promoted arithmetic types L and R, there exist |
| 3722 | // candidate operator functions of the form |
| 3723 | // |
| 3724 | // LR operator?(bool, L, R); |
| 3725 | // |
| 3726 | // where LR is the result of the usual arithmetic conversions |
| 3727 | // between types L and R. |
| 3728 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3730 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3731 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3732 | Right < LastPromotedArithmeticType; ++Right) { |
| 3733 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3734 | QualType Result |
| 3735 | = isComparison |
| 3736 | ? Context.BoolTy |
| 3737 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3738 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3739 | } |
| 3740 | } |
| 3741 | break; |
| 3742 | |
| 3743 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3744 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3745 | case OO_Caret: |
| 3746 | case OO_Pipe: |
| 3747 | case OO_LessLess: |
| 3748 | case OO_GreaterGreater: |
| 3749 | // C++ [over.built]p17: |
| 3750 | // |
| 3751 | // For every pair of promoted integral types L and R, there |
| 3752 | // exist candidate operator functions of the form |
| 3753 | // |
| 3754 | // LR operator%(L, R); |
| 3755 | // LR operator&(L, R); |
| 3756 | // LR operator^(L, R); |
| 3757 | // LR operator|(L, R); |
| 3758 | // L operator<<(L, R); |
| 3759 | // L operator>>(L, R); |
| 3760 | // |
| 3761 | // where LR is the result of the usual arithmetic conversions |
| 3762 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3763 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3764 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3765 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3766 | Right < LastPromotedIntegralType; ++Right) { |
| 3767 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3768 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3769 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3770 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3771 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3772 | } |
| 3773 | } |
| 3774 | break; |
| 3775 | |
| 3776 | case OO_Equal: |
| 3777 | // C++ [over.built]p20: |
| 3778 | // |
| 3779 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3780 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3781 | // empty, there exist candidate operator functions of the form |
| 3782 | // |
| 3783 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3784 | for (BuiltinCandidateTypeSet::iterator |
| 3785 | Enum = CandidateTypes.enumeration_begin(), |
| 3786 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3787 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3788 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3789 | CandidateSet); |
| 3790 | for (BuiltinCandidateTypeSet::iterator |
| 3791 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3792 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3793 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3794 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3795 | CandidateSet); |
| 3796 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3797 | |
| 3798 | case OO_PlusEqual: |
| 3799 | case OO_MinusEqual: |
| 3800 | // C++ [over.built]p19: |
| 3801 | // |
| 3802 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3803 | // volatile or empty, there exist candidate operator functions |
| 3804 | // of the form |
| 3805 | // |
| 3806 | // T*VQ& operator=(T*VQ&, T*); |
| 3807 | // |
| 3808 | // C++ [over.built]p21: |
| 3809 | // |
| 3810 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3811 | // cv-unqualified object type and VQ is either volatile or |
| 3812 | // empty, there exist candidate operator functions of the form |
| 3813 | // |
| 3814 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3815 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3816 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3817 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3818 | QualType ParamTypes[2]; |
| 3819 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3820 | |
| 3821 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3822 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3823 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3824 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3825 | |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3826 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3827 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3828 | // volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3829 | ParamTypes[0] |
| 3830 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3831 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3832 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3833 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3834 | } |
| 3835 | // Fall through. |
| 3836 | |
| 3837 | case OO_StarEqual: |
| 3838 | case OO_SlashEqual: |
| 3839 | // C++ [over.built]p18: |
| 3840 | // |
| 3841 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3842 | // VQ is either volatile or empty, and R is a promoted |
| 3843 | // arithmetic type, there exist candidate operator functions of |
| 3844 | // the form |
| 3845 | // |
| 3846 | // VQ L& operator=(VQ L&, R); |
| 3847 | // VQ L& operator*=(VQ L&, R); |
| 3848 | // VQ L& operator/=(VQ L&, R); |
| 3849 | // VQ L& operator+=(VQ L&, R); |
| 3850 | // VQ L& operator-=(VQ L&, R); |
| 3851 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3853 | Right < LastPromotedArithmeticType; ++Right) { |
| 3854 | QualType ParamTypes[2]; |
| 3855 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3856 | |
| 3857 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3858 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3859 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3860 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3861 | |
| 3862 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3863 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3864 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3865 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3866 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3867 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3868 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | break; |
| 3872 | |
| 3873 | case OO_PercentEqual: |
| 3874 | case OO_LessLessEqual: |
| 3875 | case OO_GreaterGreaterEqual: |
| 3876 | case OO_AmpEqual: |
| 3877 | case OO_CaretEqual: |
| 3878 | case OO_PipeEqual: |
| 3879 | // C++ [over.built]p22: |
| 3880 | // |
| 3881 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3882 | // is either volatile or empty, and R is a promoted integral |
| 3883 | // type, there exist candidate operator functions of the form |
| 3884 | // |
| 3885 | // VQ L& operator%=(VQ L&, R); |
| 3886 | // VQ L& operator<<=(VQ L&, R); |
| 3887 | // VQ L& operator>>=(VQ L&, R); |
| 3888 | // VQ L& operator&=(VQ L&, R); |
| 3889 | // VQ L& operator^=(VQ L&, R); |
| 3890 | // VQ L& operator|=(VQ L&, R); |
| 3891 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3893 | Right < LastPromotedIntegralType; ++Right) { |
| 3894 | QualType ParamTypes[2]; |
| 3895 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3896 | |
| 3897 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3898 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3899 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a4a9334 | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 3900 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3901 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3902 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3903 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 3904 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3905 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3906 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | break; |
| 3910 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3911 | case OO_Exclaim: { |
| 3912 | // C++ [over.operator]p23: |
| 3913 | // |
| 3914 | // There also exist candidate operator functions of the form |
| 3915 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3917 | // bool operator&&(bool, bool); [BELOW] |
| 3918 | // bool operator||(bool, bool); [BELOW] |
| 3919 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3920 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3921 | /*IsAssignmentOperator=*/false, |
| 3922 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3923 | break; |
| 3924 | } |
| 3925 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3926 | case OO_AmpAmp: |
| 3927 | case OO_PipePipe: { |
| 3928 | // C++ [over.operator]p23: |
| 3929 | // |
| 3930 | // There also exist candidate operator functions of the form |
| 3931 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3932 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3933 | // bool operator&&(bool, bool); |
| 3934 | // bool operator||(bool, bool); |
| 3935 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3936 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3937 | /*IsAssignmentOperator=*/false, |
| 3938 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3939 | break; |
| 3940 | } |
| 3941 | |
| 3942 | case OO_Subscript: |
| 3943 | // C++ [over.built]p13: |
| 3944 | // |
| 3945 | // For every cv-qualified or cv-unqualified object type T there |
| 3946 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3947 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3948 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3949 | // T& operator[](T*, ptrdiff_t); |
| 3950 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3951 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3952 | // T& operator[](ptrdiff_t, T*); |
| 3953 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3954 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3955 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3956 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3957 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3958 | |
| 3959 | // T& operator[](T*, ptrdiff_t) |
| 3960 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3961 | |
| 3962 | // T& operator[](ptrdiff_t, T*); |
| 3963 | ParamTypes[0] = ParamTypes[1]; |
| 3964 | ParamTypes[1] = *Ptr; |
| 3965 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3966 | } |
| 3967 | break; |
| 3968 | |
| 3969 | case OO_ArrowStar: |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3970 | // C++ [over.built]p11: |
| 3971 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 3972 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 3973 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 3974 | // there exist candidate operator functions of the form |
| 3975 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 3976 | // where CV12 is the union of CV1 and CV2. |
| 3977 | { |
| 3978 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3979 | CandidateTypes.pointer_begin(); |
| 3980 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3981 | QualType C1Ty = (*Ptr); |
| 3982 | QualType C1; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3983 | QualifierCollector Q1; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3984 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 3985 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3986 | if (!isa<RecordType>(C1)) |
| 3987 | continue; |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3988 | // heuristic to reduce number of builtin candidates in the set. |
| 3989 | // Add volatile/restrict version only if there are conversions to a |
| 3990 | // volatile/restrict type. |
| 3991 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 3992 | continue; |
| 3993 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 3994 | continue; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 3995 | } |
| 3996 | for (BuiltinCandidateTypeSet::iterator |
| 3997 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3998 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3999 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4000 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4001 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 12df37c | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4002 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4003 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4004 | break; |
| 4005 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4006 | // build CV12 T& |
| 4007 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4008 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4009 | T.isVolatileQualified()) |
| 4010 | continue; |
| 4011 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4012 | T.isRestrictQualified()) |
| 4013 | continue; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4014 | T = Q1.apply(T); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4015 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4016 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4017 | } |
| 4018 | } |
| 4019 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4020 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4021 | |
| 4022 | case OO_Conditional: |
| 4023 | // Note that we don't consider the first argument, since it has been |
| 4024 | // contextually converted to bool long ago. The candidates below are |
| 4025 | // therefore added as binary. |
| 4026 | // |
| 4027 | // C++ [over.built]p24: |
| 4028 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4029 | // there exist candidate operator functions of the form |
| 4030 | // |
| 4031 | // T operator?(bool, T, T); |
| 4032 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4033 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4034 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4035 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4036 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4037 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4038 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4039 | CandidateTypes.member_pointer_begin(), |
| 4040 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4041 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4042 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4043 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4044 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4045 | } |
| 4046 | } |
| 4047 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4048 | /// \brief Add function candidates found via argument-dependent lookup |
| 4049 | /// to the set of overloading candidates. |
| 4050 | /// |
| 4051 | /// This routine performs argument-dependent name lookup based on the |
| 4052 | /// given function name (which may also be an operator name) and adds |
| 4053 | /// all of the overload candidates found by ADL to the overload |
| 4054 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4055 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4056 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 4057 | Expr **Args, unsigned NumArgs, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4058 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4059 | OverloadCandidateSet& CandidateSet, |
| 4060 | bool PartialOverloading) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4061 | FunctionSet Functions; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4062 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4063 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 4064 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4065 | // Record all of the function candidates that we've already |
| 4066 | // added to the overload set, so that we don't add those same |
| 4067 | // candidates a second time. |
| 4068 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4069 | CandEnd = CandidateSet.end(); |
| 4070 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4071 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4072 | Functions.insert(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4073 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4074 | Functions.insert(FunTmpl); |
| 4075 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4077 | // FIXME: Pass in the explicit template arguments? |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 4078 | ArgumentDependentLookup(Name, /*Operator*/false, Args, NumArgs, Functions); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4079 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4080 | // Erase all of the candidates we already knew about. |
| 4081 | // FIXME: This is suboptimal. Is there a better way? |
| 4082 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4083 | CandEnd = CandidateSet.end(); |
| 4084 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4085 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4086 | Functions.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4087 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4088 | Functions.erase(FunTmpl); |
| 4089 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4090 | |
| 4091 | // For each of the ADL candidates we found, add it to the overload |
| 4092 | // set. |
| 4093 | for (FunctionSet::iterator Func = Functions.begin(), |
| 4094 | FuncEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4095 | Func != FuncEnd; ++Func) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4096 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4097 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4098 | continue; |
| 4099 | |
| 4100 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 4101 | false, false, PartialOverloading); |
| 4102 | } else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4103 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4104 | ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4105 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4106 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4109 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4110 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4112 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4114 | // Define viable functions to be better candidates than non-viable |
| 4115 | // functions. |
| 4116 | if (!Cand2.Viable) |
| 4117 | return Cand1.Viable; |
| 4118 | else if (!Cand1.Viable) |
| 4119 | return false; |
| 4120 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4121 | // C++ [over.match.best]p1: |
| 4122 | // |
| 4123 | // -- if F is a static member function, ICS1(F) is defined such |
| 4124 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4125 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4126 | // better nor worse than ICS1(F). |
| 4127 | unsigned StartArg = 0; |
| 4128 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4129 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4130 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4131 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | // A viable function F1 is defined to be a better function than another |
| 4133 | // 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] | 4134 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4135 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4136 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4137 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4138 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4139 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4140 | Cand2.Conversions[ArgIdx])) { |
| 4141 | case ImplicitConversionSequence::Better: |
| 4142 | // Cand1 has a better conversion sequence. |
| 4143 | HasBetterConversion = true; |
| 4144 | break; |
| 4145 | |
| 4146 | case ImplicitConversionSequence::Worse: |
| 4147 | // Cand1 can't be better than Cand2. |
| 4148 | return false; |
| 4149 | |
| 4150 | case ImplicitConversionSequence::Indistinguishable: |
| 4151 | // Do nothing. |
| 4152 | break; |
| 4153 | } |
| 4154 | } |
| 4155 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4156 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4157 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4158 | if (HasBetterConversion) |
| 4159 | return true; |
| 4160 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4161 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4162 | // specialization, or, if not that, |
| 4163 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4164 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4165 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4166 | |
| 4167 | // -- F1 and F2 are function template specializations, and the function |
| 4168 | // template for F1 is more specialized than the template for F2 |
| 4169 | // 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] | 4170 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4171 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4172 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4173 | if (FunctionTemplateDecl *BetterTemplate |
| 4174 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4175 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 6010da0 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4176 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4177 | : TPOC_Call)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4178 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4179 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4180 | // -- the context is an initialization by user-defined conversion |
| 4181 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4182 | // from the return type of F1 to the destination type (i.e., |
| 4183 | // the type of the entity being initialized) is a better |
| 4184 | // conversion sequence than the standard conversion sequence |
| 4185 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | if (Cand1.Function && Cand2.Function && |
| 4187 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4188 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4189 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4190 | Cand2.FinalConversion)) { |
| 4191 | case ImplicitConversionSequence::Better: |
| 4192 | // Cand1 has a better conversion sequence. |
| 4193 | return true; |
| 4194 | |
| 4195 | case ImplicitConversionSequence::Worse: |
| 4196 | // Cand1 can't be better than Cand2. |
| 4197 | return false; |
| 4198 | |
| 4199 | case ImplicitConversionSequence::Indistinguishable: |
| 4200 | // Do nothing |
| 4201 | break; |
| 4202 | } |
| 4203 | } |
| 4204 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4205 | return false; |
| 4206 | } |
| 4207 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4208 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4209 | /// within an overload candidate set. |
| 4210 | /// |
| 4211 | /// \param CandidateSet the set of candidate functions. |
| 4212 | /// |
| 4213 | /// \param Loc the location of the function name (or operator symbol) for |
| 4214 | /// which overload resolution occurs. |
| 4215 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4216 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4217 | /// function, Best points to the candidate function found. |
| 4218 | /// |
| 4219 | /// \returns The result of overload resolution. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4220 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4221 | SourceLocation Loc, |
| 4222 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4223 | // Find the best viable function. |
| 4224 | Best = CandidateSet.end(); |
| 4225 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4226 | Cand != CandidateSet.end(); ++Cand) { |
| 4227 | if (Cand->Viable) { |
| 4228 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 4229 | Best = Cand; |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | // If we didn't find any viable functions, abort. |
| 4234 | if (Best == CandidateSet.end()) |
| 4235 | return OR_No_Viable_Function; |
| 4236 | |
| 4237 | // Make sure that this function is better than every other viable |
| 4238 | // function. If not, we have an ambiguity. |
| 4239 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4240 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4242 | Cand != Best && |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4243 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 4244 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4245 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4246 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4247 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4248 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4249 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4250 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4252 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4253 | return OR_Deleted; |
| 4254 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4255 | // C++ [basic.def.odr]p2: |
| 4256 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4258 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4259 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4260 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4261 | if (Best->Function) |
| 4262 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4263 | return OR_Success; |
| 4264 | } |
| 4265 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4266 | namespace { |
| 4267 | |
| 4268 | enum OverloadCandidateKind { |
| 4269 | oc_function, |
| 4270 | oc_method, |
| 4271 | oc_constructor, |
| 4272 | oc_implicit_default_constructor, |
| 4273 | oc_implicit_copy_constructor, |
| 4274 | oc_implicit_copy_assignment, |
| 4275 | oc_template_specialization // function, constructor, or conversion template |
| 4276 | }; |
| 4277 | |
| 4278 | OverloadCandidateKind ClassifyOverloadCandidate(FunctionDecl *Fn) { |
| 4279 | if (Fn->getPrimaryTemplate()) |
| 4280 | return oc_template_specialization; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4281 | |
| 4282 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4283 | if (!Ctor->isImplicit()) |
| 4284 | return oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4285 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4286 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4287 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4291 | // This actually gets spelled 'candidate function' for now, but |
| 4292 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4293 | if (!Meth->isImplicit()) |
| 4294 | return oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4295 | |
| 4296 | assert(Meth->isCopyAssignment() |
| 4297 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4298 | return oc_implicit_copy_assignment; |
| 4299 | } |
| 4300 | |
| 4301 | return oc_function; |
| 4302 | } |
| 4303 | |
| 4304 | std::string DescribeFunctionTemplate(Sema &S, FunctionDecl *Fn) { |
| 4305 | FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate(); |
| 4306 | return S.getTemplateArgumentBindingsText(FunTmpl->getTemplateParameters(), |
| 4307 | *Fn->getTemplateSpecializationArgs()); |
| 4308 | } |
| 4309 | |
| 4310 | } // end anonymous namespace |
| 4311 | |
| 4312 | // Notes the location of an overload candidate. |
| 4313 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
| 4314 | OverloadCandidateKind K = ClassifyOverloadCandidate(Fn); |
| 4315 | if (K == oc_template_specialization) { |
| 4316 | Diag(Fn->getLocation(), diag::note_ovl_template_candidate) |
| 4317 | << DescribeFunctionTemplate(*this, Fn); |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4318 | return; |
| 4319 | } |
| 4320 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4321 | Diag(Fn->getLocation(), diag::note_ovl_candidate) << (unsigned) K; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4324 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4325 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4326 | /// target types of the conversion. |
| 4327 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4328 | SourceLocation CaretLoc, |
| 4329 | const PartialDiagnostic &PDiag) { |
| 4330 | Diag(CaretLoc, PDiag) |
| 4331 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4332 | for (AmbiguousConversionSequence::const_iterator |
| 4333 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4334 | NoteOverloadCandidate(*I); |
| 4335 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4338 | namespace { |
| 4339 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4340 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4341 | FunctionDecl *Fn = Cand->Function; |
| 4342 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4343 | // Note deleted candidates, but only if they're viable. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4344 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
| 4345 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(Fn); |
| 4346 | |
| 4347 | if (FnKind == oc_template_specialization) { |
| 4348 | S.Diag(Fn->getLocation(), diag::note_ovl_template_candidate_deleted) |
| 4349 | << DescribeFunctionTemplate(S, Fn) << Fn->isDeleted(); |
| 4350 | return; |
| 4351 | } |
| 4352 | |
| 4353 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
| 4354 | << FnKind << Fn->isDeleted(); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4355 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4358 | bool errReported = false; |
| 4359 | if (!Cand->Viable && Cand->Conversions.size() > 0) { |
| 4360 | for (int i = Cand->Conversions.size()-1; i >= 0; i--) { |
| 4361 | const ImplicitConversionSequence &Conversion = |
| 4362 | Cand->Conversions[i]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4363 | |
| 4364 | if (!Conversion.isAmbiguous()) |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4365 | continue; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4366 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4367 | S.DiagnoseAmbiguousConversion(Conversion, Fn->getLocation(), |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4368 | PDiag(diag::note_ovl_candidate_not_viable) << (i+1)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4369 | errReported = true; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | if (!errReported) |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4374 | S.NoteOverloadCandidate(Fn); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4378 | // Desugar the type of the surrogate down to a function type, |
| 4379 | // retaining as many typedefs as possible while still showing |
| 4380 | // the function type (and, therefore, its parameter types). |
| 4381 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4382 | bool isLValueReference = false; |
| 4383 | bool isRValueReference = false; |
| 4384 | bool isPointer = false; |
| 4385 | if (const LValueReferenceType *FnTypeRef = |
| 4386 | FnType->getAs<LValueReferenceType>()) { |
| 4387 | FnType = FnTypeRef->getPointeeType(); |
| 4388 | isLValueReference = true; |
| 4389 | } else if (const RValueReferenceType *FnTypeRef = |
| 4390 | FnType->getAs<RValueReferenceType>()) { |
| 4391 | FnType = FnTypeRef->getPointeeType(); |
| 4392 | isRValueReference = true; |
| 4393 | } |
| 4394 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 4395 | FnType = FnTypePtr->getPointeeType(); |
| 4396 | isPointer = true; |
| 4397 | } |
| 4398 | // Desugar down to a function type. |
| 4399 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 4400 | // Reconstruct the pointer/reference as appropriate. |
| 4401 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 4402 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 4403 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 4404 | |
| 4405 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 4406 | << FnType; |
| 4407 | } |
| 4408 | |
| 4409 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 4410 | const char *Opc, |
| 4411 | SourceLocation OpLoc, |
| 4412 | OverloadCandidate *Cand) { |
| 4413 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 4414 | std::string TypeStr("operator"); |
| 4415 | TypeStr += Opc; |
| 4416 | TypeStr += "("; |
| 4417 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4418 | if (Cand->Conversions.size() == 1) { |
| 4419 | TypeStr += ")"; |
| 4420 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 4421 | } else { |
| 4422 | TypeStr += ", "; |
| 4423 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4424 | TypeStr += ")"; |
| 4425 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 4430 | OverloadCandidate *Cand) { |
| 4431 | unsigned NoOperands = Cand->Conversions.size(); |
| 4432 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 4433 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4434 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 4435 | if (!ICS.isAmbiguous()) continue; |
| 4436 | |
| 4437 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
| 4438 | PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4439 | } |
| 4440 | } |
| 4441 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4442 | struct CompareOverloadCandidatesForDisplay { |
| 4443 | Sema &S; |
| 4444 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4445 | |
| 4446 | bool operator()(const OverloadCandidate *L, |
| 4447 | const OverloadCandidate *R) { |
| 4448 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4449 | if (L->Viable) { |
| 4450 | if (!R->Viable) return true; |
| 4451 | |
| 4452 | // TODO: introduce a tri-valued comparison for overload |
| 4453 | // candidates. Would be more worthwhile if we had a sort |
| 4454 | // that could exploit it. |
| 4455 | if (S.isBetterOverloadCandidate(*L, *R)) return true; |
| 4456 | if (S.isBetterOverloadCandidate(*R, *L)) return false; |
| 4457 | } else if (R->Viable) |
| 4458 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4459 | |
| 4460 | // Put declared functions first. |
| 4461 | if (L->Function) { |
| 4462 | if (!R->Function) return true; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4463 | return S.SourceMgr.isBeforeInTranslationUnit(L->Function->getLocation(), |
| 4464 | R->Function->getLocation()); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4465 | } else if (R->Function) return false; |
| 4466 | |
| 4467 | // Then surrogates. |
| 4468 | if (L->IsSurrogate) { |
| 4469 | if (!R->IsSurrogate) return true; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4470 | return S.SourceMgr.isBeforeInTranslationUnit(L->Surrogate->getLocation(), |
| 4471 | R->Surrogate->getLocation()); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4472 | } else if (R->IsSurrogate) return false; |
| 4473 | |
| 4474 | // And builtins just come in a jumble. |
| 4475 | return false; |
| 4476 | } |
| 4477 | }; |
| 4478 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4479 | } // end anonymous namespace |
| 4480 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4481 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4482 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4483 | /// set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4485 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4486 | OverloadCandidateDisplayKind OCD, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4487 | const char *Opc, |
Fariborz Jahanian | 29f9d39 | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4488 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4489 | // Sort the candidates by viability and position. Sorting directly would |
| 4490 | // be prohibitive, so we make a set of pointers and sort those. |
| 4491 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 4492 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 4493 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4494 | LastCand = CandidateSet.end(); |
| 4495 | Cand != LastCand; ++Cand) |
| 4496 | if (Cand->Viable || OCD == OCD_AllCandidates) |
| 4497 | Cands.push_back(Cand); |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4498 | std::sort(Cands.begin(), Cands.end(), |
| 4499 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4500 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4501 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4502 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4503 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 4504 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 4505 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4506 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4507 | if (Cand->Function) |
| 4508 | NoteFunctionCandidate(*this, Cand); |
| 4509 | else if (Cand->IsSurrogate) |
| 4510 | NoteSurrogateCandidate(*this, Cand); |
| 4511 | |
| 4512 | // This a builtin candidate. We do not, in general, want to list |
| 4513 | // every possible builtin candidate. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4514 | else if (Cand->Viable) { |
| 4515 | // Generally we only see ambiguities including viable builtin |
| 4516 | // operators if overload resolution got screwed up by an |
| 4517 | // ambiguous user-defined conversion. |
| 4518 | // |
| 4519 | // FIXME: It's quite possible for different conversions to see |
| 4520 | // different ambiguities, though. |
| 4521 | if (!ReportedAmbiguousConversions) { |
| 4522 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 4523 | ReportedAmbiguousConversions = true; |
| 4524 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4525 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4526 | // If this is a viable builtin, print it. |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4527 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4528 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4529 | } |
| 4530 | } |
| 4531 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4532 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4533 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4534 | /// expression with overloaded function type and @p ToType is the type |
| 4535 | /// we're trying to resolve to. For example: |
| 4536 | /// |
| 4537 | /// @code |
| 4538 | /// int f(double); |
| 4539 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4540 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4541 | /// int (*pfd)(double) = f; // selects f(double) |
| 4542 | /// @endcode |
| 4543 | /// |
| 4544 | /// This routine returns the resulting FunctionDecl if it could be |
| 4545 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4546 | /// routine will emit diagnostics if there is an error. |
| 4547 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4548 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4549 | bool Complain) { |
| 4550 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4551 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4552 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4553 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4554 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4555 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4556 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4557 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4558 | FunctionType = MemTypePtr->getPointeeType(); |
| 4559 | IsMember = true; |
| 4560 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4561 | |
| 4562 | // We only look at pointers or references to functions. |
Douglas Gregor | 6b6ba8b | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4563 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4564 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4565 | return 0; |
| 4566 | |
| 4567 | // Find the actual overloaded function declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4569 | // C++ [over.over]p1: |
| 4570 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4571 | // overloaded function name is ignored (5.1). ] |
| 4572 | Expr *OvlExpr = From->IgnoreParens(); |
| 4573 | |
| 4574 | // C++ [over.over]p1: |
| 4575 | // [...] The overloaded function name can be preceded by the & |
| 4576 | // operator. |
| 4577 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4578 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4579 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4580 | } |
| 4581 | |
Anders Carlsson | b68b028 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4582 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4583 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4584 | |
| 4585 | llvm::SmallVector<NamedDecl*,8> Fns; |
Anders Carlsson | b68b028 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4586 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4587 | // Look into the overloaded expression. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4588 | if (UnresolvedLookupExpr *UL |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4589 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 4590 | Fns.append(UL->decls_begin(), UL->decls_end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4591 | if (UL->hasExplicitTemplateArgs()) { |
| 4592 | HasExplicitTemplateArgs = true; |
| 4593 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4594 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4595 | } else if (UnresolvedMemberExpr *ME |
| 4596 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 4597 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 4598 | if (ME->hasExplicitTemplateArgs()) { |
| 4599 | HasExplicitTemplateArgs = true; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4600 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4601 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4602 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4603 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4604 | // If we didn't actually find anything, we're done. |
| 4605 | if (Fns.empty()) |
| 4606 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4608 | // Look through all of the overloaded functions, searching for one |
| 4609 | // whose type matches exactly. |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4610 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4611 | bool FoundNonTemplateFunction = false; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4612 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 4613 | E = Fns.end(); I != E; ++I) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4614 | // Look through any using declarations to find the underlying function. |
| 4615 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 4616 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4617 | // C++ [over.over]p3: |
| 4618 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4619 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4620 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4621 | // type "pointer-to-member-function." |
| 4622 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4623 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4625 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4627 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4629 | // static when converting to member pointer. |
| 4630 | if (Method->isStatic() == IsMember) |
| 4631 | continue; |
| 4632 | } else if (IsMember) |
| 4633 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4635 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | // If the name is a function template, template argument deduction is |
| 4637 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4638 | // resulting template argument list is used to generate a single |
| 4639 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4640 | // overloaded functions considered. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4641 | // 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] | 4642 | FunctionDecl *Specialization = 0; |
| 4643 | TemplateDeductionInfo Info(Context); |
| 4644 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4645 | = DeduceTemplateArguments(FunctionTemplate, |
| 4646 | (HasExplicitTemplateArgs ? &ExplicitTemplateArgs : 0), |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4647 | FunctionType, Specialization, Info)) { |
| 4648 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4649 | (void)Result; |
| 4650 | } else { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4651 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4652 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4653 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4654 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4655 | Matches.insert( |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4656 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4657 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4658 | |
| 4659 | continue; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4660 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4661 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4662 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4663 | // Skip non-static functions when converting to pointer, and static |
| 4664 | // when converting to member pointer. |
| 4665 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4666 | continue; |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4667 | |
| 4668 | // If we have explicit template arguments, skip non-templates. |
| 4669 | if (HasExplicitTemplateArgs) |
| 4670 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4671 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4672 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4673 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4674 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 4675 | QualType ResultTy; |
| 4676 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 4677 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 4678 | ResultTy)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4679 | Matches.insert(cast<FunctionDecl>(FunDecl->getCanonicalDecl())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4680 | FoundNonTemplateFunction = true; |
| 4681 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4682 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4685 | // If there were 0 or 1 matches, we're done. |
| 4686 | if (Matches.empty()) |
| 4687 | return 0; |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4688 | else if (Matches.size() == 1) { |
| 4689 | FunctionDecl *Result = *Matches.begin(); |
| 4690 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4691 | return Result; |
| 4692 | } |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4693 | |
| 4694 | // C++ [over.over]p4: |
| 4695 | // If more than one function is selected, [...] |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4696 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4697 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4698 | // [...] and any given function template specialization F1 is |
| 4699 | // eliminated if the set contains a second function template |
| 4700 | // specialization whose function template is more specialized |
| 4701 | // than the function template of F1 according to the partial |
| 4702 | // ordering rules of 14.5.5.2. |
| 4703 | |
| 4704 | // The algorithm specified above is quadratic. We instead use a |
| 4705 | // two-pass algorithm (similar to the one used to identify the |
| 4706 | // best viable function in an overload set) that identifies the |
| 4707 | // best function template (if it exists). |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4708 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4709 | Matches.end()); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4710 | FunctionDecl *Result = |
| 4711 | getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 4712 | TPOC_Other, From->getLocStart(), |
| 4713 | PDiag(), |
| 4714 | PDiag(diag::err_addr_ovl_ambiguous) |
| 4715 | << TemplateMatches[0]->getDeclName(), |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4716 | PDiag(diag::note_ovl_template_candidate)); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4717 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4718 | return Result; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4719 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 4721 | // [...] any function template specializations in the set are |
| 4722 | // eliminated if the set also contains a non-template function, [...] |
| 4723 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 4724 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 4725 | if ((*M)->getPrimaryTemplate() == 0) |
| 4726 | RemainingMatches.push_back(*M); |
| 4727 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4728 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4729 | // selected function. |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 4730 | if (RemainingMatches.size() == 1) { |
| 4731 | FunctionDecl *Result = RemainingMatches.front(); |
| 4732 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 4733 | return Result; |
| 4734 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4736 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4737 | // returns (even if we issue the diagnostics here). |
| 4738 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4739 | << RemainingMatches[0]->getDeclName(); |
| 4740 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4741 | NoteOverloadCandidate(RemainingMatches[I]); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4742 | return 0; |
| 4743 | } |
| 4744 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4745 | /// \brief Given an expression that refers to an overloaded function, try to |
| 4746 | /// resolve that overloaded function expression down to a single function. |
| 4747 | /// |
| 4748 | /// This routine can only resolve template-ids that refer to a single function |
| 4749 | /// template, where that template-id refers to a single template whose template |
| 4750 | /// arguments are either provided by the template-id or have defaults, |
| 4751 | /// as described in C++0x [temp.arg.explicit]p3. |
| 4752 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 4753 | // C++ [over.over]p1: |
| 4754 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4755 | // overloaded function name is ignored (5.1). ] |
| 4756 | Expr *OvlExpr = From->IgnoreParens(); |
| 4757 | |
| 4758 | // C++ [over.over]p1: |
| 4759 | // [...] The overloaded function name can be preceded by the & |
| 4760 | // operator. |
| 4761 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4762 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4763 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4764 | } |
| 4765 | |
| 4766 | bool HasExplicitTemplateArgs = false; |
| 4767 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 4768 | |
| 4769 | llvm::SmallVector<NamedDecl*,8> Fns; |
| 4770 | |
| 4771 | // Look into the overloaded expression. |
| 4772 | if (UnresolvedLookupExpr *UL |
| 4773 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 4774 | Fns.append(UL->decls_begin(), UL->decls_end()); |
| 4775 | if (UL->hasExplicitTemplateArgs()) { |
| 4776 | HasExplicitTemplateArgs = true; |
| 4777 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4778 | } |
| 4779 | } else if (UnresolvedMemberExpr *ME |
| 4780 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 4781 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 4782 | if (ME->hasExplicitTemplateArgs()) { |
| 4783 | HasExplicitTemplateArgs = true; |
| 4784 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | // If we didn't actually find any template-ids, we're done. |
| 4789 | if (Fns.empty() || !HasExplicitTemplateArgs) |
| 4790 | return 0; |
| 4791 | |
| 4792 | // Look through all of the overloaded functions, searching for one |
| 4793 | // whose type matches exactly. |
| 4794 | FunctionDecl *Matched = 0; |
| 4795 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 4796 | E = Fns.end(); I != E; ++I) { |
| 4797 | // C++0x [temp.arg.explicit]p3: |
| 4798 | // [...] In contexts where deduction is done and fails, or in contexts |
| 4799 | // where deduction is not done, if a template argument list is |
| 4800 | // specified and it, along with any default template arguments, |
| 4801 | // identifies a single function template specialization, then the |
| 4802 | // template-id is an lvalue for the function template specialization. |
| 4803 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 4804 | |
| 4805 | // C++ [over.over]p2: |
| 4806 | // If the name is a function template, template argument deduction is |
| 4807 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4808 | // resulting template argument list is used to generate a single |
| 4809 | // function template specialization, which is added to the set of |
| 4810 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4811 | FunctionDecl *Specialization = 0; |
| 4812 | TemplateDeductionInfo Info(Context); |
| 4813 | if (TemplateDeductionResult Result |
| 4814 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 4815 | Specialization, Info)) { |
| 4816 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4817 | (void)Result; |
| 4818 | continue; |
| 4819 | } |
| 4820 | |
| 4821 | // Multiple matches; we can't resolve to a single declaration. |
| 4822 | if (Matched) |
| 4823 | return 0; |
| 4824 | |
| 4825 | Matched = Specialization; |
| 4826 | } |
| 4827 | |
| 4828 | return Matched; |
| 4829 | } |
| 4830 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4831 | /// \brief Add a single candidate to the overload set. |
| 4832 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4833 | NamedDecl *Callee, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4834 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4835 | Expr **Args, unsigned NumArgs, |
| 4836 | OverloadCandidateSet &CandidateSet, |
| 4837 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4838 | if (isa<UsingShadowDecl>(Callee)) |
| 4839 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 4840 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4841 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4842 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4843 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 4844 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4845 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | if (FunctionTemplateDecl *FuncTemplate |
| 4849 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4850 | S.AddTemplateOverloadCandidate(FuncTemplate, ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4851 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4852 | return; |
| 4853 | } |
| 4854 | |
| 4855 | assert(false && "unhandled case in overloaded call candidate"); |
| 4856 | |
| 4857 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 4861 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4862 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4863 | Expr **Args, unsigned NumArgs, |
| 4864 | OverloadCandidateSet &CandidateSet, |
| 4865 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4866 | |
| 4867 | #ifndef NDEBUG |
| 4868 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 4869 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4870 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4871 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4872 | // and let Y be the lookup set produced by argument dependent |
| 4873 | // lookup (defined as follows). If X contains |
| 4874 | // |
| 4875 | // -- a declaration of a class member, or |
| 4876 | // |
| 4877 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4878 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4879 | // |
| 4880 | // -- a declaration that is neither a function or a function |
| 4881 | // template |
| 4882 | // |
| 4883 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4884 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4885 | if (ULE->requiresADL()) { |
| 4886 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 4887 | E = ULE->decls_end(); I != E; ++I) { |
| 4888 | assert(!(*I)->getDeclContext()->isRecord()); |
| 4889 | assert(isa<UsingShadowDecl>(*I) || |
| 4890 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 4891 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | #endif |
| 4895 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4896 | // It would be nice to avoid this copy. |
| 4897 | TemplateArgumentListInfo TABuffer; |
| 4898 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 4899 | if (ULE->hasExplicitTemplateArgs()) { |
| 4900 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 4901 | ExplicitTemplateArgs = &TABuffer; |
| 4902 | } |
| 4903 | |
| 4904 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 4905 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4906 | AddOverloadedCallCandidate(*this, *I, ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4907 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4908 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4909 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4910 | if (ULE->requiresADL()) |
| 4911 | AddArgumentDependentLookupCandidates(ULE->getName(), Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4912 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4913 | CandidateSet, |
| 4914 | PartialOverloading); |
| 4915 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4916 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4917 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 4918 | Expr **Args, unsigned NumArgs) { |
| 4919 | Fn->Destroy(SemaRef.Context); |
| 4920 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4921 | Args[Arg]->Destroy(SemaRef.Context); |
| 4922 | return SemaRef.ExprError(); |
| 4923 | } |
| 4924 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4925 | /// Attempts to recover from a call where no functions were found. |
| 4926 | /// |
| 4927 | /// Returns true if new candidates were found. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4928 | static Sema::OwningExprResult |
| 4929 | BuildRecoveryCallExpr(Sema &SemaRef, Expr *Fn, |
| 4930 | UnresolvedLookupExpr *ULE, |
| 4931 | SourceLocation LParenLoc, |
| 4932 | Expr **Args, unsigned NumArgs, |
| 4933 | SourceLocation *CommaLocs, |
| 4934 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4935 | |
| 4936 | CXXScopeSpec SS; |
| 4937 | if (ULE->getQualifier()) { |
| 4938 | SS.setScopeRep(ULE->getQualifier()); |
| 4939 | SS.setRange(ULE->getQualifierRange()); |
| 4940 | } |
| 4941 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4942 | TemplateArgumentListInfo TABuffer; |
| 4943 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 4944 | if (ULE->hasExplicitTemplateArgs()) { |
| 4945 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 4946 | ExplicitTemplateArgs = &TABuffer; |
| 4947 | } |
| 4948 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4949 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 4950 | Sema::LookupOrdinaryName); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 4951 | if (SemaRef.DiagnoseEmptyLookup(/*Scope=*/0, SS, R)) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4952 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4953 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4954 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 4955 | |
| 4956 | // Build an implicit member call if appropriate. Just drop the |
| 4957 | // casts and such from the call, we don't really care. |
| 4958 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 4959 | if ((*R.begin())->isCXXClassMember()) |
| 4960 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 4961 | else if (ExplicitTemplateArgs) |
| 4962 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 4963 | else |
| 4964 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 4965 | |
| 4966 | if (NewFn.isInvalid()) |
| 4967 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 4968 | |
| 4969 | Fn->Destroy(SemaRef.Context); |
| 4970 | |
| 4971 | // This shouldn't cause an infinite loop because we're giving it |
| 4972 | // an expression with non-empty lookup results, which should never |
| 4973 | // end up here. |
| 4974 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 4975 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 4976 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4977 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4979 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4980 | /// (which eventually refers to the declaration Func) and the call |
| 4981 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4982 | /// to a specific function. If overload resolution succeeds, returns |
| 4983 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4984 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4985 | /// arguments and Fn, and returns NULL. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4986 | Sema::OwningExprResult |
| 4987 | Sema::BuildOverloadedCallExpr(Expr *Fn, UnresolvedLookupExpr *ULE, |
| 4988 | SourceLocation LParenLoc, |
| 4989 | Expr **Args, unsigned NumArgs, |
| 4990 | SourceLocation *CommaLocs, |
| 4991 | SourceLocation RParenLoc) { |
| 4992 | #ifndef NDEBUG |
| 4993 | if (ULE->requiresADL()) { |
| 4994 | // To do ADL, we must have found an unqualified name. |
| 4995 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 4996 | |
| 4997 | // We don't perform ADL for implicit declarations of builtins. |
| 4998 | // Verify that this was correctly set up. |
| 4999 | FunctionDecl *F; |
| 5000 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5001 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5002 | F->getBuiltinID() && F->isImplicit()) |
| 5003 | assert(0 && "performing ADL for builtin"); |
| 5004 | |
| 5005 | // We don't perform ADL in C. |
| 5006 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5007 | } |
| 5008 | #endif |
| 5009 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5010 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 5011 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5012 | // Add the functions denoted by the callee to the set of candidate |
| 5013 | // functions, including those from argument-dependent lookup. |
| 5014 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5015 | |
| 5016 | // If we found nothing, try to recover. |
| 5017 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5018 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5019 | if (CandidateSet.empty()) |
| 5020 | return BuildRecoveryCallExpr(*this, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5021 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5023 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5024 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5025 | case OR_Success: { |
| 5026 | FunctionDecl *FDecl = Best->Function; |
| 5027 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 5028 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5029 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5030 | |
| 5031 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5032 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5033 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5034 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5035 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5036 | break; |
| 5037 | |
| 5038 | case OR_Ambiguous: |
| 5039 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5040 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5041 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5042 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5043 | |
| 5044 | case OR_Deleted: |
| 5045 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5046 | << Best->Function->isDeleted() |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5047 | << ULE->getName() |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5048 | << Fn->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5049 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5050 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5054 | // return NULL. |
| 5055 | Fn->Destroy(Context); |
| 5056 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5057 | Args[Arg]->Destroy(Context); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5058 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5061 | static bool IsOverloaded(const Sema::FunctionSet &Functions) { |
| 5062 | return Functions.size() > 1 || |
| 5063 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5064 | } |
| 5065 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5066 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5067 | /// operator. |
| 5068 | /// |
| 5069 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5070 | /// |
| 5071 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5072 | /// operator. |
| 5073 | /// |
| 5074 | /// \param Functions The set of non-member functions that will be |
| 5075 | /// considered by overload resolution. The caller needs to build this |
| 5076 | /// set based on the context using, e.g., |
| 5077 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5078 | /// set should not contain any member functions; those will be added |
| 5079 | /// by CreateOverloadedUnaryOp(). |
| 5080 | /// |
| 5081 | /// \param input The input argument. |
| 5082 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 5083 | unsigned OpcIn, |
| 5084 | FunctionSet &Functions, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5085 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5086 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5087 | Expr *Input = (Expr *)input.get(); |
| 5088 | |
| 5089 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5090 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5091 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5092 | |
| 5093 | Expr *Args[2] = { Input, 0 }; |
| 5094 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5096 | // For post-increment and post-decrement, add the implicit '0' as |
| 5097 | // the second argument, so that we know this is a post-increment or |
| 5098 | // post-decrement. |
| 5099 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5100 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5101 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5102 | SourceLocation()); |
| 5103 | NumArgs = 2; |
| 5104 | } |
| 5105 | |
| 5106 | if (Input->isTypeDependent()) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5107 | UnresolvedLookupExpr *Fn |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5108 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5109 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5110 | /*ADL*/ true, IsOverloaded(Functions)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5111 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5112 | FuncEnd = Functions.end(); |
| 5113 | Func != FuncEnd; ++Func) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5114 | Fn->addDecl(*Func); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5115 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5116 | input.release(); |
| 5117 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5118 | &Args[0], NumArgs, |
| 5119 | Context.DependentTy, |
| 5120 | OpLoc)); |
| 5121 | } |
| 5122 | |
| 5123 | // Build an empty overload set. |
| 5124 | OverloadCandidateSet CandidateSet; |
| 5125 | |
| 5126 | // Add the candidates from the given function set. |
| 5127 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 5128 | |
| 5129 | // Add operator candidates that are member functions. |
| 5130 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5131 | |
| 5132 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5133 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5134 | |
| 5135 | // Perform overload resolution. |
| 5136 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5137 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5138 | case OR_Success: { |
| 5139 | // We found a built-in operator or an overloaded operator. |
| 5140 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5141 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5142 | if (FnDecl) { |
| 5143 | // We matched an overloaded operator. Build a call to that |
| 5144 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5146 | // Convert the arguments. |
| 5147 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5148 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 5149 | return ExprError(); |
| 5150 | } else { |
| 5151 | // Convert the arguments. |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5152 | OwningExprResult InputInit |
| 5153 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5154 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5155 | SourceLocation(), |
| 5156 | move(input)); |
| 5157 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5158 | return ExprError(); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5159 | |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5160 | input = move(InputInit); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5161 | Input = (Expr *)input.get(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | // Determine the result type |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5165 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5166 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5167 | // Build the actual expression node. |
| 5168 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5169 | SourceLocation()); |
| 5170 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5172 | input.release(); |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5173 | Args[0] = Input; |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5174 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5175 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5176 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5177 | |
| 5178 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5179 | FnDecl)) |
| 5180 | return ExprError(); |
| 5181 | |
| 5182 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5183 | } else { |
| 5184 | // We matched a built-in operator. Convert the arguments, then |
| 5185 | // break out so that we will build the appropriate built-in |
| 5186 | // operator node. |
| 5187 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5188 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5189 | return ExprError(); |
| 5190 | |
| 5191 | break; |
| 5192 | } |
| 5193 | } |
| 5194 | |
| 5195 | case OR_No_Viable_Function: |
| 5196 | // No viable function; fall through to handling this as a |
| 5197 | // built-in operator, which will produce an error message for us. |
| 5198 | break; |
| 5199 | |
| 5200 | case OR_Ambiguous: |
| 5201 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5202 | << UnaryOperator::getOpcodeStr(Opc) |
| 5203 | << Input->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5204 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5205 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5206 | return ExprError(); |
| 5207 | |
| 5208 | case OR_Deleted: |
| 5209 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5210 | << Best->Function->isDeleted() |
| 5211 | << UnaryOperator::getOpcodeStr(Opc) |
| 5212 | << Input->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5213 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5214 | return ExprError(); |
| 5215 | } |
| 5216 | |
| 5217 | // Either we found no viable overloaded operator or we matched a |
| 5218 | // built-in operator. In either case, fall through to trying to |
| 5219 | // build a built-in operation. |
| 5220 | input.release(); |
| 5221 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5222 | } |
| 5223 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5224 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5225 | /// operator. |
| 5226 | /// |
| 5227 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5228 | /// |
| 5229 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5230 | /// operator. |
| 5231 | /// |
| 5232 | /// \param Functions The set of non-member functions that will be |
| 5233 | /// considered by overload resolution. The caller needs to build this |
| 5234 | /// set based on the context using, e.g., |
| 5235 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5236 | /// set should not contain any member functions; those will be added |
| 5237 | /// by CreateOverloadedBinOp(). |
| 5238 | /// |
| 5239 | /// \param LHS Left-hand argument. |
| 5240 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5242 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5243 | unsigned OpcIn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5244 | FunctionSet &Functions, |
| 5245 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5246 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5247 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5248 | |
| 5249 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5250 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5251 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5252 | |
| 5253 | // If either side is type-dependent, create an appropriate dependent |
| 5254 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5255 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5256 | if (Functions.empty()) { |
| 5257 | // If there are no functions to store, just build a dependent |
| 5258 | // BinaryOperator or CompoundAssignment. |
| 5259 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5260 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5261 | Context.DependentTy, OpLoc)); |
| 5262 | |
| 5263 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5264 | Context.DependentTy, |
| 5265 | Context.DependentTy, |
| 5266 | Context.DependentTy, |
| 5267 | OpLoc)); |
| 5268 | } |
| 5269 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5270 | UnresolvedLookupExpr *Fn |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5271 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5272 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5273 | /* ADL */ true, IsOverloaded(Functions)); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5274 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5275 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5276 | FuncEnd = Functions.end(); |
| 5277 | Func != FuncEnd; ++Func) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5278 | Fn->addDecl(*Func); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5279 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5280 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5281 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5282 | Context.DependentTy, |
| 5283 | OpLoc)); |
| 5284 | } |
| 5285 | |
| 5286 | // If this is the .* operator, which is not overloadable, just |
| 5287 | // create a built-in binary operator. |
| 5288 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5289 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5290 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 5291 | // If this is the assignment operator, we only perform overload resolution |
| 5292 | // if the left-hand side is a class or enumeration type. This is actually |
| 5293 | // a hack. The standard requires that we do overload resolution between the |
| 5294 | // various built-in candidates, but as DR507 points out, this can lead to |
| 5295 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 5296 | // Note that we go the traditional code path for compound assignment forms. |
| 5297 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5298 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5300 | // Build an empty overload set. |
| 5301 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5302 | |
| 5303 | // Add the candidates from the given function set. |
| 5304 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 5305 | |
| 5306 | // Add operator candidates that are member functions. |
| 5307 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5308 | |
| 5309 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5310 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5311 | |
| 5312 | // Perform overload resolution. |
| 5313 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5314 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5315 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5316 | // We found a built-in operator or an overloaded operator. |
| 5317 | FunctionDecl *FnDecl = Best->Function; |
| 5318 | |
| 5319 | if (FnDecl) { |
| 5320 | // We matched an overloaded operator. Build a call to that |
| 5321 | // operator. |
| 5322 | |
| 5323 | // Convert the arguments. |
| 5324 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5325 | OwningExprResult Arg1 |
| 5326 | = PerformCopyInitialization( |
| 5327 | InitializedEntity::InitializeParameter( |
| 5328 | FnDecl->getParamDecl(0)), |
| 5329 | SourceLocation(), |
| 5330 | Owned(Args[1])); |
| 5331 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5332 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5333 | |
| 5334 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
| 5335 | return ExprError(); |
| 5336 | |
| 5337 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5338 | } else { |
| 5339 | // Convert the arguments. |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5340 | OwningExprResult Arg0 |
| 5341 | = PerformCopyInitialization( |
| 5342 | InitializedEntity::InitializeParameter( |
| 5343 | FnDecl->getParamDecl(0)), |
| 5344 | SourceLocation(), |
| 5345 | Owned(Args[0])); |
| 5346 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5347 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5348 | |
| 5349 | OwningExprResult Arg1 |
| 5350 | = PerformCopyInitialization( |
| 5351 | InitializedEntity::InitializeParameter( |
| 5352 | FnDecl->getParamDecl(1)), |
| 5353 | SourceLocation(), |
| 5354 | Owned(Args[1])); |
| 5355 | if (Arg1.isInvalid()) |
| 5356 | return ExprError(); |
| 5357 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 5358 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | // Determine the result type |
| 5362 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5363 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5364 | ResultTy = ResultTy.getNonReferenceType(); |
| 5365 | |
| 5366 | // Build the actual expression node. |
| 5367 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 5368 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5369 | UsualUnaryConversions(FnExpr); |
| 5370 | |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5371 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5372 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5373 | Args, 2, ResultTy, |
| 5374 | OpLoc)); |
| 5375 | |
| 5376 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5377 | FnDecl)) |
| 5378 | return ExprError(); |
| 5379 | |
| 5380 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5381 | } else { |
| 5382 | // We matched a built-in operator. Convert the arguments, then |
| 5383 | // break out so that we will build the appropriate built-in |
| 5384 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5385 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5386 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5387 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5388 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5389 | return ExprError(); |
| 5390 | |
| 5391 | break; |
| 5392 | } |
| 5393 | } |
| 5394 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5395 | case OR_No_Viable_Function: { |
| 5396 | // C++ [over.match.oper]p9: |
| 5397 | // If the operator is the operator , [...] and there are no |
| 5398 | // viable functions, then the operator is assumed to be the |
| 5399 | // built-in operator and interpreted according to clause 5. |
| 5400 | if (Opc == BinaryOperator::Comma) |
| 5401 | break; |
| 5402 | |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5403 | // For class as left operand for assignment or compound assigment operator |
| 5404 | // do not fall through to handling in built-in, but report that no overloaded |
| 5405 | // assignment operator found |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5406 | OwningExprResult Result = ExprError(); |
| 5407 | if (Args[0]->getType()->isRecordType() && |
| 5408 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5409 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 5410 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5411 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5412 | } else { |
| 5413 | // No viable function; try to create a built-in operation, which will |
| 5414 | // produce an error. Then, show the non-viable candidates. |
| 5415 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5416 | } |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5417 | assert(Result.isInvalid() && |
| 5418 | "C++ binary operator overloading is missing candidates!"); |
| 5419 | if (Result.isInvalid()) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5420 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5421 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5422 | return move(Result); |
| 5423 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5424 | |
| 5425 | case OR_Ambiguous: |
| 5426 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5427 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5428 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5429 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5430 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5431 | return ExprError(); |
| 5432 | |
| 5433 | case OR_Deleted: |
| 5434 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5435 | << Best->Function->isDeleted() |
| 5436 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5437 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5438 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5439 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5440 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5441 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5442 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5443 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5444 | } |
| 5445 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5446 | Action::OwningExprResult |
| 5447 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 5448 | SourceLocation RLoc, |
| 5449 | ExprArg Base, ExprArg Idx) { |
| 5450 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 5451 | static_cast<Expr*>(Idx.get()) }; |
| 5452 | DeclarationName OpName = |
| 5453 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 5454 | |
| 5455 | // If either side is type-dependent, create an appropriate dependent |
| 5456 | // expression. |
| 5457 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5458 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5459 | UnresolvedLookupExpr *Fn |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5460 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5461 | 0, SourceRange(), OpName, LLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5462 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5463 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5464 | |
| 5465 | Base.release(); |
| 5466 | Idx.release(); |
| 5467 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5468 | Args, 2, |
| 5469 | Context.DependentTy, |
| 5470 | RLoc)); |
| 5471 | } |
| 5472 | |
| 5473 | // Build an empty overload set. |
| 5474 | OverloadCandidateSet CandidateSet; |
| 5475 | |
| 5476 | // Subscript can only be overloaded as a member function. |
| 5477 | |
| 5478 | // Add operator candidates that are member functions. |
| 5479 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5480 | |
| 5481 | // Add builtin operator candidates. |
| 5482 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5483 | |
| 5484 | // Perform overload resolution. |
| 5485 | OverloadCandidateSet::iterator Best; |
| 5486 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5487 | case OR_Success: { |
| 5488 | // We found a built-in operator or an overloaded operator. |
| 5489 | FunctionDecl *FnDecl = Best->Function; |
| 5490 | |
| 5491 | if (FnDecl) { |
| 5492 | // We matched an overloaded operator. Build a call to that |
| 5493 | // operator. |
| 5494 | |
| 5495 | // Convert the arguments. |
| 5496 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5497 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 5498 | PerformCopyInitialization(Args[1], |
| 5499 | FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5500 | AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5501 | return ExprError(); |
| 5502 | |
| 5503 | // Determine the result type |
| 5504 | QualType ResultTy |
| 5505 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5506 | ResultTy = ResultTy.getNonReferenceType(); |
| 5507 | |
| 5508 | // Build the actual expression node. |
| 5509 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5510 | LLoc); |
| 5511 | UsualUnaryConversions(FnExpr); |
| 5512 | |
| 5513 | Base.release(); |
| 5514 | Idx.release(); |
| 5515 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5516 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5517 | FnExpr, Args, 2, |
| 5518 | ResultTy, RLoc)); |
| 5519 | |
| 5520 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5521 | FnDecl)) |
| 5522 | return ExprError(); |
| 5523 | |
| 5524 | return MaybeBindToTemporary(TheCall.release()); |
| 5525 | } else { |
| 5526 | // We matched a built-in operator. Convert the arguments, then |
| 5527 | // break out so that we will build the appropriate built-in |
| 5528 | // operator node. |
| 5529 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5530 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5531 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5532 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5533 | return ExprError(); |
| 5534 | |
| 5535 | break; |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5540 | if (CandidateSet.empty()) |
| 5541 | Diag(LLoc, diag::err_ovl_no_oper) |
| 5542 | << Args[0]->getType() << /*subscript*/ 0 |
| 5543 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5544 | else |
| 5545 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 5546 | << Args[0]->getType() |
| 5547 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5548 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5549 | "[]", LLoc); |
| 5550 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | case OR_Ambiguous: |
| 5554 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5555 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5556 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5557 | "[]", LLoc); |
| 5558 | return ExprError(); |
| 5559 | |
| 5560 | case OR_Deleted: |
| 5561 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5562 | << Best->Function->isDeleted() << "[]" |
| 5563 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5564 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, |
| 5565 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5566 | return ExprError(); |
| 5567 | } |
| 5568 | |
| 5569 | // We matched a built-in operator; build it. |
| 5570 | Base.release(); |
| 5571 | Idx.release(); |
| 5572 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 5573 | Owned(Args[1]), RLoc); |
| 5574 | } |
| 5575 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5576 | /// BuildCallToMemberFunction - Build a call to a member |
| 5577 | /// function. MemExpr is the expression that refers to the member |
| 5578 | /// function (and includes the object parameter), Args/NumArgs are the |
| 5579 | /// arguments to the function call (not including the object |
| 5580 | /// parameter). The caller needs to validate that the member |
| 5581 | /// expression refers to a member function or an overloaded member |
| 5582 | /// function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5583 | Sema::OwningExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5584 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 5585 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5586 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 5587 | SourceLocation RParenLoc) { |
| 5588 | // Dig out the member expression. This holds both the object |
| 5589 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5590 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 5591 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5592 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5593 | CXXMethodDecl *Method = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5594 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 5595 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5596 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 5597 | } else { |
| 5598 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5599 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5600 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5601 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5602 | // Add overload candidates |
| 5603 | OverloadCandidateSet CandidateSet; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5604 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5605 | // FIXME: avoid copy. |
| 5606 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 5607 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 5608 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 5609 | TemplateArgs = &TemplateArgsBuffer; |
| 5610 | } |
| 5611 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5612 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 5613 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 5614 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5615 | NamedDecl *Func = *I; |
| 5616 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 5617 | if (isa<UsingShadowDecl>(Func)) |
| 5618 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 5619 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5620 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5621 | // If explicit template arguments were provided, we can't call a |
| 5622 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5623 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5624 | continue; |
| 5625 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5626 | AddMethodCandidate(Method, ActingDC, ObjectType, Args, NumArgs, |
| 5627 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5628 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5629 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5630 | ActingDC, TemplateArgs, |
| 5631 | ObjectType, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5632 | CandidateSet, |
| 5633 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5634 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5635 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5636 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5637 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 5638 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5639 | OverloadCandidateSet::iterator Best; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5640 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5641 | case OR_Success: |
| 5642 | Method = cast<CXXMethodDecl>(Best->Function); |
| 5643 | break; |
| 5644 | |
| 5645 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5646 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5647 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5648 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5649 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5650 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5651 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5652 | |
| 5653 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5654 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5655 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5656 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5657 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5658 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5659 | |
| 5660 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5661 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5662 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5663 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5664 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5665 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5666 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 5669 | MemExprE = FixOverloadedFunctionReference(MemExprE, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5670 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5671 | // If overload resolution picked a static member, build a |
| 5672 | // non-member call based on that function. |
| 5673 | if (Method->isStatic()) { |
| 5674 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 5675 | Args, NumArgs, RParenLoc); |
| 5676 | } |
| 5677 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5678 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5682 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5683 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5684 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5685 | Method->getResultType().getNonReferenceType(), |
| 5686 | RParenLoc)); |
| 5687 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5688 | // Check for a valid return type. |
| 5689 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 5690 | TheCall.get(), Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5691 | return ExprError(); |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 5692 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5693 | // Convert the object argument (for a non-static member function call). |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5694 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5695 | if (!Method->isStatic() && |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5696 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5697 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5698 | MemExpr->setBase(ObjectArg); |
| 5699 | |
| 5700 | // Convert the rest of the arguments |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5701 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5702 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5703 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5704 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5705 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5706 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5707 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 5708 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5709 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5712 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 5713 | /// type (C++ [over.call.object]), which can end up invoking an |
| 5714 | /// overloaded function call operator (@c operator()) or performing a |
| 5715 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5716 | Sema::ExprResult |
| 5717 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 5718 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5719 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5720 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5721 | SourceLocation RParenLoc) { |
| 5722 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5723 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5724 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5725 | // C++ [over.call.object]p1: |
| 5726 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5727 | // 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] | 5728 | // candidate functions includes at least the function call |
| 5729 | // operators of T. The function call operators of T are obtained by |
| 5730 | // ordinary lookup of the name operator() in the context of |
| 5731 | // (E).operator(). |
| 5732 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5733 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5734 | |
| 5735 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 5736 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 5737 | << Object->getSourceRange())) |
| 5738 | return true; |
| 5739 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5740 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 5741 | LookupQualifiedName(R, Record->getDecl()); |
| 5742 | R.suppressDiagnostics(); |
| 5743 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 5744 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5745 | Oper != OperEnd; ++Oper) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5746 | AddMethodCandidate(*Oper, Object->getType(), Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 5747 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 5748 | } |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5749 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5750 | // C++ [over.call.object]p2: |
| 5751 | // In addition, for each conversion function declared in T of the |
| 5752 | // form |
| 5753 | // |
| 5754 | // operator conversion-type-id () cv-qualifier; |
| 5755 | // |
| 5756 | // where cv-qualifier is the same cv-qualification as, or a |
| 5757 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 5758 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 5759 | // R", or the type "reference to pointer to function of |
| 5760 | // (P1,...,Pn) returning R", or the type "reference to function |
| 5761 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5762 | // is also considered as a candidate function. Similarly, |
| 5763 | // surrogate call functions are added to the set of candidate |
| 5764 | // functions for each conversion function declared in an |
| 5765 | // accessible base class provided the function is not hidden |
| 5766 | // within T by another intervening declaration. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5767 | const UnresolvedSet *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 5768 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5769 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 5770 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5771 | NamedDecl *D = *I; |
| 5772 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5773 | if (isa<UsingShadowDecl>(D)) |
| 5774 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5775 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5776 | // Skip over templated conversion functions; they aren't |
| 5777 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5778 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5779 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5780 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5781 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5782 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5783 | // Strip the reference type (if any) and then the pointer type (if |
| 5784 | // any) to get down to what might be a function type. |
| 5785 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 5786 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5787 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5788 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 5789 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5790 | AddSurrogateCandidate(Conv, ActingContext, Proto, |
| 5791 | Object->getType(), Args, NumArgs, |
| 5792 | CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5793 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5794 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5795 | // Perform overload resolution. |
| 5796 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5797 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5798 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5799 | // Overload resolution succeeded; we'll build the appropriate call |
| 5800 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5801 | break; |
| 5802 | |
| 5803 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5804 | if (CandidateSet.empty()) |
| 5805 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 5806 | << Object->getType() << /*call*/ 1 |
| 5807 | << Object->getSourceRange(); |
| 5808 | else |
| 5809 | Diag(Object->getSourceRange().getBegin(), |
| 5810 | diag::err_ovl_no_viable_object_call) |
| 5811 | << Object->getType() << Object->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5812 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5813 | break; |
| 5814 | |
| 5815 | case OR_Ambiguous: |
| 5816 | Diag(Object->getSourceRange().getBegin(), |
| 5817 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5818 | << Object->getType() << Object->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5819 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5820 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5821 | |
| 5822 | case OR_Deleted: |
| 5823 | Diag(Object->getSourceRange().getBegin(), |
| 5824 | diag::err_ovl_deleted_object_call) |
| 5825 | << Best->Function->isDeleted() |
| 5826 | << Object->getType() << Object->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5827 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5828 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5829 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5830 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5831 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5832 | // We had an error; delete all of the subexpressions and return |
| 5833 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5834 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5835 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5836 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5837 | return true; |
| 5838 | } |
| 5839 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5840 | if (Best->Function == 0) { |
| 5841 | // Since there is no function declaration, this is one of the |
| 5842 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5843 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5844 | = cast<CXXConversionDecl>( |
| 5845 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 5846 | |
| 5847 | // We selected one of the surrogate functions that converts the |
| 5848 | // object parameter to a function pointer. Perform the conversion |
| 5849 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5850 | |
| 5851 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5852 | // and then call it. |
Eli Friedman | a958a01 | 2009-12-09 04:52:43 +0000 | [diff] [blame] | 5853 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Conv); |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 5854 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 5855 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5856 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 5857 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 5861 | // that calls this method, using Object for the implicit object |
| 5862 | // parameter and passing along the remaining arguments. |
| 5863 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5864 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5865 | |
| 5866 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5867 | unsigned NumArgsToCheck = NumArgs; |
| 5868 | |
| 5869 | // Build the full argument list for the method call (the |
| 5870 | // implicit object parameter is placed at the beginning of the |
| 5871 | // list). |
| 5872 | Expr **MethodArgs; |
| 5873 | if (NumArgs < NumArgsInProto) { |
| 5874 | NumArgsToCheck = NumArgsInProto; |
| 5875 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 5876 | } else { |
| 5877 | MethodArgs = new Expr*[NumArgs + 1]; |
| 5878 | } |
| 5879 | MethodArgs[0] = Object; |
| 5880 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5881 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5882 | |
| 5883 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5884 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5885 | UsualUnaryConversions(NewFn); |
| 5886 | |
| 5887 | // Once we've built TheCall, all of the expressions are properly |
| 5888 | // owned. |
| 5889 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5890 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5891 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5892 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5893 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5894 | delete [] MethodArgs; |
| 5895 | |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 5896 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 5897 | Method)) |
| 5898 | return true; |
| 5899 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5900 | // We may have default arguments. If so, we need to allocate more |
| 5901 | // slots in the call for them. |
| 5902 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 5903 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5904 | else if (NumArgs > NumArgsInProto) |
| 5905 | NumArgsToCheck = NumArgsInProto; |
| 5906 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5907 | bool IsError = false; |
| 5908 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5909 | // Initialize the implicit object parameter. |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5910 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5911 | TheCall->setArg(0, Object); |
| 5912 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5913 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5914 | // Check the argument types. |
| 5915 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5916 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5917 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5918 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5919 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5920 | // Pass the argument. |
| 5921 | QualType ProtoArgType = Proto->getArgType(i); |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5922 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, AA_Passing); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5923 | } else { |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 5924 | OwningExprResult DefArg |
| 5925 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 5926 | if (DefArg.isInvalid()) { |
| 5927 | IsError = true; |
| 5928 | break; |
| 5929 | } |
| 5930 | |
| 5931 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 5932 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5933 | |
| 5934 | TheCall->setArg(i + 1, Arg); |
| 5935 | } |
| 5936 | |
| 5937 | // If this is a variadic call, handle args passed through "...". |
| 5938 | if (Proto->isVariadic()) { |
| 5939 | // Promote the arguments (C99 6.5.2.2p7). |
| 5940 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 5941 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5942 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5943 | TheCall->setArg(i + 1, Arg); |
| 5944 | } |
| 5945 | } |
| 5946 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 5947 | if (IsError) return true; |
| 5948 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5949 | if (CheckFunctionCall(Method, TheCall.get())) |
| 5950 | return true; |
| 5951 | |
Anders Carlsson | 1c83deb | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 5952 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5955 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5956 | /// (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] | 5957 | /// @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] | 5958 | Sema::OwningExprResult |
| 5959 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 5960 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5961 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5962 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5963 | // C++ [over.ref]p1: |
| 5964 | // |
| 5965 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 5966 | // for a class object x of type T if T::operator->() exists and if |
| 5967 | // the operator is selected as the best match function by the |
| 5968 | // overload resolution mechanism (13.3). |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5969 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 5970 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5971 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 5972 | |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 5973 | if (RequireCompleteType(Base->getLocStart(), Base->getType(), |
| 5974 | PDiag(diag::err_typecheck_incomplete_tag) |
| 5975 | << Base->getSourceRange())) |
| 5976 | return ExprError(); |
| 5977 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5978 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 5979 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 5980 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 5981 | |
| 5982 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5983 | Oper != OperEnd; ++Oper) { |
| 5984 | NamedDecl *D = *Oper; |
| 5985 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 5986 | if (isa<UsingShadowDecl>(D)) |
| 5987 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 5988 | |
| 5989 | AddMethodCandidate(cast<CXXMethodDecl>(D), ActingContext, |
| 5990 | Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5991 | /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5992 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5993 | |
| 5994 | // Perform overload resolution. |
| 5995 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5996 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 5997 | case OR_Success: |
| 5998 | // Overload resolution succeeded; we'll build the call below. |
| 5999 | break; |
| 6000 | |
| 6001 | case OR_No_Viable_Function: |
| 6002 | if (CandidateSet.empty()) |
| 6003 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6004 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6005 | else |
| 6006 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6007 | << "operator->" << Base->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6008 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6009 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6010 | |
| 6011 | case OR_Ambiguous: |
| 6012 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6013 | << "->" << Base->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6014 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6015 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6016 | |
| 6017 | case OR_Deleted: |
| 6018 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6019 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6020 | << "->" << Base->getSourceRange(); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6021 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6022 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | // Convert the object parameter. |
| 6026 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6027 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6028 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6029 | |
| 6030 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6031 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6032 | |
| 6033 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6034 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6035 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6036 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6037 | |
| 6038 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6039 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6040 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6041 | &Base, 1, ResultTy, OpLoc)); |
| 6042 | |
| 6043 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6044 | Method)) |
| 6045 | return ExprError(); |
| 6046 | return move(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6049 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6050 | /// a C++ overloaded function (possibly with some parentheses and |
| 6051 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6052 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6053 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 6054 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6055 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6056 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 6057 | if (SubExpr == PE->getSubExpr()) |
| 6058 | return PE->Retain(); |
| 6059 | |
| 6060 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6061 | } |
| 6062 | |
| 6063 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 6064 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6065 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6066 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6067 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6068 | if (SubExpr == ICE->getSubExpr()) |
| 6069 | return ICE->Retain(); |
| 6070 | |
| 6071 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6072 | ICE->getCastKind(), |
| 6073 | SubExpr, |
| 6074 | ICE->isLvalueCast()); |
| 6075 | } |
| 6076 | |
| 6077 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6078 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6079 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6080 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6081 | if (Method->isStatic()) { |
| 6082 | // Do nothing: static member functions aren't any different |
| 6083 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6084 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6085 | // Fix the sub expression, which really has to be an |
| 6086 | // UnresolvedLookupExpr holding an overloaded member function |
| 6087 | // or template. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6088 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6089 | if (SubExpr == UnOp->getSubExpr()) |
| 6090 | return UnOp->Retain(); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6091 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6092 | assert(isa<DeclRefExpr>(SubExpr) |
| 6093 | && "fixed to something other than a decl ref"); |
| 6094 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6095 | && "fixed to a member ref with no nested name qualifier"); |
| 6096 | |
| 6097 | // We have taken the address of a pointer to member |
| 6098 | // function. Perform the computation here so that we get the |
| 6099 | // appropriate pointer to member type. |
| 6100 | QualType ClassType |
| 6101 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6102 | QualType MemPtrType |
| 6103 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6104 | |
| 6105 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6106 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6107 | } |
| 6108 | } |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6109 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6110 | if (SubExpr == UnOp->getSubExpr()) |
| 6111 | return UnOp->Retain(); |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6112 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6113 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6114 | Context.getPointerType(SubExpr->getType()), |
| 6115 | UnOp->getOperatorLoc()); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6116 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6117 | |
| 6118 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6119 | // FIXME: avoid copy. |
| 6120 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6121 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6122 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6123 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6126 | return DeclRefExpr::Create(Context, |
| 6127 | ULE->getQualifier(), |
| 6128 | ULE->getQualifierRange(), |
| 6129 | Fn, |
| 6130 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6131 | Fn->getType(), |
| 6132 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6135 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6136 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6137 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6138 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6139 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6140 | TemplateArgs = &TemplateArgsBuffer; |
| 6141 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6142 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6143 | Expr *Base; |
| 6144 | |
| 6145 | // If we're filling in |
| 6146 | if (MemExpr->isImplicitAccess()) { |
| 6147 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6148 | return DeclRefExpr::Create(Context, |
| 6149 | MemExpr->getQualifier(), |
| 6150 | MemExpr->getQualifierRange(), |
| 6151 | Fn, |
| 6152 | MemExpr->getMemberLoc(), |
| 6153 | Fn->getType(), |
| 6154 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 6155 | } else { |
| 6156 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6157 | if (MemExpr->getQualifier()) |
| 6158 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6159 | Base = new (Context) CXXThisExpr(Loc, |
| 6160 | MemExpr->getBaseType(), |
| 6161 | /*isImplicit=*/true); |
| 6162 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6163 | } else |
| 6164 | Base = MemExpr->getBase()->Retain(); |
| 6165 | |
| 6166 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6167 | MemExpr->isArrow(), |
| 6168 | MemExpr->getQualifier(), |
| 6169 | MemExpr->getQualifierRange(), |
| 6170 | Fn, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6171 | MemExpr->getMemberLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6172 | TemplateArgs, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6173 | Fn->getType()); |
| 6174 | } |
| 6175 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6176 | assert(false && "Invalid reference to overloaded function"); |
| 6177 | return E->Retain(); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6180 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
| 6181 | FunctionDecl *Fn) { |
| 6182 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Fn)); |
| 6183 | } |
| 6184 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6185 | } // end namespace clang |